source: lib/http_client.c @ a72dc2b

Last change on this file since a72dc2b was a72dc2b, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-12-19T17:57:20Z

Add verify argument to ssl_connect() so HTTPS-based stuff is also secure.
(Think of Twitter, but also MSN/Yahoo! authentication.)

  • Property mode set to 100644
File size: 12.7 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       
[8a9afe4]75        return( req );
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{
[486ddb5]175        //The returncode is not used at the moment.
[8a9afe4]176        struct http_request *req = data;
177       
178        if( source == NULL )
[a72dc2b]179        {
180                if( returncode != 0 )
181                {
182                        char *err = ssl_verify_strerror( returncode );
183                        req->status_string = g_strdup_printf(
184                                "Certificate verification problem 0x%x: %s",
185                                returncode, err ? err : "Unknown" );
186                        g_free( err );
187                }
[8a9afe4]188                return http_connected( data, -1, cond );
[a72dc2b]189        }
[8a9afe4]190       
191        req->fd = ssl_getfd( source );
192       
193        return http_connected( data, req->fd, cond );
194}
195
[ba9edaa]196static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond )
[8a9afe4]197{
198        struct http_request *req = data;
199        int evil_server = 0;
200        char buffer[2048];
201        char *end1, *end2;
202        int st;
203       
204        if( req->inpa > 0 )
[ba9edaa]205                b_event_remove( req->inpa );
[8a9afe4]206       
207        if( req->ssl )
208        {
209                st = ssl_read( req->ssl, buffer, sizeof( buffer ) );
210                if( st < 0 )
211                {
212                        if( ssl_errno != SSL_AGAIN )
213                        {
[ad8b8a3]214                                /* goto cleanup; */
215                               
216                                /* YAY! We have to deal with crappy Microsoft
217                                   servers that LOVE to send invalid TLS
218                                   packets that abort connections! \o/ */
219                               
220                                goto got_reply;
[8a9afe4]221                        }
222                }
223                else if( st == 0 )
224                {
225                        goto got_reply;
226                }
227        }
228        else
229        {
230                st = read( req->fd, buffer, sizeof( buffer ) );
231                if( st < 0 )
232                {
233                        if( !sockerr_again() )
234                        {
[7deb447]235                                req->status_string = g_strdup( strerror( errno ) );
[8a9afe4]236                                goto cleanup;
237                        }
238                }
239                else if( st == 0 )
240                {
241                        goto got_reply;
242                }
243        }
244       
245        if( st > 0 )
246        {
247                req->reply_headers = g_realloc( req->reply_headers, req->bytes_read + st + 1 );
248                memcpy( req->reply_headers + req->bytes_read, buffer, st );
[52b3a99]249                req->bytes_read += st;
[8a9afe4]250        }
251       
252        /* There will be more! */
[ba9edaa]253        req->inpa = b_input_add( req->fd,
[e046390]254                                 req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_READ,
[ba9edaa]255                                 http_incoming_data, req );
[8a9afe4]256       
[80acb6d]257        if( ssl_pending( req->ssl ) )
258                return http_incoming_data( data, source, cond );
259        else
260                return FALSE;
[8a9afe4]261
262got_reply:
[0602496]263        /* Maybe if the webserver is overloaded, or when there's bad SSL
264           support... */
265        if( req->bytes_read == 0 )
[7deb447]266        {
267                req->status_string = g_strdup( "Empty HTTP reply" );
[0602496]268                goto cleanup;
[7deb447]269        }
[0602496]270       
[8a9afe4]271        /* Zero termination is very convenient. */
272        req->reply_headers[req->bytes_read] = 0;
273       
274        /* Find the separation between headers and body, and keep stupid
275           webservers in mind. */
276        end1 = strstr( req->reply_headers, "\r\n\r\n" );
277        end2 = strstr( req->reply_headers, "\n\n" );
278       
279        if( end2 && end2 < end1 )
280        {
[52b3a99]281                end1 = end2 + 1;
[8a9afe4]282                evil_server = 1;
283        }
[0eec386]284        else if( end1 )
[52b3a99]285        {
286                end1 += 2;
287        }
[0eec386]288        else
[8a9afe4]289        {
[7deb447]290                req->status_string = g_strdup( "Malformed HTTP reply" );
[0eec386]291                goto cleanup;
[52b3a99]292        }
293       
[0eec386]294        *end1 = 0;
295       
[bd31661]296        if( getenv( "BITLBEE_DEBUG" ) )
[3f808ca]297                printf( "HTTP response headers:\n%s\n", req->reply_headers );
[bd31661]298       
[0eec386]299        if( evil_server )
300                req->reply_body = end1 + 1;
301        else
302                req->reply_body = end1 + 2;
303       
[41e5202]304        req->body_size = req->reply_headers + req->bytes_read - req->reply_body;
[0eec386]305       
[52b3a99]306        if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL )
307        {
308                if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 )
[7deb447]309                {
310                        req->status_string = g_strdup( "Can't parse status code" );
[52b3a99]311                        req->status_code = -1;
[7deb447]312                }
313                else
314                {
315                        char *eol;
316                       
317                        if( evil_server )
318                                eol = strchr( end1, '\n' );
319                        else
320                                eol = strchr( end1, '\r' );
321                       
322                        req->status_string = g_strndup( end1 + 1, eol - end1 - 1 );
323                       
324                        /* Just to be sure... */
325                        if( ( eol = strchr( req->status_string, '\r' ) ) )
326                                *eol = 0;
327                        if( ( eol = strchr( req->status_string, '\n' ) ) )
328                                *eol = 0;
329                }
[52b3a99]330        }
331        else
332        {
[7deb447]333                req->status_string = g_strdup( "Can't locate status code" );
[52b3a99]334                req->status_code = -1;
335        }
336       
[3f808ca]337        if( ( ( req->status_code >= 301 && req->status_code <= 303 ) ||
338              req->status_code == 307 ) && req->redir_ttl-- > 0 )
[52b3a99]339        {
340                char *loc, *new_request, *new_host;
341                int error = 0, new_port, new_proto;
342               
[7deb447]343                /* We might fill it again, so let's not leak any memory. */
344                g_free( req->status_string );
345                req->status_string = NULL;
346               
[52b3a99]347                loc = strstr( req->reply_headers, "\nLocation: " );
348                if( loc == NULL ) /* We can't handle this redirect... */
[7deb447]349                {
350                        req->status_string = g_strdup( "Can't locate Location: header" );
[52b3a99]351                        goto cleanup;
[7deb447]352                }
[52b3a99]353               
354                loc += 11;
355                while( *loc == ' ' )
356                        loc ++;
357               
358                /* TODO/FIXME: Possibly have to handle relative redirections,
359                   and rewrite Host: headers. Not necessary for now, it's
360                   enough for passport authentication like this. */
361               
362                if( *loc == '/' )
363                {
364                        /* Just a different pathname... */
365                       
366                        /* Since we don't cache the servername, and since we
367                           don't need this yet anyway, I won't implement it. */
368                       
[7deb447]369                        req->status_string = g_strdup( "Can't handle recursive redirects" );
370                       
[52b3a99]371                        goto cleanup;
372                }
[8a9afe4]373                else
[52b3a99]374                {
375                        /* A whole URL */
376                        url_t *url;
377                        char *s;
[03a8f8e]378                        const char *new_method;
[52b3a99]379                       
380                        s = strstr( loc, "\r\n" );
381                        if( s == NULL )
382                                goto cleanup;
383                       
384                        url = g_new0( url_t, 1 );
385                        *s = 0;
386                       
387                        if( !url_set( url, loc ) )
388                        {
[7deb447]389                                req->status_string = g_strdup( "Malformed redirect URL" );
[52b3a99]390                                g_free( url );
391                                goto cleanup;
392                        }
393                       
[03a8f8e]394                        /* Find all headers and, if necessary, the POST request contents.
395                           Skip the old Host: header though. This crappy code here means
396                           anything using this http_client MUST put the Host: header at
397                           the top. */
[dff732d]398                        if( !( ( s = strstr( req->request, "\r\nHost: " ) ) &&
399                               ( s = strstr( s + strlen( "\r\nHost: " ), "\r\n" ) ) ) )
[52b3a99]400                        {
[7deb447]401                                req->status_string = g_strdup( "Error while rebuilding request string" );
[52b3a99]402                                g_free( url );
403                                goto cleanup;
404                        }
405                       
[03a8f8e]406                        /* More or less HTTP/1.0 compliant, from my reading of RFC 2616.
407                           Always perform a GET request unless we received a 301. 303 was
408                           meant for this but it's HTTP/1.1-only and we're specifically
[3f808ca]409                           speaking HTTP/1.0. ...
410                           
411                           Well except someone at identi.ca's didn't bother reading any
412                           RFCs and just return HTTP/1.1-specific status codes to HTTP/1.0
413                           requests. Fuckers. So here we are, handle 301..303,307. */
414                        if( strncmp( req->request, "GET", 3 ) == 0 )
415                                /* GETs never become POSTs. */
416                                new_method = "GET";
417                        else if( req->status_code == 302 || req->status_code == 303 )
418                                /* 302 de-facto becomes GET, 303 as specified by RFC 2616#10.3.3 */
419                                new_method = "GET";
420                        else
421                                /* 301 de-facto should stay POST, 307 specifally RFC 2616#10.3.8 */
422                                new_method = "POST";
[03a8f8e]423                       
424                        /* Okay, this isn't fun! We have to rebuild the request... :-( */
425                        new_request = g_strdup_printf( "%s %s HTTP/1.0\r\nHost: %s%s",
426                                                       new_method, url->file, url->host, s );
427                       
[52b3a99]428                        new_host = g_strdup( url->host );
429                        new_port = url->port;
430                        new_proto = url->proto;
431                       
[03a8f8e]432                        /* If we went from POST to GET, truncate the request content. */
433                        if( new_request[0] != req->request[0] && new_request[0] == 'G' &&
434                            ( s = strstr( new_request, "\r\n\r\n" ) ) )
435                                s[4] = '\0';
436                       
[52b3a99]437                        g_free( url );
438                }
439               
440                if( req->ssl )
441                        ssl_disconnect( req->ssl );
442                else
443                        closesocket( req->fd );
444               
445                req->fd = -1;
[7deb447]446                req->ssl = NULL;
[52b3a99]447               
[bd31661]448                if( getenv( "BITLBEE_DEBUG" ) )
[3f808ca]449                        printf( "New headers for redirected HTTP request:\n%s\n", new_request );
[bd31661]450       
[52b3a99]451                if( new_proto == PROTO_HTTPS )
452                {
[a72dc2b]453                        req->ssl = ssl_connect( new_host, new_port, TRUE, http_ssl_connected, req );
[52b3a99]454                        if( req->ssl == NULL )
455                                error = 1;
456                }
457                else
458                {
459                        req->fd = proxy_connect( new_host, new_port, http_connected, req );
460                        if( req->fd < 0 )
461                                error = 1;
462                }
[2db811a]463                g_free( new_host );
[52b3a99]464               
465                if( error )
466                {
[7deb447]467                        req->status_string = g_strdup( "Connection problem during redirect" );
[52b3a99]468                        g_free( new_request );
469                        goto cleanup;
470                }
471               
472                g_free( req->request );
473                g_free( req->reply_headers );
474                req->request = new_request;
475                req->request_length = strlen( new_request );
476                req->bytes_read = req->bytes_written = req->inpa = 0;
477                req->reply_headers = req->reply_body = NULL;
478               
[ba9edaa]479                return FALSE;
[8a9afe4]480        }
481       
482        /* Assume that a closed connection means we're finished, this indeed
483           breaks with keep-alive connections and faulty connections. */
484        req->finished = 1;
485
486cleanup:
487        if( req->ssl )
488                ssl_disconnect( req->ssl );
489        else
[52b3a99]490                closesocket( req->fd );
[8a9afe4]491       
[bd31661]492        if( getenv( "BITLBEE_DEBUG" ) && req )
[3f808ca]493                printf( "Finishing HTTP request with status: %s\n",
[bd31661]494                        req->status_string ? req->status_string : "NULL" );
495       
[8a9afe4]496        req->func( req );
[fb98634]497        http_free( req );
498        return FALSE;
499}
500
[516a9c6]501static void http_free( struct http_request *req )
[fb98634]502{
[8a9afe4]503        g_free( req->request );
504        g_free( req->reply_headers );
[7deb447]505        g_free( req->status_string );
[8a9afe4]506        g_free( req );
507}
[fb98634]508
Note: See TracBrowser for help on using the repository browser.