diff options
Diffstat (limited to 'ot_http.c')
| -rw-r--r-- | ot_http.c | 52 |
1 files changed, 20 insertions, 32 deletions
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "byte.h" | 15 | #include "byte.h" |
| 16 | #include "array.h" | 16 | #include "array.h" |
| 17 | #include "iob.h" | 17 | #include "iob.h" |
| 18 | #include "ip6.h" | ||
| 18 | 19 | ||
| 19 | /* Opentracker */ | 20 | /* Opentracker */ |
| 20 | #include "trackerlogic.h" | 21 | #include "trackerlogic.h" |
| @@ -34,11 +35,6 @@ enum { | |||
| 34 | SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING = 32, | 35 | SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING = 32, |
| 35 | SUCCESS_HTTP_SIZE_OFF = 17 }; | 36 | SUCCESS_HTTP_SIZE_OFF = 17 }; |
| 36 | 37 | ||
| 37 | #ifdef _DEBUG_PEERID | ||
| 38 | size_t g_this_peerid_len = 0; | ||
| 39 | char *g_this_peerid_data = NULL; | ||
| 40 | #endif | ||
| 41 | |||
| 42 | static void http_senddata( const int64 client_socket, struct ot_workstruct *ws ) { | 38 | static void http_senddata( const int64 client_socket, struct ot_workstruct *ws ) { |
| 43 | struct http_data *h = io_getcookie( client_socket ); | 39 | struct http_data *h = io_getcookie( client_socket ); |
| 44 | ssize_t written_size; | 40 | ssize_t written_size; |
| @@ -63,7 +59,7 @@ static void http_senddata( const int64 client_socket, struct ot_workstruct *ws ) | |||
| 63 | } | 59 | } |
| 64 | 60 | ||
| 65 | iob_reset( &h->data.batch ); | 61 | iob_reset( &h->data.batch ); |
| 66 | memmove( outbuf, ws->reply + written_size, ws->reply_size - written_size ); | 62 | memcpy( outbuf, ws->reply + written_size, ws->reply_size - written_size ); |
| 67 | iob_addbuf_free( &h->data.batch, outbuf, ws->reply_size - written_size ); | 63 | iob_addbuf_free( &h->data.batch, outbuf, ws->reply_size - written_size ); |
| 68 | h->flag |= STRUCT_HTTP_FLAG_IOB_USED; | 64 | h->flag |= STRUCT_HTTP_FLAG_IOB_USED; |
| 69 | 65 | ||
| @@ -89,9 +85,9 @@ ssize_t http_issue_error( const int64 client_socket, struct ot_workstruct *ws, i | |||
| 89 | 85 | ||
| 90 | ws->reply = ws->outbuf; | 86 | ws->reply = ws->outbuf; |
| 91 | if( code == CODE_HTTPERROR_302 ) | 87 | if( code == CODE_HTTPERROR_302 ) |
| 92 | ws->reply_size = snprintf( ws->reply, ws->outbuf_size, "HTTP/1.0 302 Found\r\nContent-Length: 0\r\nLocation: %s\r\n\r\n", g_redirecturl ); | 88 | ws->reply_size = snprintf( ws->reply, G_OUTBUF_SIZE, "HTTP/1.0 302 Found\r\nContent-Length: 0\r\nLocation: %s\r\n\r\n", g_redirecturl ); |
| 93 | else | 89 | else |
| 94 | ws->reply_size = snprintf( ws->reply, ws->outbuf_size, "HTTP/1.0 %s\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n", title, strlen(title)+16-4,title+4); | 90 | ws->reply_size = snprintf( ws->reply, G_OUTBUF_SIZE, "HTTP/1.0 %s\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n", title, strlen(title)+16-4,title+4); |
| 95 | 91 | ||
| 96 | #ifdef _DEBUG_HTTPERROR | 92 | #ifdef _DEBUG_HTTPERROR |
| 97 | fprintf( stderr, "DEBUG: invalid request was: %s\n", ws->debugbuf ); | 93 | fprintf( stderr, "DEBUG: invalid request was: %s\n", ws->debugbuf ); |
| @@ -245,13 +241,13 @@ static ssize_t http_handle_fullscrape( const int64 client_socket, struct ot_work | |||
| 245 | if( strstr( ws->request, "gzip" ) ) { | 241 | if( strstr( ws->request, "gzip" ) ) { |
| 246 | h->flag |= STRUCT_HTTP_FLAG_GZIP; | 242 | h->flag |= STRUCT_HTTP_FLAG_GZIP; |
| 247 | format = TASK_FLAG_GZIP; | 243 | format = TASK_FLAG_GZIP; |
| 248 | stats_issue_event( EVENT_FULLSCRAPE_REQUEST_GZIP, *(int*)h->ip, 0 ); | 244 | stats_issue_event( EVENT_FULLSCRAPE_REQUEST_GZIP, 0, (uintptr_t)h->ip ); |
| 249 | } else | 245 | } else |
| 250 | #endif | 246 | #endif |
| 251 | stats_issue_event( EVENT_FULLSCRAPE_REQUEST, *(int*)h->ip, 0 ); | 247 | stats_issue_event( EVENT_FULLSCRAPE_REQUEST, 0, (uintptr_t)h->ip ); |
| 252 | 248 | ||
| 253 | #ifdef _DEBUG_HTTPERROR | 249 | #ifdef _DEBUG_HTTPERROR |
| 254 | write( 2, ws->debugbuf, ws->debugbuf_size ); | 250 | write( 2, ws->debugbuf, G_DEBUGBUF_SIZE ); |
| 255 | #endif | 251 | #endif |
| 256 | 252 | ||
| 257 | /* Pass this task to the worker thread */ | 253 | /* Pass this task to the worker thread */ |
| @@ -263,6 +259,7 @@ write( 2, ws->debugbuf, ws->debugbuf_size ); | |||
| 263 | return ws->reply_size = -2; | 259 | return ws->reply_size = -2; |
| 264 | } | 260 | } |
| 265 | #endif | 261 | #endif |
| 262 | |||
| 266 | static ssize_t http_handle_scrape( const int64 client_socket, struct ot_workstruct *ws, char *read_ptr ) { | 263 | static ssize_t http_handle_scrape( const int64 client_socket, struct ot_workstruct *ws, char *read_ptr ) { |
| 267 | static const ot_keywords keywords_scrape[] = { { "info_hash", 1 }, { NULL, -3 } }; | 264 | static const ot_keywords keywords_scrape[] = { { "info_hash", 1 }, { NULL, -3 } }; |
| 268 | 265 | ||
| @@ -306,9 +303,6 @@ static ot_keywords keywords_announce[] = { { "port", 1 }, { "left", 2 }, { "even | |||
| 306 | #ifdef WANT_IP_FROM_QUERY_STRING | 303 | #ifdef WANT_IP_FROM_QUERY_STRING |
| 307 | { "ip", 7 }, | 304 | { "ip", 7 }, |
| 308 | #endif | 305 | #endif |
| 309 | #ifdef _DEBUG_PEERID | ||
| 310 | { "peer_id", 8 }, | ||
| 311 | #endif | ||
| 312 | { NULL, -3 } }; | 306 | { NULL, -3 } }; |
| 313 | static ot_keywords keywords_announce_event[] = { { "completed", 1 }, { "stopped", 2 }, { NULL, -3 } }; | 307 | static ot_keywords keywords_announce_event[] = { { "completed", 1 }, { "stopped", 2 }, { NULL, -3 } }; |
| 314 | static ssize_t http_handle_announce( const int64 client_socket, struct ot_workstruct *ws, char *read_ptr ) { | 308 | static ssize_t http_handle_announce( const int64 client_socket, struct ot_workstruct *ws, char *read_ptr ) { |
| @@ -332,10 +326,6 @@ static ssize_t http_handle_announce( const int64 client_socket, struct ot_workst | |||
| 332 | numwant = 50; | 326 | numwant = 50; |
| 333 | scanon = 1; | 327 | scanon = 1; |
| 334 | 328 | ||
| 335 | #ifdef _DEBUG_PEERID | ||
| 336 | ws->peer_id = NULL; | ||
| 337 | #endif | ||
| 338 | |||
| 339 | while( scanon ) { | 329 | while( scanon ) { |
| 340 | switch( scan_find_keywords(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM ) ) { | 330 | switch( scan_find_keywords(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM ) ) { |
| 341 | case -2: scanon = 0; break; /* TERMINATOR */ | 331 | case -2: scanon = 0; break; /* TERMINATOR */ |
| @@ -383,17 +373,15 @@ static ssize_t http_handle_announce( const int64 client_socket, struct ot_workst | |||
| 383 | break; | 373 | break; |
| 384 | #ifdef WANT_IP_FROM_QUERY_STRING | 374 | #ifdef WANT_IP_FROM_QUERY_STRING |
| 385 | case 7: /* matched "ip" */ | 375 | case 7: /* matched "ip" */ |
| 386 | len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ); | 376 | { |
| 387 | if( ( len <= 0 ) || scan_fixed_ip( write_ptr, len, (unsigned char*)/*tmp*/ws->reply ) ) HTTPERROR_400_PARAM; | 377 | char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply+16; |
| 388 | OT_SETIP( &peer, /*tmp*/ws->reply ); | 378 | len = scan_urlencoded_query( &read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE ); |
| 379 | tmp_buf2[len] = 0; | ||
| 380 | if( ( len <= 0 ) || scan_ip6( tmp_buf2, tmp_buf1 ) ) HTTPERROR_400_PARAM; | ||
| 381 | OT_SETIP( &peer, tmp_buf1 ); | ||
| 382 | } | ||
| 389 | break; | 383 | break; |
| 390 | #endif | 384 | #endif |
| 391 | #ifdef _DEBUG_PEERID | ||
| 392 | case 8: /* matched "peer_id" */ | ||
| 393 | ws->peer_id_size = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ); | ||
| 394 | ws->peer_id = ws->peer_id_size > 0 ? write_ptr : 0; | ||
| 395 | break; | ||
| 396 | #endif | ||
| 397 | } | 385 | } |
| 398 | } | 386 | } |
| 399 | 387 | ||
| @@ -402,9 +390,9 @@ static ssize_t http_handle_announce( const int64 client_socket, struct ot_workst | |||
| 402 | return ws->reply_size = sprintf( ws->reply, "d14:failure reason80:Your client forgot to send your torrent's info_hash. Please upgrade your client.e" ); | 390 | return ws->reply_size = sprintf( ws->reply, "d14:failure reason80:Your client forgot to send your torrent's info_hash. Please upgrade your client.e" ); |
| 403 | 391 | ||
| 404 | if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) | 392 | if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) |
| 405 | ws->reply_size = remove_peer_from_torrent( hash, &peer, ws->reply, FLAG_TCP ); | 393 | ws->reply_size = remove_peer_from_torrent( *hash, &peer, ws->reply, FLAG_TCP ); |
| 406 | else | 394 | else |
| 407 | ws->reply_size = add_peer_to_torrent_and_return_peers(hash, &peer, FLAG_TCP, numwant, ws->reply ); | 395 | ws->reply_size = add_peer_to_torrent_and_return_peers( *hash, &peer, FLAG_TCP, numwant, ws->reply ); |
| 408 | 396 | ||
| 409 | if( !ws->reply_size ) HTTPERROR_500; | 397 | if( !ws->reply_size ) HTTPERROR_500; |
| 410 | 398 | ||
| @@ -418,9 +406,9 @@ ssize_t http_handle_request( const int64 client_socket, struct ot_workstruct *ws | |||
| 418 | 406 | ||
| 419 | #ifdef _DEBUG_HTTPERROR | 407 | #ifdef _DEBUG_HTTPERROR |
| 420 | reply_off = ws->request_size; | 408 | reply_off = ws->request_size; |
| 421 | if( ws->request_size >= (ssize_t)ws->debugbuf_size ) | 409 | if( ws->request_size >= G_DEBUGBUF_SIZE ) |
| 422 | reply_off = ws->debugbuf_size - 1; | 410 | reply_off = G_DEBUGBUF_SIZE - 1; |
| 423 | memmove( ws->debugbuf, ws->request, reply_off ); | 411 | memcpy( ws->debugbuf, ws->request, reply_off ); |
| 424 | ws->debugbuf[ reply_off ] = 0; | 412 | ws->debugbuf[ reply_off ] = 0; |
| 425 | #endif | 413 | #endif |
| 426 | 414 | ||
