summaryrefslogtreecommitdiff
path: root/noncore/net/ubrowser/httpcomm.cpp
Unidiff
Diffstat (limited to 'noncore/net/ubrowser/httpcomm.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/ubrowser/httpcomm.cpp256
1 files changed, 256 insertions, 0 deletions
diff --git a/noncore/net/ubrowser/httpcomm.cpp b/noncore/net/ubrowser/httpcomm.cpp
new file mode 100644
index 0000000..51068db
--- a/dev/null
+++ b/noncore/net/ubrowser/httpcomm.cpp
@@ -0,0 +1,256 @@
1/*
2Opie-uBrowser. a very small web browser, using on QTextBrowser for html display/parsing
3Copyright (C) 2002 Thomas Stephens
4
5This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7version.
8
9This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11Public License for more details.
12
13You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
14Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15*/
16
17#include "httpcomm.h"
18
19HttpComm::HttpComm(QSocket *newSocket, QTextBrowser *newBrowser):QObject()
20{
21 socket = newSocket;
22
23 connect(socket, SIGNAL(hostFound()), this, SLOT(hostFound()) );
24 connect(socket, SIGNAL(connected()), this, SLOT(connected()) );
25 connect(socket, SIGNAL(readyRead()), this, SLOT(incoming()) );
26 connect(socket, SIGNAL(connectionClosed()), this, SLOT(connectionClosed()) );
27
28 headerRead=false;
29 length = 0;
30 bRead = 0;
31 chunked=false;
32 lengthset=false;
33
34 browser=newBrowser;
35}
36
37void HttpComm::setUp(QString *newName)
38{
39 name = newName;
40}
41
42void HttpComm::setStuff(QString newHost, QString newPortS, QString newFile, QTextDrag *newText)
43{
44 host = newHost;
45 portS = newPortS;
46 file = newFile;
47 text = newText;
48}
49
50void HttpComm::hostFound()
51{
52 printf("HttpComm::hostFound: host found\n");
53}
54
55void HttpComm::connected()
56{
57 QString request("GET " + file + " HTTP/1.1\r\nHost: " + host + ':' + portS + "\r\nConnection: close\r\n\r\n");
58 //QString request("GET " + file + " HTTP/1.0\r\n\r\n");
59 printf("HttpComm::data: bytes written: %d\n", socket->writeBlock(request.latin1(), request.length()) );
60 printf("HttpComm::data: request sent:\n%s", request.latin1());
61
62 headerRead=false;
63 bRead=0;
64 length = 0;
65 header="";
66 body="";
67 chunked=false;
68 lengthset=false;
69}
70
71void HttpComm::incoming()
72{
73 int ba=socket->bytesAvailable(), i=0, j=0, semi=0;
74 char *tempString = new char [ba];
75 bool nextChunk=false;
76 bool done=false;
77 socket->readBlock(tempString, ba);
78 printf("HttpComm::incoming: ba: %d\n", ba);
79 QString sclength;
80
81 if(headerRead == false)
82 {
83 for(i=0; i<ba; i++)
84 {
85 if(tempString[i] != '\r')
86 {
87 if(tempString[i] == '\n' && header[header.length()-1] == '\n')
88 {
89 j=i;
90 headerRead = true;
91 parseHeader();
92 goto body;
93 }
94 else
95 {
96 header+=tempString[i];
97 }
98 }
99 // printf("%d %d\n", ba, i);
100 }
101 }
102 else
103 {
104 body:
105 printf("HttpComm::incoming: reading body\n");
106 printf("HttpComm::incoming: j is: %d\n", j);
107 if(!chunked)
108 {
109//make sure we didnt just leave that break above...
110 if(j != 0)
111 {
112 for( ; j<ba ; j++)
113 {
114 body+=tempString[j];
115 bRead++;
116 // printf("bRead1: %d\n", bRead);
117 }
118 }
119 else
120 {
121 body += tempString;
122 bRead+=ba;
123 // printf("bRead2: %d\n", bRead);
124 }
125
126 if(bRead >= length)
127 {
128 printf("HttpComm::incoming: finished reading body\n");
129 processBody();
130 socket->close();
131 }
132 }
133 else
134 {
135 QString tempQString = tempString;
136 //remove the http header, if one exists
137 if(j != 0)
138 {
139 tempQString.remove(0, j);
140 }
141 while(!done)
142 {
143 switch(status)
144 {
145 //case 0=need to read chunk length
146 case 0:
147 j = tempQString.find('\n');
148 sclength = tempQString;
149 sclength.truncate(j);
150 clength = sclength.toUInt(0, 16);
151 printf("HttpComm::Incoming: chunk length: %d\n", clength);
152 if(clength==0)
153 {
154 processBody();
155 done=true;
156 }
157 if(ba <= j)
158 {
159 status=1;
160 done=true;
161 // break;
162 }
163 else
164 {
165 done=false;
166 }
167 bRead=0;
168 break;
169 //if there is more fall through to:
170 //chunk length just read, still more in tempQstring
171 case 1:
172 //the current data extends beyond the end of the chunk
173 if(bRead + tempQString.length() > clength)
174 {
175 QString newTQstring = tempQString;
176 newTQstring.truncate(clength-bRead);
177 body+=newTQstring;
178 printf("HttpComm::incoming: start new body piece 1: \n");
179 printf("%s", newTQstring.latin1() );
180 printf("HttpComm::incoming: end new body piece 1.\n");
181 status=0;
182 j=clength-bRead;
183 done=false;
184 break;
185 }
186 //the chunk extends beyond the current data;
187 else
188 {
189 body+=tempQString;
190 bRead+=ba;
191 printf("HttpComm::incoming: start new body piece 2: \n");
192 printf("%s", tempQString.latin1() );
193 printf("HttpComm::incoming: end new body piece 2.\n");
194 done=true;
195 break;
196 }
197 break;
198 }
199 }
200 }
201 }
202 delete tempString;
203}
204
205void HttpComm::connectionClosed()
206{
207 printf("HttpComm::connectionClosed: connection closed\n");
208 processBody();
209}
210
211void HttpComm::parseHeader()
212{
213 QStringList headerLines, tempList;
214 int i;
215
216 printf("HttpComm::parseHeader: start header\n\n");
217 printf("%s", header.latin1());
218 printf("HttpComm::parseHeader: end header\n");
219
220 headerLines = QStringList::split('\n', header);
221
222 for(i=0; i<headerLines.count(); i++)
223 {
224 if(headerLines[i].startsWith("Content-Length:") )
225 {
226 tempList = QStringList::split(':', headerLines[i]);
227 tempList[1].stripWhiteSpace();
228 length = tempList[1].toUInt();
229 }
230 else if(headerLines[i].startsWith("Transfer-Encoding: chunked") )
231 {
232 printf("HttpComm::parseHeader: using chunked encoding\n");
233 chunked = true;
234 status=0;
235 }
236 }
237
238 printf("HttpConn::parseHeader: content-length: %d\n", length);
239}
240
241void HttpComm::processBody()
242{
243 //printf("HttpComm::processBody: start body\n\n");
244 //printf("%s", body.latin1());
245 //printf("HttpComm::processBody: end body\n");
246
247 int lastSlash = file.findRev('/');
248
249 QString end = file;
250 end.truncate(lastSlash-1);
251 QString context("http://"+host+':'+portS+end);
252
253 browser->setTextFormat(RichText);
254 browser->setText(body, context);
255 printf("%s\n", context.latin1() );
256}