@ -54,7 +54,8 @@ sandbox__parse_http_request(struct sandbox *sandbox, size_t length)
{
// Why is our start address sandbox->request_response_data + sandbox->request_response_data_length?
// it's like a cursor to keep track of what we've read so far
http_parser_execute ( & sandbox - > http_parser , & runtime__http_parser_settings , sandbox - > request_response_data + sandbox - > request_response_data_length , length ) ;
http_parser_execute ( & sandbox - > http_parser , & runtime__http_parser_settings ,
sandbox - > request_response_data + sandbox - > request_response_data_length , length ) ;
return 0 ;
}
@ -81,7 +82,8 @@ current_sandbox__receive_and_parse_client_request(void)
struct http_request * rh = & curr - > http_request ;
if ( rh - > message_end ) break ;
r = recv ( curr - > client_socket_descriptor , ( curr - > request_response_data + curr - > request_response_data_length ) ,
r = recv ( curr - > client_socket_descriptor ,
( curr - > request_response_data + curr - > request_response_data_length ) ,
curr - > module - > max_request_size - curr - > request_response_data_length , 0 ) ;
if ( r < 0 ) {
perror ( " recv2 " ) ;
@ -89,7 +91,9 @@ current_sandbox__receive_and_parse_client_request(void)
}
}
# else
int r = uv_read_start ( ( uv_stream_t * ) & curr - > client_libuv_stream , libuv_callbacks__on_allocate_setup_request_response_data , libuv_callbacks__on_read_parse_http_request ) ;
int r = uv_read_start ( ( uv_stream_t * ) & curr - > client_libuv_stream ,
libuv_callbacks__on_allocate_setup_request_response_data ,
libuv_callbacks__on_read_parse_http_request ) ;
worker_thread__process_io ( ) ;
if ( curr - > request_response_data_length = = 0 ) return 0 ;
# endif
@ -119,8 +123,8 @@ current_sandbox__build_and_send_client_response(void)
strncpy ( curr - > request_response_data + sndsz + strlen ( " Content-type: " ) , HTTP_RESP_CONTTYPE_PLAIN ,
strlen ( HTTP_RESP_CONTTYPE_PLAIN ) ) ;
} else {
strncpy ( curr - > request_response_data + sndsz + strlen ( " Content-type: " ) , curr - > module - > response_content_type ,
strlen ( curr - > module - > response_content_type ) ) ;
strncpy ( curr - > request_response_data + sndsz + strlen ( " Content-type: " ) ,
curr - > module - > response_content_type , strlen ( curr - > module - > response_content_type ) ) ;
}
sndsz + = strlen ( HTTP_RESP_CONTTYPE ) ;
char len [ 10 ] = { 0 } ;
@ -155,7 +159,8 @@ done:
. data = curr ,
} ;
uv_buf_t bufv = uv_buf_init ( curr - > request_response_data , sndsz ) ;
int r = uv_write ( & req , ( uv_stream_t * ) & curr - > client_libuv_stream , & bufv , 1 , libuv_callbacks__on_write_wakeup_sandbox ) ;
int r = uv_write ( & req , ( uv_stream_t * ) & curr - > client_libuv_stream , & bufv , 1 ,
libuv_callbacks__on_write_wakeup_sandbox ) ;
worker_thread__process_io ( ) ;
# endif
return 0 ;
@ -163,7 +168,8 @@ done:
/**
* Sandbox execution logic
* Handles setup , request parsing , WebAssembly initialization , function execution , response building and sending , and cleanup
* Handles setup , request parsing , WebAssembly initialization , function execution , response building and sending , and
* cleanup
* */
void
current_sandbox__main ( void )
@ -216,7 +222,6 @@ current_sandbox__main(void)
// If the HTTP Request returns 1, we've successfully received and parsed the HTTP request, so execute it!
if ( current_sandbox__receive_and_parse_client_request ( ) > 0 ) {
//
current_sandbox - > request_response_data_length = response_header_length ;
@ -229,7 +234,8 @@ current_sandbox__main(void)
current_sandbox__setup_arguments ( argument_count ) ;
// Executing the function within the WebAssembly sandbox
current_sandbox - > return_value = module__main ( current_module , argument_count , current_sandbox - > arguments_offset ) ;
current_sandbox - > return_value = module__main ( current_module , argument_count ,
current_sandbox - > arguments_offset ) ;
// Retrieve the result from the WebAssembly sandbox, construct the HTTP response, and send to client
current_sandbox__build_and_send_client_response ( ) ;
@ -287,7 +293,8 @@ sandbox__allocate_memory(struct module *module)
}
struct sandbox *
sandbox__allocate ( struct module * module , char * arguments , int socket_descriptor , const struct sockaddr * socket_address , u64 start_time )
sandbox__allocate ( struct module * module , char * arguments , int socket_descriptor , const struct sockaddr * socket_address ,
u64 start_time )
{
if ( ! module__is_valid ( module ) ) return NULL ;
@ -314,7 +321,8 @@ sandbox__allocate(struct module *module, char *arguments, int socket_descriptor,
ps_list_init_d ( sandbox ) ;
// Setup the sandbox's context, stack, and instruction pointer
arch_context_init ( & sandbox - > ctxt , ( reg_t ) current_sandbox__main , ( reg_t ) ( sandbox - > stack_start + sandbox - > stack_size ) ) ;
arch_context_init ( & sandbox - > ctxt , ( reg_t ) current_sandbox__main ,
( reg_t ) ( sandbox - > stack_start + sandbox - > stack_size ) ) ;
return sandbox ;
}
@ -340,7 +348,8 @@ sandbox__free(struct sandbox *sandbox)
size_t stksz = sandbox - > stack_size ;
// depending on the memory type
// free_linear_memory(sandbox->linear_memory_start, sandbox->linear_memory_size, sandbox->linear_memory_max_size);
// free_linear_memory(sandbox->linear_memory_start, sandbox->linear_memory_size,
// sandbox->linear_memory_max_size);
int ret ;
// mmaped memory includes sandbox structure in there.