static char debug_request[8192];
#endif
+typedef enum {
+ STRUCT_HTTP_FLAG_ARRAY_USED = 1,
+ STRUCT_HTTP_FLAG_IOB_USED = 2
+} STRUCT_HTTP_FLAG;
+
struct http_data {
union {
- array request;
- io_batch batch;
+ array request;
+ io_batch batch;
};
- unsigned char ip[4];
- int blessed;
+ unsigned char ip[4];
+ STRUCT_HTTP_FLAG flag;
};
+#define NOTBLESSED( h ) byte_diff( &h->ip, 4, g_adminip )
/* Prototypes */
if( !h )
return free( buffer );
- array_reset( &h->request );
+ if( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) {
+ h->flag &= ~STRUCT_HTTP_FLAG_ARRAY_USED;
+ array_reset( &h->request );
+ }
header = malloc( SUCCESS_HTTP_HEADER_LENGTH );
if( !header ) {
iob_reset( &h->batch );
iob_addbuf_free( &h->batch, header, header_size );
iob_addbuf_munmap( &h->batch, buffer, size );
+ h->flag |= STRUCT_HTTP_FLAG_IOB_USED;
/* writeable sockets timeout after twice the pool timeout
which defaults to 5 minutes (e.g. after 10 minutes) */
ssize_t written_size;
/* whoever sends data is not interested in its input-array */
- if( h )
+ if( h && ( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) ) {
+ h->flag &= ~STRUCT_HTTP_FLAG_ARRAY_USED;
array_reset( &h->request );
+ }
written_size = write( s, buffer, size );
if( ( written_size < 0 ) || ( (size_t)written_size == size ) ) {
free( h ); io_close( s );
} else {
- char * outbuf = malloc( size - written_size );
+ char * outbuf;
tai6464 t;
- if( !outbuf ) {
+ if( !h ) return;
+ if( !( outbuf = malloc( size - written_size ) ) ) {
free(h); io_close( s );
return;
}
iob_reset( &h->batch );
memmove( outbuf, buffer + written_size, size - written_size );
iob_addbuf_free( &h->batch, outbuf, size - written_size );
+ h->flag |= STRUCT_HTTP_FLAG_IOB_USED;
/* writeable sockets timeout after twice the pool timeout
which defaults to 5 minutes (e.g. after 10 minutes) */
******************************/
case 4: /* sync ? */
if( byte_diff( data, 4, "sync") ) HTTPERROR_404;
- if( !h->blessed ) HTTPERROR_403_IP;
+ if( NOTBLESSED( h ) ) HTTPERROR_403_IP;
LOG_TO_STDERR( "sync: %d.%d.%d.%d\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] );
ssize_t l;
if( ( l = io_tryread( clientsocket, static_inbuf, sizeof static_inbuf ) ) <= 0 ) {
- if( h ) {
+ if( h && ( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) ) {
array_reset( &h->request );
free( h );
}
if( !array_start( &h->request ) ) {
if( memchr( static_inbuf, '\n', l ) )
return httpresponse( clientsocket, static_inbuf );
+ h->flag |= STRUCT_HTTP_FLAG_ARRAY_USED;
return array_catb( &h->request, static_inbuf, l );
}
+ h->flag |= STRUCT_HTTP_FLAG_ARRAY_USED;
array_catb( &h->request, static_inbuf, l );
if( array_failed( &h->request ) )
return httperror( clientsocket, "500 Server Error", "Request too long.");
- if( ( !h->blessed ) && ( array_bytes( &h->request ) > 8192 ) )
- return httperror( clientsocket, "500 request too long", "You sent too much headers");
+ if( ( array_bytes( &h->request ) > 8192 ) && NOTBLESSED( h ) )
+ return httperror( clientsocket, "500 request too long", "You sent too much headers");
if( memchr( array_start( &h->request ), '\n', array_length( &h->request, 1 ) ) )
return httpresponse( clientsocket, array_start( &h->request ) );
byte_zero( h, sizeof( struct http_data ) );
memmove( h->ip, ip, sizeof( ip ) );
- if( !byte_diff( &h->ip, 4, g_adminip ) )
- h->blessed = 1;
-
io_setcookie( i, h );
++ot_overall_tcp_connections;
while( ( i = io_timeouted() ) != -1 ) {
struct http_data* h=io_getcookie( i );
if( h ) {
- iob_reset( &h->batch );
- array_reset( &h->request );
+ if( h->flag & STRUCT_HTTP_FLAG_IOB_USED )
+ iob_reset( &h->batch );
+ if( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED )
+ array_reset( &h->request );
free( h );
}
io_close(i);