Changeset 03a8f8e


Ignore:
Timestamp:
2011-11-13T01:41:42Z (12 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
80acb6d
Parents:
dff732d
Message:

Cleanup of http_client fix. Use g_strdup_printf and completely avoid strcat,
truncate request if we switched from POST to GET.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/http_client.c

    rdff732d r03a8f8e  
    22  * BitlBee -- An IRC to other IM-networks gateway                     *
    33  *                                                                    *
    4   * Copyright 2002-2005 Wilmer van der Gaast and others                *
     4  * Copyright 2002-2011 Wilmer van der Gaast and others                *
    55  \********************************************************************/
    66
     
    354354                        url_t *url;
    355355                        char *s;
     356                        const char *new_method;
    356357                       
    357358                        s = strstr( loc, "\r\n" );
     
    369370                        }
    370371                       
    371                         /* Okay, this isn't fun! We have to rebuild the request... :-( */
    372                         new_request = g_malloc( req->request_length + strlen( url->file ) );
    373                        
    374                         /* So, now I just allocated enough memory, so I'm
    375                            going to use strcat(), whether you like it or not. :-) */
    376                        
    377                         *s = 0;
    378                         sprintf( new_request, "%s %s HTTP/1.0\r\nHost: %s",
    379                                  req->status_code == 303 || req->request[0] == 'G' ? "GET" : "POST", url->file, url->host );
    380                         *s = ' ';
    381                        
     372                        /* Find all headers and, if necessary, the POST request contents.
     373                           Skip the old Host: header though. This crappy code here means
     374                           anything using this http_client MUST put the Host: header at
     375                           the top. */
    382376                        if( !( ( s = strstr( req->request, "\r\nHost: " ) ) &&
    383377                               ( s = strstr( s + strlen( "\r\nHost: " ), "\r\n" ) ) ) )
    384378                        {
    385379                                req->status_string = g_strdup( "Error while rebuilding request string" );
    386                                 g_free( new_request );
    387380                                g_free( url );
    388381                                goto cleanup;
    389382                        }
    390383                       
    391                         strcat( new_request, s );
     384                        /* More or less HTTP/1.0 compliant, from my reading of RFC 2616.
     385                           Always perform a GET request unless we received a 301. 303 was
     386                           meant for this but it's HTTP/1.1-only and we're specifically
     387                           speaking HTTP/1.0. */
     388                        new_method = req->status_code != 301 || req->request[0] == 'G' ? "GET" : "POST";
     389                       
     390                        /* Okay, this isn't fun! We have to rebuild the request... :-( */
     391                        new_request = g_strdup_printf( "%s %s HTTP/1.0\r\nHost: %s%s",
     392                                                       new_method, url->file, url->host, s );
     393                       
    392394                        new_host = g_strdup( url->host );
    393395                        new_port = url->port;
    394396                        new_proto = url->proto;
     397                       
     398                        /* If we went from POST to GET, truncate the request content. */
     399                        if( new_request[0] != req->request[0] && new_request[0] == 'G' &&
     400                            ( s = strstr( new_request, "\r\n\r\n" ) ) )
     401                                s[4] = '\0';
    395402                       
    396403                        g_free( url );
Note: See TracChangeset for help on using the changeset viewer.