[8a9afe4] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[ca8037e] | 4 | * Copyright 2002-2013 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] | 34 | static gboolean http_connected( gpointer data, int source, b_input_condition cond ); |
---|
[486ddb5] | 35 | static gboolean http_ssl_connected( gpointer data, int returncode, void *source, b_input_condition cond ); |
---|
[ba9edaa] | 36 | static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond ); |
---|
[516a9c6] | 37 | static void http_free( struct http_request *req ); |
---|
[8a9afe4] | 38 | |
---|
| 39 | |
---|
[516a9c6] | 40 | struct 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; |
---|
[ca8037e] | 71 | req->content_length = -1; |
---|
[8a9afe4] | 72 | |
---|
[3f808ca] | 73 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
| 74 | printf( "About to send HTTP request:\n%s\n", req->request ); |
---|
| 75 | |
---|
[11ec078] | 76 | return req; |
---|
[8a9afe4] | 77 | } |
---|
| 78 | |
---|
[516a9c6] | 79 | struct http_request *http_dorequest_url( char *url_string, http_input_function func, gpointer data ) |
---|
[0790644] | 80 | { |
---|
| 81 | url_t *url = g_new0( url_t, 1 ); |
---|
| 82 | char *request; |
---|
| 83 | void *ret; |
---|
| 84 | |
---|
| 85 | if( !url_set( url, url_string ) ) |
---|
| 86 | { |
---|
| 87 | g_free( url ); |
---|
| 88 | return NULL; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS ) |
---|
| 92 | { |
---|
| 93 | g_free( url ); |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | request = g_strdup_printf( "GET %s HTTP/1.0\r\n" |
---|
| 98 | "Host: %s\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] | 112 | static 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 | |
---|
| 164 | error: |
---|
[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] | 173 | static 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 | |
---|
[ca8037e] | 195 | typedef enum { |
---|
| 196 | CR_OK, |
---|
| 197 | CR_EOF, |
---|
| 198 | CR_ERROR, |
---|
| 199 | CR_ABORT, |
---|
| 200 | } http_ret_t; |
---|
| 201 | |
---|
[8bd866f] | 202 | static gboolean http_handle_headers( struct http_request *req ); |
---|
[ca8037e] | 203 | static http_ret_t http_process_chunked_data( struct http_request *req, const char *buffer, int len ); |
---|
| 204 | static http_ret_t http_process_data( struct http_request *req, const char *buffer, int len ); |
---|
[8bd866f] | 205 | |
---|
[ba9edaa] | 206 | static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond ) |
---|
[8a9afe4] | 207 | { |
---|
| 208 | struct http_request *req = data; |
---|
[5f2f728] | 209 | char buffer[4096]; |
---|
[8a9afe4] | 210 | int st; |
---|
| 211 | |
---|
| 212 | if( req->inpa > 0 ) |
---|
[ba9edaa] | 213 | b_event_remove( req->inpa ); |
---|
[8a9afe4] | 214 | |
---|
| 215 | if( req->ssl ) |
---|
| 216 | { |
---|
| 217 | st = ssl_read( req->ssl, buffer, sizeof( buffer ) ); |
---|
| 218 | if( st < 0 ) |
---|
| 219 | { |
---|
| 220 | if( ssl_errno != SSL_AGAIN ) |
---|
| 221 | { |
---|
[ad8b8a3] | 222 | /* goto cleanup; */ |
---|
| 223 | |
---|
| 224 | /* YAY! We have to deal with crappy Microsoft |
---|
| 225 | servers that LOVE to send invalid TLS |
---|
| 226 | packets that abort connections! \o/ */ |
---|
| 227 | |
---|
[8bd866f] | 228 | goto eof; |
---|
[8a9afe4] | 229 | } |
---|
| 230 | } |
---|
| 231 | else if( st == 0 ) |
---|
| 232 | { |
---|
[8bd866f] | 233 | goto eof; |
---|
[8a9afe4] | 234 | } |
---|
| 235 | } |
---|
| 236 | else |
---|
| 237 | { |
---|
| 238 | st = read( req->fd, buffer, sizeof( buffer ) ); |
---|
| 239 | if( st < 0 ) |
---|
| 240 | { |
---|
| 241 | if( !sockerr_again() ) |
---|
| 242 | { |
---|
[7deb447] | 243 | req->status_string = g_strdup( strerror( errno ) ); |
---|
[8a9afe4] | 244 | goto cleanup; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | else if( st == 0 ) |
---|
| 248 | { |
---|
[8bd866f] | 249 | goto eof; |
---|
[8a9afe4] | 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
[ca8037e] | 253 | if( st > 0 ) |
---|
[8a9afe4] | 254 | { |
---|
[ca8037e] | 255 | http_ret_t c; |
---|
[8bd866f] | 256 | |
---|
[ca8037e] | 257 | if( req->flags & HTTPC_CHUNKED ) |
---|
| 258 | c = http_process_chunked_data( req, buffer, st ); |
---|
| 259 | else |
---|
| 260 | c = http_process_data( req, buffer, st ); |
---|
[8bd866f] | 261 | |
---|
[ca8037e] | 262 | if( c == CR_EOF ) |
---|
| 263 | goto eof; |
---|
| 264 | else if( c == CR_ERROR || c == CR_ABORT ) |
---|
| 265 | return FALSE; |
---|
[8a9afe4] | 266 | } |
---|
| 267 | |
---|
[ca8037e] | 268 | if( req->content_length != -1 && |
---|
| 269 | req->body_size >= req->content_length ) |
---|
| 270 | goto eof; |
---|
| 271 | |
---|
[8bd866f] | 272 | if( ssl_pending( req->ssl ) ) |
---|
| 273 | return http_incoming_data( data, source, cond ); |
---|
| 274 | |
---|
[8a9afe4] | 275 | /* There will be more! */ |
---|
[ba9edaa] | 276 | req->inpa = b_input_add( req->fd, |
---|
[e046390] | 277 | req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_READ, |
---|
[ba9edaa] | 278 | http_incoming_data, req ); |
---|
[8a9afe4] | 279 | |
---|
[8bd866f] | 280 | return FALSE; |
---|
[8a9afe4] | 281 | |
---|
[8bd866f] | 282 | eof: |
---|
[dd672e2] | 283 | req->flags |= HTTPC_EOF; |
---|
| 284 | |
---|
[0602496] | 285 | /* Maybe if the webserver is overloaded, or when there's bad SSL |
---|
| 286 | support... */ |
---|
| 287 | if( req->bytes_read == 0 ) |
---|
[7deb447] | 288 | { |
---|
| 289 | req->status_string = g_strdup( "Empty HTTP reply" ); |
---|
[0602496] | 290 | goto cleanup; |
---|
[7deb447] | 291 | } |
---|
[8bd866f] | 292 | |
---|
| 293 | cleanup: |
---|
[286cd48] | 294 | /* Avoid g_source_remove warnings */ |
---|
| 295 | req->inpa = 0; |
---|
| 296 | |
---|
[8bd866f] | 297 | if( req->ssl ) |
---|
| 298 | ssl_disconnect( req->ssl ); |
---|
| 299 | else |
---|
| 300 | closesocket( req->fd ); |
---|
| 301 | |
---|
[ca8037e] | 302 | if( req->body_size < req->content_length ) |
---|
[8bd866f] | 303 | { |
---|
[ca8037e] | 304 | req->status_code = -1; |
---|
| 305 | g_free( req->status_string ); |
---|
| 306 | req->status_string = g_strdup( "Response truncated" ); |
---|
[8bd866f] | 307 | } |
---|
| 308 | |
---|
| 309 | if( getenv( "BITLBEE_DEBUG" ) && req ) |
---|
| 310 | printf( "Finishing HTTP request with status: %s\n", |
---|
| 311 | req->status_string ? req->status_string : "NULL" ); |
---|
| 312 | |
---|
| 313 | req->func( req ); |
---|
| 314 | http_free( req ); |
---|
| 315 | return FALSE; |
---|
| 316 | } |
---|
| 317 | |
---|
[ca8037e] | 318 | static http_ret_t http_process_chunked_data( struct http_request *req, const char *buffer, int len ) |
---|
| 319 | { |
---|
| 320 | char *chunk, *eos, *s; |
---|
| 321 | |
---|
| 322 | if( len < 0 ) |
---|
| 323 | return TRUE; |
---|
| 324 | |
---|
| 325 | if( len > 0 ) |
---|
| 326 | { |
---|
| 327 | req->cbuf = g_realloc( req->cbuf, req->cblen + len + 1 ); |
---|
| 328 | memcpy( req->cbuf + req->cblen, buffer, len ); |
---|
| 329 | req->cblen += len; |
---|
| 330 | req->cbuf[req->cblen] = '\0'; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /* Turns out writing a proper chunked-encoding state machine is not |
---|
[ab19567] | 334 | that simple. :-( I've tested this one feeding it byte by byte so |
---|
| 335 | I hope it's solid now. */ |
---|
[ca8037e] | 336 | chunk = req->cbuf; |
---|
| 337 | eos = req->cbuf + req->cblen; |
---|
| 338 | while( TRUE ) |
---|
| 339 | { |
---|
| 340 | int clen = 0; |
---|
| 341 | |
---|
| 342 | /* Might be a \r\n from the last chunk. */ |
---|
| 343 | s = chunk; |
---|
| 344 | while( isspace( *s ) ) |
---|
| 345 | s ++; |
---|
| 346 | /* Chunk length. Might be incomplete. */ |
---|
| 347 | if( s < eos && sscanf( s, "%x", &clen ) != 1 ) |
---|
| 348 | return CR_ERROR; |
---|
| 349 | while( isxdigit( *s ) ) |
---|
| 350 | s ++; |
---|
| 351 | |
---|
| 352 | /* If we read anything here, it *must* be \r\n. */ |
---|
| 353 | if( strncmp( s, "\r\n", MIN( 2, eos - s ) ) != 0 ) |
---|
| 354 | return CR_ERROR; |
---|
| 355 | s += 2; |
---|
| 356 | |
---|
| 357 | if( s >= eos ) |
---|
| 358 | break; |
---|
| 359 | |
---|
| 360 | /* 0-length chunk means end of response. */ |
---|
| 361 | if( clen == 0 ) |
---|
| 362 | return CR_EOF; |
---|
| 363 | |
---|
[ab19567] | 364 | /* Wait for the whole chunk to arrive. */ |
---|
[ca8037e] | 365 | if( s + clen > eos ) |
---|
| 366 | break; |
---|
| 367 | if( http_process_data( req, s, clen ) != CR_OK ) |
---|
| 368 | return CR_ABORT; |
---|
| 369 | |
---|
| 370 | chunk = s + clen; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | if( chunk != req->cbuf ) |
---|
| 374 | { |
---|
| 375 | req->cblen = eos - chunk; |
---|
| 376 | s = g_memdup( chunk, req->cblen + 1 ); |
---|
| 377 | g_free( req->cbuf ); |
---|
| 378 | req->cbuf = s; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | return CR_OK; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | static http_ret_t http_process_data( struct http_request *req, const char *buffer, int len ) |
---|
| 385 | { |
---|
| 386 | if( len <= 0 ) |
---|
| 387 | return CR_OK; |
---|
| 388 | |
---|
| 389 | if( !req->reply_body ) |
---|
| 390 | { |
---|
| 391 | req->reply_headers = g_realloc( req->reply_headers, req->bytes_read + len + 1 ); |
---|
| 392 | memcpy( req->reply_headers + req->bytes_read, buffer, len ); |
---|
| 393 | req->bytes_read += len; |
---|
| 394 | req->reply_headers[req->bytes_read] = '\0'; |
---|
| 395 | |
---|
| 396 | if( strstr( req->reply_headers, "\r\n\r\n" ) || |
---|
| 397 | strstr( req->reply_headers, "\n\n" ) ) |
---|
| 398 | { |
---|
| 399 | /* We've now received all headers. Look for something |
---|
| 400 | interesting. */ |
---|
| 401 | if( !http_handle_headers( req ) ) |
---|
| 402 | return CR_ABORT; |
---|
| 403 | |
---|
| 404 | /* Start parsing the body as chunked if required. */ |
---|
| 405 | if( req->flags & HTTPC_CHUNKED ) |
---|
| 406 | return http_process_chunked_data( req, NULL, 0 ); |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | else |
---|
| 410 | { |
---|
| 411 | int pos = req->reply_body - req->sbuf; |
---|
| 412 | req->sbuf = g_realloc( req->sbuf, req->sblen + len + 1 ); |
---|
| 413 | memcpy( req->sbuf + req->sblen, buffer, len ); |
---|
| 414 | req->bytes_read += len; |
---|
| 415 | req->sblen += len; |
---|
| 416 | req->sbuf[req->sblen] = '\0'; |
---|
| 417 | req->reply_body = req->sbuf + pos; |
---|
| 418 | req->body_size = req->sblen - pos; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | if( ( req->flags & HTTPC_STREAMING ) && req->reply_body ) |
---|
| 422 | req->func( req ); |
---|
| 423 | |
---|
| 424 | return CR_OK; |
---|
| 425 | } |
---|
| 426 | |
---|
[8bd866f] | 427 | /* Splits headers and body. Checks result code, in case of 300s it'll handle |
---|
| 428 | redirects. If this returns FALSE, don't call any callbacks! */ |
---|
| 429 | static gboolean http_handle_headers( struct http_request *req ) |
---|
| 430 | { |
---|
[ca8037e] | 431 | char *end1, *end2, *s; |
---|
[8bd866f] | 432 | int evil_server = 0; |
---|
| 433 | |
---|
[8a9afe4] | 434 | /* Zero termination is very convenient. */ |
---|
[8bd866f] | 435 | req->reply_headers[req->bytes_read] = '\0'; |
---|
[8a9afe4] | 436 | |
---|
| 437 | /* Find the separation between headers and body, and keep stupid |
---|
| 438 | webservers in mind. */ |
---|
| 439 | end1 = strstr( req->reply_headers, "\r\n\r\n" ); |
---|
| 440 | end2 = strstr( req->reply_headers, "\n\n" ); |
---|
| 441 | |
---|
| 442 | if( end2 && end2 < end1 ) |
---|
| 443 | { |
---|
[52b3a99] | 444 | end1 = end2 + 1; |
---|
[8a9afe4] | 445 | evil_server = 1; |
---|
| 446 | } |
---|
[0eec386] | 447 | else if( end1 ) |
---|
[52b3a99] | 448 | { |
---|
| 449 | end1 += 2; |
---|
| 450 | } |
---|
[0eec386] | 451 | else |
---|
[8a9afe4] | 452 | { |
---|
[7deb447] | 453 | req->status_string = g_strdup( "Malformed HTTP reply" ); |
---|
[8bd866f] | 454 | return TRUE; |
---|
[52b3a99] | 455 | } |
---|
| 456 | |
---|
[ca8037e] | 457 | *end1 = '\0'; |
---|
[0eec386] | 458 | |
---|
[bd31661] | 459 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
[3f808ca] | 460 | printf( "HTTP response headers:\n%s\n", req->reply_headers ); |
---|
[bd31661] | 461 | |
---|
[0eec386] | 462 | if( evil_server ) |
---|
| 463 | req->reply_body = end1 + 1; |
---|
| 464 | else |
---|
| 465 | req->reply_body = end1 + 2; |
---|
| 466 | |
---|
[ca8037e] | 467 | /* Separately allocated space for headers and body. */ |
---|
| 468 | req->sblen = req->body_size = req->reply_headers + req->bytes_read - req->reply_body; |
---|
| 469 | req->sbuf = req->reply_body = g_memdup( req->reply_body, req->body_size + 1 ); |
---|
| 470 | req->reply_headers = g_realloc( req->reply_headers, end1 - req->reply_headers + 1 ); |
---|
[0eec386] | 471 | |
---|
[52b3a99] | 472 | if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL ) |
---|
| 473 | { |
---|
[8bd866f] | 474 | if( sscanf( end1 + 1, "%hd", &req->status_code ) != 1 ) |
---|
[7deb447] | 475 | { |
---|
| 476 | req->status_string = g_strdup( "Can't parse status code" ); |
---|
[52b3a99] | 477 | req->status_code = -1; |
---|
[7deb447] | 478 | } |
---|
| 479 | else |
---|
| 480 | { |
---|
| 481 | char *eol; |
---|
| 482 | |
---|
| 483 | if( evil_server ) |
---|
| 484 | eol = strchr( end1, '\n' ); |
---|
| 485 | else |
---|
| 486 | eol = strchr( end1, '\r' ); |
---|
| 487 | |
---|
| 488 | req->status_string = g_strndup( end1 + 1, eol - end1 - 1 ); |
---|
| 489 | |
---|
| 490 | /* Just to be sure... */ |
---|
| 491 | if( ( eol = strchr( req->status_string, '\r' ) ) ) |
---|
| 492 | *eol = 0; |
---|
| 493 | if( ( eol = strchr( req->status_string, '\n' ) ) ) |
---|
| 494 | *eol = 0; |
---|
| 495 | } |
---|
[52b3a99] | 496 | } |
---|
| 497 | else |
---|
| 498 | { |
---|
[7deb447] | 499 | req->status_string = g_strdup( "Can't locate status code" ); |
---|
[52b3a99] | 500 | req->status_code = -1; |
---|
| 501 | } |
---|
| 502 | |
---|
[3f808ca] | 503 | if( ( ( req->status_code >= 301 && req->status_code <= 303 ) || |
---|
| 504 | req->status_code == 307 ) && req->redir_ttl-- > 0 ) |
---|
[52b3a99] | 505 | { |
---|
| 506 | char *loc, *new_request, *new_host; |
---|
| 507 | int error = 0, new_port, new_proto; |
---|
| 508 | |
---|
[7deb447] | 509 | /* We might fill it again, so let's not leak any memory. */ |
---|
| 510 | g_free( req->status_string ); |
---|
| 511 | req->status_string = NULL; |
---|
| 512 | |
---|
[52b3a99] | 513 | loc = strstr( req->reply_headers, "\nLocation: " ); |
---|
| 514 | if( loc == NULL ) /* We can't handle this redirect... */ |
---|
[7deb447] | 515 | { |
---|
| 516 | req->status_string = g_strdup( "Can't locate Location: header" ); |
---|
[8bd866f] | 517 | return TRUE; |
---|
[7deb447] | 518 | } |
---|
[52b3a99] | 519 | |
---|
| 520 | loc += 11; |
---|
| 521 | while( *loc == ' ' ) |
---|
| 522 | loc ++; |
---|
| 523 | |
---|
| 524 | /* TODO/FIXME: Possibly have to handle relative redirections, |
---|
| 525 | and rewrite Host: headers. Not necessary for now, it's |
---|
| 526 | enough for passport authentication like this. */ |
---|
| 527 | |
---|
| 528 | if( *loc == '/' ) |
---|
| 529 | { |
---|
| 530 | /* Just a different pathname... */ |
---|
| 531 | |
---|
| 532 | /* Since we don't cache the servername, and since we |
---|
| 533 | don't need this yet anyway, I won't implement it. */ |
---|
| 534 | |
---|
[ca8037e] | 535 | req->status_string = g_strdup( "Can't handle relative redirects" ); |
---|
[7deb447] | 536 | |
---|
[8bd866f] | 537 | return TRUE; |
---|
[52b3a99] | 538 | } |
---|
[8a9afe4] | 539 | else |
---|
[52b3a99] | 540 | { |
---|
| 541 | /* A whole URL */ |
---|
| 542 | url_t *url; |
---|
[dd7b931] | 543 | char *s, *version, *headers; |
---|
[03a8f8e] | 544 | const char *new_method; |
---|
[52b3a99] | 545 | |
---|
| 546 | s = strstr( loc, "\r\n" ); |
---|
| 547 | if( s == NULL ) |
---|
[8bd866f] | 548 | return TRUE; |
---|
[52b3a99] | 549 | |
---|
| 550 | url = g_new0( url_t, 1 ); |
---|
| 551 | *s = 0; |
---|
| 552 | |
---|
| 553 | if( !url_set( url, loc ) ) |
---|
| 554 | { |
---|
[7deb447] | 555 | req->status_string = g_strdup( "Malformed redirect URL" ); |
---|
[52b3a99] | 556 | g_free( url ); |
---|
[8bd866f] | 557 | return TRUE; |
---|
[52b3a99] | 558 | } |
---|
| 559 | |
---|
[03a8f8e] | 560 | /* Find all headers and, if necessary, the POST request contents. |
---|
| 561 | Skip the old Host: header though. This crappy code here means |
---|
| 562 | anything using this http_client MUST put the Host: header at |
---|
| 563 | the top. */ |
---|
[dff732d] | 564 | if( !( ( s = strstr( req->request, "\r\nHost: " ) ) && |
---|
| 565 | ( s = strstr( s + strlen( "\r\nHost: " ), "\r\n" ) ) ) ) |
---|
[52b3a99] | 566 | { |
---|
[7deb447] | 567 | req->status_string = g_strdup( "Error while rebuilding request string" ); |
---|
[52b3a99] | 568 | g_free( url ); |
---|
[8bd866f] | 569 | return TRUE; |
---|
[52b3a99] | 570 | } |
---|
[dd7b931] | 571 | headers = s; |
---|
[52b3a99] | 572 | |
---|
[03a8f8e] | 573 | /* More or less HTTP/1.0 compliant, from my reading of RFC 2616. |
---|
| 574 | Always perform a GET request unless we received a 301. 303 was |
---|
| 575 | meant for this but it's HTTP/1.1-only and we're specifically |
---|
[3f808ca] | 576 | speaking HTTP/1.0. ... |
---|
| 577 | |
---|
| 578 | Well except someone at identi.ca's didn't bother reading any |
---|
| 579 | RFCs and just return HTTP/1.1-specific status codes to HTTP/1.0 |
---|
| 580 | requests. Fuckers. So here we are, handle 301..303,307. */ |
---|
| 581 | if( strncmp( req->request, "GET", 3 ) == 0 ) |
---|
| 582 | /* GETs never become POSTs. */ |
---|
| 583 | new_method = "GET"; |
---|
| 584 | else if( req->status_code == 302 || req->status_code == 303 ) |
---|
| 585 | /* 302 de-facto becomes GET, 303 as specified by RFC 2616#10.3.3 */ |
---|
| 586 | new_method = "GET"; |
---|
| 587 | else |
---|
| 588 | /* 301 de-facto should stay POST, 307 specifally RFC 2616#10.3.8 */ |
---|
| 589 | new_method = "POST"; |
---|
[03a8f8e] | 590 | |
---|
[dd7b931] | 591 | if( ( version = strstr( req->request, " HTTP/" ) ) && |
---|
| 592 | ( s = strstr( version, "\r\n" ) ) ) |
---|
| 593 | { |
---|
| 594 | version ++; |
---|
| 595 | version = g_strndup( version, s - version ); |
---|
| 596 | } |
---|
| 597 | else |
---|
| 598 | version = g_strdup( "HTTP/1.0" ); |
---|
| 599 | |
---|
[03a8f8e] | 600 | /* Okay, this isn't fun! We have to rebuild the request... :-( */ |
---|
[dd7b931] | 601 | new_request = g_strdup_printf( "%s %s %s\r\nHost: %s%s", |
---|
| 602 | new_method, url->file, version, |
---|
| 603 | url->host, headers ); |
---|
[03a8f8e] | 604 | |
---|
[52b3a99] | 605 | new_host = g_strdup( url->host ); |
---|
| 606 | new_port = url->port; |
---|
| 607 | new_proto = url->proto; |
---|
| 608 | |
---|
[03a8f8e] | 609 | /* If we went from POST to GET, truncate the request content. */ |
---|
| 610 | if( new_request[0] != req->request[0] && new_request[0] == 'G' && |
---|
| 611 | ( s = strstr( new_request, "\r\n\r\n" ) ) ) |
---|
| 612 | s[4] = '\0'; |
---|
| 613 | |
---|
[52b3a99] | 614 | g_free( url ); |
---|
[dd7b931] | 615 | g_free( version ); |
---|
[52b3a99] | 616 | } |
---|
| 617 | |
---|
| 618 | if( req->ssl ) |
---|
| 619 | ssl_disconnect( req->ssl ); |
---|
| 620 | else |
---|
| 621 | closesocket( req->fd ); |
---|
| 622 | |
---|
| 623 | req->fd = -1; |
---|
[7deb447] | 624 | req->ssl = NULL; |
---|
[52b3a99] | 625 | |
---|
[bd31661] | 626 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
[3f808ca] | 627 | printf( "New headers for redirected HTTP request:\n%s\n", new_request ); |
---|
[bd31661] | 628 | |
---|
[52b3a99] | 629 | if( new_proto == PROTO_HTTPS ) |
---|
| 630 | { |
---|
[a72dc2b] | 631 | req->ssl = ssl_connect( new_host, new_port, TRUE, http_ssl_connected, req ); |
---|
[52b3a99] | 632 | if( req->ssl == NULL ) |
---|
| 633 | error = 1; |
---|
| 634 | } |
---|
| 635 | else |
---|
| 636 | { |
---|
| 637 | req->fd = proxy_connect( new_host, new_port, http_connected, req ); |
---|
| 638 | if( req->fd < 0 ) |
---|
| 639 | error = 1; |
---|
| 640 | } |
---|
[2db811a] | 641 | g_free( new_host ); |
---|
[52b3a99] | 642 | |
---|
| 643 | if( error ) |
---|
| 644 | { |
---|
[7deb447] | 645 | req->status_string = g_strdup( "Connection problem during redirect" ); |
---|
[52b3a99] | 646 | g_free( new_request ); |
---|
[8bd866f] | 647 | return TRUE; |
---|
[52b3a99] | 648 | } |
---|
| 649 | |
---|
| 650 | g_free( req->request ); |
---|
| 651 | g_free( req->reply_headers ); |
---|
[ab19567] | 652 | g_free( req->sbuf ); |
---|
[52b3a99] | 653 | req->request = new_request; |
---|
| 654 | req->request_length = strlen( new_request ); |
---|
| 655 | req->bytes_read = req->bytes_written = req->inpa = 0; |
---|
| 656 | req->reply_headers = req->reply_body = NULL; |
---|
[ab19567] | 657 | req->sbuf = req->cbuf = NULL; |
---|
| 658 | req->sblen = req->cblen = 0; |
---|
[52b3a99] | 659 | |
---|
[ba9edaa] | 660 | return FALSE; |
---|
[8a9afe4] | 661 | } |
---|
[ca8037e] | 662 | |
---|
| 663 | if( ( s = get_rfc822_header( req->reply_headers, "Content-Length", 0 ) ) && |
---|
| 664 | sscanf( s, "%d", &req->content_length ) != 1 ) |
---|
| 665 | req->content_length = -1; |
---|
| 666 | g_free( s ); |
---|
| 667 | |
---|
| 668 | if( ( s = get_rfc822_header( req->reply_headers, "Transfer-Encoding", 0 ) ) ) |
---|
| 669 | { |
---|
| 670 | if( strcasestr( s, "chunked" ) ) |
---|
| 671 | { |
---|
| 672 | req->flags |= HTTPC_CHUNKED; |
---|
| 673 | req->cbuf = req->sbuf; |
---|
| 674 | req->cblen = req->sblen; |
---|
| 675 | |
---|
| 676 | req->reply_body = req->sbuf = g_strdup( "" ); |
---|
| 677 | req->body_size = req->sblen = 0; |
---|
| 678 | } |
---|
| 679 | g_free( s ); |
---|
| 680 | } |
---|
[8a9afe4] | 681 | |
---|
[8bd866f] | 682 | return TRUE; |
---|
| 683 | } |
---|
[8a9afe4] | 684 | |
---|
[8bd866f] | 685 | void http_flush_bytes( struct http_request *req, size_t len ) |
---|
| 686 | { |
---|
[1388d30] | 687 | if( len <= 0 || len > req->body_size || !( req->flags & HTTPC_STREAMING ) ) |
---|
| 688 | return; |
---|
| 689 | |
---|
| 690 | req->reply_body += len; |
---|
| 691 | req->body_size -= len; |
---|
| 692 | |
---|
| 693 | if( req->reply_body - req->sbuf >= 512 ) |
---|
[55ccc9a0] | 694 | { |
---|
[1388d30] | 695 | char *new = g_memdup( req->reply_body, req->body_size + 1 ); |
---|
| 696 | g_free( req->sbuf ); |
---|
| 697 | req->reply_body = req->sbuf = new; |
---|
| 698 | req->sblen = req->body_size; |
---|
[55ccc9a0] | 699 | } |
---|
[fb98634] | 700 | } |
---|
| 701 | |
---|
[1388d30] | 702 | void http_close( struct http_request *req ) |
---|
| 703 | { |
---|
| 704 | if( !req ) |
---|
| 705 | return; |
---|
| 706 | |
---|
[898c08e] | 707 | if( req->inpa > 0 ) |
---|
| 708 | b_event_remove( req->inpa ); |
---|
| 709 | |
---|
[1388d30] | 710 | if( req->ssl ) |
---|
| 711 | ssl_disconnect( req->ssl ); |
---|
| 712 | else |
---|
| 713 | closesocket( req->fd ); |
---|
| 714 | |
---|
| 715 | http_free( req ); |
---|
| 716 | } |
---|
| 717 | |
---|
[516a9c6] | 718 | static void http_free( struct http_request *req ) |
---|
[fb98634] | 719 | { |
---|
[8a9afe4] | 720 | g_free( req->request ); |
---|
| 721 | g_free( req->reply_headers ); |
---|
[7deb447] | 722 | g_free( req->status_string ); |
---|
[8bd866f] | 723 | g_free( req->sbuf ); |
---|
[ca8037e] | 724 | g_free( req->cbuf ); |
---|
[8a9afe4] | 725 | g_free( req ); |
---|
| 726 | } |
---|