From 55ccc9a0dffdc96b7a5c6d41de2e97d2cd1741d9 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 22 Sep 2012 12:52:01 +0100 Subject: Solve a whole bunch of Twitter module crashes: Twitter responses seem to be getting truncated sometimes. Treat this as an error in http_client already instead of returning partial data. --- lib/http_client.c | 17 +++++++++++++++-- lib/http_client.h | 2 +- lib/misc.c | 4 ++-- lib/misc.h | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/http_client.c b/lib/http_client.c index 98a99f7c..6e7c7b72 100644 --- a/lib/http_client.c +++ b/lib/http_client.c @@ -197,7 +197,8 @@ static gboolean http_incoming_data( gpointer data, int source, b_input_condition struct http_request *req = data; int evil_server = 0; char buffer[2048]; - char *end1, *end2; + char *end1, *end2, *s; + size_t content_length; int st; if( req->inpa > 0 ) @@ -480,7 +481,7 @@ got_reply: /* Assume that a closed connection means we're finished, this indeed breaks with keep-alive connections and faulty connections. */ - req->finished = 1; + /* req->finished = 1; */ cleanup: if( req->ssl ) @@ -488,6 +489,18 @@ cleanup: else closesocket( req->fd ); + if( ( s = get_rfc822_header( req->reply_headers, "Content-Length", 0 ) ) && + sscanf( s, "%zd", &content_length ) == 1 ) + { + if( content_length < req->body_size ) + { + req->status_code = -1; + g_free( req->status_string ); + req->status_string = g_strdup( "Response truncated" ); + } + } + g_free( s ); + if( getenv( "BITLBEE_DEBUG" ) && req ) printf( "Finishing HTTP request with status: %s\n", req->status_string ? req->status_string : "NULL" ); diff --git a/lib/http_client.h b/lib/http_client.h index 27c484ff..623f17a0 100644 --- a/lib/http_client.h +++ b/lib/http_client.h @@ -58,7 +58,7 @@ struct http_request char *reply_headers; char *reply_body; int body_size; /* The number of bytes in reply_body. */ - int finished; /* Set to non-0 if the request was completed + /* int finished; Set to non-0 if the request was completed successfully. */ int redir_ttl; /* You can set it to 0 if you don't want http_client to follow them. */ diff --git a/lib/misc.c b/lib/misc.c index a7065757..485406a6 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -729,10 +729,10 @@ char **split_command_parts( char *command ) return cmd; } -char *get_rfc822_header( char *text, char *header, int len ) +char *get_rfc822_header( const char *text, const char *header, int len ) { int hlen = strlen( header ), i; - char *ret; + const char *ret; if( text == NULL ) return NULL; diff --git a/lib/misc.h b/lib/misc.h index 7e03de2d..e2ec0274 100644 --- a/lib/misc.h +++ b/lib/misc.h @@ -67,6 +67,6 @@ G_MODULE_EXPORT char *word_wrap( const char *msg, int line_len ); G_MODULE_EXPORT gboolean ssl_sockerr_again( void *ssl ); G_MODULE_EXPORT int md5_verify_password( char *password, char *hash ); G_MODULE_EXPORT char **split_command_parts( char *command ); -G_MODULE_EXPORT char *get_rfc822_header( char *text, char *header, int len ); +G_MODULE_EXPORT char *get_rfc822_header( const char *text, const char *header, int len ); #endif -- cgit v1.2.3 From d0752e8b08d37395fd036046552fa6b4fb92ac17 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 22 Sep 2012 13:12:12 +0100 Subject: Little cleanup. Use xt_from_string() where possible. --- lib/xmltree.c | 7 +++++-- lib/xmltree.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/xmltree.c b/lib/xmltree.c index 74292be9..91d256d2 100644 --- a/lib/xmltree.c +++ b/lib/xmltree.c @@ -262,13 +262,16 @@ void xt_cleanup( struct xt_parser *xt, struct xt_node *node, int depth ) } } -struct xt_node *xt_from_string( const char *in ) +struct xt_node *xt_from_string( const char *in, int len ) { struct xt_parser *parser; struct xt_node *ret; + if( len == 0 ) + len = strlen( in ); + parser = xt_new( NULL, NULL ); - xt_feed( parser, in, strlen( in ) ); + xt_feed( parser, in, len ); ret = parser->root; parser->root = NULL; xt_free( parser ); diff --git a/lib/xmltree.h b/lib/xmltree.h index 5a0dbc8e..ac77fada 100644 --- a/lib/xmltree.h +++ b/lib/xmltree.h @@ -81,7 +81,7 @@ void xt_reset( struct xt_parser *xt ); int xt_feed( struct xt_parser *xt, const char *text, int text_len ); int xt_handle( struct xt_parser *xt, struct xt_node *node, int depth ); void xt_cleanup( struct xt_parser *xt, struct xt_node *node, int depth ); -struct xt_node *xt_from_string( const char *in ); +struct xt_node *xt_from_string( const char *in, int text_len ); char *xt_to_string( struct xt_node *node ); void xt_print( struct xt_node *node ); struct xt_node *xt_dup( struct xt_node *node ); -- cgit v1.2.3 From 11ec07811cc41e1b244467d25772ef021be9db1b Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 22 Sep 2012 13:44:47 +0100 Subject: Since I can't figure out where the stalls are coming from at this point, at least have a work-around for it. After hitting the Twitter timer a few times while a previous fetch still seems to be running, close the connection. Auto-reconnect will do the rest. --- lib/http_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/http_client.c b/lib/http_client.c index 6e7c7b72..7ed539d0 100644 --- a/lib/http_client.c +++ b/lib/http_client.c @@ -72,7 +72,7 @@ struct http_request *http_dorequest( char *host, int port, int ssl, char *reques if( getenv( "BITLBEE_DEBUG" ) ) printf( "About to send HTTP request:\n%s\n", req->request ); - return( req ); + return req; } struct http_request *http_dorequest_url( char *url_string, http_input_function func, gpointer data ) -- cgit v1.2.3