From 900d86eb6c266c470919c53133ea169bd502a222 Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 16:00:28 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=AD=A3HTTP=20Server?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/haywire/connection_consumer.c | 1 + srcs/httpserver/src/haywire/http_response.c | 6 ++-- srcs/httpserver/src/haywire/http_server.c | 30 ++++++++++--------- srcs/httpserver/src/haywire/http_server.h | 3 +- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/srcs/httpserver/src/haywire/connection_consumer.c b/srcs/httpserver/src/haywire/connection_consumer.c index 2cdcfa1..bdd9bd1 100644 --- a/srcs/httpserver/src/haywire/connection_consumer.c +++ b/srcs/httpserver/src/haywire/connection_consumer.c @@ -7,6 +7,7 @@ #include "http_response_cache.h" static bool tcp_nodelay; +extern uv_read_cb http_stream_on_read; void ipc_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { int rc; diff --git a/srcs/httpserver/src/haywire/http_response.c b/srcs/httpserver/src/haywire/http_response.c index 9310abb..08d1627 100644 --- a/srcs/httpserver/src/haywire/http_response.c +++ b/srcs/httpserver/src/haywire/http_response.c @@ -7,6 +7,8 @@ #include "http_response_cache.h" #include "hw_string.h" +extern http_server_write_response_cb http_server_write_response; + #define CRLF "\r\n" KHASH_MAP_INIT_STR(string_hashmap, char *) @@ -78,8 +80,8 @@ hw_string *create_response_buffer(hw_http_response *response) { int header_buffer_incr = 512; int body_size = resp->body.length; int header_size_remaining = header_buffer_incr; - int response_size = header_size_remaining + sizeof(length_header) + num_chars(resp->body.length) + 2 * line_sep_size - + body_size + line_sep_size; + int response_size = header_size_remaining + sizeof(length_header) + num_chars(resp->body.length) + + 2 * line_sep_size + body_size + line_sep_size; response_string->value = malloc(response_size); diff --git a/srcs/httpserver/src/haywire/http_server.c b/srcs/httpserver/src/haywire/http_server.c index fbf0c3a..495f5d0 100644 --- a/srcs/httpserver/src/haywire/http_server.c +++ b/srcs/httpserver/src/haywire/http_server.c @@ -1,6 +1,6 @@ #ifdef PLATFORM_POSIX #include -#endif// PLATFORM_POSIX +#endif // PLATFORM_POSIX #include #include @@ -39,15 +39,17 @@ static uv_tcp_t server; static http_parser_settings parser_settings; static struct sockaddr_in listen_address; -uv_loop_t *uv_loop; -void *routes; -hw_string *http_v1_0; -hw_string *http_v1_1; -hw_string *server_name; -int listener_count; -uv_async_t *listener_async_handles; -uv_loop_t *listener_event_loops; -uv_barrier_t *listeners_created_barrier; +uv_loop_t *uv_loop; +void *routes; +hw_string *http_v1_0; +hw_string *http_v1_1; +hw_string *server_name; +int listener_count; +uv_async_t *listener_async_handles; +uv_loop_t *listener_event_loops; +uv_barrier_t *listeners_created_barrier; +uv_read_cb http_stream_on_read; +http_server_write_response_cb http_server_write_response; int hw_init_with_config(configuration *c) { #ifdef DEBUG @@ -132,7 +134,7 @@ void hw_http_add_route(char *route, http_request_callback callback, void *user_d routes = kh_init(string_hashmap); } set_route(routes, route, route_entry); - dzlog_debug("Added route path: [%s]\n", route);// TODO: Replace with logging instead. + dzlog_debug("Added route path: [%s]\n", route); // TODO: Replace with logging instead. } void free_http_server() { @@ -145,13 +147,13 @@ void free_http_server() { free((char *)v); }); kh_destroy(string_hashmap, routes); - uv_close((uv_handle_t*)&server, NULL); + uv_close((uv_handle_t *)&server, NULL); uninit_http_request_cache(); dzlog_debug("HTTP Server Close http://%s:%d\n", config->http_listen_address, config->http_listen_port); } int hw_http_open() { - int threads = (int)config->thread_count; + int threads = (int)config->thread_count; static uv_async_t service_handle; if (routes == NULL) { @@ -168,7 +170,7 @@ int hw_http_open() { #ifdef UNIX signal(SIGPIPE, SIG_IGN); -#endif// UNIX +#endif // UNIX listener_count = threads; diff --git a/srcs/httpserver/src/haywire/http_server.h b/srcs/httpserver/src/haywire/http_server.h index ae41451..217ca4d 100644 --- a/srcs/httpserver/src/haywire/http_server.h +++ b/srcs/httpserver/src/haywire/http_server.h @@ -25,8 +25,7 @@ extern uv_async_t *listener_async_handles; extern uv_loop_t *listener_event_loops; extern uv_barrier_t *listeners_created_barrier; -void (*http_stream_on_read)(uv_stream_t *, ssize_t, const uv_buf_t *); -int (*http_server_write_response)(hw_write_context *, hw_string *); +typedef int (*http_server_write_response_cb)(hw_write_context *, hw_string *); http_connection *create_http_connection(); void http_stream_on_connect(uv_stream_t *stream, int status);