Optimize code

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-01-10 23:30:24 +08:00
parent 769b6d2dbd
commit 33e2f8e272
9 changed files with 135 additions and 75 deletions

View File

@ -74,13 +74,10 @@ Select package libuhttpd in menuconfig and compile new image.
``` ```
#include <uhttpd.h> #include <uhttpd.h>
//#define EXAMPLE_SSL
#define port 8000
static void hello_action(struct uh_client *cl) static void hello_action(struct uh_client *cl)
{ {
int body_len = 0; int body_len = 0;
cl->send_header(cl, 200, "OK", -1); cl->send_header(cl, 200, "OK", -1);
cl->append_header(cl, "Myheader", "Hello"); cl->append_header(cl, "Myheader", "Hello");
cl->header_end(cl); cl->header_end(cl);
@ -95,9 +92,42 @@ static void hello_action(struct uh_client *cl)
cl->request_done(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) int main(int argc, char **argv)
{ {
struct uh_server *srv = NULL; 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); uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
@ -107,14 +137,16 @@ int main(int argc, char **argv)
if (!srv) if (!srv)
goto done; goto done;
uh_log_debug("Listen on: *:%d", port); #if (!UHTTPD_SSL_SUPPORT)
if (ssl)
#ifdef EXAMPLE_SSL uh_log_debug("SSl is not compiled in");
#if (UHTTPD_SSL_SUPPORT) #else
if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
goto done; goto done;
#endif #endif
#endif
uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port);
srv->add_action(srv, "/hello", hello_action); srv->add_action(srv, "/hello", hello_action);
uloop_run(); uloop_run();

View File

@ -75,13 +75,10 @@ Select package libuhttpd in menuconfig and compile new image.
``` ```
#include <uhttpd.h> #include <uhttpd.h>
//#define EXAMPLE_SSL
#define port 8000
static void hello_action(struct uh_client *cl) static void hello_action(struct uh_client *cl)
{ {
int body_len = 0; int body_len = 0;
cl->send_header(cl, 200, "OK", -1); cl->send_header(cl, 200, "OK", -1);
cl->append_header(cl, "Myheader", "Hello"); cl->append_header(cl, "Myheader", "Hello");
cl->header_end(cl); cl->header_end(cl);
@ -96,9 +93,42 @@ static void hello_action(struct uh_client *cl)
cl->request_done(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) int main(int argc, char **argv)
{ {
struct uh_server *srv = NULL; 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); uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
@ -108,14 +138,16 @@ int main(int argc, char **argv)
if (!srv) if (!srv)
goto done; goto done;
uh_log_debug("Listen on: *:%d", port); #if (!UHTTPD_SSL_SUPPORT)
if (ssl)
#ifdef EXAMPLE_SSL uh_log_debug("SSl is not compiled in");
#if (UHTTPD_SSL_SUPPORT) #else
if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
goto done; goto done;
#endif #endif
#endif
uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port);
srv->add_action(srv, "/hello", hello_action); srv->add_action(srv, "/hello", hello_action);
uloop_run(); uloop_run();

View File

@ -17,10 +17,6 @@
#include <uhttpd.h> #include <uhttpd.h>
//#define EXAMPLE_SSL
#define port 8000
static void hello_action(struct uh_client *cl) static void hello_action(struct uh_client *cl)
{ {
int body_len = 0; int body_len = 0;
@ -39,9 +35,42 @@ static void hello_action(struct uh_client *cl)
cl->request_done(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) int main(int argc, char **argv)
{ {
struct uh_server *srv = NULL; 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); uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
@ -51,14 +80,15 @@ int main(int argc, char **argv)
if (!srv) if (!srv)
goto done; goto done;
uh_log_debug("Listen on: *:%d", port); #if (!UHTTPD_SSL_SUPPORT)
if (ssl)
#ifdef EXAMPLE_SSL uh_log_debug("SSl is not compiled in");
#if (UHTTPD_SSL_SUPPORT) #else
if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0) if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
goto done; goto done;
#endif #endif
#endif
uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port);
srv->add_action(srv, "/hello", hello_action); srv->add_action(srv, "/hello", hello_action);

View File

@ -14,16 +14,9 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${LIBUBOX_INCLUDE_DIR})
set(EXTRA_LIBS ${LIBUBOX_LIBRARY} dl) set(EXTRA_LIBS ${LIBUBOX_LIBRARY} dl)
set(SOURCE_FILES uhttpd.c client.c log.c utils.c file.c action.c) 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) set(UHTTPD_SSL_SUPPORT_CONFIG 1)
option(UHTTPD_SSL_SUPPORT "SSL support" ON) option(UHTTPD_SSL_SUPPORT "SSL support" ON)
if(UHTTPD_SSL_SUPPORT) if(UHTTPD_SSL_SUPPORT)
list(APPEND SOURCE_FILES uh_ssl.c) list(APPEND SOURCE_FILES uh_ssl.c)
else() else()
@ -57,5 +50,4 @@ install(
message("") message("")
message(STATUS "UHTTPD_VERSION: ${UHTTPD_VERSION_MAJOR}.${UHTTPD_VERSION_MINOR}.${UHTTPD_VERSION_PATCH}") message(STATUS "UHTTPD_VERSION: ${UHTTPD_VERSION_MAJOR}.${UHTTPD_VERSION_MINOR}.${UHTTPD_VERSION_PATCH}")
message(STATUS "UHTTPD_SSL_SUPPORT: ${UHTTPD_SSL_SUPPORT}") message(STATUS "UHTTPD_SSL_SUPPORT: ${UHTTPD_SSL_SUPPORT}")
message(STATUS "UHTTPD_DEBUG: ${UHTTPD_DEBUG}")
message("") message("")

View File

@ -213,8 +213,6 @@ static void client_free(struct uh_client *cl)
kvlist_free(&cl->request.var); kvlist_free(&cl->request.var);
kvlist_free(&cl->request.header); kvlist_free(&cl->request.header);
cl->srv->nclients--; cl->srv->nclients--;
uh_log_debug("client_free: %s:%d", inet_ntoa(cl->peer_addr.sin_addr), cl->peer_addr.sin_port);
free(cl); 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) if (req->version < UH_HTTP_VER_1_1)
cl->connection_close = true; 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; return CLIENT_STATE_HEADER;
} }

View File

@ -23,8 +23,6 @@
#define UHTTPD_VERSION_PATCH @UHTTPD_VERSION_PATCH@ #define UHTTPD_VERSION_PATCH @UHTTPD_VERSION_PATCH@
#define UHTTPD_VERSION_STRING "@UHTTPD_VERSION_MAJOR@.@UHTTPD_VERSION_MINOR@.@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@ #define UHTTPD_SSL_SUPPORT @UHTTPD_SSL_SUPPORT_CONFIG@
#endif #endif

View File

@ -398,12 +398,6 @@ bool handle_file_request(struct uh_client *cl, const char *path)
if (!pi) if (!pi)
return false; 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) if (pi->redirected)
return true; return true;

View File

@ -17,15 +17,15 @@
#include "log.h" #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; va_list ap;
static char buf[128]; static char buf[128];
snprintf(buf, sizeof(buf), "(%s:%d) ", filename, line); snprintf(buf, sizeof(buf), "(%s:%d) ", filename, line);
va_start(ap, format); va_start(ap, fmt);
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), format, ap); vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, ap);
va_end(ap); va_end(ap);
if (priority == LOG_ERR && errno > 0) { if (priority == LOG_ERR && errno > 0) {
@ -33,13 +33,6 @@ void __uh_log(const char *filename, int line, int priority, const char *format,
errno = 0; errno = 0;
} }
syslog(priority, "%s", buf); ulog(priority, "%s\n", buf);
#if (UHTTPD_DEBUG)
fprintf(stderr, "%s\n", buf);
#else
if (priority == LOG_ERR)
fprintf(stderr, "%s\n", buf);
#endif
} }

View File

@ -18,25 +18,21 @@
#ifndef _LOG_H #ifndef _LOG_H
#define _LOG_H #define _LOG_H
#include <syslog.h>
#include "common.h" #include "common.h"
#include <libubox/ulog.h>
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
/* /*
* Use the syslog output log and include the name and number of rows at the call * 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(fmt...) uh_log(LOG_DEBUG, fmt)
#define uh_log_debug(format...) uh_log(LOG_DEBUG, format) #define uh_log_info(fmt...) uh_log(LOG_INFO, fmt)
#else #define uh_log_err(fmt...) uh_log(LOG_ERR, fmt)
#define uh_log_debug(format...)
#endif
#define uh_log_info(format...) uh_log(LOG_INFO, format) void __uh_log(const char *filename, int line, int priority, const char *fmt, ...);
#define uh_log_err(format...) uh_log(LOG_ERR, format)
void __uh_log(const char *filename, int line, int priority, const char *format, ...);
#endif #endif