From 3d92d0665f92d77b47ee355f4331895830cc5030 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 13 Nov 2017 23:36:17 +0800 Subject: [PATCH] Fix bug: Incorrect use the function uh_con_reuse() Signed-off-by: Jianhui Zhao --- example/helloworld.c | 2 ++ src/uhttp.c | 42 +++++------------------------------------- src/uhttp_internal.h | 1 + 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/example/helloworld.c b/example/helloworld.c index 3d65237..0347049 100755 --- a/example/helloworld.c +++ b/example/helloworld.c @@ -11,6 +11,7 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents) void route_test(struct uh_connection *con) { struct uh_value *url = uh_get_url(con); + struct uh_value *path = uh_get_url(con); struct uh_value *header_host = uh_get_header(con, "Host"); struct uh_value *header_ua = uh_get_header(con, "User-Agent"); @@ -18,6 +19,7 @@ void route_test(struct uh_connection *con) uh_printf_chunk(con, "

Hello World

"); uh_printf_chunk(con, "

Libuhttp v%s

", uh_version()); uh_printf_chunk(con, "

Url: %.*s

", (int)url->len, url->at); + uh_printf_chunk(con, "

Path: %.*s

", (int)path->len, path->at); if (header_host) uh_printf_chunk(con, "

Host: %.*s

", (int)header_host->len, header_host->at); diff --git a/src/uhttp.c b/src/uhttp.c index f8f3066..bf0e2f7 100755 --- a/src/uhttp.c +++ b/src/uhttp.c @@ -249,10 +249,10 @@ static int on_message_complete(http_parser *parser) #endif list_for_each_entry(r, &con->srv->routes, list) { - if (uh_value_cmp(&con->req.url, r->path)) { + if (uh_value_cmp(&con->req.path, r->path)) { r->cb(con); if (!(con->flags & UH_CON_CLOSE)) - uh_con_reuse(con); + con->flags |= UH_CON_REUSE; return 0; } } @@ -353,6 +353,9 @@ static void connection_write_cb(struct ev_loop *loop, ev_io *w, int revents) if (!http_should_keep_alive(&con->parser)) con->flags |= UH_CON_CLOSE; + + if (con->flags & UH_CON_REUSE) + uh_con_reuse(con); } if (con->flags & UH_CON_CLOSE) @@ -642,41 +645,6 @@ inline struct uh_value *uh_get_query(struct uh_connection *con) return &con->req.query; } -static inline char c2hex(char c) -{ - return c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10; /* accept small letters just in case */ -} - -static char *uh_unescape(char *str) -{ - char *p = str; - char *q = str; - - if (!str) - return (""); - - while (*p) { - if (*p == '%') { - p++; - if (*p) - *q = c2hex(*p++) * 16; - if (*p) - *q = (*q + c2hex(*p++)); - q++; - } else { - if (*p == '+') { - *q++ = ' '; - p++; - } else { - *q++ = *p++; - } - } - } - - *q++ = 0; - return str; -} - struct uh_value *uh_get_header(struct uh_connection *con, const char *name) { int i; diff --git a/src/uhttp_internal.h b/src/uhttp_internal.h index 0b7a1ae..4628606 100755 --- a/src/uhttp_internal.h +++ b/src/uhttp_internal.h @@ -16,6 +16,7 @@ #define UH_CON_CLOSE (1 << 0) #define UH_CON_SSL_HANDSHAKE_DONE (1 << 1) /* SSL hanshake has completed */ #define UH_CON_PARSERING (1 << 2) /* Whether executed http_parser_execute() */ +#define UH_CON_REUSE (1 << 3) #define likely(x) (__builtin_expect(!!(x), 1)) #define unlikely(x) (__builtin_expect(!!(x), 0))