source: lib/http_client.c @ 1388d30

Last change on this file since 1388d30 was 1388d30, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-11T17:57:20Z

Mostly finished HTTP streaming support: Shrink the buffer and add a
http_close().

  • Property mode set to 100644
File size: 15.0 KB
RevLine 
[8a9afe4]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[03a8f8e]4  * Copyright 2002-2011 Wilmer van der Gaast and others                *
[8a9afe4]5  \********************************************************************/
6
[52b3a99]7/* HTTP(S) module                                                       */
[8a9afe4]8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#include <string.h>
[52b3a99]27#include <stdio.h>
[8a9afe4]28
29#include "http_client.h"
[52b3a99]30#include "url.h"
[e4d6271]31#include "sock.h"
[8a9afe4]32
33
[ba9edaa]34static gboolean http_connected( gpointer data, int source, b_input_condition cond );
[486ddb5]35static gboolean http_ssl_connected( gpointer data, int returncode, void *source, b_input_condition cond );
[ba9edaa]36static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond );
[516a9c6]37static void http_free( struct http_request *req );
[8a9afe4]38
39
[516a9c6]40struct http_request *http_dorequest( char *host, int port, int ssl, char *request, http_input_function func, gpointer data )
[8a9afe4]41{
42        struct http_request *req;
43        int error = 0;
44       
45        req = g_new0( struct http_request, 1 );
46       
47        if( ssl )
48        {
[a72dc2b]49                req->ssl = ssl_connect( host, port, TRUE, http_ssl_connected, req );
[8a9afe4]50                if( req->ssl == NULL )
51                        error = 1;
52        }
53        else
54        {
55                req->fd = proxy_connect( host, port, http_connected, req );
56                if( req->fd < 0 )
57                        error = 1;
58        }
59       
60        if( error )
61        {
[fb98634]62                http_free( req );
63                return NULL;
[8a9afe4]64        }
65       
[52b3a99]66        req->func = func;
67        req->data = data;
[8a9afe4]68        req->request = g_strdup( request );
69        req->request_length = strlen( request );
[7885d0f]70        req->redir_ttl = 3;
[8a9afe4]71       
[3f808ca]72        if( getenv( "BITLBEE_DEBUG" ) )
73                printf( "About to send HTTP request:\n%s\n", req->request );
74       
[11ec078]75        return req;
[8a9afe4]76}
77
[516a9c6]78struct http_request *http_dorequest_url( char *url_string, http_input_function func, gpointer data )
[0790644]79{
80        url_t *url = g_new0( url_t, 1 );
81        char *request;
82        void *ret;
83       
84        if( !url_set( url, url_string ) )
85        {
86                g_free( url );
87                return NULL;
88        }
89       
90        if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS )
91        {
92                g_free( url );
93                return NULL;
94        }
95       
96        request = g_strdup_printf( "GET %s HTTP/1.0\r\n"
97                                   "Host: %s\r\n"
[2423c93]98                                   "Connection: close\r\n"
[1b5ab36]99                                   "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n"
[0790644]100                                   "\r\n", url->file, url->host );
101       
102        ret = http_dorequest( url->host, url->port,
103                              url->proto == PROTO_HTTPS, request, func, data );
104       
105        g_free( url );
106        g_free( request );
[266fe2f]107        return ret;
[0790644]108}
109
[8a9afe4]110/* This one is actually pretty simple... Might get more calls if we can't write
111   the whole request at once. */
[ba9edaa]112static gboolean http_connected( gpointer data, int source, b_input_condition cond )
[8a9afe4]113{
114        struct http_request *req = data;
115        int st;
116       
117        if( source < 0 )
118                goto error;
119       
120        if( req->inpa > 0 )
[ba9edaa]121                b_event_remove( req->inpa );
[8a9afe4]122       
123        sock_make_nonblocking( req->fd );
124       
125        if( req->ssl )
126        {
127                st = ssl_write( req->ssl, req->request + req->bytes_written,
128                                req->request_length - req->bytes_written );
129                if( st < 0 )
130                {
131                        if( ssl_errno != SSL_AGAIN )
132                        {
133                                ssl_disconnect( req->ssl );
134                                goto error;
135                        }
136                }
137        }
138        else
139        {
140                st = write( source, req->request + req->bytes_written,
141                                    req->request_length - req->bytes_written );
142                if( st < 0 )
143                {
144                        if( !sockerr_again() )
145                        {
[52b3a99]146                                closesocket( req->fd );
[8a9afe4]147                                goto error;
148                        }
149                }
150        }
151       
152        if( st > 0 )
153                req->bytes_written += st;
154       
155        if( req->bytes_written < req->request_length )
[ba9edaa]156                req->inpa = b_input_add( source,
[e046390]157                                         req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_WRITE,
[ba9edaa]158                                         http_connected, req );
[8a9afe4]159        else
[e046390]160                req->inpa = b_input_add( source, B_EV_IO_READ, http_incoming_data, req );
[8a9afe4]161       
[ba9edaa]162        return FALSE;
[8a9afe4]163       
164error:
[a72dc2b]165        if( req->status_string == NULL )
166                req->status_string = g_strdup( "Error while writing HTTP request" );
[7deb447]167       
[8a9afe4]168        req->func( req );
[fb98634]169        http_free( req );
[ba9edaa]170        return FALSE;
[8a9afe4]171}
172
[486ddb5]173static gboolean http_ssl_connected( gpointer data, int returncode, void *source, b_input_condition cond )
[8a9afe4]174{
175        struct http_request *req = data;
176       
177        if( source == NULL )
[a72dc2b]178        {
179                if( returncode != 0 )
180                {
181                        char *err = ssl_verify_strerror( returncode );
182                        req->status_string = g_strdup_printf(
183                                "Certificate verification problem 0x%x: %s",
184                                returncode, err ? err : "Unknown" );
185                        g_free( err );
186                }
[8a9afe4]187                return http_connected( data, -1, cond );
[a72dc2b]188        }
[8a9afe4]189       
190        req->fd = ssl_getfd( source );
191       
192        return http_connected( data, req->fd, cond );
193}
194
[8bd866f]195static gboolean http_handle_headers( struct http_request *req );
196
[ba9edaa]197static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond )
[8a9afe4]198{
199        struct http_request *req = data;
200        char buffer[2048];
[8bd866f]201        char *s;
[55ccc9a0]202        size_t content_length;
[8a9afe4]203        int st;
204       
205        if( req->inpa > 0 )
[ba9edaa]206                b_event_remove( req->inpa );
[8a9afe4]207       
208        if( req->ssl )
209        {
210                st = ssl_read( req->ssl, buffer, sizeof( buffer ) );
211                if( st < 0 )
212                {
213                        if( ssl_errno != SSL_AGAIN )
214                        {
[ad8b8a3]215                                /* goto cleanup; */
216                               
217                                /* YAY! We have to deal with crappy Microsoft
218                                   servers that LOVE to send invalid TLS
219                                   packets that abort connections! \o/ */
220                               
[8bd866f]221                                goto eof;
[8a9afe4]222                        }
223                }
224                else if( st == 0 )
225                {
[8bd866f]226                        goto eof;
[8a9afe4]227                }
228        }
229        else
230        {
231                st = read( req->fd, buffer, sizeof( buffer ) );
232                if( st < 0 )
233                {
234                        if( !sockerr_again() )
235                        {
[7deb447]236                                req->status_string = g_strdup( strerror( errno ) );
[8a9afe4]237                                goto cleanup;
238                        }
239                }
240                else if( st == 0 )
241                {
[8bd866f]242                        goto eof;
[8a9afe4]243                }
244        }
245       
[8bd866f]246        if( st > 0 && !req->sbuf )
[8a9afe4]247        {
248                req->reply_headers = g_realloc( req->reply_headers, req->bytes_read + st + 1 );
249                memcpy( req->reply_headers + req->bytes_read, buffer, st );
[52b3a99]250                req->bytes_read += st;
[8bd866f]251               
252                st = 0;
253        }
254       
255        if( st >= 0 && ( req->flags & HTTPC_STREAMING ) )
256        {
257                if( !req->reply_body &&
258                    ( strstr( req->reply_headers, "\r\n\r\n" ) ||
259                      strstr( req->reply_headers, "\n\n" ) ) )
260                {
261                        size_t hlen;
262                       
263                        /* We've now received all headers, so process them once
264                           before we start feeding back data. */
265                        if( !http_handle_headers( req ) )
266                                return FALSE;
267                       
268                        hlen = req->reply_body - req->reply_headers;
269                       
270                        req->sblen = req->bytes_read - hlen;
271                        req->sbuf = g_memdup( req->reply_body, req->sblen + 1 );
272                        req->reply_headers = g_realloc( req->reply_headers, hlen + 1 );
273                       
274                        req->reply_body = req->sbuf;
275                }
276               
277                if( st > 0 )
278                {
279                        int pos = req->reply_body - req->sbuf;
280                        req->sbuf = g_realloc( req->sbuf, req->sblen + st + 1 );
281                        memcpy( req->sbuf + req->sblen, buffer, st );
282                        req->bytes_read += st;
283                        req->sblen += st;
[ddc2de5]284                        req->sbuf[req->sblen] = '\0';
[8bd866f]285                        req->reply_body = req->sbuf + pos;
286                        req->body_size = req->sblen - pos;
287                }
288               
289                if( req->reply_body )
290                        req->func( req );
[8a9afe4]291        }
292       
[8bd866f]293        if( ssl_pending( req->ssl ) )
294                return http_incoming_data( data, source, cond );
295       
[8a9afe4]296        /* There will be more! */
[ba9edaa]297        req->inpa = b_input_add( req->fd,
[e046390]298                                 req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_READ,
[ba9edaa]299                                 http_incoming_data, req );
[8a9afe4]300       
[8bd866f]301        return FALSE;
[8a9afe4]302
[8bd866f]303eof:
[0602496]304        /* Maybe if the webserver is overloaded, or when there's bad SSL
305           support... */
306        if( req->bytes_read == 0 )
[7deb447]307        {
308                req->status_string = g_strdup( "Empty HTTP reply" );
[0602496]309                goto cleanup;
[7deb447]310        }
[0602496]311       
[8bd866f]312        if( !( req->flags & HTTPC_STREAMING ) )
313        {
314                /* Returns FALSE if we were redirected, in which case we should abort
315                   and not run any callback yet. */
316                if( !http_handle_headers( req ) )
317                        return FALSE;
318        }
319
320cleanup:
321        if( req->ssl )
322                ssl_disconnect( req->ssl );
323        else
324                closesocket( req->fd );
325       
326        if( ( s = get_rfc822_header( req->reply_headers, "Content-Length", 0 ) ) &&
327            sscanf( s, "%zd", &content_length ) == 1 )
328        {
329                if( content_length < req->body_size )
330                {
331                        req->status_code = -1;
332                        g_free( req->status_string );
333                        req->status_string = g_strdup( "Response truncated" );
334                }
335        }
336        g_free( s );
337       
338        if( getenv( "BITLBEE_DEBUG" ) && req )
339                printf( "Finishing HTTP request with status: %s\n",
340                        req->status_string ? req->status_string : "NULL" );
341       
342        req->func( req );
343        http_free( req );
344        return FALSE;
345}
346
347/* Splits headers and body. Checks result code, in case of 300s it'll handle
348   redirects. If this returns FALSE, don't call any callbacks! */
349static gboolean http_handle_headers( struct http_request *req )
350{
351        char *end1, *end2;
352        int evil_server = 0;
353       
[8a9afe4]354        /* Zero termination is very convenient. */
[8bd866f]355        req->reply_headers[req->bytes_read] = '\0';
[8a9afe4]356       
357        /* Find the separation between headers and body, and keep stupid
358           webservers in mind. */
359        end1 = strstr( req->reply_headers, "\r\n\r\n" );
360        end2 = strstr( req->reply_headers, "\n\n" );
361       
362        if( end2 && end2 < end1 )
363        {
[52b3a99]364                end1 = end2 + 1;
[8a9afe4]365                evil_server = 1;
366        }
[0eec386]367        else if( end1 )
[52b3a99]368        {
369                end1 += 2;
370        }
[0eec386]371        else
[8a9afe4]372        {
[7deb447]373                req->status_string = g_strdup( "Malformed HTTP reply" );
[8bd866f]374                return TRUE;
[52b3a99]375        }
376       
[0eec386]377        *end1 = 0;
378       
[bd31661]379        if( getenv( "BITLBEE_DEBUG" ) )
[3f808ca]380                printf( "HTTP response headers:\n%s\n", req->reply_headers );
[bd31661]381       
[0eec386]382        if( evil_server )
383                req->reply_body = end1 + 1;
384        else
385                req->reply_body = end1 + 2;
386       
[41e5202]387        req->body_size = req->reply_headers + req->bytes_read - req->reply_body;
[0eec386]388       
[52b3a99]389        if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL )
390        {
[8bd866f]391                if( sscanf( end1 + 1, "%hd", &req->status_code ) != 1 )
[7deb447]392                {
393                        req->status_string = g_strdup( "Can't parse status code" );
[52b3a99]394                        req->status_code = -1;
[7deb447]395                }
396                else
397                {
398                        char *eol;
399                       
400                        if( evil_server )
401                                eol = strchr( end1, '\n' );
402                        else
403                                eol = strchr( end1, '\r' );
404                       
405                        req->status_string = g_strndup( end1 + 1, eol - end1 - 1 );
406                       
407                        /* Just to be sure... */
408                        if( ( eol = strchr( req->status_string, '\r' ) ) )
409                                *eol = 0;
410                        if( ( eol = strchr( req->status_string, '\n' ) ) )
411                                *eol = 0;
412                }
[52b3a99]413        }
414        else
415        {
[7deb447]416                req->status_string = g_strdup( "Can't locate status code" );
[52b3a99]417                req->status_code = -1;
418        }
419       
[3f808ca]420        if( ( ( req->status_code >= 301 && req->status_code <= 303 ) ||
421              req->status_code == 307 ) && req->redir_ttl-- > 0 )
[52b3a99]422        {
423                char *loc, *new_request, *new_host;
424                int error = 0, new_port, new_proto;
425               
[7deb447]426                /* We might fill it again, so let's not leak any memory. */
427                g_free( req->status_string );
428                req->status_string = NULL;
429               
[52b3a99]430                loc = strstr( req->reply_headers, "\nLocation: " );
431                if( loc == NULL ) /* We can't handle this redirect... */
[7deb447]432                {
433                        req->status_string = g_strdup( "Can't locate Location: header" );
[8bd866f]434                        return TRUE;
[7deb447]435                }
[52b3a99]436               
437                loc += 11;
438                while( *loc == ' ' )
439                        loc ++;
440               
441                /* TODO/FIXME: Possibly have to handle relative redirections,
442                   and rewrite Host: headers. Not necessary for now, it's
443                   enough for passport authentication like this. */
444               
445                if( *loc == '/' )
446                {
447                        /* Just a different pathname... */
448                       
449                        /* Since we don't cache the servername, and since we
450                           don't need this yet anyway, I won't implement it. */
451                       
[7deb447]452                        req->status_string = g_strdup( "Can't handle recursive redirects" );
453                       
[8bd866f]454                        return TRUE;
[52b3a99]455                }
[8a9afe4]456                else
[52b3a99]457                {
458                        /* A whole URL */
459                        url_t *url;
460                        char *s;
[03a8f8e]461                        const char *new_method;
[52b3a99]462                       
463                        s = strstr( loc, "\r\n" );
464                        if( s == NULL )
[8bd866f]465                                return TRUE;
[52b3a99]466                       
467                        url = g_new0( url_t, 1 );
468                        *s = 0;
469                       
470                        if( !url_set( url, loc ) )
471                        {
[7deb447]472                                req->status_string = g_strdup( "Malformed redirect URL" );
[52b3a99]473                                g_free( url );
[8bd866f]474                                return TRUE;
[52b3a99]475                        }
476                       
[03a8f8e]477                        /* Find all headers and, if necessary, the POST request contents.
478                           Skip the old Host: header though. This crappy code here means
479                           anything using this http_client MUST put the Host: header at
480                           the top. */
[dff732d]481                        if( !( ( s = strstr( req->request, "\r\nHost: " ) ) &&
482                               ( s = strstr( s + strlen( "\r\nHost: " ), "\r\n" ) ) ) )
[52b3a99]483                        {
[7deb447]484                                req->status_string = g_strdup( "Error while rebuilding request string" );
[52b3a99]485                                g_free( url );
[8bd866f]486                                return TRUE;
[52b3a99]487                        }
488                       
[03a8f8e]489                        /* More or less HTTP/1.0 compliant, from my reading of RFC 2616.
490                           Always perform a GET request unless we received a 301. 303 was
491                           meant for this but it's HTTP/1.1-only and we're specifically
[3f808ca]492                           speaking HTTP/1.0. ...
493                           
494                           Well except someone at identi.ca's didn't bother reading any
495                           RFCs and just return HTTP/1.1-specific status codes to HTTP/1.0
496                           requests. Fuckers. So here we are, handle 301..303,307. */
497                        if( strncmp( req->request, "GET", 3 ) == 0 )
498                                /* GETs never become POSTs. */
499                                new_method = "GET";
500                        else if( req->status_code == 302 || req->status_code == 303 )
501                                /* 302 de-facto becomes GET, 303 as specified by RFC 2616#10.3.3 */
502                                new_method = "GET";
503                        else
504                                /* 301 de-facto should stay POST, 307 specifally RFC 2616#10.3.8 */
505                                new_method = "POST";
[03a8f8e]506                       
507                        /* Okay, this isn't fun! We have to rebuild the request... :-( */
508                        new_request = g_strdup_printf( "%s %s HTTP/1.0\r\nHost: %s%s",
509                                                       new_method, url->file, url->host, s );
510                       
[52b3a99]511                        new_host = g_strdup( url->host );
512                        new_port = url->port;
513                        new_proto = url->proto;
514                       
[03a8f8e]515                        /* If we went from POST to GET, truncate the request content. */
516                        if( new_request[0] != req->request[0] && new_request[0] == 'G' &&
517                            ( s = strstr( new_request, "\r\n\r\n" ) ) )
518                                s[4] = '\0';
519                       
[52b3a99]520                        g_free( url );
521                }
522               
523                if( req->ssl )
524                        ssl_disconnect( req->ssl );
525                else
526                        closesocket( req->fd );
527               
528                req->fd = -1;
[7deb447]529                req->ssl = NULL;
[52b3a99]530               
[bd31661]531                if( getenv( "BITLBEE_DEBUG" ) )
[3f808ca]532                        printf( "New headers for redirected HTTP request:\n%s\n", new_request );
[bd31661]533       
[52b3a99]534                if( new_proto == PROTO_HTTPS )
535                {
[a72dc2b]536                        req->ssl = ssl_connect( new_host, new_port, TRUE, http_ssl_connected, req );
[52b3a99]537                        if( req->ssl == NULL )
538                                error = 1;
539                }
540                else
541                {
542                        req->fd = proxy_connect( new_host, new_port, http_connected, req );
543                        if( req->fd < 0 )
544                                error = 1;
545                }
[2db811a]546                g_free( new_host );
[52b3a99]547               
548                if( error )
549                {
[7deb447]550                        req->status_string = g_strdup( "Connection problem during redirect" );
[52b3a99]551                        g_free( new_request );
[8bd866f]552                        return TRUE;
[52b3a99]553                }
554               
555                g_free( req->request );
556                g_free( req->reply_headers );
557                req->request = new_request;
558                req->request_length = strlen( new_request );
559                req->bytes_read = req->bytes_written = req->inpa = 0;
560                req->reply_headers = req->reply_body = NULL;
561               
[ba9edaa]562                return FALSE;
[8a9afe4]563        }
564       
[8bd866f]565        return TRUE;
566}
[8a9afe4]567
[8bd866f]568void http_flush_bytes( struct http_request *req, size_t len )
569{
[1388d30]570        if( len <= 0 || len > req->body_size || !( req->flags & HTTPC_STREAMING ) )
571                return;
572       
573        req->reply_body += len;
574        req->body_size -= len;
575       
576        if( req->reply_body - req->sbuf >= 512 )
[55ccc9a0]577        {
[1388d30]578                printf( "Wasting %ld bytes, cleaning up stream buffer\n", req->reply_body - req->sbuf );
579                char *new = g_memdup( req->reply_body, req->body_size + 1 );
580                g_free( req->sbuf );
581                req->reply_body = req->sbuf = new;
582                req->sblen = req->body_size;
[55ccc9a0]583        }
[fb98634]584}
585
[1388d30]586void http_close( struct http_request *req )
587{
588        if( !req )
589                return;
590       
591        if( req->ssl )
592                ssl_disconnect( req->ssl );
593        else
594                closesocket( req->fd );
595       
596        http_free( req );
597}
598
[516a9c6]599static void http_free( struct http_request *req )
[fb98634]600{
[8a9afe4]601        g_free( req->request );
602        g_free( req->reply_headers );
[7deb447]603        g_free( req->status_string );
[8bd866f]604        g_free( req->sbuf );
[8a9afe4]605        g_free( req );
606}
Note: See TracBrowser for help on using the repository browser.