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.cpp342
1 files changed, 0 insertions, 342 deletions
diff --git a/noncore/net/ubrowser/httpcomm.cpp b/noncore/net/ubrowser/httpcomm.cpp
deleted file mode 100644
index 4f189ce..0000000
--- a/noncore/net/ubrowser/httpcomm.cpp
+++ b/dev/null
@@ -1,342 +0,0 @@
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, QImageDrag *newImage, bool newIsImage)
43{
44 host = newHost;
45 portS = newPortS;
46 file = newFile;
47 text = newText;
48 isImage = newIsImage;
49 image = newImage;
50}
51
52void HttpComm::hostFound()
53{
54 printf("HttpComm::hostFound: host found\n");
55}
56
57void HttpComm::connected()
58{
59 QString request("GET " + file + " HTTP/1.1\r\nHost: " + host + ':' + portS + "\r\nConnection: close\r\n\r\n");
60 //QString request("GET " + file + " HTTP/1.0\r\n\r\n");
61 printf("HttpComm::data: bytes written: %d\n", socket->writeBlock(request.latin1(), request.length()) );
62 printf("HttpComm::data: request sent:\n%s", request.latin1());
63
64 headerRead=false;
65 bRead=0;
66 length = 0;
67 header="";
68 body="";
69 chunked=false;
70 lengthset=false;
71}
72
73void HttpComm::incoming()
74{
75 int ba=socket->size(), i=0, j=0, semi=0;
76 char *tempString = new char [ba];
77 int br = socket->readBlock(tempString, ba);
78 socket->flush();
79 bool nextChunk=false;
80 bool done=false;
81 printf("HttpComm::incoming: ba: %d\n", ba);
82 printf("HttpComm::incoming: bytes read from socket: %d\n", br);
83 //printf("HttpComm::incoming: tempString length: %d\n");
84 QString sclength;
85
86 if(headerRead == false)
87 {
88 for(i=0; i<ba; i++)
89 {
90 if(tempString[i] != '\r')
91 {
92 if(tempString[i] == '\n' && header[header.length()-1] == '\n')
93 {
94 j=i;
95 headerRead = true;
96 parseHeader();
97 goto body;
98 }
99 else
100 {
101 header+=tempString[i];
102 }
103 }
104 // printf("%d %d\n", ba, i);
105 }
106 }
107 else
108 {
109 body:
110 printf("HttpComm::incoming: reading body\n");
111 printf("HttpComm::incoming: j is: %d\n", j);
112 if(!chunked)
113 {
114//make sure we didnt just leave that break above...
115 if(j != 0)
116 {
117 for( ; j<ba ; j++)
118 {
119 body+=tempString[j];
120 bRead++;
121 // printf("bRead1: %d\n", bRead);
122 }
123 }
124 else
125 {
126 body += tempString;
127 bRead+=ba;
128 // printf("bRead2: %d\n", bRead);
129 }
130
131 if(bRead >= length)
132 {
133 printf("HttpComm::incoming: finished reading body\n");
134 processBody();
135 socket->close();
136 }
137 }
138 else
139 {
140 int startclength=0;
141 QString tempQString = tempString;
142 //remove the http header, if one exists
143 if(j != 0)
144 {
145 tempQString.remove(0, j+1);
146 printf("HttpComm::incoming: removing http header. Result: \n%s", tempQString.latin1());
147 }
148 while(!done)
149 {
150 switch(status)
151 {
152 //case 0=need to read chunk length
153 case 0:
154 j = tempQString.find('\n');
155 sclength = tempQString;
156 sclength.truncate(j);
157 clength = sclength.toUInt(0, 16);
158 printf("HttpComm::Incoming: chunk length: %d\n", clength);
159 printf("HttpComm::Incoming: chunk length string: %s\n", sclength.latin1());
160 //end of data
161 if(clength==0)
162 {
163 processBody();
164 done=true;
165 return;
166 }
167 //still more, but it hasnt been recieved yet
168 if(ba <= j)
169 {
170 status=2;
171 done=true;
172 break;
173 }
174 //still more data waiting
175 else
176 {
177 done=false;
178 //remove the chunk length header
179 tempQString.remove(0,j+1);
180 }
181 bRead=0;
182 // break;
183 //if there is more fall through to:
184 //chunk length just read, still more in tempQstring
185 case 1:
186 //the current data extends beyond the end of the chunk
187 if(bRead + tempQString.length() > clength)
188 {
189 QString newTQstring = tempQString;
190 newTQstring.truncate(clength-bRead);
191 bRead+=newTQstring.length();
192 body+=newTQstring;
193 printf("HttpComm::incoming: start new body piece 1: \n");
194 printf("%s", newTQstring.latin1() );
195 printf("HttpComm::incoming: end new body piece 1.\n");
196 status=0;
197 tempQString = tempQString.remove(0, newTQstring.length());
198 startclength = tempQString.find('\n');
199 printf("HttpComm::incoming: startclength: %d\n", startclength);
200 tempQString = tempQString.remove(0, startclength+1);
201 done=false;
202 // break;
203 }
204 //the chunk extends beyond the current data;
205 else
206 {
207 if(tempQString.length() <= ba)
208 {
209 printf("HttpComm::incoming: not truncating tempQString\n");
210 body+=tempQString;
211 bRead+=tempQString.length();
212 }
213 else
214 {
215 printf("HttpComm::incoming: truncating tempQString\n");
216 tempQString.truncate(ba);
217 body+=tempQString;
218 bRead+=tempQString.length();
219 }
220 printf("HttpComm::incoming: start new body piece 2: \n");
221 printf("%s", tempQString.latin1() );
222 printf("HttpComm::incoming: end new body piece 2.\n");
223 done=true;
224 status=2;
225 // break;
226 }
227 break;
228 //just got data in, continue reading chunk
229 case 2:
230 //the current data extends beyond the end of the chunk
231 if(bRead + tempQString.length() > clength)
232 {
233 QString newTQstring = tempQString;
234 newTQstring.truncate(clength-bRead);
235 bRead+=newTQstring.length();
236 body+=newTQstring;
237 printf("HttpComm::incoming: start new body piece 3: \n");
238 printf("%s", newTQstring.latin1() );
239 printf("HttpComm::incoming: end new body piece 3.\n");
240 status=0;
241 tempQString = tempQString.remove(0, newTQstring.length());
242 startclength = tempQString.find('\n');
243 printf("HttpComm::incoming: startclength, tempQString length: %d %d\n", startclength, tempQString.length());
244 tempQString = tempQString.remove(0, startclength+1);
245 done=false;
246 // break;
247 }
248 //the chunk extends beyond the current data;
249 else
250 {
251 if(tempQString.length() <= ba)
252 {
253 printf("HttpComm::incoming: not truncating tempQString\n");
254 body+=tempQString;
255 bRead+=tempQString.length();
256 }
257 else
258 {
259 printf("HttpComm::incoming: truncating tempQString\n");
260 tempQString.truncate(ba);
261 body+=tempQString;
262 bRead+=tempQString.length();
263 }
264 printf("HttpComm::incoming: start new body piece 4: \n");
265 printf("%s", tempQString.latin1() );
266 printf("HttpComm::incoming: end new body piece 4.\n");
267 done=true;
268 status=2;
269 // break;
270 }
271 break;
272 }
273 printf("HttpComm::incoming: chunked encoding: bRead: %d\n", bRead);
274 }
275 }
276 }
277 delete tempString;
278}
279
280void HttpComm::connectionClosed()
281{
282 printf("HttpComm::connectionClosed: connection closed\n");
283 processBody();
284}
285
286void HttpComm::parseHeader()
287{
288 QStringList headerLines, tempList;
289 int i;
290
291 printf("HttpComm::parseHeader: start header\n\n");
292 printf("%s", header.latin1());
293 printf("HttpComm::parseHeader: end header\n");
294
295 headerLines = QStringList::split('\n', header);
296
297 for(i=0; i<headerLines.count(); i++)
298 {
299 if(headerLines[i].startsWith("Content-Length:") )
300 {
301 tempList = QStringList::split(':', headerLines[i]);
302 tempList[1].stripWhiteSpace();
303 length = tempList[1].toUInt();
304 }
305 else if(headerLines[i].startsWith("Transfer-Encoding: chunked") )
306 {
307 printf("HttpComm::parseHeader: using chunked encoding\n");
308 chunked = true;
309 status=0;
310 }
311 }
312
313 printf("HttpConn::parseHeader: content-length: %d\n", length);
314}
315
316void HttpComm::processBody()
317{
318 printf("HttpComm::processBody: processing body\n");
319 //printf("HttpComm::processBody: start body\n\n");
320 //printf("%s", body.latin1());
321 //printf("HttpComm::processBody: end body\n");
322
323 int lastSlash = file.findRev('/');
324
325 QString end = file;
326 end.truncate(lastSlash+1);
327 QString context = "http://"+host+':'+portS+end;
328 printf("HttpComm::processBody: context: %s\n", context.latin1() );
329
330 if(!isImage)
331 {
332 browser->setTextFormat(RichText);
333 browser->mimeSourceFactory()->setFilePath(context);
334 browser->setText(body, context);
335 }
336 else
337 {
338 QImage tempImage(body.latin1());
339 image->setImage(tempImage);
340 browser->update();
341 }
342}