From 33e2f8e27234bc88d0ee9ede30d9f00407f1900a Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Wed, 10 Jan 2018 23:30:24 +0800 Subject: [PATCH] Optimize code Signed-off-by: Jianhui Zhao --- README.md | 52 +++++++++++++++++++++++++++++++++++--------- README_ZH.md | 52 +++++++++++++++++++++++++++++++++++--------- example/helloworld.c | 50 +++++++++++++++++++++++++++++++++--------- src/CMakeLists.txt | 8 ------- src/client.c | 7 ------ src/config.h.in | 2 -- src/file.c | 6 ----- src/log.c | 15 ++++--------- src/log.h | 18 ++++++--------- 9 files changed, 135 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index a9d4c5b..8b185ec 100755 --- a/README.md +++ b/README.md @@ -74,13 +74,10 @@ Select package libuhttpd in menuconfig and compile new image. ``` #include -//#define EXAMPLE_SSL - -#define port 8000 - static void hello_action(struct uh_client *cl) { int body_len = 0; + cl->send_header(cl, 200, "OK", -1); cl->append_header(cl, "Myheader", "Hello"); cl->header_end(cl); @@ -95,9 +92,42 @@ static void hello_action(struct uh_client *cl) cl->request_done(cl); } +static void usage(const char *prog) +{ + fprintf(stderr, "Usage: %s [option]\n" + " -p port # Default port is 8080\n" + " -s # SSl on\n" + " -v # verbose\n", prog); + exit(1); +} + int main(int argc, char **argv) { struct uh_server *srv = NULL; + int verbose = false; + int ssl = false; + int port = 8080; + int opt; + + while ((opt = getopt(argc, argv, "p:vs")) != -1) { + switch (opt) + { + case 'p': + port = atoi(optarg); + break; + case 's': + ssl = true; + break; + case 'v': + verbose = true; + break; + default: /* '?' */ + usage(argv[0]); + } + } + + if (!verbose) + ulog_threshold(LOG_ERR); uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING); @@ -107,14 +137,16 @@ int main(int argc, char **argv) if (!srv) goto done; - uh_log_debug("Listen on: *:%d", port); - -#ifdef EXAMPLE_SSL -#if (UHTTPD_SSL_SUPPORT) - if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) +#if (!UHTTPD_SSL_SUPPORT) + if (ssl) + uh_log_debug("SSl is not compiled in"); +#else + if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) goto done; #endif -#endif + + uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port); + srv->add_action(srv, "/hello", hello_action); uloop_run(); diff --git a/README_ZH.md b/README_ZH.md index 14ad702..6a39bcf 100755 --- a/README_ZH.md +++ b/README_ZH.md @@ -75,13 +75,10 @@ Select package libuhttpd in menuconfig and compile new image. ``` #include -//#define EXAMPLE_SSL - -#define port 8000 - static void hello_action(struct uh_client *cl) { int body_len = 0; + cl->send_header(cl, 200, "OK", -1); cl->append_header(cl, "Myheader", "Hello"); cl->header_end(cl); @@ -96,9 +93,42 @@ static void hello_action(struct uh_client *cl) cl->request_done(cl); } +static void usage(const char *prog) +{ + fprintf(stderr, "Usage: %s [option]\n" + " -p port # Default port is 8080\n" + " -s # SSl on\n" + " -v # verbose\n", prog); + exit(1); +} + int main(int argc, char **argv) { struct uh_server *srv = NULL; + int verbose = false; + int ssl = false; + int port = 8080; + int opt; + + while ((opt = getopt(argc, argv, "p:vs")) != -1) { + switch (opt) + { + case 'p': + port = atoi(optarg); + break; + case 's': + ssl = true; + break; + case 'v': + verbose = true; + break; + default: /* '?' */ + usage(argv[0]); + } + } + + if (!verbose) + ulog_threshold(LOG_ERR); uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING); @@ -108,14 +138,16 @@ int main(int argc, char **argv) if (!srv) goto done; - uh_log_debug("Listen on: *:%d", port); - -#ifdef EXAMPLE_SSL -#if (UHTTPD_SSL_SUPPORT) - if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) +#if (!UHTTPD_SSL_SUPPORT) + if (ssl) + uh_log_debug("SSl is not compiled in"); +#else + if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) goto done; #endif -#endif + + uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port); + srv->add_action(srv, "/hello", hello_action); uloop_run(); diff --git a/example/helloworld.c b/example/helloworld.c index 631a9d6..5e6350c 100755 --- a/example/helloworld.c +++ b/example/helloworld.c @@ -17,10 +17,6 @@ #include -//#define EXAMPLE_SSL - -#define port 8000 - static void hello_action(struct uh_client *cl) { int body_len = 0; @@ -39,9 +35,42 @@ static void hello_action(struct uh_client *cl) cl->request_done(cl); } +static void usage(const char *prog) +{ + fprintf(stderr, "Usage: %s [option]\n" + " -p port # Default port is 8080\n" + " -s # SSl on\n" + " -v # verbose\n", prog); + exit(1); +} + int main(int argc, char **argv) { struct uh_server *srv = NULL; + int verbose = false; + int ssl = false; + int port = 8080; + int opt; + + while ((opt = getopt(argc, argv, "p:vs")) != -1) { + switch (opt) + { + case 'p': + port = atoi(optarg); + break; + case 's': + ssl = true; + break; + case 'v': + verbose = true; + break; + default: /* '?' */ + usage(argv[0]); + } + } + + if (!verbose) + ulog_threshold(LOG_ERR); uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING); @@ -51,14 +80,15 @@ int main(int argc, char **argv) if (!srv) goto done; - uh_log_debug("Listen on: *:%d", port); - -#ifdef EXAMPLE_SSL -#if (UHTTPD_SSL_SUPPORT) - if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) +#if (!UHTTPD_SSL_SUPPORT) + if (ssl) + uh_log_debug("SSl is not compiled in"); +#else + if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) goto done; #endif -#endif + + uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port); srv->add_action(srv, "/hello", hello_action); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 95f5e69..062e7ef 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,16 +14,9 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${LIBUBOX_INCLUDE_DIR}) set(EXTRA_LIBS ${LIBUBOX_LIBRARY} dl) set(SOURCE_FILES uhttpd.c client.c log.c utils.c file.c action.c) -set(UHTTPD_DEBUG_CONFIG 0) -option(UHTTPD_DEBUG "Turn on debug" OFF) -if(UHTTPD_DEBUG) - set(UHTTPD_DEBUG_CONFIG 1) -endif() - set(UHTTPD_SSL_SUPPORT_CONFIG 1) option(UHTTPD_SSL_SUPPORT "SSL support" ON) - if(UHTTPD_SSL_SUPPORT) list(APPEND SOURCE_FILES uh_ssl.c) else() @@ -57,5 +50,4 @@ install( message("") message(STATUS "UHTTPD_VERSION: ${UHTTPD_VERSION_MAJOR}.${UHTTPD_VERSION_MINOR}.${UHTTPD_VERSION_PATCH}") message(STATUS "UHTTPD_SSL_SUPPORT: ${UHTTPD_SSL_SUPPORT}") -message(STATUS "UHTTPD_DEBUG: ${UHTTPD_DEBUG}") message("") diff --git a/src/client.c b/src/client.c index 549201c..4594009 100755 --- a/src/client.c +++ b/src/client.c @@ -213,8 +213,6 @@ static void client_free(struct uh_client *cl) kvlist_free(&cl->request.var); kvlist_free(&cl->request.header); cl->srv->nclients--; - - uh_log_debug("client_free: %s:%d", inet_ntoa(cl->peer_addr.sin_addr), cl->peer_addr.sin_port); free(cl); } } @@ -280,11 +278,6 @@ static int client_parse_request(struct uh_client *cl, char *data) if (req->version < UH_HTTP_VER_1_1) cl->connection_close = true; - uh_log_debug("http path: %s", kvlist_get(&req->url, "path")); - uh_log_debug("http query: %s", kvlist_get(&req->url, "query")); - uh_log_debug("http method: %s", http_methods[h_method]); - uh_log_debug("http version: %s", http_versions[h_version]); - return CLIENT_STATE_HEADER; } diff --git a/src/config.h.in b/src/config.h.in index bef2081..338af65 100755 --- a/src/config.h.in +++ b/src/config.h.in @@ -23,8 +23,6 @@ #define UHTTPD_VERSION_PATCH @UHTTPD_VERSION_PATCH@ #define UHTTPD_VERSION_STRING "@UHTTPD_VERSION_MAJOR@.@UHTTPD_VERSION_MINOR@.@UHTTPD_VERSION_PATCH@" -#define UHTTPD_DEBUG @UHTTPD_DEBUG_CONFIG@ - #define UHTTPD_SSL_SUPPORT @UHTTPD_SSL_SUPPORT_CONFIG@ #endif diff --git a/src/file.c b/src/file.c index a71a9e0..c124975 100755 --- a/src/file.c +++ b/src/file.c @@ -398,12 +398,6 @@ bool handle_file_request(struct uh_client *cl, const char *path) if (!pi) return false; - uh_log_debug("pi->root: %s", pi->root); - uh_log_debug("pi->phys: %s", pi->phys); - uh_log_debug("pi->name: %s", pi->name); - uh_log_debug("pi->info: %s", pi->info); - uh_log_debug("pi->redirected: %d", pi->redirected); - if (pi->redirected) return true; diff --git a/src/log.c b/src/log.c index c6984fe..551388d 100755 --- a/src/log.c +++ b/src/log.c @@ -17,29 +17,22 @@ #include "log.h" -void __uh_log(const char *filename, int line, int priority, const char *format, ...) +void __uh_log(const char *filename, int line, int priority, const char *fmt, ...) { va_list ap; static char buf[128]; snprintf(buf, sizeof(buf), "(%s:%d) ", filename, line); - va_start(ap, format); - vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), format, ap); + va_start(ap, fmt); + vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, ap); va_end(ap); if (priority == LOG_ERR && errno > 0) { snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ":%s", strerror(errno)); errno = 0; } - - syslog(priority, "%s", buf); -#if (UHTTPD_DEBUG) - fprintf(stderr, "%s\n", buf); -#else - if (priority == LOG_ERR) - fprintf(stderr, "%s\n", buf); -#endif + ulog(priority, "%s\n", buf); } diff --git a/src/log.h b/src/log.h index 4e08f68..dcb1c82 100755 --- a/src/log.h +++ b/src/log.h @@ -18,25 +18,21 @@ #ifndef _LOG_H #define _LOG_H -#include #include "common.h" +#include + #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) /* * Use the syslog output log and include the name and number of rows at the call */ -#define uh_log(priority, format...) __uh_log(__FILENAME__, __LINE__, priority, format) +#define uh_log(priority, fmt...) __uh_log(__FILENAME__, __LINE__, priority, fmt) -#if (UHTTPD_DEBUG) -#define uh_log_debug(format...) uh_log(LOG_DEBUG, format) -#else -#define uh_log_debug(format...) -#endif +#define uh_log_debug(fmt...) uh_log(LOG_DEBUG, fmt) +#define uh_log_info(fmt...) uh_log(LOG_INFO, fmt) +#define uh_log_err(fmt...) uh_log(LOG_ERR, fmt) -#define uh_log_info(format...) uh_log(LOG_INFO, format) -#define uh_log_err(format...) uh_log(LOG_ERR, format) - -void __uh_log(const char *filename, int line, int priority, const char *format, ...); +void __uh_log(const char *filename, int line, int priority, const char *fmt, ...); #endif