diff --git a/opentracker.c b/opentracker.c index c1fe945..57eca12 100644 --- a/opentracker.c +++ b/opentracker.c @@ -143,10 +143,8 @@ static size_t header_complete( char * request, ssize_t byte_count ) { static void handle_dead( const int64 sock ) { struct http_data* cookie=io_getcookie( sock ); if( cookie ) { - if( cookie->flag & STRUCT_HTTP_FLAG_IOB_USED ) - iob_reset( &cookie->data.batch ); - if( cookie->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) - array_reset( &cookie->data.request ); + iob_reset( &cookie->batch ); + array_reset( &cookie->request ); if( cookie->flag & STRUCT_HTTP_FLAG_WAITINGFORTASK ) mutex_workqueue_canceltask( sock ); free( cookie ); @@ -154,46 +152,42 @@ static void handle_dead( const int64 sock ) { io_close( sock ); } -static ssize_t handle_read( const int64 sock, struct ot_workstruct *ws ) { +static void handle_read( const int64 sock, struct ot_workstruct *ws ) { struct http_data* cookie = io_getcookie( sock ); ssize_t byte_count; if( ( byte_count = io_tryread( sock, ws->inbuf, G_INBUF_SIZE ) ) <= 0 ) { handle_dead( sock ); - return 0; + return; } /* If we get the whole request in one packet, handle it without copying */ - if( !array_start( &cookie->data.request ) ) { - if( memchr( ws->inbuf, '\n', byte_count ) ) { + if( !array_start( &cookie->request ) ) { + if( ( ws->header_size = header_complete( ws->inbuf, byte_count ) ) ) { ws->request = ws->inbuf; ws->request_size = byte_count; - return http_handle_request( sock, ws ); - } - - /* ... else take a copy */ - cookie->flag |= STRUCT_HTTP_FLAG_ARRAY_USED; - array_catb( &cookie->data.request, ws->inbuf, byte_count ); - return 0; + http_handle_request( sock, ws ); + } else + array_catb( &cookie->request, ws->inbuf, byte_count ); + return; } - array_catb( &cookie->data.request, ws->inbuf, byte_count ); - - if( array_failed( &cookie->data.request ) || - array_bytes( &cookie->data.request ) > 8192 ) - return http_issue_error( sock, ws, CODE_HTTPERROR_500 ); - - if( !memchr( array_start( &cookie->data.request ), '\n', array_bytes( &cookie->data.request ) ) ) - return 0; + array_catb( &cookie->request, ws->inbuf, byte_count ); + if( array_failed( &cookie->request ) || array_bytes( &cookie->request ) > 8192 ) { + http_issue_error( sock, ws, CODE_HTTPERROR_500 ); + return; + } - ws->request = array_start( &cookie->data.request ); - ws->request_size = array_bytes( &cookie->data.request ); - return http_handle_request( sock, ws ); + while( ( ws->header_size = header_complete( array_start( &cookie->request ), array_bytes( &cookie->request ) ) ) ) { + ws->request = array_start( &cookie->request ); + ws->request_size = array_bytes( &cookie->request ); + http_handle_request( sock, ws ); + } } static void handle_write( const int64 sock ) { struct http_data* cookie=io_getcookie( sock ); - if( !cookie || ( iob_send( sock, &cookie->data.batch ) <= 0 ) ) + if( !cookie || ( iob_send( sock, &cookie->batch ) <= 0 ) ) handle_dead( sock ); } @@ -214,12 +208,12 @@ static void handle_accept( const int64 serversocket ) { io_close( sock ); continue; } - io_setcookie( sock, cookie ); - io_wantread( sock ); - memset(cookie, 0, sizeof( struct http_data ) ); memcpy(cookie->ip,ip,sizeof(ot_ip6)); + io_setcookie( sock, cookie ); + io_wantread( sock ); + stats_issue_event( EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); /* That breaks taia encapsulation. But there is no way to take system @@ -228,17 +222,16 @@ static void handle_accept( const int64 serversocket ) { tai_unix( &(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT) ); io_timeout( sock, t ); } - - if( errno == EAGAIN ) - io_eagain( serversocket ); } -static void server_mainloop( ) { +static void * server_mainloop( void * args ) { struct ot_workstruct ws; time_t next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL; struct iovec *iovector; int iovec_entries; + (void)args; + /* Initialize our "thread local storage" */ ws.inbuf = malloc( G_INBUF_SIZE ); ws.outbuf = malloc( G_OUTBUF_SIZE ); @@ -282,6 +275,7 @@ static void server_mainloop( ) { /* Enforce setting the clock */ signal_handler( SIGALRM ); } + return 0; } static int64_t ot_try_bind( ot_ip6 ip, uint16_t port, PROTO_FLAG proto ) { @@ -475,7 +469,7 @@ void load_state(const char * const state_filename ) { if( inbuf[ i++ ] != ':' || !( consumed = scan_ulonglong( inbuf+i, &downcount ) ) ) continue; add_torrent_from_saved_state( infohash, base, downcount ); } - + fclose( state_filehandle ); } @@ -606,7 +600,7 @@ int main( int argc, char **argv ) { /* Kick off our initial clock setting alarm */ alarm(5); - server_mainloop( ); + server_mainloop( 0 ); return 0; } diff --git a/ot_http.c b/ot_http.c index 72e8c57..9c9fb0f 100644 --- a/ot_http.c +++ b/ot_http.c @@ -18,6 +18,7 @@ #include "iob.h" #include "ip6.h" #include "scan.h" +#include "case.h" /* Opentracker */ #include "trackerlogic.h" @@ -44,33 +45,42 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) { struct http_data *cookie = io_getcookie( sock ); ssize_t written_size; + if( !cookie ) { io_close(sock); return; } + /* whoever sends data is not interested in its input-array */ - if( cookie && ( cookie->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) ) { - cookie->flag &= ~STRUCT_HTTP_FLAG_ARRAY_USED; - array_reset( &cookie->data.request ); - } + if( ws->keep_alive && ws->header_size != ws->request_size ) { + size_t rest = ws->request_size - ws->header_size; + if( array_start(&cookie->request) ) { + memmove( array_start(&cookie->request), ws->request + ws->header_size, rest ); + array_truncate( &cookie->request, 1, rest ); + } else + array_catb(&cookie->request, ws->request + ws->header_size, rest ); + } else + array_reset( &cookie->request ); written_size = write( sock, ws->reply, ws->reply_size ); - if( ( written_size < 0 ) || ( written_size == ws->reply_size ) ) { - free( cookie ); io_close( sock ); - } else { + if( ( written_size < 0 ) || ( ( written_size == ws->reply_size ) && !ws->keep_alive ) ) { + array_reset( &cookie->request ); + free( cookie ); io_close( sock ); return; + } + + if( written_size < ws->reply_size ) { char * outbuf; tai6464 t; - if( !cookie ) return; - if( !( outbuf = malloc( ws->reply_size - written_size ) ) ) { + if( !( outbuf = malloc( ws->reply_size - written_size ) ) ) { free(cookie); io_close( sock ); return; } - iob_reset( &cookie->data.batch ); memcpy( outbuf, ws->reply + written_size, ws->reply_size - written_size ); - iob_addbuf_free( &cookie->data.batch, outbuf, ws->reply_size - written_size ); - cookie->flag |= STRUCT_HTTP_FLAG_IOB_USED; + iob_addbuf_free( &cookie->batch, outbuf, ws->reply_size - written_size ); /* writeable short data sockets just have a tcp timeout */ - taia_uint( &t, 0 ); io_timeout( sock, t ); - io_dontwantread( sock ); + if( !ws->keep_alive ) { + taia_uint( &t, 0 ); io_timeout( sock, t ); + io_dontwantread( sock ); + } io_wantwrite( sock ); } } @@ -93,7 +103,7 @@ ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code ) if( code == CODE_HTTPERROR_302 ) 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 ); else - 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