source: protocols/http_client.c @ 68b518d6

Last change on this file since 68b518d6 was 68b518d6, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-26T09:03:38Z

Merging from main development tree.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2005 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* HTTP(S) module                                                       */
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>
27#include <stdio.h>
28
29#include "http_client.h"
30#include "url.h"
31#include "sock.h"
32
33
34static gboolean http_connected( gpointer data, int source, b_input_condition cond );
35static gboolean http_ssl_connected( gpointer data, void *source, b_input_condition cond );
36static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond );
37
38
39void *http_dorequest( char *host, int port, int ssl, char *request, http_input_function func, gpointer data )
40{
41        struct http_request *req;
42        int error = 0;
43       
44        req = g_new0( struct http_request, 1 );
45       
46        if( ssl )
47        {
48                req->ssl = ssl_connect( host, port, http_ssl_connected, req );
49                if( req->ssl == NULL )
50                        error = 1;
51        }
52        else
53        {
54                req->fd = proxy_connect( host, port, http_connected, req );
55                if( req->fd < 0 )
56                        error = 1;
57        }
58       
59        if( error )
60        {
61                g_free( req );
62                return( NULL );
63        }
64       
65        req->func = func;
66        req->data = data;
67        req->request = g_strdup( request );
68        req->request_length = strlen( request );
69       
70        return( req );
71}
72
73void *http_dorequest_url( char *url_string, http_input_function func, gpointer data )
74{
75        url_t *url = g_new0( url_t, 1 );
76        char *request;
77        void *ret;
78       
79        if( !url_set( url, url_string ) )
80        {
81                g_free( url );
82                return NULL;
83        }
84       
85        if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS )
86        {
87                g_free( url );
88                return NULL;
89        }
90       
91        request = g_strdup_printf( "GET %s HTTP/1.0\r\n"
92                                   "Host: %s\r\n"
93                                   "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n"
94                                   "\r\n", url->file, url->host );
95       
96        ret = http_dorequest( url->host, url->port,
97                              url->proto == PROTO_HTTPS, request, func, data );
98       
99        g_free( url );
100        g_free( request );
101        return ret;
102}
103
104/* This one is actually pretty simple... Might get more calls if we can't write
105   the whole request at once. */
106static gboolean http_connected( gpointer data, int source, b_input_condition cond )
107{
108        struct http_request *req = data;
109        int st;
110       
111        if( source < 0 )
112                goto error;
113       
114        if( req->inpa > 0 )
115                b_event_remove( req->inpa );
116       
117        sock_make_nonblocking( req->fd );
118       
119        if( req->ssl )
120        {
121                st = ssl_write( req->ssl, req->request + req->bytes_written,
122                                req->request_length - req->bytes_written );
123                if( st < 0 )
124                {
125                        if( ssl_errno != SSL_AGAIN )
126                        {
127                                ssl_disconnect( req->ssl );
128                                goto error;
129                        }
130                }
131        }
132        else
133        {
134                st = write( source, req->request + req->bytes_written,
135                                    req->request_length - req->bytes_written );
136                if( st < 0 )
137                {
138                        if( !sockerr_again() )
139                        {
140                                closesocket( req->fd );
141                                goto error;
142                        }
143                }
144        }
145       
146        if( st > 0 )
147                req->bytes_written += st;
148       
149        if( req->bytes_written < req->request_length )
150                req->inpa = b_input_add( source,
151                                         req->ssl ? ssl_getdirection( req->ssl ) : GAIM_INPUT_WRITE,
152                                         http_connected, req );
153        else
154                req->inpa = b_input_add( source, GAIM_INPUT_READ, http_incoming_data, req );
155       
156        return FALSE;
157       
158error:
159        req->func( req );
160       
161        g_free( req->request );
162        g_free( req );
163       
164        return FALSE;
165}
166
167static gboolean http_ssl_connected( gpointer data, void *source, b_input_condition cond )
168{
169        struct http_request *req = data;
170       
171        if( source == NULL )
172                return http_connected( data, -1, cond );
173       
174        req->fd = ssl_getfd( source );
175       
176        return http_connected( data, req->fd, cond );
177}
178
179static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond )
180{
181        struct http_request *req = data;
182        int evil_server = 0;
183        char buffer[2048];
184        char *end1, *end2;
185        int st;
186       
187        if( req->inpa > 0 )
188                b_event_remove( req->inpa );
189       
190        if( req->ssl )
191        {
192                st = ssl_read( req->ssl, buffer, sizeof( buffer ) );
193                if( st < 0 )
194                {
195                        if( ssl_errno != SSL_AGAIN )
196                        {
197                                /* goto cleanup; */
198                               
199                                /* YAY! We have to deal with crappy Microsoft
200                                   servers that LOVE to send invalid TLS
201                                   packets that abort connections! \o/ */
202                               
203                                goto got_reply;
204                        }
205                }
206                else if( st == 0 )
207                {
208                        goto got_reply;
209                }
210        }
211        else
212        {
213                st = read( req->fd, buffer, sizeof( buffer ) );
214                if( st < 0 )
215                {
216                        if( !sockerr_again() )
217                        {
218                                goto cleanup;
219                        }
220                }
221                else if( st == 0 )
222                {
223                        goto got_reply;
224                }
225        }
226       
227        if( st > 0 )
228        {
229                req->reply_headers = g_realloc( req->reply_headers, req->bytes_read + st + 1 );
230                memcpy( req->reply_headers + req->bytes_read, buffer, st );
231                req->bytes_read += st;
232        }
233       
234        /* There will be more! */
235        req->inpa = b_input_add( req->fd,
236                                 req->ssl ? ssl_getdirection( req->ssl ) : GAIM_INPUT_READ,
237                                 http_incoming_data, req );
238       
239        return FALSE;
240
241got_reply:
242        /* Maybe if the webserver is overloaded, or when there's bad SSL
243           support... */
244        if( req->bytes_read == 0 )
245                goto cleanup;
246       
247        /* Zero termination is very convenient. */
248        req->reply_headers[req->bytes_read] = 0;
249       
250        /* Find the separation between headers and body, and keep stupid
251           webservers in mind. */
252        end1 = strstr( req->reply_headers, "\r\n\r\n" );
253        end2 = strstr( req->reply_headers, "\n\n" );
254       
255        if( end2 && end2 < end1 )
256        {
257                end1 = end2 + 1;
258                evil_server = 1;
259        }
260        else if( end1 )
261        {
262                end1 += 2;
263        }
264        else
265        {
266                goto cleanup;
267        }
268       
269        *end1 = 0;
270       
271        if( evil_server )
272                req->reply_body = end1 + 1;
273        else
274                req->reply_body = end1 + 2;
275       
276        req->body_size = req->reply_headers + req->bytes_read - req->reply_body;
277       
278        if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL )
279        {
280                if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 )
281                        req->status_code = -1;
282        }
283        else
284        {
285                req->status_code = -1;
286        }
287       
288        if( req->status_code == 301 || req->status_code == 302 )
289        {
290                char *loc, *new_request, *new_host;
291                int error = 0, new_port, new_proto;
292               
293                loc = strstr( req->reply_headers, "\nLocation: " );
294                if( loc == NULL ) /* We can't handle this redirect... */
295                        goto cleanup;
296               
297                loc += 11;
298                while( *loc == ' ' )
299                        loc ++;
300               
301                /* TODO/FIXME: Possibly have to handle relative redirections,
302                   and rewrite Host: headers. Not necessary for now, it's
303                   enough for passport authentication like this. */
304               
305                if( *loc == '/' )
306                {
307                        /* Just a different pathname... */
308                       
309                        /* Since we don't cache the servername, and since we
310                           don't need this yet anyway, I won't implement it. */
311                       
312                        goto cleanup;
313                }
314                else
315                {
316                        /* A whole URL */
317                        url_t *url;
318                        char *s;
319                       
320                        s = strstr( loc, "\r\n" );
321                        if( s == NULL )
322                                goto cleanup;
323                       
324                        url = g_new0( url_t, 1 );
325                        *s = 0;
326                       
327                        if( !url_set( url, loc ) )
328                        {
329                                g_free( url );
330                                goto cleanup;
331                        }
332                       
333                        /* Okay, this isn't fun! We have to rebuild the request... :-( */
334                        new_request = g_malloc( req->request_length + strlen( url->file ) );
335                       
336                        /* So, now I just allocated enough memory, so I'm
337                           going to use strcat(), whether you like it or not. :-) */
338                       
339                        /* First, find the GET/POST/whatever from the original request. */
340                        s = strchr( req->request, ' ' );
341                        if( s == NULL )
342                        {
343                                g_free( new_request );
344                                g_free( url );
345                                goto cleanup;
346                        }
347                       
348                        *s = 0;
349                        sprintf( new_request, "%s %s HTTP/1.0\r\n", req->request, url->file );
350                        *s = ' ';
351                       
352                        s = strstr( req->request, "\r\n" );
353                        if( s == NULL )
354                        {
355                                g_free( new_request );
356                                g_free( url );
357                                goto cleanup;
358                        }
359                       
360                        strcat( new_request, s + 2 );
361                        new_host = g_strdup( url->host );
362                        new_port = url->port;
363                        new_proto = url->proto;
364                       
365                        g_free( url );
366                }
367               
368                if( req->ssl )
369                        ssl_disconnect( req->ssl );
370                else
371                        closesocket( req->fd );
372               
373                req->fd = -1;
374                req->ssl = 0;
375               
376                if( new_proto == PROTO_HTTPS )
377                {
378                        req->ssl = ssl_connect( new_host, new_port, http_ssl_connected, req );
379                        if( req->ssl == NULL )
380                                error = 1;
381                }
382                else
383                {
384                        req->fd = proxy_connect( new_host, new_port, http_connected, req );
385                        if( req->fd < 0 )
386                                error = 1;
387                }
388                g_free( new_host );
389               
390                if( error )
391                {
392                        g_free( new_request );
393                        goto cleanup;
394                }
395               
396                g_free( req->request );
397                g_free( req->reply_headers );
398                req->request = new_request;
399                req->request_length = strlen( new_request );
400                req->bytes_read = req->bytes_written = req->inpa = 0;
401                req->reply_headers = req->reply_body = NULL;
402               
403                return FALSE;
404        }
405       
406        /* Assume that a closed connection means we're finished, this indeed
407           breaks with keep-alive connections and faulty connections. */
408        req->finished = 1;
409
410cleanup:
411        if( req->ssl )
412                ssl_disconnect( req->ssl );
413        else
414                closesocket( req->fd );
415       
416        req->func( req );
417       
418        g_free( req->request );
419        g_free( req->reply_headers );
420        g_free( req );
421       
422        return FALSE;
423}
Note: See TracBrowser for help on using the repository browser.