struct http_request request;
struct s2n_connection *s2n;
+
+ uint64_t blind_time;
}
_clients[ MAX_CLIENTS ];
int _ticks_without_action = 0;
if( s2n_error_get_type( s2n_errno ) != S2N_ERR_T_BLOCKED )
{
write_s2n_error( stderr );
- s2n_connection_free( c->s2n );
- close( c->connfd );
- c->state = k_state_none;
+ c->blind_time = s2n_connection_get_delay( c->s2n );
+ c->state = k_state_closing;
+
+ printf( "Blinding for %lu\n", c->blind_time );
}
else verifying ++;
}
}
/* returns number of shutting down */
-int _process_shutdowns(void)
+int _process_shutdowns( uint64_t delta_ns )
{
int closing = 0;
for( int i=0; i<MAX_CLIENTS; i ++ )
struct client *c = &_clients[ i ];
if( c->state == k_state_closing )
{
+ if( c->blind_time )
+ {
+ if( delta_ns < c->blind_time )
+ {
+ c->blind_time -= delta_ns;
+ closing ++;
+ continue;
+ }
+ else
+ {
+ c->blind_time = 0;
+ }
+ }
+
s2n_blocked_status blocked;
if( s2n_shutdown( c->s2n, &blocked ) == 0 )
{
return closing;
}
-int s2nsend_busy( struct s2n_connection *conn, const void *buf, int len )
+int s2nsend_busy( struct client *client, const void *buf, int len )
{
int written = 0;
while( written < len )
{
s2n_blocked_status blocked;
- int w = s2n_send( conn, buf + written, len - written, &blocked);
+ int w = s2n_send( client->s2n, buf + written, len - written, &blocked);
if( w >= 0 )
{
written += w;
}
else
{
+ client->blind_time = s2n_connection_get_delay( client->s2n );
+ client->state = k_state_closing;
write_s2n_error( stderr );
+ printf( "Blinding for %lu\n", client->blind_time );
return -1;
}
ehttp_parse_state[ client->request.state ],
client->request.line, client->request.col );
- if( s2nsend_busy( client->s2n, k_response_parsefail,
- strlen( k_response_parsefail )) == -1 )
- {
- s2n_connection_free( client->s2n );
- close( client->connfd );
- client->state = k_state_none;
- }
- else
- {
- client->state = k_state_closing;
- }
+ s2nsend_busy( client, k_response_parsefail,
+ strlen( k_response_parsefail ));
+ client->state = k_state_closing;
+ memset( &client->request, 0, sizeof(struct http_request) );
return -1;
}
else
client->request.resource_len ) )
{
printf( "Gave website :D\n" );
- if( s2nsend_busy( client->s2n, WEBSITE, strlen(WEBSITE) )
- == -1 )
- {
- s2n_connection_free( client->s2n );
- close( client->connfd );
- client->state = k_state_none;
- return -1;
- }
+ s2nsend_busy( client, WEBSITE, strlen(WEBSITE) );
}
else
{
fprintf( stderr, "Responding #%d with 501\n", client->connfd );
- if( s2nsend_busy( client->s2n,
- k_response_temp, strlen(k_response_temp) )
- == -1 )
- {
- s2n_connection_free( client->s2n );
- close( client->connfd );
- client->state = k_state_none;
- return -1;
- }
+ s2nsend_busy( client, k_response_temp,
+ strlen(k_response_temp) );
}
- printf( "Setting state to closing!\n" );
- /* reset parser */
- memset( &client->request, 0, sizeof(struct http_request) );
+ printf( "Closing connection\n" );
client->state = k_state_closing;
+ memset( &client->request, 0, sizeof(struct http_request) );
}
}
}
int _process_requests(void)
{
- int closing = 0;
for( int i=0; i<MAX_CLIENTS; i ++ )
{
struct client *c = &_clients[ i ];
{
_accept_any_new_client( listenfd, s2n_conf );
_process_handshakes();
- _process_shutdowns();
+ _process_shutdowns( 5000000 );
_process_requests();
struct timespec spec;
printf( "Closing active connections\n" );
while(1)
{
- if( _process_shutdowns() )
+ if( _process_shutdowns( 100000000 ) )
{
struct timespec spec;
spec.tv_sec = 0;