From d66de88dfaa4bf77ebd87887ddf950a11cd88abf Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Sat, 25 Nov 2017 23:31:50 +0800 Subject: [PATCH] Optimize code Signed-off-by: Jianhui Zhao --- src/log.c | 6 ++---- src/uhttp.c | 31 +++++++++++-------------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/log.c b/src/log.c index 33a5509..70cbae5 100755 --- a/src/log.c +++ b/src/log.c @@ -18,13 +18,11 @@ void __uh_log(const char *filename, int line, int priority, const char *format, syslog(priority, "%s", buf); - - #if (UHTTP_DEBUG) - fprintf(stderr, "%s\n", buf); + fprintf(stderr, "%s", buf); #else if (priority == LOG_ERR) - fprintf(stderr, "%s\n", buf); + fprintf(stderr, "%s", buf); #endif } diff --git a/src/uhttp.c b/src/uhttp.c index 351f3ff..9763c62 100755 --- a/src/uhttp.c +++ b/src/uhttp.c @@ -72,7 +72,9 @@ static int on_url(http_parser *parser, const char *at, size_t len) { struct uh_connection *con = container_of(parser, struct uh_connection, parser); struct http_parser_url url; - + + uh_log_debug("Url:[%.*s]\n", (int)len, at); + con->req.url.at = at; con->req.url.len = len; @@ -103,6 +105,8 @@ static int on_header_field(http_parser *parser, const char *at, size_t len) struct uh_connection *con = container_of(parser, struct uh_connection, parser); struct uh_header *header = con->req.header; + uh_log_debug("header field:[%.*s]\n", (int)len, at); + header[con->req.header_num].field.at = at; header[con->req.header_num].field.len = len; @@ -113,7 +117,9 @@ static int on_header_value(http_parser *parser, const char *at, size_t len) { struct uh_connection *con = container_of(parser, struct uh_connection, parser); struct uh_header *header = con->req.header; - + + uh_log_debug("header value:[%.*s]\n", (int)len, at); + header[con->req.header_num].value.at = at; header[con->req.header_num].value.len = len; con->req.header_num += 1; @@ -136,7 +142,9 @@ static int on_headers_complete(http_parser *parser) static int on_body(http_parser *parser, const char *at, size_t len) { struct uh_connection *con = container_of(parser, struct uh_connection, parser); - + + uh_log_debug("body:[%.*s]\n", (int)len, at); + if (!con->req.body.at) con->req.body.at = at; @@ -164,21 +172,6 @@ static int on_message_complete(http_parser *parser) { struct uh_connection *con = container_of(parser, struct uh_connection, parser); struct uh_route *r; -#if (UHTTP_DEBUG) - int i; - struct uh_header *header = con->req.header; - - uh_log_debug("Url:[%.*s]\n", (int)con->req.url.len, con->req.url.at); - uh_log_debug("Path:[%.*s]\n", (int)con->req.path.len, con->req.path.at); - uh_log_debug("Query:[%.*s]\n", (int)con->req.query.len, con->req.query.at); - - for (i = 0; i < con->req.header_num; i++) { - uh_log_debug("[%.*s:%.*s]\n", (int)header[i].field.len, header[i].field.at, - (int)header[i].value.len, header[i].value.at); - } - - uh_log_debug("Body:[%.*s]\n", (int)con->req.body.len, con->req.body.at); -#endif list_for_each_entry(r, &con->srv->routes, list) { if (uh_value_cmp(&con->req.path, r->path)) { @@ -237,9 +230,7 @@ handshake_done: buf->len += len; -#if (UHTTP_DEBUG) uh_log_debug("read:[%.*s]\n", len, base); -#endif if (!(con->flags & UH_CON_PARSERING)) { if (!memmem(buf->base, buf->len, "\r\n\r\n", 4)) {