summaryrefslogtreecommitdiff
authorspiralman <spiralman>2002-08-02 21:09:06 (UTC)
committer spiralman <spiralman>2002-08-02 21:09:06 (UTC)
commitb228f8134306f469ec78ae8ab00ea5fdc0853538 (patch) (unidiff)
treeefaedf7f15eb661665a71b678f6a07ce22a25c95
parentd3a54af5288cd30fc1a4f2dafc9f848b245046d6 (diff)
downloadopie-b228f8134306f469ec78ae8ab00ea5fdc0853538.zip
opie-b228f8134306f469ec78ae8ab00ea5fdc0853538.tar.gz
opie-b228f8134306f469ec78ae8ab00ea5fdc0853538.tar.bz2
fixed some bugs in chunked encoding stuff, but it still doesnt work. feels very close though...
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/ubrowser/httpcomm.cpp33
1 files changed, 26 insertions, 7 deletions
diff --git a/noncore/net/ubrowser/httpcomm.cpp b/noncore/net/ubrowser/httpcomm.cpp
index b086b58..3c14053 100644
--- a/noncore/net/ubrowser/httpcomm.cpp
+++ b/noncore/net/ubrowser/httpcomm.cpp
@@ -57,242 +57,261 @@ void HttpComm::connected()
57 QString request("GET " + file + " HTTP/1.1\r\nHost: " + host + ':' + portS + "\r\nConnection: close\r\n\r\n"); 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"); 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()) ); 59 printf("HttpComm::data: bytes written: %d\n", socket->writeBlock(request.latin1(), request.length()) );
60 printf("HttpComm::data: request sent:\n%s", request.latin1()); 60 printf("HttpComm::data: request sent:\n%s", request.latin1());
61 61
62 headerRead=false; 62 headerRead=false;
63 bRead=0; 63 bRead=0;
64 length = 0; 64 length = 0;
65 header=""; 65 header="";
66 body=""; 66 body="";
67 chunked=false; 67 chunked=false;
68 lengthset=false; 68 lengthset=false;
69} 69}
70 70
71void HttpComm::incoming() 71void HttpComm::incoming()
72{ 72{
73 int ba=socket->bytesAvailable(), i=0, j=0, semi=0; 73 int ba=socket->bytesAvailable(), i=0, j=0, semi=0;
74 char *tempString = new char [ba]; 74 char *tempString = new char [ba];
75 bool nextChunk=false; 75 bool nextChunk=false;
76 bool done=false; 76 bool done=false;
77 socket->readBlock(tempString, ba); 77 socket->readBlock(tempString, ba);
78 printf("HttpComm::incoming: ba: %d\n", ba); 78 printf("HttpComm::incoming: ba: %d\n", ba);
79 QString sclength; 79 QString sclength;
80 80
81 if(headerRead == false) 81 if(headerRead == false)
82 { 82 {
83 for(i=0; i<ba; i++) 83 for(i=0; i<ba; i++)
84 { 84 {
85 if(tempString[i] != '\r') 85 if(tempString[i] != '\r')
86 { 86 {
87 if(tempString[i] == '\n' && header[header.length()-1] == '\n') 87 if(tempString[i] == '\n' && header[header.length()-1] == '\n')
88 { 88 {
89 j=i; 89 j=i;
90 headerRead = true; 90 headerRead = true;
91 parseHeader(); 91 parseHeader();
92 goto body; 92 goto body;
93 } 93 }
94 else 94 else
95 { 95 {
96 header+=tempString[i]; 96 header+=tempString[i];
97 } 97 }
98 } 98 }
99 // printf("%d %d\n", ba, i); 99 // printf("%d %d\n", ba, i);
100 } 100 }
101 } 101 }
102 else 102 else
103 { 103 {
104 body: 104 body:
105 printf("HttpComm::incoming: reading body\n"); 105 printf("HttpComm::incoming: reading body\n");
106 printf("HttpComm::incoming: j is: %d\n", j); 106 printf("HttpComm::incoming: j is: %d\n", j);
107 if(!chunked) 107 if(!chunked)
108 { 108 {
109//make sure we didnt just leave that break above... 109//make sure we didnt just leave that break above...
110 if(j != 0) 110 if(j != 0)
111 { 111 {
112 for( ; j<ba ; j++) 112 for( ; j<ba ; j++)
113 { 113 {
114 body+=tempString[j]; 114 body+=tempString[j];
115 bRead++; 115 bRead++;
116 // printf("bRead1: %d\n", bRead); 116 // printf("bRead1: %d\n", bRead);
117 } 117 }
118 } 118 }
119 else 119 else
120 { 120 {
121 body += tempString; 121 body += tempString;
122 bRead+=ba; 122 bRead+=ba;
123 // printf("bRead2: %d\n", bRead); 123 // printf("bRead2: %d\n", bRead);
124 } 124 }
125 125
126 if(bRead >= length) 126 if(bRead >= length)
127 { 127 {
128 printf("HttpComm::incoming: finished reading body\n"); 128 printf("HttpComm::incoming: finished reading body\n");
129 processBody(); 129 processBody();
130 socket->close(); 130 socket->close();
131 } 131 }
132 } 132 }
133 else 133 else
134 { 134 {
135 QString tempQString = tempString; 135 QString tempQString = tempString;
136 //remove the http header, if one exists 136 //remove the http header, if one exists
137 if(j != 0) 137 if(j != 0)
138 { 138 {
139 tempQString.remove(0, j+1); 139 tempQString.remove(0, j+1);
140 printf("HttpComm::incoming: removing http header. Result: \n%s", tempQString.latin1()); 140 printf("HttpComm::incoming: removing http header. Result: \n%s", tempQString.latin1());
141 } 141 }
142 while(!done) 142 while(!done)
143 { 143 {
144 switch(status) 144 switch(status)
145 { 145 {
146 //case 0=need to read chunk length 146 //case 0=need to read chunk length
147 case 0: 147 case 0:
148 j = tempQString.find('\n'); 148 j = tempQString.find('\n');
149 sclength = tempQString; 149 sclength = tempQString;
150 sclength.truncate(j); 150 sclength.truncate(j);
151 clength = sclength.toUInt(0, 16); 151 clength = sclength.toUInt(0, 16);
152 printf("HttpComm::Incoming: chunk length: %d\n", clength); 152 printf("HttpComm::Incoming: chunk length: %d\n", clength);
153 printf("HttpComm::Incoming: chunk length string: %s\n", sclength.latin1());
153 //end of data 154 //end of data
154 if(clength==0) 155 if(clength==0)
155 { 156 {
156 processBody(); 157 processBody();
157 done=true; 158 done=true;
158 return; 159 return;
159 } 160 }
160 //still more, but it hasnt been recieved yet 161 //still more, but it hasnt been recieved yet
161 if(ba <= j) 162 if(ba <= j)
162 { 163 {
163 status=1; 164 status=2;
164 done=true; 165 done=true;
165 break; 166 break;
166 } 167 }
167 //still more data waiting 168 //still more data waiting
168 else 169 else
169 { 170 {
170 done=false; 171 done=false;
171 //remove the chunk length header 172 //remove the chunk length header
172 tempQString.remove(0,j+1); 173 tempQString.remove(0,j+1);
173 } 174 }
174 bRead=0; 175 bRead=0;
175 // break; 176 // break;
176 //if there is more fall through to: 177 //if there is more fall through to:
177 //chunk length just read, still more in tempQstring 178 //chunk length just read, still more in tempQstring
178 case 1: 179 case 1:
179 //the current data extends beyond the end of the chunk 180 //the current data extends beyond the end of the chunk
180 if(bRead + tempQString.length() > clength) 181 if(bRead + tempQString.length() > clength)
181 { 182 {
182 QString newTQstring = tempQString; 183 QString newTQstring = tempQString;
183 newTQstring.truncate(clength-bRead); 184 newTQstring.truncate(clength-bRead);
184 bRead+=newTQstring.length(); 185 bRead+=newTQstring.length();
185 body+=newTQstring; 186 body+=newTQstring;
186 printf("HttpComm::incoming: start new body piece 1: \n"); 187 printf("HttpComm::incoming: start new body piece 1: \n");
187 printf("%s", newTQstring.latin1() ); 188 printf("%s", newTQstring.latin1() );
188 printf("HttpComm::incoming: end new body piece 1.\n"); 189 printf("HttpComm::incoming: end new body piece 1.\n");
189 status=0; 190 status=0;
190 j=clength-bRead; 191 tempQString = tempQString.remove(0, clength);
191 done=false; 192 done=false;
192 // break; 193 // break;
193 } 194 }
194 //the chunk extends beyond the current data; 195 //the chunk extends beyond the current data;
195 else 196 else
196 { 197 {
197 body+=tempQString; 198 if(tempQString.length() <= ba)
198 bRead+=tempQString.length(); 199 {
200 body+=tempQString;
201 bRead+=tempQString.length();
202 }
203 else
204 {
205 tempQString.truncate(ba);
206 body+=tempQString;
207 bRead+=tempQString.length();
208 }
199 printf("HttpComm::incoming: start new body piece 2: \n"); 209 printf("HttpComm::incoming: start new body piece 2: \n");
200 printf("%s", tempQString.latin1() ); 210 printf("%s", tempQString.latin1() );
201 printf("HttpComm::incoming: end new body piece 2.\n"); 211 printf("HttpComm::incoming: end new body piece 2.\n");
202 done=true; 212 done=true;
203 status=2; 213 status=2;
204 // break; 214 // break;
205 } 215 }
206 break; 216 break;
207 //just got data in, continue reading chunk 217 //just got data in, continue reading chunk
208 case 2: 218 case 2:
209 //the current data extends beyond the end of the chunk 219 //the current data extends beyond the end of the chunk
210 if(bRead + tempQString.length() > clength) 220 if(bRead + tempQString.length() > clength)
211 { 221 {
212 QString newTQstring = tempQString; 222 QString newTQstring = tempQString;
213 newTQstring.truncate(clength-bRead); 223 newTQstring.truncate(clength-bRead);
214 bRead+=newTQstring.length(); 224 bRead+=newTQstring.length();
215 body+=newTQstring; 225 body+=newTQstring;
216 printf("HttpComm::incoming: start new body piece 3: \n"); 226 printf("HttpComm::incoming: start new body piece 3: \n");
217 printf("%s", newTQstring.latin1() ); 227 printf("%s", newTQstring.latin1() );
218 printf("HttpComm::incoming: end new body piece 3.\n"); 228 printf("HttpComm::incoming: end new body piece 3.\n");
219 status=0; 229 status=0;
220 j=clength-bRead; 230 tempQString = tempQString.remove(0, clength);
221 done=false; 231 done=false;
222 // break; 232 // break;
223 } 233 }
224 //the chunk extends beyond the current data; 234 //the chunk extends beyond the current data;
225 else 235 else
226 { 236 {
227 body+=tempQString; 237 if(tempQString.length() <= ba)
228 bRead+=tempQString.length(); 238 {
239 body+=tempQString;
240 bRead+=tempQString.length();
241 }
242 else
243 {
244 tempQString.truncate(ba);
245 body+=tempQString;
246 bRead+=tempQString.length();
247 }
229 printf("HttpComm::incoming: start new body piece 4: \n"); 248 printf("HttpComm::incoming: start new body piece 4: \n");
230 printf("%s", tempQString.latin1() ); 249 printf("%s", tempQString.latin1() );
231 printf("HttpComm::incoming: end new body piece 4.\n"); 250 printf("HttpComm::incoming: end new body piece 4.\n");
232 done=true; 251 done=true;
233 status=2; 252 status=2;
234 // break; 253 // break;
235 } 254 }
236 break; 255 break;
237 } 256 }
238 printf("HttpComm::incoming: chunked encoding: bRead: %d\n", bRead); 257 printf("HttpComm::incoming: chunked encoding: bRead: %d\n", bRead);
239 } 258 }
240 } 259 }
241 } 260 }
242 delete tempString; 261 delete tempString;
243} 262}
244 263
245void HttpComm::connectionClosed() 264void HttpComm::connectionClosed()
246{ 265{
247 printf("HttpComm::connectionClosed: connection closed\n"); 266 printf("HttpComm::connectionClosed: connection closed\n");
248 processBody(); 267 processBody();
249} 268}
250 269
251void HttpComm::parseHeader() 270void HttpComm::parseHeader()
252{ 271{
253 QStringList headerLines, tempList; 272 QStringList headerLines, tempList;
254 int i; 273 int i;
255 274
256 printf("HttpComm::parseHeader: start header\n\n"); 275 printf("HttpComm::parseHeader: start header\n\n");
257 printf("%s", header.latin1()); 276 printf("%s", header.latin1());
258 printf("HttpComm::parseHeader: end header\n"); 277 printf("HttpComm::parseHeader: end header\n");
259 278
260 headerLines = QStringList::split('\n', header); 279 headerLines = QStringList::split('\n', header);
261 280
262 for(i=0; i<headerLines.count(); i++) 281 for(i=0; i<headerLines.count(); i++)
263 { 282 {
264 if(headerLines[i].startsWith("Content-Length:") ) 283 if(headerLines[i].startsWith("Content-Length:") )
265 { 284 {
266 tempList = QStringList::split(':', headerLines[i]); 285 tempList = QStringList::split(':', headerLines[i]);
267 tempList[1].stripWhiteSpace(); 286 tempList[1].stripWhiteSpace();
268 length = tempList[1].toUInt(); 287 length = tempList[1].toUInt();
269 } 288 }
270 else if(headerLines[i].startsWith("Transfer-Encoding: chunked") ) 289 else if(headerLines[i].startsWith("Transfer-Encoding: chunked") )
271 { 290 {
272 printf("HttpComm::parseHeader: using chunked encoding\n"); 291 printf("HttpComm::parseHeader: using chunked encoding\n");
273 chunked = true; 292 chunked = true;
274 status=0; 293 status=0;
275 } 294 }
276 } 295 }
277 296
278 printf("HttpConn::parseHeader: content-length: %d\n", length); 297 printf("HttpConn::parseHeader: content-length: %d\n", length);
279} 298}
280 299
281void HttpComm::processBody() 300void HttpComm::processBody()
282{ 301{
283 printf("HttpComm::processBody: processing body\n"); 302 printf("HttpComm::processBody: processing body\n");
284 //printf("HttpComm::processBody: start body\n\n"); 303 //printf("HttpComm::processBody: start body\n\n");
285 //printf("%s", body.latin1()); 304 //printf("%s", body.latin1());
286 //printf("HttpComm::processBody: end body\n"); 305 //printf("HttpComm::processBody: end body\n");
287 306
288 int lastSlash = file.findRev('/'); 307 int lastSlash = file.findRev('/');
289 308
290 QString end = file; 309 QString end = file;
291 end.truncate(lastSlash+1); 310 end.truncate(lastSlash+1);
292 QString context("http://"+host+':'+portS+end); 311 QString context("http://"+host+':'+portS+end);
293 printf("HttpComm::processBody: context: %s\n", context.latin1() ); 312 printf("HttpComm::processBody: context: %s\n", context.latin1() );
294 313
295 browser->setTextFormat(RichText); 314 browser->setTextFormat(RichText);
296 browser->mimeSourceFactory()->setFilePath(context); 315 browser->mimeSourceFactory()->setFilePath(context);
297 browser->setText(body, context); 316 browser->setText(body, context);
298} 317}