Changeset 9779c18 for protocols/http_client.c
- Timestamp:
- 2006-06-03T20:20:43Z (18 years ago)
- Branches:
- master
- Children:
- 5973412
- Parents:
- a15c097 (diff), fb62f81f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/http_client.c
ra15c097 r9779c18 69 69 70 70 return( req ); 71 } 72 73 void *http_dorequest_url( char *url_string, http_input_function func, gpointer data ) 74 { 75 url_t *url = g_new0( url_t, 1 ); 76 char *request; 77 void *ret; 78 79 if( !url_set( url, url_string ) ) 80 { 81 g_free( url ); 82 return NULL; 83 } 84 85 if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS ) 86 { 87 g_free( url ); 88 return NULL; 89 } 90 91 request = g_strdup_printf( "GET %s HTTP/1.0\r\n" 92 "Host: %s\r\n" 93 "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n" 94 "\r\n", url->file, url->host ); 95 96 ret = http_dorequest( url->host, url->port, 97 url->proto == PROTO_HTTPS, request, func, data ); 98 99 g_free( url ); 100 g_free( request ); 101 return ret; 71 102 } 72 103 … … 126 157 127 158 error: 159 req->status_string = g_strdup( "Error while writing HTTP request" ); 160 128 161 req->func( req ); 129 162 … … 185 218 if( !sockerr_again() ) 186 219 { 220 req->status_string = g_strdup( strerror( errno ) ); 187 221 goto cleanup; 188 222 } … … 209 243 210 244 got_reply: 245 /* Maybe if the webserver is overloaded, or when there's bad SSL 246 support... */ 247 if( req->bytes_read == 0 ) 248 { 249 req->status_string = g_strdup( "Empty HTTP reply" ); 250 goto cleanup; 251 } 252 211 253 /* Zero termination is very convenient. */ 212 254 req->reply_headers[req->bytes_read] = 0; … … 222 264 evil_server = 1; 223 265 } 224 else 266 else if( end1 ) 225 267 { 226 268 end1 += 2; 227 269 } 228 229 if( end1 ) 230 { 231 *end1 = 0; 232 233 if( evil_server ) 234 req->reply_body = end1 + 1; 270 else 271 { 272 req->status_string = g_strdup( "Malformed HTTP reply" ); 273 goto cleanup; 274 } 275 276 *end1 = 0; 277 278 if( evil_server ) 279 req->reply_body = end1 + 1; 280 else 281 req->reply_body = end1 + 2; 282 283 req->body_size = req->reply_headers + req->bytes_read - req->reply_body; 284 285 if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL ) 286 { 287 if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 ) 288 { 289 req->status_string = g_strdup( "Can't parse status code" ); 290 req->status_code = -1; 291 } 235 292 else 236 req->reply_body = end1 + 2; 237 } 238 239 if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL ) 240 { 241 if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 ) 242 req->status_code = -1; 243 } 244 else 245 { 293 { 294 char *eol; 295 296 if( evil_server ) 297 eol = strchr( end1, '\n' ); 298 else 299 eol = strchr( end1, '\r' ); 300 301 req->status_string = g_strndup( end1 + 1, eol - end1 - 1 ); 302 303 /* Just to be sure... */ 304 if( ( eol = strchr( req->status_string, '\r' ) ) ) 305 *eol = 0; 306 if( ( eol = strchr( req->status_string, '\n' ) ) ) 307 *eol = 0; 308 } 309 } 310 else 311 { 312 req->status_string = g_strdup( "Can't locate status code" ); 246 313 req->status_code = -1; 247 314 } … … 252 319 int error = 0, new_port, new_proto; 253 320 321 /* We might fill it again, so let's not leak any memory. */ 322 g_free( req->status_string ); 323 req->status_string = NULL; 324 254 325 loc = strstr( req->reply_headers, "\nLocation: " ); 255 326 if( loc == NULL ) /* We can't handle this redirect... */ 327 { 328 req->status_string = g_strdup( "Can't locate Location: header" ); 256 329 goto cleanup; 330 } 257 331 258 332 loc += 11; … … 271 345 don't need this yet anyway, I won't implement it. */ 272 346 347 req->status_string = g_strdup( "Can't handle recursive redirects" ); 348 273 349 goto cleanup; 274 350 } … … 288 364 if( !url_set( url, loc ) ) 289 365 { 366 req->status_string = g_strdup( "Malformed redirect URL" ); 290 367 g_free( url ); 291 368 goto cleanup; … … 298 375 going to use strcat(), whether you like it or not. :-) */ 299 376 300 /* First, find the GET/POST/whatever from the original request. */ 301 s = strchr( req->request, ' ' ); 377 sprintf( new_request, "GET %s HTTP/1.0", url->file ); 378 379 s = strstr( req->request, "\r\n" ); 302 380 if( s == NULL ) 303 381 { 382 req->status_string = g_strdup( "Error while rebuilding request string" ); 304 383 g_free( new_request ); 305 384 g_free( url ); … … 307 386 } 308 387 309 *s = 0; 310 sprintf( new_request, "%s %s HTTP/1.0\r\n", req->request, url->file ); 311 *s = ' '; 312 313 s = strstr( req->request, "\r\n" ); 314 if( s == NULL ) 315 { 316 g_free( new_request ); 317 g_free( url ); 318 goto cleanup; 319 } 320 321 strcat( new_request, s + 2 ); 388 strcat( new_request, s ); 322 389 new_host = g_strdup( url->host ); 323 390 new_port = url->port; … … 333 400 334 401 req->fd = -1; 335 req->ssl = 0;402 req->ssl = NULL; 336 403 337 404 if( new_proto == PROTO_HTTPS ) … … 351 418 if( error ) 352 419 { 420 req->status_string = g_strdup( "Connection problem during redirect" ); 353 421 g_free( new_request ); 354 422 goto cleanup; … … 379 447 g_free( req->request ); 380 448 g_free( req->reply_headers ); 449 g_free( req->status_string ); 381 450 g_free( req ); 382 451 }
Note: See TracChangeset
for help on using the changeset viewer.