bool us_http_server_handler_init(us_reactor_handler_t *self_, us_allocator_t *allocator, int fd, void *extra, int32_t max_response_buffer_size, us_webapp_director_t *director) { bool r = false; us_http_server_handler_t *self = (us_http_server_handler_t *)self_; us_log_tracepoint(); r = us_reactor_handler_tcp_init(&self->m_base.m_base, allocator, fd, extra, max_response_buffer_size); self->m_base.m_base.destroy = us_http_server_handler_destroy; if (r) { int32_t local_allocator_size = US_HTTP_SERVER_HANDLER_LOCAL_BUFFER_SIZE + max_response_buffer_size; self->m_local_allocator_buffer = us_new_array(allocator, char, local_allocator_size); self->m_request_header = 0; self->m_response_header = 0; if (self->m_local_allocator_buffer) { us_simple_allocator_init(&self->m_local_allocator, self->m_local_allocator_buffer, local_allocator_size); self->m_request_header = us_http_request_header_create(&self->m_local_allocator.m_base); self->m_response_header = us_http_response_header_create(&self->m_local_allocator.m_base); self->m_response_content = us_buffer_create(&self->m_local_allocator.m_base, max_response_buffer_size); self->m_director = director; if (self->m_request_header && self->m_response_header && self->m_response_content) { us_http_server_handler_set_state_waiting_for_connection(self); r = true; } } } if (!r) { us_log_error("Unable to init http server handler"); self->m_base.m_base.destroy(&self->m_base.m_base); } return r; }
bool us_testutil_start( int32_t US_UNUSED( sys_allocator_size ), int32_t session_allocator_size, int argc, const char **argv ) { bool r = true; (void)argc; (void)argv; #if US_ENABLE_NETWORK r = us_platform_init_sockets(); if ( !r ) { return r; } #endif /* Initialize the system and the session allocators */ us_testutil_sys_allocator = 0; us_testutil_session_allocator = 0; #if US_ENABLE_MALLOC /* With malloc if we have it */ us_testutil_sys_allocator = us_malloc_allocator_init( &us_testutil_sys_allocator_impl ); #else (void)sys_allocator_size; /* or with statically allocated bss segment data if we don't have malloc */ us_testutil_sys_allocator = us_simple_allocator_init( &us_testutil_sys_allocator_impl, &us_testutil_session_sys_buffer, ( int32_t )( US_TESTUTIL_BUFFER_SIZE_IN_WORDS * sizeof( int32_t ) ) ); #endif (void)session_allocator_size; us_testutil_session_allocator = us_simple_allocator_init( &us_testutil_session_allocator_impl, &us_testutil_session_sys_buffer, ( int32_t )( US_TESTUTIL_BUFFER_SIZE_IN_WORDS * sizeof( int32_t ) ) ); /* check for allocation failure */ if ( !us_testutil_sys_allocator || !us_testutil_session_allocator ) { r = false; } /* Initialize any printing mechanism if enabled */ #if US_ENABLE_PRINTING #if US_ENABLE_STDIO /* Using stdio.h FILE stdout if available */ us_testutil_printer_stdout = us_print_file_init( &us_testutil_printer_stdout_impl, stdout ); us_testutil_printer_stderr = us_print_file_init( &us_testutil_printer_stderr_impl, stderr ); #else /* Using statically allocated character buffer if not */ us_testutil_printer_stdout = us_printraw_init( &us_testutil_printer_stdout_impl, us_testutil_printbuffer_stdout, US_TESTUTIL_PRINTBUFFER_SIZE ); us_testutil_printer_stderr = us_printraw_init( &us_testutil_printer_stderr_impl, us_testutil_printbuffer_stderr, US_TESTUTIL_PRINTBUFFER_SIZE ); #endif /* report any failure of the printer */ if ( !us_testutil_printer_stdout || !us_testutil_printer_stderr ) { r = false; } us_stdout = us_testutil_printer_stdout; us_stderr = us_testutil_printer_stderr; #endif return r; }