From bd8ec5f31dc3de61e433ff645d993b8dfb5e6a7b Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 13 Nov 2017 21:20:42 +0800 Subject: [PATCH] Optimize code Signed-off-by: Jianhui Zhao --- src/uhttp.c | 15 ++++++++++----- src/uhttp_internal.h | 12 ++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/uhttp.c b/src/uhttp.c index 0fc14c6..8c6dc49 100755 --- a/src/uhttp.c +++ b/src/uhttp.c @@ -275,7 +275,7 @@ handshake_done: if (!(con->flags & UH_CON_PARSERING)) { if (!memmem(buf->base, buf->len, "\r\n\r\n", 4)) { - if (buf->len > UH_MAX_HTTP_HEAD_SIZE) { + if (buf->len > UH_HEAD_SIZE_LIMIT) { uh_log_err("HTTP head size too big"); uh_send_error(con, UH_STATUS_BAD_REQUEST, NULL); } @@ -288,12 +288,17 @@ handshake_done: } parsered = http_parser_execute(&con->parser, &parser_settings, base, len); - if (unlikely(parsered != len && !(con->flags & UH_CON_CLOSE))) { - uh_log_err("http parser failed:%s", http_errno_description(HTTP_PARSER_ERRNO(&con->parser))); + + if (unlikely(con->flags & UH_CON_CLOSE)) + return; + + if (unlikely(parsered != len)) { + uh_log_err("http_parser_execute() failed:%s", http_errno_description(HTTP_PARSER_ERRNO(&con->parser))); uh_send_error(con, UH_STATUS_BAD_REQUEST, NULL); - } else { - ev_timer_mode(loop, &con->timer_watcher, UH_CONNECTION_TIMEOUT, 0); + return; } + + ev_timer_mode(loop, &con->timer_watcher, UH_CONNECTION_TIMEOUT, 0); } static void connection_write_cb(struct ev_loop *loop, ev_io *w, int revents) diff --git a/src/uhttp_internal.h b/src/uhttp_internal.h index 748b042..3498866 100755 --- a/src/uhttp_internal.h +++ b/src/uhttp_internal.h @@ -6,11 +6,11 @@ #include "list.h" #include "uhttp.h" -#define UH_BUFFER_SIZE 2048 -#define UH_CONNECTION_TIMEOUT 30 -#define UH_MAX_HTTP_HEAD_SIZE 1024 -#define UH_MAX_HTTP_BODY_SIZE (2 * 1024 * 1024) -#define UH_MAX_HTTP_HEADERS 20 +#define UH_BUFFER_SIZE 2048 +#define UH_CONNECTION_TIMEOUT 30 +#define UH_HEAD_SIZE_LIMIT 1024 +#define UH_BODY_SIZE_LIMIT (2 * 1024 * 1024) +#define UH_HEADER_NUM_LIMIT 20 #define UH_CON_CLOSE (1 << 0) #define UH_CON_SSL_HANDSHAKE_DONE (1 << 1) /* SSL hanshake has completed */ @@ -51,7 +51,7 @@ struct uh_request { struct uh_value url; struct uh_value body; int header_num; - struct uh_header header[UH_MAX_HTTP_HEADERS]; + struct uh_header header[UH_HEADER_NUM_LIMIT]; }; struct uh_connection {