From a03f2e301f1d128f41e161a3ffddd90c55ed6771 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Thu, 29 Dec 2016 03:10:24 +0100 Subject: [PATCH] Added debug levels to warnings --- include/libhttp.h | 2 +- src/httplib_accept_new_connection.c | 8 ++++---- src/httplib_addenv.c | 4 ++-- src/httplib_check_acl.c | 2 +- src/httplib_check_authorization.c | 2 +- src/httplib_close_socket_gracefully.c | 2 +- src/httplib_connect_client.c | 2 +- src/httplib_cry.c | 11 +++++++++-- src/httplib_fclose_on_exec.c | 2 +- src/httplib_get_response_code_text.c | 2 +- src/httplib_handle_cgi_request.c | 20 +++++++++---------- src/httplib_handle_form_request.c | 28 +++++++++++++-------------- src/httplib_handle_request.c | 2 +- src/httplib_initialize_ssl.c | 2 +- src/httplib_load_dll.c | 4 ++-- src/httplib_mkcol.c | 2 +- src/httplib_prepare_cgi_environment.c | 2 +- src/httplib_read_auth_file.c | 8 ++++---- src/httplib_read_websocket.c | 8 ++++---- src/httplib_refresh_trust.c | 2 +- src/httplib_remove_bad_file.c | 2 +- src/httplib_remove_directory.c | 2 +- src/httplib_scan_directory.c | 2 +- src/httplib_send_file_data.c | 2 +- src/httplib_set_close_on_exec.c | 2 +- src/httplib_set_gpass_option.c | 2 +- src/httplib_set_handler_type.c | 4 ++-- src/httplib_set_ports_option.c | 26 ++++++++++++------------- src/httplib_set_ssl_option.c | 10 +++++----- src/httplib_set_uid_option.c | 8 ++++---- src/httplib_spawn_process.c | 14 +++++++------- src/httplib_ssi.c | 16 +++++++-------- src/httplib_ssl_use_pem_file.c | 8 ++++---- src/httplib_start.c | 8 ++++---- src/httplib_store_body.c | 2 +- src/httplib_vsnprintf.c | 2 +- src/httplib_websocket_client_write.c | 2 +- src/httplib_worker_thread.c | 2 +- 38 files changed, 118 insertions(+), 111 deletions(-) diff --git a/include/libhttp.h b/include/libhttp.h index 53349fa4..97455521 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -924,7 +924,7 @@ LIBHTTP_API int httplib_atomic_inc( volatile int *addr ); LIBHTTP_API int httplib_base64_encode( const unsigned char *src, int src_len, char *dst, int dst_len ); LIBHTTP_API unsigned httplib_check_feature( unsigned feature ); 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 void httplib_cry( enum debug_level_t debug_level, const struct httplib_context *ctx, const struct httplib_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(4, 5); 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 ); diff --git a/src/httplib_accept_new_connection.c b/src/httplib_accept_new_connection.c index 36f7d5fa..46a12868 100644 --- a/src/httplib_accept_new_connection.c +++ b/src/httplib_accept_new_connection.c @@ -54,7 +54,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt if ( ! XX_httplib_check_acl( ctx, ntohl(*(uint32_t *)&so.rsa.sin.sin_addr )) ) { XX_httplib_sockaddr_to_string( src_addr, sizeof(src_addr), &so.rsa ); - httplib_cry( ctx, NULL, "%s: %s is not allowed to connect", __func__, src_addr ); + httplib_cry( DEBUG_LEVEL_INFO, ctx, NULL, "%s: %s is not allowed to connect", __func__, src_addr ); closesocket( so.sock ); so.sock = INVALID_SOCKET; } @@ -72,7 +72,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt if ( getsockname( so.sock, &so.lsa.sa, &len ) != 0 ) { - httplib_cry( ctx, NULL, "%s: getsockname() failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: getsockname() failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } /* @@ -86,7 +86,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt if ( setsockopt( so.sock, SOL_SOCKET, SO_KEEPALIVE, (SOCK_OPT_TYPE)&on, sizeof(on) ) != 0 ) { - httplib_cry( ctx, NULL, "%s: setsockopt(SOL_SOCKET SO_KEEPALIVE) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: setsockopt(SOL_SOCKET SO_KEEPALIVE) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } /* @@ -100,7 +100,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt if ( ctx->tcp_nodelay && XX_httplib_set_tcp_nodelay( so.sock, 1 ) != 0 ) { - httplib_cry( ctx, NULL, "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } if ( ctx->request_timeout > 0 ) XX_httplib_set_sock_timeout( so.sock, ctx->request_timeout ); diff --git a/src/httplib_addenv.c b/src/httplib_addenv.c index e2250c78..1333ac27 100644 --- a/src/httplib_addenv.c +++ b/src/httplib_addenv.c @@ -79,7 +79,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { * Out of memory */ - httplib_cry( env->conn->ctx, env->conn, "%s: Cannot allocate memory for CGI variable [%s]", __func__, fmt ); + httplib_cry( DEBUG_LEVEL_ERROR, env->conn->ctx, env->conn, "%s: Cannot allocate memory for CGI variable [%s]", __func__, fmt ); return; } @@ -133,7 +133,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { if ( space < 2 ) { - httplib_cry( env->conn->ctx, env->conn, "%s: Cannot register CGI variable [%s]", __func__, fmt ); + httplib_cry( DEBUG_LEVEL_ERROR, env->conn->ctx, env->conn, "%s: Cannot register CGI variable [%s]", __func__, fmt ); return; } diff --git a/src/httplib_check_acl.c b/src/httplib_check_acl.c index 28d57159..f0c9bbdb 100644 --- a/src/httplib_check_acl.c +++ b/src/httplib_check_acl.c @@ -59,7 +59,7 @@ int XX_httplib_check_acl( struct httplib_context *ctx, uint32_t remote_ip ) { if ( (flag != '+' && flag != '-') || XX_httplib_parse_net( &vec.ptr[1], &net, &mask ) == 0 ) { - httplib_cry( ctx, NULL, "%s: subnet must be [+|-]x.x.x.x[/x]", __func__ ); + httplib_cry( DEBUG_LEVEL_WARNING, ctx, NULL, "%s: subnet must be [+|-]x.x.x.x[/x]", __func__ ); return -1; } diff --git a/src/httplib_check_authorization.c b/src/httplib_check_authorization.c index 7c4d1b72..f0e276b8 100644 --- a/src/httplib_check_authorization.c +++ b/src/httplib_check_authorization.c @@ -54,7 +54,7 @@ bool XX_httplib_check_authorization( struct httplib_connection *conn, const char if ( truncated || ! XX_httplib_fopen( conn, fname, "r", &file ) ) { - httplib_cry( conn->ctx, conn, "%s: cannot open %s: %s", __func__, fname, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: cannot open %s: %s", __func__, fname, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } break; } diff --git a/src/httplib_close_socket_gracefully.c b/src/httplib_close_socket_gracefully.c index 247653a1..0a23f581 100644 --- a/src/httplib_close_socket_gracefully.c +++ b/src/httplib_close_socket_gracefully.c @@ -67,7 +67,7 @@ void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ) { else { if ( setsockopt( conn->client.sock, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger) ) != 0 ) { - httplib_cry( conn->ctx, conn, "%s: setsockopt(SOL_SOCKET SO_LINGER) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: setsockopt(SOL_SOCKET SO_LINGER) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } } diff --git a/src/httplib_connect_client.c b/src/httplib_connect_client.c index fe7c9ad1..5a8ea4b4 100644 --- a/src/httplib_connect_client.c +++ b/src/httplib_connect_client.c @@ -112,7 +112,7 @@ static struct httplib_connection *httplib_connect_client_impl( const struct http conn->client.sock = sock; conn->client.lsa = sa; - if ( getsockname( sock, psa, &len ) != 0 ) httplib_cry( &fake_ctx, conn, "%s: getsockname() failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + if ( getsockname( sock, psa, &len ) != 0 ) httplib_cry( DEBUG_LEVEL_ERROR, &fake_ctx, conn, "%s: getsockname() failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); conn->client.has_ssl = (use_ssl) ? true : false; httplib_pthread_mutex_init( &conn->mutex, &XX_httplib_pthread_mutex_attr ); diff --git a/src/httplib_cry.c b/src/httplib_cry.c index 91519089..2873b3af 100644 --- a/src/httplib_cry.c +++ b/src/httplib_cry.c @@ -26,14 +26,14 @@ #include "httplib_ssl.h" /* - * void httplib_cry( const struct httplib_context *ctx, const struct httplib_connection *conn, const char *fmt, ... ); + * void httplib_cry( enum debug_level_t debug_level, const struct httplib_context *ctx, const struct httplib_connection *conn, const char *fmt, ... ); * * The function httplib_cry() prints a formatted error message to the opened * error log stream. It first tries to use a user supplied error handler. If * that doesn't work, the alternative is to write to an error log file. */ -void httplib_cry( const struct httplib_context *ctx, const struct httplib_connection *conn, const char *fmt, ... ) { +void httplib_cry( enum debug_level_t debug_level, const struct httplib_context *ctx, const struct httplib_connection *conn, const char *fmt, ... ) { char buf[MG_BUF_LEN]; char src_addr[IP_ADDR_STR_LEN]; @@ -50,6 +50,13 @@ void httplib_cry( const struct httplib_context *ctx, const struct httplib_connec if ( ctx == NULL ) return; + /* + * Check if the message is severe enough to display. This is controlled + * with a context specific debug level. + */ + + if ( debug_level > ctx->debug_level ) return; + /* * Gather all the information from the parameters of this function and * create a NULL terminated string buffer with the error message. diff --git a/src/httplib_fclose_on_exec.c b/src/httplib_fclose_on_exec.c index bfb30db0..1a5b993c 100644 --- a/src/httplib_fclose_on_exec.c +++ b/src/httplib_fclose_on_exec.c @@ -39,7 +39,7 @@ void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *c if ( fcntl( fileno( filep->fp ), F_SETFD, FD_CLOEXEC) != 0 ) { - if ( conn != NULL && conn->ctx != NULL ) httplib_cry( conn->ctx, conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + if ( conn != NULL && conn->ctx != NULL ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } #endif diff --git a/src/httplib_get_response_code_text.c b/src/httplib_get_response_code_text.c index f98a45ad..69cd0fea 100644 --- a/src/httplib_get_response_code_text.c +++ b/src/httplib_get_response_code_text.c @@ -118,7 +118,7 @@ const char *httplib_get_response_code_text( struct httplib_connection *conn, int * This error code is unknown. This should not happen. */ - if ( conn != NULL && conn->ctx != NULL ) httplib_cry( conn->ctx, conn, "Unknown HTTP response code: %u", response_code ); + if ( conn != NULL && conn->ctx != NULL ) httplib_cry( DEBUG_LEVEL_INFO, conn->ctx, conn, "Unknown HTTP response code: %u", response_code ); /* * Return at least a category according to RFC 2616 Section 10. diff --git a/src/httplib_handle_cgi_request.c b/src/httplib_handle_cgi_request.c index 6e8bc706..2f3f810b 100644 --- a/src/httplib_handle_cgi_request.c +++ b/src/httplib_handle_cgi_request.c @@ -85,7 +85,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( truncated ) { - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Path too long", prog ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Path too long", prog ); XX_httplib_send_http_error( conn, 500, "Error: %s", "CGI path too long" ); goto done; @@ -106,7 +106,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( pipe(fdin) != 0 || pipe(fdout) != 0 || pipe(fderr) != 0 ) { status = httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ); - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Can not create CGI pipes: %s", prog, status ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Can not create CGI pipes: %s", prog, status ); XX_httplib_send_http_error( conn, 500, "Error: Cannot create CGI pipe: %s", status ); goto done; @@ -117,7 +117,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( pid == (pid_t)-1 ) { status = httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ); - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Can not spawn CGI process: %s", prog, status ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Can not spawn CGI process: %s", prog, status ); XX_httplib_send_http_error( conn, 500, "Error: Cannot spawn CGI process [%s]: %s", prog, status ); goto done; @@ -152,7 +152,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( (in = fdopen( fdin[1], "wb" )) == NULL ) { status = httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ); - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Can not open stdin: %s", prog, status ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Can not open stdin: %s", prog, status ); XX_httplib_send_http_error( conn, 500, "Error: CGI can not open fdin\nfopen: %s", status ); goto done; @@ -161,7 +161,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( (out = fdopen( fdout[0], "rb" )) == NULL ) { status = httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ); - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Can not open stdout: %s", prog, status ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Can not open stdout: %s", prog, status ); XX_httplib_send_http_error( conn, 500, "Error: CGI can not open fdout\nfopen: %s", status ); goto done; @@ -170,7 +170,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( (err = fdopen( fderr[0], "rb" )) == NULL ) { status = httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ); - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Can not open stderr: %s", prog, status ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Can not open stderr: %s", prog, status ); XX_httplib_send_http_error( conn, 500, "Error: CGI can not open fdout\nfopen: %s", status ); goto done; @@ -194,7 +194,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char * Error sending the body data */ - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Forward body data failed", prog ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Forward body data failed", prog ); goto done; } @@ -222,7 +222,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( buf == NULL ) { XX_httplib_send_http_error( conn, 500, "Error: Not enough memory for CGI buffer (%u bytes)", (unsigned int)buflen ); - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Not enough memory for buffer (%u " "bytes)", prog, (unsigned int)buflen ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\": Not enough memory for buffer (%u " "bytes)", prog, (unsigned int)buflen ); goto done; } @@ -239,12 +239,12 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( i > 0 ) { - httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf ); XX_httplib_send_http_error( conn, 500, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf ); } else { - httplib_cry( conn->ctx, conn, "Error: CGI program sent malformed or too big " "(>%u bytes) HTTP headers: [%.*s]", (unsigned)buflen, data_len, buf ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Error: CGI program sent malformed or too big " "(>%u bytes) HTTP headers: [%.*s]", (unsigned)buflen, data_len, buf ); XX_httplib_send_http_error( conn, 500, diff --git a/src/httplib_handle_form_request.c b/src/httplib_handle_form_request.c index fdfde548..1e55a8b3 100644 --- a/src/httplib_handle_form_request.c +++ b/src/httplib_handle_form_request.c @@ -55,7 +55,7 @@ static int url_encoded_field_found(const struct httplib_connection *conn, * Log error message and skip this field. */ - httplib_cry( conn->ctx, conn, "%s: Cannot decode filename", __func__ ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: Cannot decode filename", __func__ ); return FORM_FIELD_STORAGE_SKIP; } @@ -67,7 +67,7 @@ static int url_encoded_field_found(const struct httplib_connection *conn, if ( fdh->field_get == NULL ) { - httplib_cry( conn->ctx, conn, "%s: Function \"Get\" not available", __func__ ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: Function \"Get\" not available", __func__ ); return FORM_FIELD_STORAGE_SKIP; } } @@ -75,7 +75,7 @@ static int url_encoded_field_found(const struct httplib_connection *conn, if ( fdh->field_store == NULL ) { - httplib_cry( conn->ctx, conn, "%s: Function \"Store\" not available", __func__ ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: Function \"Store\" not available", __func__ ); return FORM_FIELD_STORAGE_SKIP; } } @@ -103,7 +103,7 @@ static int url_encoded_field_get(const struct httplib_connection *conn, * Log error message and stop parsing the form data. */ - httplib_cry( conn->ctx, conn, "%s: Not enough memory (required: %lu)", __func__, (unsigned long)(value_len + 1)); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Not enough memory (required: %lu)", __func__, (unsigned long)(value_len + 1)); return FORM_FIELD_STORAGE_ABORT; } @@ -282,7 +282,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp); if ((n != (size_t)vallen) || (ferror(fstore.fp))) { - httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); fclose( fstore.fp ); fstore.fp = NULL; XX_httplib_remove_bad_file( conn, path ); @@ -302,14 +302,14 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ } else { - httplib_cry( conn->ctx, conn, "%s: Error saving file %s", __func__, path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Error saving file %s", __func__, path ); XX_httplib_remove_bad_file( conn, path ); } fstore.fp = NULL; } - } else httplib_cry( conn->ctx, conn, "%s: Cannot create file %s", __func__, path ); + } else httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot create file %s", __func__, path ); } /* @@ -410,7 +410,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL; file_size = 0; - if (!fstore.fp) httplib_cry( conn->ctx, conn, "%s: Cannot create file %s", __func__, path); + if (!fstore.fp) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot create file %s", __func__, path); } get_block = 0; @@ -455,7 +455,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if ( fstore.fp ) { size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp); if ((n != (size_t)vallen) || (ferror(fstore.fp))) { - httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; XX_httplib_remove_bad_file(conn, path); @@ -506,7 +506,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ } else { - httplib_cry( conn->ctx, conn, "%s: Error saving file %s", __func__, path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Error saving file %s", __func__, path ); XX_httplib_remove_bad_file( conn, path ); } @@ -708,7 +708,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if ( XX_httplib_fopen( conn, path, "wb", &fstore ) == 0 ) fstore.fp = NULL; file_size = 0; - if ( ! fstore.fp ) httplib_cry( conn->ctx, conn, "%s: Cannot create file %s", __func__, path ); + if ( ! fstore.fp ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot create file %s", __func__, path ); } get_block = 0; @@ -752,7 +752,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if ( n != towrite || ferror( fstore.fp ) ) { - httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); fclose( fstore.fp ); fstore.fp = NULL; XX_httplib_remove_bad_file( conn, path ); @@ -808,7 +808,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if ( n != towrite || ferror( fstore.fp ) ) { - httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); fclose( fstore.fp ); fstore.fp = NULL; XX_httplib_remove_bad_file( conn, path ); @@ -832,7 +832,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ } else { - httplib_cry( conn->ctx, conn, "%s: Error saving file %s", __func__, path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Error saving file %s", __func__, path ); XX_httplib_remove_bad_file( conn, path ); } fstore.fp = NULL; diff --git a/src/httplib_handle_request.c b/src/httplib_handle_request.c index a6b65025..8af4ab89 100644 --- a/src/httplib_handle_request.c +++ b/src/httplib_handle_request.c @@ -116,7 +116,7 @@ void XX_httplib_handle_request( struct httplib_connection *conn ) { */ XX_httplib_send_http_error( conn, 503, "%s", "Error: SSL forward not configured properly" ); - httplib_cry( conn->ctx, conn, "Can not redirect to SSL, no SSL port available" ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Can not redirect to SSL, no SSL port available" ); } return; diff --git a/src/httplib_initialize_ssl.c b/src/httplib_initialize_ssl.c index 6a544ae8..57f7df49 100644 --- a/src/httplib_initialize_ssl.c +++ b/src/httplib_initialize_ssl.c @@ -78,7 +78,7 @@ int XX_httplib_initialize_ssl( struct httplib_context *ctx ) { if ( (XX_httplib_ssl_mutexes = httplib_malloc( size )) == NULL ) { - httplib_cry( ctx, NULL, "%s: cannot allocate mutexes: %s", __func__, XX_httplib_ssl_error() ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: cannot allocate mutexes: %s", __func__, XX_httplib_ssl_error() ); return 0; } diff --git a/src/httplib_load_dll.c b/src/httplib_load_dll.c index 9cd6ea46..d14d2506 100644 --- a/src/httplib_load_dll.c +++ b/src/httplib_load_dll.c @@ -82,7 +82,7 @@ void *XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, st if ( dll_handle == NULL ) { - httplib_cry( ctx, NULL, "%s: cannot load %s", __func__, dll_name ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: cannot load %s", __func__, dll_name ); return NULL; } @@ -109,7 +109,7 @@ void *XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, st if ( u.fp == NULL ) { - httplib_cry( ctx, NULL, "%s: %s: cannot find %s", __func__, dll_name, fp->name ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: %s: cannot find %s", __func__, dll_name, fp->name ); dlclose( dll_handle ); return NULL; diff --git a/src/httplib_mkcol.c b/src/httplib_mkcol.c index ccc0519b..563ca463 100644 --- a/src/httplib_mkcol.c +++ b/src/httplib_mkcol.c @@ -58,7 +58,7 @@ void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { if ( ! XX_httplib_stat( conn, path, & de.file ) ) { - httplib_cry( conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } if ( de.file.last_modified ) { diff --git a/src/httplib_prepare_cgi_environment.c b/src/httplib_prepare_cgi_environment.c index 1c848c8d..58c1eb13 100644 --- a/src/httplib_prepare_cgi_environment.c +++ b/src/httplib_prepare_cgi_environment.c @@ -138,7 +138,7 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const XX_httplib_snprintf( conn, &truncated, http_var_name, sizeof(http_var_name), "HTTP_%s", conn->request_info.http_headers[i].name ); if ( truncated ) { - httplib_cry( conn->ctx, conn, "%s: HTTP header variable too long [%s]", __func__, conn->request_info.http_headers[i].name ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: HTTP header variable too long [%s]", __func__, conn->request_info.http_headers[i].name ); continue; } diff --git a/src/httplib_read_auth_file.c b/src/httplib_read_auth_file.c index 300ac5e7..827a5468 100644 --- a/src/httplib_read_auth_file.c +++ b/src/httplib_read_auth_file.c @@ -91,7 +91,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct XX_httplib_fclose( &fp ); } - else httplib_cry( workdata->conn->ctx, workdata->conn, "%s: cannot open authorization file: %s", __func__, workdata->buf ); + else httplib_cry( DEBUG_LEVEL_ERROR, workdata->conn->ctx, workdata->conn, "%s: cannot open authorization file: %s", __func__, workdata->buf ); continue; } @@ -100,7 +100,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct * future) */ - httplib_cry( workdata->conn->ctx, workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); + httplib_cry( DEBUG_LEVEL_ERROR, workdata->conn->ctx, workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); continue; } @@ -108,7 +108,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct if ( workdata->f_domain == NULL ) { - httplib_cry( workdata->conn->ctx, workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); + httplib_cry( DEBUG_LEVEL_ERROR, workdata->conn->ctx, workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); continue; } @@ -119,7 +119,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct if ( workdata->f_ha1 == NULL ) { - httplib_cry( workdata->conn->ctx, workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); + httplib_cry( DEBUG_LEVEL_ERROR, workdata->conn->ctx, workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); continue; } diff --git a/src/httplib_read_websocket.c b/src/httplib_read_websocket.c index 9892ffd8..700bc1e4 100644 --- a/src/httplib_read_websocket.c +++ b/src/httplib_read_websocket.c @@ -96,7 +96,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock if ( conn->data_len < conn->request_len ) { - httplib_cry( conn->ctx, conn, "websocket error: data len less than request len, closing connection" ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "websocket error: data len less than request len, closing connection" ); break; } @@ -145,7 +145,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock * connection */ - httplib_cry( conn->ctx, conn, "websocket out of memory; closing connection" ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "websocket out of memory; closing connection" ); break; } } @@ -161,7 +161,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock if ( body_len < header_len ) { - httplib_cry( conn->ctx, conn, "websocket error: body len less than header len, closing connection" ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "websocket error: body len less than header len, closing connection" ); break; } @@ -195,7 +195,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock if (error) { - httplib_cry( conn->ctx, conn, "Websocket pull failed; closing connection" ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Websocket pull failed; closing connection" ); break; } diff --git a/src/httplib_refresh_trust.c b/src/httplib_refresh_trust.c index 65432ebf..af0d612e 100644 --- a/src/httplib_refresh_trust.c +++ b/src/httplib_refresh_trust.c @@ -66,7 +66,7 @@ int XX_httplib_refresh_trust( struct httplib_connection *conn ) { if ( SSL_CTX_load_verify_locations( conn->ctx->ssl_ctx, conn->ctx->ssl_ca_file, conn->ctx->ssl_ca_path ) != 1 ) { - httplib_cry( conn->ctx, conn, + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "SSL_CTX_load_verify_locations error: %s " "ssl_verify_peer requires setting " "either ssl_ca_path or ssl_ca_file. Is any of them " diff --git a/src/httplib_remove_bad_file.c b/src/httplib_remove_bad_file.c index 24ef0b14..039c9c12 100644 --- a/src/httplib_remove_bad_file.c +++ b/src/httplib_remove_bad_file.c @@ -38,6 +38,6 @@ void XX_httplib_remove_bad_file( const struct httplib_connection *conn, const ch int r = httplib_remove( path ); - if ( r != 0 && conn != NULL && conn->ctx != NULL ) httplib_cry( conn->ctx, conn, "%s: Cannot remove invalid file %s", __func__, path ); + if ( r != 0 && conn != NULL && conn->ctx != NULL ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Cannot remove invalid file %s", __func__, path ); } /* XX_httplib_remove_bad_file */ diff --git a/src/httplib_remove_directory.c b/src/httplib_remove_directory.c index 4f7c5e11..d204d329 100644 --- a/src/httplib_remove_directory.c +++ b/src/httplib_remove_directory.c @@ -85,7 +85,7 @@ int XX_httplib_remove_directory( struct httplib_connection *conn, const char *di if ( ! XX_httplib_stat( conn, path, & de.file ) ) { - httplib_cry( conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); ok = 0; } if ( de.file.membuf == NULL ) { diff --git a/src/httplib_scan_directory.c b/src/httplib_scan_directory.c index ccec57b0..36d9e0c6 100644 --- a/src/httplib_scan_directory.c +++ b/src/httplib_scan_directory.c @@ -66,7 +66,7 @@ int XX_httplib_scan_directory( struct httplib_connection *conn, const char *dir, if ( ! XX_httplib_stat( conn, path, &de.file ) ) { - httplib_cry( conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } de.file_name = dp->d_name; diff --git a/src/httplib_send_file_data.c b/src/httplib_send_file_data.c index ae5ca3e3..f5e8916d 100644 --- a/src/httplib_send_file_data.c +++ b/src/httplib_send_file_data.c @@ -140,7 +140,7 @@ void XX_httplib_send_file_data( struct httplib_connection *conn, struct file *fi #endif if ( offset > 0 && fseeko( filep->fp, offset, SEEK_SET ) != 0 ) { - httplib_cry( conn->ctx, conn, "%s: fseeko() failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: fseeko() failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); XX_httplib_send_http_error( conn, 500, "%s", "Error: Unable to access file at requested position." ); } diff --git a/src/httplib_set_close_on_exec.c b/src/httplib_set_close_on_exec.c index e750c2a0..c6e23f79 100644 --- a/src/httplib_set_close_on_exec.c +++ b/src/httplib_set_close_on_exec.c @@ -45,7 +45,7 @@ void XX_httplib_set_close_on_exec( SOCKET fd, const struct httplib_context *ctx if ( fcntl( fd, F_SETFD, FD_CLOEXEC ) != 0 ) { - if ( ctx != NULL ) httplib_cry( ctx, NULL, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + if ( ctx != NULL ) httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } #endif /* _WIN32 */ diff --git a/src/httplib_set_gpass_option.c b/src/httplib_set_gpass_option.c index 334f0369..7b087ec2 100644 --- a/src/httplib_set_gpass_option.c +++ b/src/httplib_set_gpass_option.c @@ -46,7 +46,7 @@ int XX_httplib_set_gpass_option( struct httplib_context *ctx ) { if ( path != NULL && ! XX_httplib_stat( NULL, path, &file ) ) { - httplib_cry( ctx, NULL, "Cannot open %s: %s", path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "Cannot open %s: %s", path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); return 0; } return 1; diff --git a/src/httplib_set_handler_type.c b/src/httplib_set_handler_type.c index 337ab0fe..878d5378 100644 --- a/src/httplib_set_handler_type.c +++ b/src/httplib_set_handler_type.c @@ -140,7 +140,7 @@ void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, if ( tmp_rh == NULL ) { httplib_unlock_context( ctx ); - httplib_cry( ctx, NULL, "%s", "Cannot create new request handler struct, OOM" ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s", "Cannot create new request handler struct, OOM" ); return; } @@ -151,7 +151,7 @@ void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, httplib_unlock_context( ctx ); tmp_rh = httplib_free( tmp_rh ); - httplib_cry( ctx, NULL, "%s", "Cannot create new request handler struct, OOM" ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s", "Cannot create new request handler struct, OOM" ); return; } diff --git a/src/httplib_set_ports_option.c b/src/httplib_set_ports_option.c index ea35e6d6..4e9dcc53 100644 --- a/src/httplib_set_ports_option.c +++ b/src/httplib_set_ports_option.c @@ -73,7 +73,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( ! parse_port_string( &vec, &so, &ip_version ) ) { - httplib_cry( ctx, NULL, + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%.*s: invalid port spec (entry %i). Expecting list of: %s", (int)vec.len, vec.ptr, @@ -85,14 +85,14 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { #if !defined(NO_SSL) if ( so.has_ssl && ctx->ssl_ctx == NULL ) { - httplib_cry( ctx, NULL, "Cannot add SSL socket (entry %i). Is -ssl_certificate option set?", ports_total ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "Cannot add SSL socket (entry %i). Is -ssl_certificate option set?", ports_total ); continue; } #endif /* ! NO_SLL */ if ( ( so.sock = socket( so.lsa.sa.sa_family, SOCK_STREAM, 6 ) ) == INVALID_SOCKET ) { - httplib_cry( ctx, NULL, "cannot create socket (entry %i)", ports_total ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot create socket (entry %i)", ports_total ); continue; } @@ -116,7 +116,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { * Set reuse option, but don't abort on errors. */ - httplib_cry( ctx, NULL, "cannot set socket option SO_EXCLUSIVEADDRUSE (entry %i)", ports_total ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot set socket option SO_EXCLUSIVEADDRUSE (entry %i)", ports_total ); } #else /* _WIN32 */ if ( setsockopt( so.sock, SOL_SOCKET, SO_REUSEADDR, (SOCK_OPT_TYPE)&on, sizeof(on) ) != 0 ) { @@ -125,7 +125,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { * Set reuse option, but don't abort on errors. */ - httplib_cry( ctx, NULL, "cannot set socket option SO_REUSEADDR (entry %i)", ports_total ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot set socket option SO_REUSEADDR (entry %i)", ports_total ); } #endif /* _WIN32 */ @@ -139,7 +139,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { * Set IPv6 only option, but don't abort on errors. */ - httplib_cry( ctx, NULL, "cannot set socket option IPV6_V6ONLY (entry %i)", ports_total ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot set socket option IPV6_V6ONLY (entry %i)", ports_total ); } } } @@ -150,7 +150,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( bind( so.sock, &so.lsa.sa, len ) != 0 ) { - httplib_cry( ctx, NULL, "cannot bind to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot bind to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -163,7 +163,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( bind( so.sock, &so.lsa.sa, len ) != 0 ) { - httplib_cry( ctx, NULL, "cannot bind to IPv6 %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot bind to IPv6 %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -171,13 +171,13 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { } else { - httplib_cry( ctx, NULL, "cannot bind: address family not supported (entry %i)", ports_total ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot bind: address family not supported (entry %i)", ports_total ); continue; } if ( listen( so.sock, SOMAXCONN ) != 0 ) { - httplib_cry( ctx, NULL, "cannot listen to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "cannot listen to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -186,7 +186,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( getsockname( so.sock, &(usa.sa), &len ) != 0 || usa.sa.sa_family != so.lsa.sa.sa_family ) { int err = (int)ERRNO; - httplib_cry( ctx, NULL, "call to getsockname failed %.*s: %d (%s)", (int)vec.len, vec.ptr, err, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "call to getsockname failed %.*s: %d (%s)", (int)vec.len, vec.ptr, err, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -203,7 +203,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( ptr != NULL ) ctx->listening_sockets = ptr; else { - httplib_cry( ctx, NULL, "%s", "Out of memory" ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s", "Out of memory" ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -214,7 +214,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( pfd != NULL ) ctx->listening_socket_fds = pfd; else { - httplib_cry( ctx, NULL, "%s", "Out of memory" ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s", "Out of memory" ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; diff --git a/src/httplib_set_ssl_option.c b/src/httplib_set_ssl_option.c index 1204d88a..79b7672b 100644 --- a/src/httplib_set_ssl_option.c +++ b/src/httplib_set_ssl_option.c @@ -77,7 +77,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { ctx->ssl_ctx = SSL_CTX_new( SSLv23_server_method() ); if ( ctx->ssl_ctx == NULL ) { - httplib_cry( ctx, NULL, "SSL_CTX_new (server) error: %s", XX_httplib_ssl_error() ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "SSL_CTX_new (server) error: %s", XX_httplib_ssl_error() ); return false; } @@ -101,7 +101,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( callback_ret < 0 ) { - httplib_cry( ctx, NULL, "SSL callback returned error: %i", callback_ret ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "SSL callback returned error: %i", callback_ret ); return false; } @@ -130,7 +130,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( SSL_CTX_load_verify_locations( ctx->ssl_ctx, ctx->ssl_ca_file, ctx->ssl_ca_path ) != 1 ) { - httplib_cry( ctx, NULL, + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "SSL_CTX_load_verify_locations error: %s " "ssl_verify_peer requires setting " "either ssl_ca_path or ssl_ca_file. Is any of them " @@ -145,7 +145,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( ctx->ssl_verify_paths && SSL_CTX_set_default_verify_paths( ctx->ssl_ctx ) != 1 ) { - httplib_cry( ctx, NULL, "SSL_CTX_set_default_verify_paths error: %s", XX_httplib_ssl_error()); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "SSL_CTX_set_default_verify_paths error: %s", XX_httplib_ssl_error()); return false; } @@ -156,7 +156,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( SSL_CTX_set_cipher_list( ctx->ssl_ctx, ctx->ssl_cipher_list ) != 1 ) { - httplib_cry( ctx, NULL, "SSL_CTX_set_cipher_list error: %s", XX_httplib_ssl_error()); + httplib_cry( DEBUG_LEVEL_WARNING, ctx, NULL, "SSL_CTX_set_cipher_list error: %s", XX_httplib_ssl_error()); } } diff --git a/src/httplib_set_uid_option.c b/src/httplib_set_uid_option.c index 9ffb07a4..70145d94 100644 --- a/src/httplib_set_uid_option.c +++ b/src/httplib_set_uid_option.c @@ -56,10 +56,10 @@ bool XX_httplib_set_uid_option( struct httplib_context *ctx ) { if ( uid == NULL ) return true; - if ( (pw = getpwnam(uid)) == NULL ) httplib_cry( ctx, NULL, "%s: unknown user [%s]", __func__, uid ); - else if ( setgid(pw->pw_gid) == -1 ) httplib_cry( ctx, NULL, "%s: setgid(%s): %s", __func__, uid, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); - else if ( setgroups(0, NULL) ) httplib_cry( ctx, NULL, "%s: setgroups(): %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); - else if ( setuid(pw->pw_uid) == -1 ) httplib_cry( ctx, NULL, "%s: setuid(%s): %s", __func__, uid, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + if ( (pw = getpwnam(uid)) == NULL ) httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: unknown user [%s]", __func__, uid ); + else if ( setgid(pw->pw_gid) == -1 ) httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: setgid(%s): %s", __func__, uid, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + else if ( setgroups(0, NULL) ) httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: setgroups(): %s", __func__, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + else if ( setuid(pw->pw_uid) == -1 ) httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s: setuid(%s): %s", __func__, uid, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); else return true; return false; diff --git a/src/httplib_spawn_process.c b/src/httplib_spawn_process.c index e3db84d6..224e5f89 100644 --- a/src/httplib_spawn_process.c +++ b/src/httplib_spawn_process.c @@ -133,7 +133,7 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro if ( CreateProcessA( NULL, cmdline, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, envblk, NULL, &si, &pi ) == 0 ) { - httplib_cry( conn->ctx, conn, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); pi.hProcess = (pid_t)-1; /* @@ -182,10 +182,10 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro * Child */ - if ( chdir( dir ) != 0 ) httplib_cry( conn->ctx, conn, "%s: chdir(%s): %s", __func__, dir, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); - else if ( dup2( fdin[0], 0 ) == -1 ) httplib_cry( conn->ctx, conn, "%s: dup2(%d, 0): %s", __func__, fdin[0], httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); - else if ( dup2( fdout[1], 1 ) == -1 ) httplib_cry( conn->ctx, conn, "%s: dup2(%d, 1): %s", __func__, fdout[1], httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); - else if ( dup2( fderr[1], 2 ) == -1 ) httplib_cry( conn->ctx, conn, "%s: dup2(%d, 2): %s", __func__, fderr[1], httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + if ( chdir( dir ) != 0 ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: chdir(%s): %s", __func__, dir, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + else if ( dup2( fdin[0], 0 ) == -1 ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: dup2(%d, 0): %s", __func__, fdin[0], httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + else if ( dup2( fdout[1], 1 ) == -1 ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: dup2(%d, 1): %s", __func__, fdout[1], httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + else if ( dup2( fderr[1], 2 ) == -1 ) httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: dup2(%d, 2): %s", __func__, fderr[1], httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); else { /* * Keep stderr and stdout in two different pipes. @@ -220,12 +220,12 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro if ( interp == NULL ) { execle( prog, prog, NULL, envp ); - httplib_cry( conn->ctx, conn, "%s: execle(%s): %s", __func__, prog, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: execle(%s): %s", __func__, prog, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } else { execle( interp, interp, prog, NULL, envp ); - httplib_cry( conn->ctx, conn, "%s: execle(%s %s): %s", __func__, interp, prog, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: execle(%s %s): %s", __func__, interp, prog, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } } diff --git a/src/httplib_ssi.c b/src/httplib_ssi.c index ae0a27a3..68235bc8 100644 --- a/src/httplib_ssi.c +++ b/src/httplib_ssi.c @@ -98,19 +98,19 @@ static void do_ssi_include( struct httplib_connection *conn, const char *ssi, ch } else { - httplib_cry( conn->ctx, conn, "Bad SSI #include: [%s]", tag ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Bad SSI #include: [%s]", tag ); return; } if ( truncated ) { - httplib_cry( conn->ctx, conn, "SSI #include path length overflow: [%s]", tag ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "SSI #include path length overflow: [%s]", tag ); return; } if ( ! XX_httplib_fopen( conn, path, "rb", &file ) ) { - httplib_cry( conn->ctx, conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); return; } @@ -135,14 +135,14 @@ static void do_ssi_exec( struct httplib_connection *conn, char *tag ) { if ( sscanf(tag, " \"%1023[^\"]\"", cmd) != 1 ) { - httplib_cry( conn->ctx, conn, "Bad SSI #exec: [%s]", tag ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Bad SSI #exec: [%s]", tag ); } else { cmd[1023] = 0; if ( (file.fp = popen( cmd, "r" ) ) == NULL ) { - httplib_cry( conn->ctx, conn, "Cannot SSI #exec: [%s]: %s", cmd, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Cannot SSI #exec: [%s]: %s", cmd, httplib_error_string( ERRNO, error_string, ERROR_STRING_LEN ) ); } else { @@ -175,7 +175,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st if ( include_level > 10 ) { - httplib_cry( conn->ctx, conn, "SSI #include level is too deep (%s)", path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "SSI #include level is too deep (%s)", path ); return; } @@ -213,7 +213,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st do_ssi_exec(conn, buf + 9); #endif /* !NO_POPEN */ } - else httplib_cry( conn->ctx, conn, "%s: unknown SSI " "command: \"%s\"", path, buf ); + else httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: unknown SSI " "command: \"%s\"", path, buf ); } len = 0; @@ -232,7 +232,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st else if ( len == (int)sizeof(buf) - 2 ) { - httplib_cry( conn->ctx, conn, "%s: SSI tag is too large", path ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: SSI tag is too large", path ); len = 0; } diff --git a/src/httplib_ssl_use_pem_file.c b/src/httplib_ssl_use_pem_file.c index cc877df8..9c0ee76b 100644 --- a/src/httplib_ssl_use_pem_file.c +++ b/src/httplib_ssl_use_pem_file.c @@ -43,7 +43,7 @@ int XX_httplib_ssl_use_pem_file( struct httplib_context *ctx, const char *pem ) if ( SSL_CTX_use_certificate_file( ctx->ssl_ctx, pem, 1 ) == 0 ) { - httplib_cry( ctx, NULL, "%s: cannot open certificate file %s: %s", __func__, pem, XX_httplib_ssl_error() ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: cannot open certificate file %s: %s", __func__, pem, XX_httplib_ssl_error() ); return 0; } @@ -53,19 +53,19 @@ int XX_httplib_ssl_use_pem_file( struct httplib_context *ctx, const char *pem ) if ( SSL_CTX_use_PrivateKey_file( ctx->ssl_ctx, pem, 1 ) == 0 ) { - httplib_cry( ctx, NULL, "%s: cannot open private key file %s: %s", __func__, pem, XX_httplib_ssl_error() ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: cannot open private key file %s: %s", __func__, pem, XX_httplib_ssl_error() ); return 0; } if ( SSL_CTX_check_private_key( ctx->ssl_ctx ) == 0 ) { - httplib_cry( ctx, NULL, "%s: certificate and private key do not match: %s", __func__, pem ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: certificate and private key do not match: %s", __func__, pem ); return 0; } if ( SSL_CTX_use_certificate_chain_file( ctx->ssl_ctx, pem ) == 0 ) { - httplib_cry( ctx, NULL, "%s: cannot use certificate chain file %s: %s", __func__, pem, XX_httplib_ssl_error() ); + httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s: cannot use certificate chain file %s: %s", __func__, pem, XX_httplib_ssl_error() ); return 0; } diff --git a/src/httplib_start.c b/src/httplib_start.c index 892252e8..befd92dd 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -122,7 +122,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks */ httplib_atomic_dec( & XX_httplib_sTlsInit ); - httplib_cry( ctx, NULL, "Cannot initialize thread local storage" ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "Cannot initialize thread local storage" ); ctx = httplib_free( ctx ); return NULL; @@ -253,9 +253,9 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks wta = httplib_free( wta ); - if ( i > 0 ) httplib_cry( ctx, NULL, "Cannot start worker thread %i: error %ld", i + 1, (long)ERRNO ); + if ( i > 0 ) httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "Cannot start worker thread %i: error %ld", i + 1, (long)ERRNO ); - else return cleanup( ctx, "Cannot create threads: error %ld", (long)ERRNO ); + else return cleanup( ctx, "Cannot create worker threads: error %ld", (long)ERRNO ); break; } @@ -692,7 +692,7 @@ static struct httplib_context *cleanup( struct httplib_context *ctx, const char va_end( ap ); buf[sizeof(buf)-1] = 0; - httplib_cry( ctx, NULL, "%s", buf ); + httplib_cry( DEBUG_LEVEL_CRASH, ctx, NULL, "%s", buf ); } XX_httplib_free_context( ctx ); diff --git a/src/httplib_store_body.c b/src/httplib_store_body.c index b321c55c..951da953 100644 --- a/src/httplib_store_body.c +++ b/src/httplib_store_body.c @@ -47,7 +47,7 @@ int64_t httplib_store_body( struct httplib_connection *conn, const char *path ) if ( conn->consumed_content != 0 ) { - httplib_cry( conn->ctx, conn, "%s: Contents already consumed", __func__ ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s: Contents already consumed", __func__ ); return -11; } diff --git a/src/httplib_vsnprintf.c b/src/httplib_vsnprintf.c index 201fe42d..b7cb91a1 100644 --- a/src/httplib_vsnprintf.c +++ b/src/httplib_vsnprintf.c @@ -60,7 +60,7 @@ void XX_httplib_vsnprintf( const struct httplib_connection *conn, bool *truncate else { if ( truncated != NULL ) *truncated = true; - if ( conn != NULL && conn->ctx != NULL ) httplib_cry( conn->ctx, conn, "truncating vsnprintf buffer: [%.*s]", (int)((buflen > 200) ? 200 : (buflen - 1)), buf ); + if ( conn != NULL && conn->ctx != NULL ) httplib_cry( DEBUG_LEVEL_WARNING, conn->ctx, conn, "truncating vsnprintf buffer: [%.*s]", (int)((buflen > 200) ? 200 : (buflen - 1)), buf ); n = (int)buflen - 1; } buf[n] = '\0'; diff --git a/src/httplib_websocket_client_write.c b/src/httplib_websocket_client_write.c index 353281a2..189bc120 100644 --- a/src/httplib_websocket_client_write.c +++ b/src/httplib_websocket_client_write.c @@ -52,7 +52,7 @@ int httplib_websocket_client_write( struct httplib_connection *conn, int opcode, if ( masked_data == NULL ) { - httplib_cry( conn->ctx, conn, "Cannot allocate buffer for masked websocket response: Out of memory" ); + httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "Cannot allocate buffer for masked websocket response: Out of memory" ); return -1; } diff --git a/src/httplib_worker_thread.c b/src/httplib_worker_thread.c index e66679a5..d2a6581e 100644 --- a/src/httplib_worker_thread.c +++ b/src/httplib_worker_thread.c @@ -90,7 +90,7 @@ static void *worker_thread_run( struct worker_thread_args *thread_args ) { if ( ctx->callbacks.init_thread != NULL ) ctx->callbacks.init_thread( ctx, 1 ); /* call init_thread for a worker thread (type 1) */ conn = httplib_calloc( 1, sizeof(*conn) + MAX_REQUEST_SIZE ); - if ( conn == NULL ) httplib_cry( ctx, NULL, "%s", "Cannot create new connection struct, OOM" ); + if ( conn == NULL ) httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s", "Cannot create new connection struct, OOM" ); else { httplib_pthread_setspecific( XX_httplib_sTlsKey, &tls );