author | spiralman <spiralman> | 2003-03-15 16:19:51 (UTC) |
---|---|---|
committer | spiralman <spiralman> | 2003-03-15 16:19:51 (UTC) |
commit | d421f8708983df4bab1f9069e87cda94e1d4aeea (patch) (unidiff) | |
tree | 3aa7587f72705f73e65a5dc1aa3e824311fba2d1 | |
parent | 69823b154b29cd62c9d53f7ebdaae4cb7dd61939 (diff) | |
download | opie-d421f8708983df4bab1f9069e87cda94e1d4aeea.zip opie-d421f8708983df4bab1f9069e87cda94e1d4aeea.tar.gz opie-d421f8708983df4bab1f9069e87cda94e1d4aeea.tar.bz2 |
HttpFactory::data() now returns 0 if there is a 404 error (previously, if there was a 404 error on an image, the browser would get stuck in an infinite loop).
also, fixed html detection
-rw-r--r-- | noncore/net/ubrowser/httpfactory.cpp | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/noncore/net/ubrowser/httpfactory.cpp b/noncore/net/ubrowser/httpfactory.cpp index 4ace4cb..b37e9f9 100644 --- a/noncore/net/ubrowser/httpfactory.cpp +++ b/noncore/net/ubrowser/httpfactory.cpp | |||
@@ -130,212 +130,218 @@ const QMimeSource * HttpFactory::data(const QString &abs_name) const | |||
130 | serverAddr.sin_addr.s_addr = inet_addr( inet_ntoa(*((struct in_addr *)serverInfo->h_addr )) ); | 130 | serverAddr.sin_addr.s_addr = inet_addr( inet_ntoa(*((struct in_addr *)serverInfo->h_addr )) ); |
131 | memset( &(serverAddr.sin_zero), '\0', 8 ); | 131 | memset( &(serverAddr.sin_zero), '\0', 8 ); |
132 | 132 | ||
133 | if(::connect( con, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr)) == -1 ) | 133 | if(::connect( con, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr)) == -1 ) |
134 | { | 134 | { |
135 | QMessageBox *mb = new QMessageBox("Error!", | 135 | QMessageBox *mb = new QMessageBox("Error!", |
136 | "couldnt connect to socket", | 136 | "couldnt connect to socket", |
137 | QMessageBox::NoIcon, | 137 | QMessageBox::NoIcon, |
138 | QMessageBox::Ok, | 138 | QMessageBox::Ok, |
139 | QMessageBox::NoButton, | 139 | QMessageBox::NoButton, |
140 | QMessageBox::NoButton); | 140 | QMessageBox::NoButton); |
141 | mb->exec(); | 141 | mb->exec(); |
142 | perror("HttpFactory::data:"); | 142 | perror("HttpFactory::data:"); |
143 | return 0; | 143 | return 0; |
144 | } | 144 | } |
145 | 145 | ||
146 | 146 | ||
147 | bytesSent = send( con, request.latin1(), request.length(), 0); | 147 | bytesSent = send( con, request.latin1(), request.length(), 0); |
148 | printf("HttpFactory::data: bytes written: %d out of: %d\n", bytesSent, request.length() ); | 148 | printf("HttpFactory::data: bytes written: %d out of: %d\n", bytesSent, request.length() ); |
149 | printf("HttpFactory::data: request sent:\n%s", request.latin1()); | 149 | printf("HttpFactory::data: request sent:\n%s", request.latin1()); |
150 | 150 | ||
151 | data = this->processResponse( con, isText ); | 151 | data = this->processResponse( con, isText ); |
152 | 152 | ||
153 | ::close( con ); | 153 | ::close( con ); |
154 | 154 | ||
155 | if(isText) | 155 | if(isText) |
156 | { | 156 | { |
157 | text->setText( QString( data ) ); | 157 | text->setText( QString( data ) ); |
158 | return text; | 158 | return text; |
159 | } | 159 | } |
160 | else | 160 | else |
161 | { | 161 | { |
162 | image->setImage( QImage( data ) ); | 162 | image->setImage( QImage( data ) ); |
163 | return image; | 163 | return image; |
164 | } | 164 | } |
165 | } | 165 | } |
166 | 166 | ||
167 | const QMimeSource * HttpFactory::data(const QString &abs_or_rel_name, const QString & context) const | 167 | const QMimeSource * HttpFactory::data(const QString &abs_or_rel_name, const QString & context) const |
168 | { | 168 | { |
169 | printf("HttpFactory::data: using relative data func\n"); | 169 | printf("HttpFactory::data: using relative data func\n"); |
170 | 170 | ||
171 | if(abs_or_rel_name.startsWith(context)) | 171 | if(abs_or_rel_name.startsWith(context)) |
172 | { | 172 | { |
173 | return data(abs_or_rel_name); | 173 | return data(abs_or_rel_name); |
174 | } | 174 | } |
175 | else | 175 | else |
176 | { | 176 | { |
177 | return data(context + abs_or_rel_name); | 177 | return data(context + abs_or_rel_name); |
178 | } | 178 | } |
179 | 179 | ||
180 | return 0; | 180 | return 0; |
181 | } | 181 | } |
182 | 182 | ||
183 | const QByteArray HttpFactory::processResponse( int sockfd, bool &isText ) const | 183 | const QByteArray HttpFactory::processResponse( int sockfd, bool &isText ) const |
184 | { | 184 | { |
185 | QByteArray inputBin( 1 ); | 185 | QByteArray inputBin( 1 ); |
186 | QByteArray conClosedErr( 27 ); | 186 | QByteArray conClosedErr( 27 ); |
187 | char conClosedErrMsg[] = "Connection to server closed"; | 187 | char conClosedErrMsg[] = "Connection to server closed"; |
188 | QString currentLine; | 188 | QString currentLine; |
189 | bool done=false, chunked=false; | 189 | bool done=false, chunked=false; |
190 | int dataLength = 0; | 190 | int dataLength = 0; |
191 | 191 | ||
192 | for( int i = 0; i < 27; i++) | 192 | for( int i = 0; i < 27; i++) |
193 | { | 193 | { |
194 | conClosedErr[i] = conClosedErrMsg[i]; | 194 | conClosedErr[i] = conClosedErrMsg[i]; |
195 | } | 195 | } |
196 | 196 | ||
197 | while( !done ) | 197 | while( !done ) |
198 | { | 198 | { |
199 | recv( sockfd, inputBin.data(), inputBin.size(), 0 ); | 199 | recv( sockfd, inputBin.data(), inputBin.size(), 0 ); |
200 | currentLine += *(inputBin.data()); | 200 | currentLine += *(inputBin.data()); |
201 | 201 | ||
202 | if( *(inputBin.data()) == '\n' ) | 202 | if( *(inputBin.data()) == '\n' ) |
203 | { | 203 | { |
204 | if( currentLine.isEmpty() || currentLine.startsWith( "\r") ) | 204 | if( currentLine.isEmpty() || currentLine.startsWith( "\r") ) |
205 | { | 205 | { |
206 | printf( "HttpFactory::processResponse: end of header\n" ); | 206 | printf( "HttpFactory::processResponse: end of header\n" ); |
207 | if( chunked ) | 207 | if( chunked ) |
208 | { | 208 | { |
209 | return recieveChunked( sockfd ); | 209 | return recieveChunked( sockfd ); |
210 | } else { | 210 | } else { |
211 | return recieveNormal( sockfd, dataLength ); | 211 | return recieveNormal( sockfd, dataLength ); |
212 | } | 212 | } |
213 | done = true; | 213 | done = true; |
214 | } | 214 | } |
215 | 215 | ||
216 | if( currentLine.contains( "Transfer-Encoding: chunked", false) >= 1 ) | 216 | if( currentLine.contains( "Transfer-Encoding: chunked", false) >= 1 ) |
217 | { | 217 | { |
218 | chunked = true; | 218 | chunked = true; |
219 | printf( "HttpFactory::processResponse: chunked encoding\n" ); | 219 | printf( "HttpFactory::processResponse: chunked encoding\n" ); |
220 | } | 220 | } |
221 | 221 | ||
222 | if( currentLine.contains( "Content-Type: text", false ) >= 1 ) | 222 | if( currentLine.contains( "Content-Type: text", false ) >= 1 ) |
223 | { | 223 | { |
224 | isText = true; | 224 | isText = true; |
225 | printf( "HttpFactory::processResponse: content type text\n" ); | 225 | printf( "HttpFactory::processResponse: content type text\n" ); |
226 | if( currentLine.contains( "html", false ) ) | 226 | if( currentLine.contains( "html", false ) >= 1) |
227 | { | 227 | { |
228 | browser->setTextFormat(Qt::RichText); | 228 | browser->setTextFormat(Qt::RichText); |
229 | printf( "HttpFactory::processResponse: content type html\n" ); | 229 | printf( "HttpFactory::processResponse: content type html\n" ); |
230 | } | 230 | } |
231 | } | 231 | } |
232 | 232 | ||
233 | if( currentLine.contains( "Content-Type: image", false ) >= 1 ) | 233 | if( currentLine.contains( "Content-Type: image", false ) >= 1 ) |
234 | { | 234 | { |
235 | isText = false; | 235 | isText = false; |
236 | printf( "HttpFactory::processResponse: content type image\n" ); | 236 | printf( "HttpFactory::processResponse: content type image\n" ); |
237 | } | 237 | } |
238 | 238 | ||
239 | if( currentLine.contains( "Content-Length", false ) >= 1 ) | 239 | if( currentLine.contains( "Content-Length", false ) >= 1 ) |
240 | { | 240 | { |
241 | currentLine.remove( 0, 16 ); | 241 | currentLine.remove( 0, 16 ); |
242 | dataLength = currentLine.toInt(); | 242 | dataLength = currentLine.toInt(); |
243 | printf( "HttpFactory::processResponse: content length: %d\n", dataLength ); | 243 | printf( "HttpFactory::processResponse: content length: %d\n", dataLength ); |
244 | } | 244 | } |
245 | 245 | ||
246 | if( currentLine.contains( "404", false ) >= 1 ) | ||
247 | { | ||
248 | printf( "HttpFactory::processResponse: 404 error\n" ); | ||
249 | return 0; | ||
250 | } | ||
251 | |||
246 | currentLine = ""; | 252 | currentLine = ""; |
247 | printf("HttpFactory::processResponse: reseting currentLine: %s\n", currentLine.latin1() ); | 253 | printf("HttpFactory::processResponse: reseting currentLine: %s\n", currentLine.latin1() ); |
248 | } | 254 | } |
249 | } | 255 | } |
250 | } | 256 | } |
251 | 257 | ||
252 | const QByteArray HttpFactory::recieveNormal( int sockfd, int dataLen ) const | 258 | const QByteArray HttpFactory::recieveNormal( int sockfd, int dataLen ) const |
253 | { | 259 | { |
254 | printf( "HttpFactory::recieveNormal: recieving w/out chunked encoding\n" ); | 260 | printf( "HttpFactory::recieveNormal: recieving w/out chunked encoding\n" ); |
255 | 261 | ||
256 | QByteArray data( dataLen ); | 262 | QByteArray data( dataLen ); |
257 | QByteArray temp( dataLen ); | 263 | QByteArray temp( dataLen ); |
258 | int recieved, i; | 264 | int recieved, i; |
259 | 265 | ||
260 | recieved = recv( sockfd, temp.data(), temp.size(), 0 ); | 266 | recieved = recv( sockfd, temp.data(), temp.size(), 0 ); |
261 | printf( "HttpFactory::recieveNormal: found some data: %s\n", (char *)temp.data() ); | 267 | printf( "HttpFactory::recieveNormal: found some data: %s\n", (char *)temp.data() ); |
262 | for( i = 0; i < recieved; i++ ) | 268 | for( i = 0; i < recieved; i++ ) |
263 | { | 269 | { |
264 | data[i] = temp[i]; | 270 | data[i] = temp[i]; |
265 | } | 271 | } |
266 | dataLen -= recieved; | 272 | dataLen -= recieved; |
267 | while( dataLen > 0 ) | 273 | while( dataLen > 0 ) |
268 | { | 274 | { |
269 | recieved = recv( sockfd, temp.data(), temp.size(), 0 ); | 275 | recieved = recv( sockfd, temp.data(), temp.size(), 0 ); |
270 | dataLen -= recieved; | 276 | dataLen -= recieved; |
271 | // printf( "HttpFactory::recieveNormal: found some more data: %s\n", (char *)temp.data() ); | 277 | // printf( "HttpFactory::recieveNormal: found some more data: %s\n", (char *)temp.data() ); |
272 | for( int j = 0; j < recieved; j++ ) | 278 | for( int j = 0; j < recieved; j++ ) |
273 | { | 279 | { |
274 | data[i] = temp[j]; | 280 | data[i] = temp[j]; |
275 | i++; | 281 | i++; |
276 | } | 282 | } |
277 | temp.fill('\0'); | 283 | temp.fill('\0'); |
278 | } | 284 | } |
279 | 285 | ||
280 | printf( "HttpFactory::recieveNormal: end of data\n" ); | 286 | printf( "HttpFactory::recieveNormal: end of data\n" ); |
281 | return data; | 287 | return data; |
282 | } | 288 | } |
283 | 289 | ||
284 | const QByteArray HttpFactory::recieveChunked( int sockfd ) const | 290 | const QByteArray HttpFactory::recieveChunked( int sockfd ) const |
285 | { | 291 | { |
286 | printf( "HttpFactory::recieveChunked: recieving data with chunked encoding\n" ); | 292 | printf( "HttpFactory::recieveChunked: recieving data with chunked encoding\n" ); |
287 | 293 | ||
288 | QByteArray data; | 294 | QByteArray data; |
289 | QByteArray temp( 1 ); | 295 | QByteArray temp( 1 ); |
290 | int recieved, i = 0, cSize = 0; | 296 | int recieved, i = 0, cSize = 0; |
291 | QString cSizeS; | 297 | QString cSizeS; |
292 | 298 | ||
293 | printf( "HttpFactory::recieveChunked: temp.size(): %d\n", temp.size() ); | 299 | printf( "HttpFactory::recieveChunked: temp.size(): %d\n", temp.size() ); |
294 | recv( sockfd, temp.data(), temp.size(), 0 ); | 300 | recv( sockfd, temp.data(), temp.size(), 0 ); |
295 | while( *(temp.data()) != '\n' && *(temp.data()) != ';' ) | 301 | while( *(temp.data()) != '\n' && *(temp.data()) != ';' ) |
296 | { | 302 | { |
297 | // printf( "HttpFactory::recieveChunked: temp.size(): %d\n", temp.size() ); | 303 | // printf( "HttpFactory::recieveChunked: temp.size(): %d\n", temp.size() ); |
298 | // printf( "HttpFactory::recieveChunked: temp.data(): %c\n", temp[0] ); | 304 | // printf( "HttpFactory::recieveChunked: temp.data(): %c\n", temp[0] ); |
299 | cSizeS += temp[0]; | 305 | cSizeS += temp[0]; |
300 | recv( sockfd, temp.data(), temp.size(), 0 ); | 306 | recv( sockfd, temp.data(), temp.size(), 0 ); |
301 | } | 307 | } |
302 | 308 | ||
303 | printf( "HttpFactory::recieveChunked: cSizeS: %s\n", cSizeS.latin1() ); | 309 | printf( "HttpFactory::recieveChunked: cSizeS: %s\n", cSizeS.latin1() ); |
304 | cSize = cSizeS.toInt( 0, 16 ); | 310 | cSize = cSizeS.toInt( 0, 16 ); |
305 | printf( "HttpFactory::recieveChunked: first chunk of size: %d\n", cSize ); | 311 | printf( "HttpFactory::recieveChunked: first chunk of size: %d\n", cSize ); |
306 | 312 | ||
307 | if( *(temp.data()) == ';' ) | 313 | if( *(temp.data()) == ';' ) |
308 | { | 314 | { |
309 | while( *(temp.data()) != '\n' ) | 315 | while( *(temp.data()) != '\n' ) |
310 | { | 316 | { |
311 | recv( sockfd, temp.data(), temp.size(), 0 ); | 317 | recv( sockfd, temp.data(), temp.size(), 0 ); |
312 | } | 318 | } |
313 | } | 319 | } |
314 | 320 | ||
315 | temp.fill( '\0', cSize ); | 321 | temp.fill( '\0', cSize ); |
316 | data.fill( '\0', cSize ); | 322 | data.fill( '\0', cSize ); |
317 | 323 | ||
318 | while( cSize > 0 ) | 324 | while( cSize > 0 ) |
319 | { | 325 | { |
320 | while( cSize > 0 ) | 326 | while( cSize > 0 ) |
321 | { | 327 | { |
322 | recieved = recv( sockfd, temp.data(), temp.size(), 0 ); | 328 | recieved = recv( sockfd, temp.data(), temp.size(), 0 ); |
323 | cSize -= recieved; | 329 | cSize -= recieved; |
324 | for( int j = 0; j < recieved; j++ ) | 330 | for( int j = 0; j < recieved; j++ ) |
325 | { | 331 | { |
326 | data[i] = temp[j]; | 332 | data[i] = temp[j]; |
327 | i++; | 333 | i++; |
328 | } | 334 | } |
329 | temp.fill('\0', cSize); | 335 | temp.fill('\0', cSize); |
330 | } | 336 | } |
331 | 337 | ||
332 | printf( "HttpFactory::recieveChunked: current data:\n%s", data.data() ); | 338 | printf( "HttpFactory::recieveChunked: current data:\n%s", data.data() ); |
333 | 339 | ||
334 | temp.fill('\0', 1); | 340 | temp.fill('\0', 1); |
335 | cSizeS = ""; | 341 | cSizeS = ""; |
336 | cSize = 0; | 342 | cSize = 0; |
337 | 343 | ||
338 | recv( sockfd, temp.data(), temp.size(), 0 ); | 344 | recv( sockfd, temp.data(), temp.size(), 0 ); |
339 | if( *(temp.data()) == '\r' ) | 345 | if( *(temp.data()) == '\r' ) |
340 | { | 346 | { |
341 | recv( sockfd, temp.data(), temp.size(), 0 ); | 347 | recv( sockfd, temp.data(), temp.size(), 0 ); |