From 0790644bf8ccfbb1f09f4e7209fc18c2e97f10d8 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Wed, 17 May 2006 15:15:20 +0200 Subject: Added http_dorequest_url(). --- protocols/http_client.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index 9417e200..893ba551 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -70,6 +70,37 @@ void *http_dorequest( char *host, int port, int ssl, char *request, http_input_f return( req ); } +void *http_dorequest_url( char *url_string, http_input_function func, gpointer data ) +{ + url_t *url = g_new0( url_t, 1 ); + char *request; + void *ret; + + if( !url_set( url, url_string ) ) + { + g_free( url ); + return NULL; + } + + if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS ) + { + g_free( url ); + return NULL; + } + + request = g_strdup_printf( "GET %s HTTP/1.0\r\n" + "Host: %s\r\n" + "User-Agent: BitlBee " BITLBEE_VERSION "\r\n" + "\r\n", url->file, url->host ); + + ret = http_dorequest( url->host, url->port, + url->proto == PROTO_HTTPS, request, func, data ); + + g_free( url ); + g_free( request ); + return NULL; +} + /* This one is actually pretty simple... Might get more calls if we can't write the whole request at once. */ static void http_connected( gpointer data, int source, GaimInputCondition cond ) -- cgit v1.2.3 From 266fe2fe078833bc5489a3fddd970b9307a7bbfa Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 18 May 2006 09:11:15 +0200 Subject: Fixed return value bug in http_dorequest_url(). --- protocols/http_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index 893ba551..46cb8b7b 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -98,7 +98,7 @@ void *http_dorequest_url( char *url_string, http_input_function func, gpointer d g_free( url ); g_free( request ); - return NULL; + return ret; } /* This one is actually pretty simple... Might get more calls if we can't write -- cgit v1.2.3 From 0eec3866ac883667045cc028d5f0dac0b4872de7 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 18 May 2006 18:41:18 +0200 Subject: Added a body_size attribute to http_client and fixed a possible NULL dereference bug. --- protocols/http_client.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index 46cb8b7b..49e6dd83 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -252,21 +252,24 @@ got_reply: end1 = end2 + 1; evil_server = 1; } - else + else if( end1 ) { end1 += 2; } - - if( end1 ) + else { - *end1 = 0; - - if( evil_server ) - req->reply_body = end1 + 1; - else - req->reply_body = end1 + 2; + goto cleanup; } + *end1 = 0; + + if( evil_server ) + req->reply_body = end1 + 1; + else + req->reply_body = end1 + 2; + + req->body_size = req->reply_headers + bytes_read - req->reply_body; + if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL ) { if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 ) -- cgit v1.2.3 From 41e520279f8633d11f79623574b40af7f8949403 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 18 May 2006 18:50:06 +0200 Subject: Damn typo... --- protocols/http_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index 49e6dd83..ead6eb09 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -268,7 +268,7 @@ got_reply: else req->reply_body = end1 + 2; - req->body_size = req->reply_headers + bytes_read - req->reply_body; + req->body_size = req->reply_headers + req->bytes_read - req->reply_body; if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL ) { -- cgit v1.2.3 From 1b5ab3608488e0cbbc75933a59af1fa7e2ff60b0 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 18 May 2006 19:05:28 +0200 Subject: User-Agent: header. --- protocols/http_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index ead6eb09..5db31782 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -90,7 +90,7 @@ void *http_dorequest_url( char *url_string, http_input_function func, gpointer d request = g_strdup_printf( "GET %s HTTP/1.0\r\n" "Host: %s\r\n" - "User-Agent: BitlBee " BITLBEE_VERSION "\r\n" + "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n" "\r\n", url->file, url->host ); ret = http_dorequest( url->host, url->port, -- cgit v1.2.3 From 0602496cb6cedc917abbd0a12468e9329c6967e1 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 25 May 2006 13:22:06 +0200 Subject: Better handling of completely empty HTTP replies in http_client.c. --- protocols/http_client.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index 5db31782..e181438c 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -239,6 +239,11 @@ static void http_incoming_data( gpointer data, int source, GaimInputCondition co return; got_reply: + /* Maybe if the webserver is overloaded, or when there's bad SSL + support... */ + if( req->bytes_read == 0 ) + goto cleanup; + /* Zero termination is very convenient. */ req->reply_headers[req->bytes_read] = 0; -- cgit v1.2.3 From 7deb4471891059edf6000ffc7502a2a7bdc70e78 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Fri, 26 May 2006 20:32:50 +0200 Subject: Added status_string variable to http_client. --- protocols/http_client.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index e181438c..88aadff3 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -156,6 +156,8 @@ static void http_connected( gpointer data, int source, GaimInputCondition cond ) return; error: + req->status_string = g_strdup( "Error while writing HTTP request" ); + req->func( req ); g_free( req->request ); @@ -215,6 +217,7 @@ static void http_incoming_data( gpointer data, int source, GaimInputCondition co { if( !sockerr_again() ) { + req->status_string = g_strdup( strerror( errno ) ); goto cleanup; } } @@ -242,7 +245,10 @@ got_reply: /* Maybe if the webserver is overloaded, or when there's bad SSL support... */ if( req->bytes_read == 0 ) + { + req->status_string = g_strdup( "Empty HTTP reply" ); goto cleanup; + } /* Zero termination is very convenient. */ req->reply_headers[req->bytes_read] = 0; @@ -263,6 +269,7 @@ got_reply: } else { + req->status_string = g_strdup( "Malformed HTTP reply" ); goto cleanup; } @@ -278,10 +285,31 @@ got_reply: if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL ) { if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 ) + { + req->status_string = g_strdup( "Can't parse status code" ); req->status_code = -1; + } + else + { + char *eol; + + if( evil_server ) + eol = strchr( end1, '\n' ); + else + eol = strchr( end1, '\r' ); + + req->status_string = g_strndup( end1 + 1, eol - end1 - 1 ); + + /* Just to be sure... */ + if( ( eol = strchr( req->status_string, '\r' ) ) ) + *eol = 0; + if( ( eol = strchr( req->status_string, '\n' ) ) ) + *eol = 0; + } } else { + req->status_string = g_strdup( "Can't locate status code" ); req->status_code = -1; } @@ -290,9 +318,16 @@ got_reply: char *loc, *new_request, *new_host; int error = 0, new_port, new_proto; + /* We might fill it again, so let's not leak any memory. */ + g_free( req->status_string ); + req->status_string = NULL; + loc = strstr( req->reply_headers, "\nLocation: " ); if( loc == NULL ) /* We can't handle this redirect... */ + { + req->status_string = g_strdup( "Can't locate Location: header" ); goto cleanup; + } loc += 11; while( *loc == ' ' ) @@ -309,6 +344,8 @@ got_reply: /* Since we don't cache the servername, and since we don't need this yet anyway, I won't implement it. */ + req->status_string = g_strdup( "Can't handle recursive redirects" ); + goto cleanup; } else @@ -326,6 +363,7 @@ got_reply: if( !url_set( url, loc ) ) { + req->status_string = g_strdup( "Malformed redirect URL" ); g_free( url ); goto cleanup; } @@ -340,6 +378,7 @@ got_reply: s = strchr( req->request, ' ' ); if( s == NULL ) { + req->status_string = g_strdup( "Error while rebuilding request string" ); g_free( new_request ); g_free( url ); goto cleanup; @@ -352,6 +391,7 @@ got_reply: s = strstr( req->request, "\r\n" ); if( s == NULL ) { + req->status_string = g_strdup( "Error while rebuilding request string" ); g_free( new_request ); g_free( url ); goto cleanup; @@ -371,7 +411,7 @@ got_reply: closesocket( req->fd ); req->fd = -1; - req->ssl = 0; + req->ssl = NULL; if( new_proto == PROTO_HTTPS ) { @@ -389,6 +429,7 @@ got_reply: if( error ) { + req->status_string = g_strdup( "Connection problem during redirect" ); g_free( new_request ); goto cleanup; } @@ -417,5 +458,6 @@ cleanup: g_free( req->request ); g_free( req->reply_headers ); + g_free( req->status_string ); g_free( req ); } -- cgit v1.2.3 From fe237200e4b3921e190d13693402e14d63fe2fa4 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Fri, 26 May 2006 20:35:16 +0200 Subject: Always use GET-requests on redirects. --- protocols/http_client.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'protocols/http_client.c') diff --git a/protocols/http_client.c b/protocols/http_client.c index 88aadff3..c793d9d4 100644 --- a/protocols/http_client.c +++ b/protocols/http_client.c @@ -374,19 +374,7 @@ got_reply: /* So, now I just allocated enough memory, so I'm going to use strcat(), whether you like it or not. :-) */ - /* First, find the GET/POST/whatever from the original request. */ - s = strchr( req->request, ' ' ); - if( s == NULL ) - { - req->status_string = g_strdup( "Error while rebuilding request string" ); - g_free( new_request ); - g_free( url ); - goto cleanup; - } - - *s = 0; - sprintf( new_request, "%s %s HTTP/1.0\r\n", req->request, url->file ); - *s = ' '; + sprintf( new_request, "GET %s HTTP/1.0", url->file ); s = strstr( req->request, "\r\n" ); if( s == NULL ) @@ -397,7 +385,7 @@ got_reply: goto cleanup; } - strcat( new_request, s + 2 ); + strcat( new_request, s ); new_host = g_strdup( url->host ); new_port = url->port; new_proto = url->proto; -- cgit v1.2.3