From 0e4deecb3ef9d845ae71db8fd6cbbfffc367d0bd Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Thu, 29 Dec 2016 02:10:38 +0100 Subject: [PATCH] Added the concept of debug levels --- Makefile | 10 ++++++++ include/libhttp.h | 11 ++++++++ src/httplib_get_debug_level.c | 37 +++++++++++++++++++++++++++ src/httplib_main.h | 2 ++ src/httplib_set_debug_level.c | 43 +++++++++++++++++++++++++++++++ src/httplib_start.c | 48 +++++++++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+) create mode 100644 src/httplib_get_debug_level.c create mode 100644 src/httplib_set_debug_level.c diff --git a/Makefile b/Makefile index 5033d6db..838470f2 100644 --- a/Makefile +++ b/Makefile @@ -227,6 +227,7 @@ OBJLIST = \ ${OBJDIR}httplib_get_builtin_mime_type${OBJEXT} \ ${OBJDIR}httplib_get_context${OBJEXT} \ ${OBJDIR}httplib_get_cookie${OBJEXT} \ + ${OBJDIR}httplib_get_debug_level${OBJEXT} \ ${OBJDIR}httplib_get_first_ssl_listener_index${OBJEXT} \ ${OBJDIR}httplib_get_header${OBJEXT} \ ${OBJDIR}httplib_get_mime_type${OBJEXT} \ @@ -331,6 +332,7 @@ OBJLIST = \ ${OBJDIR}httplib_set_acl_option${OBJEXT} \ ${OBJDIR}httplib_set_auth_handler${OBJEXT} \ ${OBJDIR}httplib_set_close_on_exec${OBJEXT} \ + ${OBJDIR}httplib_set_debug_level${OBJEXT} \ ${OBJDIR}httplib_set_gpass_option${OBJEXT} \ ${OBJDIR}httplib_set_handler_type${OBJEXT} \ ${OBJDIR}httplib_set_non_blocking_mode${OBJEXT} \ @@ -624,6 +626,10 @@ ${OBJDIR}httplib_get_cookie${OBJEXT} : ${SRCDIR}httplib_get_cookie.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h +${OBJDIR}httplib_get_debug_level${OBJEXT} : ${SRCDIR}httplib_get_debug_level.c \ + ${SRCDIR}httplib_main.h \ + ${INCDIR}libhttp.h + ${OBJDIR}httplib_get_first_ssl_listener_index${OBJEXT} : ${SRCDIR}httplib_get_first_ssl_listener_index.c \ ${SRCDIR}httplib_ssl.h \ ${SRCDIR}httplib_main.h \ @@ -1101,6 +1107,10 @@ ${OBJDIR}httplib_set_close_on_exec${OBJEXT} : ${SRCDIR}httplib_set_close_on_e ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h +${OBJDIR}httplib_set_debug_level${OBJEXT} : ${SRCDIR}httplib_set_debug_level.c \ + ${SRCDIR}httplib_main.h \ + ${INCDIR}libhttp.h + ${OBJDIR}httplib_set_gpass_option${OBJEXT} : ${SRCDIR}httplib_set_gpass_option.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/include/libhttp.h b/include/libhttp.h index ba78cde4..53349fa4 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -880,6 +880,15 @@ struct httplib_client_options { LIBHTTP_API struct httplib_connection *httplib_connect_client_secure(const struct httplib_client_options *client_options, char *error_buffer, size_t error_buffer_size); +enum debug_level_t { + DEBUG_LEVEL_NONE, + DEBUG_LEVEL_CRASH, + DEBUG_LEVEL_ERROR, + DEBUG_LEVEL_WARNING, + DEBUG_LEVEL_INFO +}; + + enum { TIMEOUT_INFINITE = -1 }; @@ -918,6 +927,7 @@ LIBHTTP_API int httplib_closedir( DIR *dir ); LIBHTTP_API void httplib_cry( const struct httplib_context *ctx, const struct httplib_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(3, 4); LIBHTTP_API char * httplib_error_string( int error_code, char *buf, size_t buf_len ); LIBHTTP_API const char * httplib_get_builtin_mime_type( const char *file_name ); +LIBHTTP_API enum debug_level_t httplib_get_debug_level( struct httplib_context *ctx ); LIBHTTP_API const char * httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen ); LIBHTTP_API uint64_t httplib_get_random( void ); LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn ); @@ -947,6 +957,7 @@ LIBHTTP_API struct dirent * httplib_readdir( DIR *dir ); LIBHTTP_API int httplib_remove( const char *path ); LIBHTTP_API void httplib_send_file( struct httplib_connection *conn, const char *path, const char *mime_type, const char *additional_headers ); LIBHTTP_API void httplib_set_alloc_callback_func( httplib_alloc_callback_func log_func ); +LIBHTTP_API enum debug_level_t httplib_set_debug_level( struct httplib_context *ctx, enum debug_level_t new_level ); LIBHTTP_API void httplib_set_user_connection_data( struct httplib_connection *conn, void *data ); LIBHTTP_API struct httplib_context * httplib_start(const struct httplib_callbacks *callbacks, void *user_data, const struct httplib_option_t *options ); LIBHTTP_API void httplib_stop( struct httplib_context *ctx ); diff --git a/src/httplib_get_debug_level.c b/src/httplib_get_debug_level.c new file mode 100644 index 00000000..9629b6a9 --- /dev/null +++ b/src/httplib_get_debug_level.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Lammert Bies + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "httplib_main.h" + +/* + * enum debug_level_t httplib_get_debug_level( struct httplib_context *ctx ); + * + * The function httplib_get_debug_level() returns the debug level for a + * context. + */ + +enum debug_level_t httplib_get_debug_level( struct httplib_context *ctx ) { + + if ( ctx == NULL ) return DEBUG_LEVEL_NONE; + return ctx->debug_level; + +} /* httplib_get_debug_level */ diff --git a/src/httplib_main.h b/src/httplib_main.h index 29323cd1..51fed66f 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -564,6 +564,8 @@ struct httplib_context { struct ttimers *timers; #endif + enum debug_level_t debug_level; + char * access_control_allow_origin; char * access_control_list; char * access_log_file; diff --git a/src/httplib_set_debug_level.c b/src/httplib_set_debug_level.c new file mode 100644 index 00000000..e527a8ac --- /dev/null +++ b/src/httplib_set_debug_level.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016 Lammert Bies + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "httplib_main.h" + +/* + * enum debug_level_t httplib_set_debug_level( struct httplib_context *ctx, enum debug_level_t new_level ); + * + * The function httplib_get_debug_level() sets the debug level for a context + * and returns the previous debug level. + */ + +enum debug_level_t httplib_set_debug_level( struct httplib_context *ctx, enum debug_level_t new_level ) { + + enum debug_level_t prev_level; + + if ( ctx == NULL ) return DEBUG_LEVEL_NONE; + + prev_level = ctx->debug_level; + ctx->debug_level = new_level; + + return prev_level; + +} /* httplib_get_debug_level */ diff --git a/src/httplib_start.c b/src/httplib_start.c index 066fcdc4..892252e8 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -32,6 +32,7 @@ #include "httplib_utils.h" static bool check_bool( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name, bool *config ); +static bool check_dbg( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name, enum debug_level_t *config ); static bool check_dir( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name, char **config ); static bool check_file( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name, char **config ); static bool check_int( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name, int *config, int minval, int maxval ); @@ -290,6 +291,7 @@ static bool process_options( struct httplib_context *ctx, const struct httplib_o ctx->cgi_environment = NULL; ctx->cgi_interpreter = NULL; ctx->cgi_pattern = NULL; + ctx->debug_level = DEBUG_LEVEL_WARNING; ctx->decode_url = true; ctx->document_root = NULL; ctx->enable_directory_listing = true; @@ -369,6 +371,7 @@ static bool process_options( struct httplib_context *ctx, const struct httplib_o if ( check_str( ctx, options, "cgi_environment", & ctx->cgi_environment ) ) return true; if ( check_file( ctx, options, "cgi_interpreter", & ctx->cgi_interpreter ) ) return true; if ( check_patt( ctx, options, "cgi_pattern", & ctx->cgi_pattern ) ) return true; + if ( check_dbg( ctx, options, "debug_level", & ctx->debug_level ) ) return true; if ( check_bool( ctx, options, "decode_url", & ctx->decode_url ) ) return true; if ( check_dir( ctx, options, "document_root", & ctx->document_root ) ) return true; if ( check_bool( ctx, options, "enable_directory_listing", & ctx->enable_directory_listing ) ) return true; @@ -622,6 +625,51 @@ static bool check_int( struct httplib_context *ctx, const struct httplib_option_ } /* check_int */ + +/* + * static bool check_dbg( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name ); + * + * The function check_dbg() checks if an option is equal to a debug level + * config parameter and stores the value if that is the case. If the value + * cannot be recognized, true is returned and the function performs a complete + * cleanup. If the option name could not be found, the function returns false + * to indicate that the search should go on. If the value could be found and is + * valid, also false is returned. + */ + +static bool check_dbg( struct httplib_context *ctx, const struct httplib_option_t *option, const char *name, enum debug_level_t *config ) { + + int val; + + if ( ctx == NULL || option == NULL || option->name == NULL || name == NULL || config == NULL ) { + + cleanup( ctx, "Internal error parsing debug level option" ); + return true; + } + + if ( httplib_strcasecmp( option->name, name ) ) return false; + + if ( ! XX_httplib_option_value_to_int( option->value, &val ) ) { + + switch ( val ) { + + case DEBUG_LEVEL_NONE : + case DEBUG_LEVEL_CRASH : + case DEBUG_LEVEL_ERROR : + case DEBUG_LEVEL_WARNING : + case DEBUG_LEVEL_INFO : + *config = val; + return false; + } + } + + cleanup( ctx, "Invalid value \"%s\" for option \"%s\"", option->value, option->name ); + return true; + +} /* check_dbg */ + + + /* * static struct httplib_context *cleanup( struct httplib_context *ctx, const char *fmt, ... ); *