From 8118c908eb644a507a11710baf9f0cbda49c0edc Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 26 Dec 2016 19:25:35 +0100 Subject: [PATCH] Removed httplib_fc fake context --- Makefile | 5 --- include/libhttp.h | 20 ++---------- src/httplib_accept_new_connection.c | 10 +++--- src/httplib_addenv.c | 10 ++++-- src/httplib_atomic_dec.c | 2 +- src/httplib_check_acl.c | 2 +- src/httplib_check_authorization.c | 2 +- src/httplib_close_socket_gracefully.c | 4 +-- src/httplib_connect_client.c | 2 +- src/httplib_connect_socket.c | 2 +- src/httplib_cry.c | 12 ++++---- src/httplib_fc.c | 44 --------------------------- src/httplib_fclose_on_exec.c | 2 +- src/httplib_get_response_code_text.c | 2 +- src/httplib_handle_cgi_request.c | 34 ++++++++++----------- src/httplib_handle_form_request.c | 28 ++++++++--------- src/httplib_handle_request.c | 2 +- src/httplib_initialize_ssl.c | 2 +- src/httplib_is_file_in_memory.c | 2 +- src/httplib_load_dll.c | 4 +-- src/httplib_main.h | 3 +- src/httplib_mkcol.c | 4 +-- src/httplib_prepare_cgi_environment.c | 4 +-- src/httplib_read_auth_file.c | 8 ++--- src/httplib_read_websocket.c | 4 +-- src/httplib_refresh_trust.c | 8 +++-- 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 | 4 +-- src/httplib_set_close_on_exec.c | 29 ++++++------------ src/httplib_set_gpass_option.c | 4 +-- src/httplib_set_handler_type.c | 4 +-- src/httplib_set_ports_option.c | 28 ++++++++--------- src/httplib_set_ssl_option.c | 10 +++--- src/httplib_set_uid_option.c | 8 ++--- src/httplib_spawn_process.c | 16 +++++----- src/httplib_ssi.c | 18 +++++------ src/httplib_ssl_use_pem_file.c | 10 +++--- src/httplib_start.c | 8 ++--- src/httplib_store_body.c | 2 +- src/httplib_vsnprintf.c | 2 +- src/httplib_websocket_client_write.c | 4 ++- src/httplib_worker_thread.c | 2 +- 44 files changed, 156 insertions(+), 221 deletions(-) delete mode 100644 src/httplib_fc.c diff --git a/Makefile b/Makefile index 9194da56..67cd070f 100644 --- a/Makefile +++ b/Makefile @@ -199,7 +199,6 @@ OBJLIST = \ ${OBJDIR}httplib_discard_unread_request_data${OBJEXT} \ ${OBJDIR}httplib_download${OBJEXT} \ ${OBJDIR}httplib_event_queue${OBJEXT} \ - ${OBJDIR}httplib_fc${OBJEXT} \ ${OBJDIR}httplib_fclose${OBJEXT} \ ${OBJDIR}httplib_fclose_on_exec${OBJEXT} \ ${OBJDIR}httplib_fgets${OBJEXT} \ @@ -560,10 +559,6 @@ ${OBJDIR}httplib_event_queue${OBJEXT} : ${SRCDIR}httplib_event_queue.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}httplib_fc${OBJEXT} : ${SRCDIR}httplib_fc.c \ - ${SRCDIR}httplib_main.h \ - ${INCDIR}libhttp.h - ${OBJDIR}httplib_fclose${OBJEXT} : ${SRCDIR}httplib_fclose.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/include/libhttp.h b/include/libhttp.h index 93f50c28..e719af50 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -313,14 +313,6 @@ struct httplib_callbacks { LIBHTTP_API struct httplib_context *httplib_start(const struct httplib_callbacks *callbacks, void *user_data, const char **configuration_options); -/* Stop the web server. - - Must be called last, when an application wants to stop the web server and - release all associated resources. This function blocks until all LibHTTP - threads are stopped. Context pointer becomes invalid. */ -LIBHTTP_API void httplib_stop(struct httplib_context *); - - /* httplib_request_handler Called when a new request comes in. This callback is URI based @@ -857,16 +849,6 @@ LIBHTTP_API int httplib_url_encode(const char *src, char *dst, size_t dst_len); LIBHTTP_API char *httplib_md5(char buf[33], ...); -/* Print error message to the opened error log stream. - This utilizes the provided logging configuration. - conn: connection - fmt: format string without the line return - ...: variable argument list - Example: - httplib_cry(conn,"i like %s", "logging"); */ -LIBHTTP_API void httplib_cry(const struct httplib_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3); - - /* utility methods to compare two buffers, case insensitive. */ @@ -976,6 +958,7 @@ LIBHTTP_API int httplib_atomic_dec( volatile int *addr ); 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 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 uint64_t httplib_get_random( void ); LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn ); LIBHTTP_API int httplib_kill( pid_t pid, int sig_num ); @@ -1004,6 +987,7 @@ 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 void httplib_set_user_connection_data( struct httplib_connection *conn, void *data ); +LIBHTTP_API void httplib_stop( struct httplib_context *ctx ); LIBHTTP_API int httplib_strcasecmp( const char *s1, const char *s2 ); LIBHTTP_API const char * httplib_strcasestr( const char *big_str, const char *small_str ); LIBHTTP_API char * httplib_strdup( const char *str ); diff --git a/src/httplib_accept_new_connection.c b/src/httplib_accept_new_connection.c index 67a9c590..fb83eb46 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( XX_httplib_fc(ctx), "%s: %s is not allowed to connect", __func__, src_addr ); + httplib_cry( ctx, NULL, "%s: %s is not allowed to connect", __func__, src_addr ); closesocket( so.sock ); so.sock = INVALID_SOCKET; } @@ -64,14 +64,14 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt * Put so socket structure into the queue */ - XX_httplib_set_close_on_exec( so.sock, XX_httplib_fc(ctx) ); + XX_httplib_set_close_on_exec( so.sock, ctx ); so.has_ssl = listener->has_ssl; so.has_redir = listener->has_redir; if ( getsockname( so.sock, &so.lsa.sa, &len ) != 0 ) { - httplib_cry( XX_httplib_fc(ctx), "%s: getsockname() failed: %s", __func__, strerror(ERRNO) ); + httplib_cry( ctx, NULL, "%s: getsockname() failed: %s", __func__, strerror(ERRNO) ); } /* @@ -85,7 +85,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( XX_httplib_fc(ctx), "%s: setsockopt(SOL_SOCKET SO_KEEPALIVE) failed: %s", __func__, strerror(ERRNO) ); + httplib_cry( ctx, NULL, "%s: setsockopt(SOL_SOCKET SO_KEEPALIVE) failed: %s", __func__, strerror(ERRNO) ); } /* @@ -101,7 +101,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt if ( XX_httplib_set_tcp_nodelay( so.sock, 1 ) != 0 ) { - httplib_cry( XX_httplib_fc(ctx), "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", __func__, strerror(ERRNO) ); + httplib_cry( ctx, NULL, "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", __func__, strerror(ERRNO) ); } } diff --git a/src/httplib_addenv.c b/src/httplib_addenv.c index dda9027b..e2250c78 100644 --- a/src/httplib_addenv.c +++ b/src/httplib_addenv.c @@ -36,6 +36,10 @@ * a CGI script is called. The environment variable has the form * VARIABLE=VALUE\0 an is appended to the buffer. This function assumes that * env != NULL and also fmt != NULL. + * + * The function assumes that a connection must be present, otherwise calling a + * CGI script has not much value. Therefore the function will return directly + * if no connection, or no server context is known. */ #if !defined(NO_CGI) @@ -48,6 +52,8 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { char *added; va_list ap; + if ( env == NULL || env->conn == NULL || env->conn->ctx == NULL ) return; + /* * Calculate how much space is left in the buffer */ @@ -73,7 +79,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { * Out of memory */ - httplib_cry( env->conn, "%s: Cannot allocate memory for CGI variable [%s]", __func__, fmt ); + httplib_cry( env->conn->ctx, env->conn, "%s: Cannot allocate memory for CGI variable [%s]", __func__, fmt ); return; } @@ -127,7 +133,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { if ( space < 2 ) { - httplib_cry( env->conn, "%s: Cannot register CGI variable [%s]", __func__, fmt ); + httplib_cry( env->conn->ctx, env->conn, "%s: Cannot register CGI variable [%s]", __func__, fmt ); return; } diff --git a/src/httplib_atomic_dec.c b/src/httplib_atomic_dec.c index f2f042d0..7bad99a5 100644 --- a/src/httplib_atomic_dec.c +++ b/src/httplib_atomic_dec.c @@ -46,7 +46,7 @@ LIBHTTP_API int httplib_atomic_dec( volatile int *addr ) { * so whatever you use, the other SDK is likely to raise a warning. */ - return InterlockedDecrement( addr ); + return InterlockedDecrement( (volatile LONG *)addr ); #elif defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 0))) diff --git a/src/httplib_check_acl.c b/src/httplib_check_acl.c index f83e712f..1dcbf1ee 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( XX_httplib_fc(ctx), "%s: subnet must be [+|-]x.x.x.x[/x]", __func__ ); + httplib_cry( 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 e2b7e6d8..38f9b1a7 100644 --- a/src/httplib_check_authorization.c +++ b/src/httplib_check_authorization.c @@ -53,7 +53,7 @@ bool XX_httplib_check_authorization( struct httplib_connection *conn, const char if ( truncated || ! XX_httplib_fopen( conn, fname, "r", &file ) ) { - httplib_cry( conn, "%s: cannot open %s: %s", __func__, fname, strerror(errno) ); + httplib_cry( conn->ctx, conn, "%s: cannot open %s: %s", __func__, fname, strerror(errno) ); } break; } diff --git a/src/httplib_close_socket_gracefully.c b/src/httplib_close_socket_gracefully.c index 9fb03c08..d0a5c7f2 100644 --- a/src/httplib_close_socket_gracefully.c +++ b/src/httplib_close_socket_gracefully.c @@ -44,7 +44,7 @@ void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ) { int error_code; socklen_t opt_len; - if ( conn == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; error_code = 0; opt_len = sizeof(error_code); @@ -66,7 +66,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, "%s: setsockopt(SOL_SOCKET SO_LINGER) failed: %s", __func__, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: setsockopt(SOL_SOCKET SO_LINGER) failed: %s", __func__, strerror(ERRNO) ); } } diff --git a/src/httplib_connect_client.c b/src/httplib_connect_client.c index c2e531f6..f1404b2d 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(conn, "%s: getsockname() failed: %s", __func__, strerror(ERRNO) ); + if ( getsockname( sock, psa, &len ) != 0 ) httplib_cry( &fake_ctx, conn, "%s: getsockname() failed: %s", __func__, strerror(ERRNO) ); conn->client.has_ssl = (use_ssl) ? true : false; httplib_pthread_mutex_init( &conn->mutex, &XX_httplib_pthread_mutex_attr ); diff --git a/src/httplib_connect_socket.c b/src/httplib_connect_socket.c index e191e530..6c9b6ca3 100644 --- a/src/httplib_connect_socket.c +++ b/src/httplib_connect_socket.c @@ -121,7 +121,7 @@ int XX_httplib_connect_socket( struct httplib_context *ctx, const char *host, in return 0; } - XX_httplib_set_close_on_exec( *sock, XX_httplib_fc(ctx) ); + XX_httplib_set_close_on_exec( *sock, ctx ); if ( ip_ver == 4 && connect( *sock, (struct sockaddr *)&sa->sin, sizeof(sa->sin) ) == 0 ) return 1; if ( ip_ver == 6 && connect( *sock, (struct sockaddr *)&sa->sin6, sizeof(sa->sin6) ) == 0 ) return 1; diff --git a/src/httplib_cry.c b/src/httplib_cry.c index 81531a33..b1708877 100644 --- a/src/httplib_cry.c +++ b/src/httplib_cry.c @@ -29,13 +29,13 @@ #include "httplib_ssl.h" /* - * void httplib_cry( const struct httplib_connection *conn, const char *fmt, ... ); + * void httplib_cry( 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. */ -void httplib_cry( const struct httplib_connection *conn, const char *fmt, ... ) { +void httplib_cry( 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]; @@ -48,7 +48,7 @@ void httplib_cry( const struct httplib_connection *conn, const char *fmt, ... ) va_end( ap ); buf[sizeof(buf)-1] = 0; - if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn == NULL || ctx == NULL ) return; /* * Do not lock when getting the callback value, here and below. @@ -56,11 +56,11 @@ void httplib_cry( const struct httplib_connection *conn, const char *fmt, ... ) * same way string option can. */ - if ( conn->ctx->callbacks.log_message == NULL || conn->ctx->callbacks.log_message( conn, buf ) == 0 ) { + if ( ctx->callbacks.log_message == NULL || ctx->callbacks.log_message( conn, buf ) == 0 ) { - if ( conn->ctx->cfg[ERROR_LOG_FILE] != NULL ) { + if ( ctx->cfg[ERROR_LOG_FILE] != NULL ) { - if ( XX_httplib_fopen( conn, conn->ctx->cfg[ERROR_LOG_FILE], "a+", &fi ) == 0 ) fi.fp = NULL; + if ( XX_httplib_fopen( conn, ctx->cfg[ERROR_LOG_FILE], "a+", &fi ) == 0 ) fi.fp = NULL; } else fi.fp = NULL; diff --git a/src/httplib_fc.c b/src/httplib_fc.c deleted file mode 100644 index 3867d3c9..00000000 --- a/src/httplib_fc.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2016 Lammert Bies - * Copyright (c) 2013-2016 the Civetweb developers - * Copyright (c) 2004-2013 Sergey Lyubka - * - * 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. - * - * ============ - * Release: 2.0 - */ - -#include "httplib_main.h" - -/* - * struct httplib_connection *XX_httplib_fc( struct httplib_context *ctx ); - * - * The function XX_httplib_fc() returns a fake connection structure specific - * for logging if a connection is not applicable at the moment of logging. - */ - -struct httplib_connection *XX_httplib_fc( struct httplib_context *ctx ) { - - static struct httplib_connection fake_connection; - - fake_connection.ctx = ctx; - return & fake_connection; - -} /* XX_httplib_fc */ diff --git a/src/httplib_fclose_on_exec.c b/src/httplib_fclose_on_exec.c index 7aaf4fc0..c0401c71 100644 --- a/src/httplib_fclose_on_exec.c +++ b/src/httplib_fclose_on_exec.c @@ -36,7 +36,7 @@ void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *c #else if ( fcntl( fileno( filep->fp ), F_SETFD, FD_CLOEXEC) != 0 ) { - httplib_cry( conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO) ); + if ( conn != NULL && conn->ctx != NULL ) httplib_cry( conn->ctx, conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO) ); } #endif diff --git a/src/httplib_get_response_code_text.c b/src/httplib_get_response_code_text.c index 829d106c..f98a45ad 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 ) httplib_cry( conn, "Unknown HTTP response code: %u", response_code ); + if ( conn != NULL && conn->ctx != NULL ) httplib_cry( 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 506d5c0d..6490f2e5 100644 --- a/src/httplib_handle_cgi_request.c +++ b/src/httplib_handle_cgi_request.c @@ -63,7 +63,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char struct file fout = STRUCT_FILE_INITIALIZER; pid_t pid = (pid_t)-1; - if ( conn == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; in = NULL; out = NULL; @@ -84,7 +84,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( truncated ) { - httplib_cry( conn, "Error: CGI program \"%s\": Path too long", prog ); + httplib_cry( 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; @@ -105,7 +105,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( pipe(fdin) != 0 || pipe(fdout) != 0 || pipe(fderr) != 0 ) { status = strerror( ERRNO ); - httplib_cry( conn, "Error: CGI program \"%s\": Can not create CGI pipes: %s", prog, status ); + httplib_cry( 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; @@ -116,7 +116,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( pid == (pid_t)-1 ) { status = strerror(ERRNO); - httplib_cry( conn, "Error: CGI program \"%s\": Can not spawn CGI process: %s", prog, status ); + httplib_cry( 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; @@ -126,12 +126,12 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char * Make sure child closes all pipe descriptors. It must dup them to 0,1 */ - XX_httplib_set_close_on_exec( (SOCKET)fdin[0], conn ); /* stdin read */ - XX_httplib_set_close_on_exec( (SOCKET)fdout[1], conn ); /* stdout write */ - XX_httplib_set_close_on_exec( (SOCKET)fderr[1], conn ); /* stderr write */ - XX_httplib_set_close_on_exec( (SOCKET)fdin[1], conn ); /* stdin write */ - XX_httplib_set_close_on_exec( (SOCKET)fdout[0], conn ); /* stdout read */ - XX_httplib_set_close_on_exec( (SOCKET)fderr[0], conn ); /* stderr read */ + XX_httplib_set_close_on_exec( (SOCKET)fdin[0], conn->ctx ); /* stdin read */ + XX_httplib_set_close_on_exec( (SOCKET)fdout[1], conn->ctx ); /* stdout write */ + XX_httplib_set_close_on_exec( (SOCKET)fderr[1], conn->ctx ); /* stderr write */ + XX_httplib_set_close_on_exec( (SOCKET)fdin[1], conn->ctx ); /* stdin write */ + XX_httplib_set_close_on_exec( (SOCKET)fdout[0], conn->ctx ); /* stdout read */ + XX_httplib_set_close_on_exec( (SOCKET)fderr[0], conn->ctx ); /* stderr read */ /* * Parent closes only one side of the pipes. @@ -151,7 +151,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( (in = fdopen( fdin[1], "wb" )) == NULL ) { status = strerror(ERRNO); - httplib_cry( conn, "Error: CGI program \"%s\": Can not open stdin: %s", prog, status ); + httplib_cry( 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; @@ -160,7 +160,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( (out = fdopen( fdout[0], "rb" )) == NULL ) { status = strerror(ERRNO); - httplib_cry( conn, "Error: CGI program \"%s\": Can not open stdout: %s", prog, status ); + httplib_cry( 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; @@ -169,7 +169,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( (err = fdopen( fderr[0], "rb" )) == NULL ) { status = strerror(ERRNO); - httplib_cry( conn, "Error: CGI program \"%s\": Can not open stderr: %s", prog, status ); + httplib_cry( 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; @@ -193,7 +193,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char * Error sending the body data */ - httplib_cry( conn, "Error: CGI program \"%s\": Forward body data failed", prog ); + httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Forward body data failed", prog ); goto done; } @@ -221,7 +221,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, "Error: CGI program \"%s\": Not enough memory for buffer (%u " "bytes)", prog, (unsigned int)buflen ); + httplib_cry( conn->ctx, conn, "Error: CGI program \"%s\": Not enough memory for buffer (%u " "bytes)", prog, (unsigned int)buflen ); goto done; } @@ -238,12 +238,12 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char if ( i > 0 ) { - httplib_cry( conn, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf ); + httplib_cry( 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, "Error: CGI program sent malformed or too big " "(>%u bytes) HTTP headers: [%.*s]", (unsigned)buflen, data_len, buf ); + httplib_cry( 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 57b208e1..9d394a6c 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, "%s: Cannot decode filename", __func__ ); + httplib_cry( 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, "%s: Function \"Get\" not available", __func__ ); + httplib_cry( 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, "%s: Function \"Store\" not available", __func__ ); + httplib_cry( 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, "%s: Not enough memory (required: %lu)", __func__, (unsigned long)(value_len + 1)); + httplib_cry( conn->ctx, conn, "%s: Not enough memory (required: %lu)", __func__, (unsigned long)(value_len + 1)); return FORM_FIELD_STORAGE_ABORT; } @@ -283,7 +283,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, "%s: Cannot write file %s", __func__, path ); + httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); fclose( fstore.fp ); fstore.fp = NULL; XX_httplib_remove_bad_file( conn, path ); @@ -303,14 +303,14 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ } else { - httplib_cry( conn, "%s: Error saving file %s", __func__, path ); + httplib_cry( conn->ctx, conn, "%s: Error saving file %s", __func__, path ); XX_httplib_remove_bad_file( conn, path ); } fstore.fp = NULL; } - } else httplib_cry( conn, "%s: Cannot create file %s", __func__, path ); + } else httplib_cry( conn->ctx, conn, "%s: Cannot create file %s", __func__, path ); } /* @@ -411,7 +411,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, "%s: Cannot create file %s", __func__, path); + if (!fstore.fp) httplib_cry( conn->ctx, conn, "%s: Cannot create file %s", __func__, path); } get_block = 0; @@ -456,7 +456,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, "%s: Cannot write file %s", __func__, path); + httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; XX_httplib_remove_bad_file(conn, path); @@ -507,7 +507,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ } else { - httplib_cry( conn, "%s: Error saving file %s", __func__, path ); + httplib_cry( conn->ctx, conn, "%s: Error saving file %s", __func__, path ); XX_httplib_remove_bad_file( conn, path ); } @@ -709,7 +709,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, "%s: Cannot create file %s", __func__, path ); + if ( ! fstore.fp ) httplib_cry( conn->ctx, conn, "%s: Cannot create file %s", __func__, path ); } get_block = 0; @@ -753,7 +753,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if ( n != towrite || ferror( fstore.fp ) ) { - httplib_cry( conn, "%s: Cannot write file %s", __func__, path ); + httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); fclose( fstore.fp ); fstore.fp = NULL; XX_httplib_remove_bad_file( conn, path ); @@ -809,7 +809,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ if ( n != towrite || ferror( fstore.fp ) ) { - httplib_cry( conn, "%s: Cannot write file %s", __func__, path ); + httplib_cry( conn->ctx, conn, "%s: Cannot write file %s", __func__, path ); fclose( fstore.fp ); fstore.fp = NULL; XX_httplib_remove_bad_file( conn, path ); @@ -833,7 +833,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_ } else { - httplib_cry( conn, "%s: Error saving file %s", __func__, path ); + httplib_cry( 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 4b81c738..7512aaed 100644 --- a/src/httplib_handle_request.c +++ b/src/httplib_handle_request.c @@ -118,7 +118,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, "Can not redirect to SSL, no SSL port available" ); + httplib_cry( 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 b16960ca..6a544ae8 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( XX_httplib_fc(ctx), "%s: cannot allocate mutexes: %s", __func__, XX_httplib_ssl_error() ); + httplib_cry( ctx, NULL, "%s: cannot allocate mutexes: %s", __func__, XX_httplib_ssl_error() ); return 0; } diff --git a/src/httplib_is_file_in_memory.c b/src/httplib_is_file_in_memory.c index dd8ce198..e9ac8f3f 100644 --- a/src/httplib_is_file_in_memory.c +++ b/src/httplib_is_file_in_memory.c @@ -38,7 +38,7 @@ bool XX_httplib_is_file_in_memory( const struct httplib_connection *conn, const size_t size; - if ( conn == NULL || filep == NULL ) return false; + if ( conn == NULL || conn->ctx == NULL || filep == NULL ) return false; size = 0; diff --git a/src/httplib_load_dll.c b/src/httplib_load_dll.c index 26b30b55..9cd6ea46 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( XX_httplib_fc(ctx), "%s: cannot load %s", __func__, dll_name ); + httplib_cry( 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( XX_httplib_fc(ctx), "%s: %s: cannot find %s", __func__, dll_name, fp->name ); + httplib_cry( ctx, NULL, "%s: %s: cannot find %s", __func__, dll_name, fp->name ); dlclose( dll_handle ); return NULL; diff --git a/src/httplib_main.h b/src/httplib_main.h index 74fd2717..0256f06d 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -799,7 +799,6 @@ int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, void XX_httplib_delete_file( struct httplib_connection *conn, const char *path ); void XX_httplib_dir_scan_callback( struct de *de, void *data ); void XX_httplib_discard_unread_request_data( struct httplib_connection *conn ); -struct httplib_connection * XX_httplib_fc( struct httplib_context *ctx ); int XX_httplib_fclose( struct file *filep ); void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *conn ); const char * XX_httplib_fgets( char *buf, size_t size, struct file *filep, char **p ); @@ -877,7 +876,7 @@ void XX_httplib_send_options( struct httplib_connection *conn ); int XX_httplib_send_static_cache_header( struct httplib_connection *conn ); int XX_httplib_send_websocket_handshake( struct httplib_connection *conn, const char *websock_key ); int XX_httplib_set_acl_option( struct httplib_context *ctx ); -void XX_httplib_set_close_on_exec( SOCKET sock, struct httplib_connection *conn ); +void XX_httplib_set_close_on_exec( SOCKET sock, const struct httplib_context *ctx ); int XX_httplib_set_gpass_option( struct httplib_context *ctx ); void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, int handler_type, int is_delete_request, httplib_request_handler handler, httplib_websocket_connect_handler connect_handler, httplib_websocket_ready_handler ready_handler, httplib_websocket_data_handler data_handler, httplib_websocket_close_handler close_handler, httplib_authorization_handler auth_handler, void *cbdata ); int XX_httplib_set_non_blocking_mode( SOCKET sock ); diff --git a/src/httplib_mkcol.c b/src/httplib_mkcol.c index e1bde5fa..a6a58fac 100644 --- a/src/httplib_mkcol.c +++ b/src/httplib_mkcol.c @@ -45,7 +45,7 @@ void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { char date[64]; time_t curtime; - if ( conn == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; curtime = time( NULL ); @@ -57,7 +57,7 @@ void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { if ( ! XX_httplib_stat( conn, path, & de.file ) ) { - httplib_cry( conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO) ); } if ( de.file.last_modified ) { diff --git a/src/httplib_prepare_cgi_environment.c b/src/httplib_prepare_cgi_environment.c index 27cab19f..09fade4a 100644 --- a/src/httplib_prepare_cgi_environment.c +++ b/src/httplib_prepare_cgi_environment.c @@ -49,7 +49,7 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const int i; bool truncated; - if ( conn == NULL || prog == NULL || env == NULL ) return; + if ( conn == NULL || conn->ctx == NULL || prog == NULL || env == NULL ) return; env->conn = conn; env->buflen = CGI_ENVIRONMENT_SIZE; @@ -139,7 +139,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, "%s: HTTP header variable too long [%s]", __func__, conn->request_info.http_headers[i].name ); + httplib_cry( 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 ee190134..300ac5e7 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, "%s: cannot open authorization file: %s", __func__, workdata->buf ); + else httplib_cry( 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, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); + httplib_cry( 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, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); + httplib_cry( 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, "%s: syntax error in authorization file: %s", __func__, workdata->buf ); + httplib_cry( 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 322dcc37..1652da01 100644 --- a/src/httplib_read_websocket.c +++ b/src/httplib_read_websocket.c @@ -140,7 +140,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock * connection */ - httplib_cry( conn, "websocket out of memory; closing connection" ); + httplib_cry( conn->ctx, conn, "websocket out of memory; closing connection" ); break; } } @@ -185,7 +185,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock if (error) { - httplib_cry(conn, "Websocket pull failed; closing connection"); + httplib_cry( conn->ctx, conn, "Websocket pull failed; closing connection" ); break; } diff --git a/src/httplib_refresh_trust.c b/src/httplib_refresh_trust.c index 93e7b786..8059c6b8 100644 --- a/src/httplib_refresh_trust.c +++ b/src/httplib_refresh_trust.c @@ -49,9 +49,11 @@ int XX_httplib_refresh_trust( struct httplib_connection *conn ) { char *pem; int should_verify_peer; - p_reload_lock = & reload_lock; + if ( conn == NULL || conn->ctx == NULL ) return 0; + + p_reload_lock = & reload_lock; + pem = conn->ctx->cfg[SSL_CERTIFICATE]; - pem = conn->ctx->cfg[SSL_CERTIFICATE]; if ( pem == NULL && conn->ctx->callbacks.init_ssl == NULL ) return 0; if ( stat( pem, &cert_buf ) != -1 ) t = (long int)cert_buf.st_mtime; @@ -67,7 +69,7 @@ int XX_httplib_refresh_trust( struct httplib_connection *conn ) { if ( SSL_CTX_load_verify_locations( conn->ctx->ssl_ctx, conn->ctx->cfg[SSL_CA_FILE], conn->ctx->cfg[SSL_CA_PATH] ) != 1 ) { - httplib_cry( XX_httplib_fc(conn->ctx), + httplib_cry( 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 bc0f8409..24ef0b14 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 ) httplib_cry( conn, "%s: Cannot remove invalid file %s", __func__, path ); + if ( r != 0 && conn != NULL && conn->ctx != NULL ) httplib_cry( 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 6138d272..3e2af178 100644 --- a/src/httplib_remove_directory.c +++ b/src/httplib_remove_directory.c @@ -84,7 +84,7 @@ int XX_httplib_remove_directory( struct httplib_connection *conn, const char *di if ( ! XX_httplib_stat( conn, path, & de.file ) ) { - httplib_cry( conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO) ); ok = 0; } if ( de.file.membuf == NULL ) { diff --git a/src/httplib_scan_directory.c b/src/httplib_scan_directory.c index c4f292ad..695de5e9 100644 --- a/src/httplib_scan_directory.c +++ b/src/httplib_scan_directory.c @@ -65,7 +65,7 @@ int XX_httplib_scan_directory( struct httplib_connection *conn, const char *dir, if ( ! XX_httplib_stat( conn, path, &de.file ) ) { - httplib_cry( conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO) ); } de.file_name = dp->d_name; diff --git a/src/httplib_send_file_data.c b/src/httplib_send_file_data.c index d8ed26fa..59d2e0c8 100644 --- a/src/httplib_send_file_data.c +++ b/src/httplib_send_file_data.c @@ -49,7 +49,7 @@ void XX_httplib_send_file_data( struct httplib_connection *conn, struct file *fi const char *asc; #endif /* __linux__ */ - if ( filep == NULL || conn == NULL ) return; + if ( filep == NULL || conn == NULL || conn->ctx == NULL ) return; /* * Sanity check the offset @@ -142,7 +142,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, "%s: fseeko() failed: %s", __func__, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO) ); 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 2f0e3e14..a2e4818c 100644 --- a/src/httplib_set_close_on_exec.c +++ b/src/httplib_set_close_on_exec.c @@ -27,36 +27,25 @@ #include "httplib_main.h" -#if defined(_WIN32) - /* * conn parameter may be NULL */ -void XX_httplib_set_close_on_exec( SOCKET sock, struct httplib_connection *conn ) { +void XX_httplib_set_close_on_exec( SOCKET fd, const struct httplib_context *ctx ) { + +#if defined(_WIN32) + + UNUSED_PARAMETER(ctx); - UNUSED_PARAMETER(conn); -#if defined(_WIN32_WCE) -#else SetHandleInformation( (HANDLE)(intptr_t)sock, HANDLE_FLAG_INHERIT, 0 ); -#endif -} /* XX_httplib_set_close_on_exec */ - - -#else - -/* - * conn may be NULL - */ - -void XX_httplib_set_close_on_exec( SOCKET fd, struct httplib_connection *conn ) { +#else /* _WIN32 */ if ( fcntl( fd, F_SETFD, FD_CLOEXEC ) != 0 ) { - if ( conn != NULL ) httplib_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO) ); + if ( ctx != NULL ) httplib_cry( ctx, NULL, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO) ); } -} /* XX_httplib_set_close_on_exec */ +#endif /* _WIN32 */ -#endif /* _WIN32 */ +} /* XX_httplib_set_close_on_exec */ diff --git a/src/httplib_set_gpass_option.c b/src/httplib_set_gpass_option.c index 05d18c3f..4775925d 100644 --- a/src/httplib_set_gpass_option.c +++ b/src/httplib_set_gpass_option.c @@ -43,9 +43,9 @@ int XX_httplib_set_gpass_option( struct httplib_context *ctx ) { path = ctx->cfg[GLOBAL_PASSWORDS_FILE]; - if ( path != NULL && ! XX_httplib_stat( XX_httplib_fc(ctx), path, &file ) ) { + if ( path != NULL && ! XX_httplib_stat( NULL, path, &file ) ) { - httplib_cry( XX_httplib_fc(ctx), "Cannot open %s: %s", path, strerror(ERRNO) ); + httplib_cry( ctx, NULL, "Cannot open %s: %s", path, strerror(ERRNO) ); return 0; } return 1; diff --git a/src/httplib_set_handler_type.c b/src/httplib_set_handler_type.c index b97cdf40..f2d85021 100644 --- a/src/httplib_set_handler_type.c +++ b/src/httplib_set_handler_type.c @@ -139,7 +139,7 @@ void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, if ( tmp_rh == NULL ) { httplib_unlock_context( ctx ); - httplib_cry( XX_httplib_fc(ctx), "%s", "Cannot create new request handler struct, OOM" ); + httplib_cry( ctx, NULL, "%s", "Cannot create new request handler struct, OOM" ); return; } @@ -150,7 +150,7 @@ void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, httplib_unlock_context( ctx ); httplib_free( tmp_rh ); - httplib_cry( XX_httplib_fc(ctx), "%s", "Cannot create new request handler struct, OOM" ); + httplib_cry( 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 4b99f397..6836399f 100644 --- a/src/httplib_set_ports_option.c +++ b/src/httplib_set_ports_option.c @@ -72,7 +72,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( ! parse_port_string( & vec, & so, & ip_version ) ) { - httplib_cry( XX_httplib_fc(ctx), + httplib_cry( ctx, NULL, "%.*s: invalid port spec (entry %i). Expecting list of: %s", (int)vec.len, vec.ptr, @@ -84,14 +84,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( XX_httplib_fc(ctx), "Cannot add SSL socket (entry %i). Is -ssl_certificate option set?", ports_total ); + httplib_cry( 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( XX_httplib_fc(ctx), "cannot create socket (entry %i)", ports_total ); + httplib_cry( ctx, NULL, "cannot create socket (entry %i)", ports_total ); continue; } @@ -115,7 +115,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { * Set reuse option, but don't abort on errors. */ - httplib_cry( XX_httplib_fc(ctx), "cannot set socket option SO_EXCLUSIVEADDRUSE (entry %i)", ports_total ); + httplib_cry( 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 ) { @@ -124,7 +124,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { * Set reuse option, but don't abort on errors. */ - httplib_cry( XX_httplib_fc(ctx), "cannot set socket option SO_REUSEADDR (entry %i)", ports_total ); + httplib_cry( ctx, NULL, "cannot set socket option SO_REUSEADDR (entry %i)", ports_total ); } #endif /* _WIN32 */ @@ -138,7 +138,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { * Set IPv6 only option, but don't abort on errors. */ - httplib_cry( XX_httplib_fc(ctx), "cannot set socket option IPV6_V6ONLY (entry %i)", ports_total ); + httplib_cry( ctx, NULL, "cannot set socket option IPV6_V6ONLY (entry %i)", ports_total ); } } } @@ -149,7 +149,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( bind( so.sock, &so.lsa.sa, len ) != 0 ) { - httplib_cry( XX_httplib_fc(ctx), "cannot bind to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno) ); + httplib_cry( ctx, NULL, "cannot bind to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -162,7 +162,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( bind( so.sock, &so.lsa.sa, len ) != 0 ) { - httplib_cry( XX_httplib_fc(ctx), "cannot bind to IPv6 %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno) ); + httplib_cry( ctx, NULL, "cannot bind to IPv6 %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -170,13 +170,13 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { } else { - httplib_cry( XX_httplib_fc(ctx), "cannot bind: address family not supported (entry %i)", ports_total ); + httplib_cry( ctx, NULL, "cannot bind: address family not supported (entry %i)", ports_total ); continue; } if ( listen( so.sock, SOMAXCONN ) != 0 ) { - httplib_cry( XX_httplib_fc(ctx), "cannot listen to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno) ); + httplib_cry( ctx, NULL, "cannot listen to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -185,7 +185,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( XX_httplib_fc(ctx), "call to getsockname failed %.*s: %d (%s)", (int)vec.len, vec.ptr, err, strerror(errno) ); + httplib_cry( ctx, NULL, "call to getsockname failed %.*s: %d (%s)", (int)vec.len, vec.ptr, err, strerror(errno) ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -202,7 +202,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( ptr != NULL ) ctx->listening_sockets = ptr; else { - httplib_cry( XX_httplib_fc(ctx), "%s", "Out of memory" ); + httplib_cry( ctx, NULL, "%s", "Out of memory" ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; @@ -213,14 +213,14 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { if ( pfd != NULL ) ctx->listening_socket_fds = pfd; else { - httplib_cry( XX_httplib_fc(ctx), "%s", "Out of memory" ); + httplib_cry( ctx, NULL, "%s", "Out of memory" ); closesocket( so.sock ); so.sock = INVALID_SOCKET; continue; } - XX_httplib_set_close_on_exec( so.sock, XX_httplib_fc(ctx) ); + XX_httplib_set_close_on_exec( so.sock, ctx ); ctx->listening_sockets[ctx->num_listening_sockets] = so; ctx->num_listening_sockets++; diff --git a/src/httplib_set_ssl_option.c b/src/httplib_set_ssl_option.c index 663487a7..ed44f34a 100644 --- a/src/httplib_set_ssl_option.c +++ b/src/httplib_set_ssl_option.c @@ -78,7 +78,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( XX_httplib_fc(ctx), "SSL_CTX_new (server) error: %s", XX_httplib_ssl_error() ); + httplib_cry( ctx, NULL, "SSL_CTX_new (server) error: %s", XX_httplib_ssl_error() ); return false; } @@ -105,7 +105,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( callback_ret < 0 ) { - httplib_cry( XX_httplib_fc(ctx), "SSL callback returned error: %i", callback_ret ); + httplib_cry( ctx, NULL, "SSL callback returned error: %i", callback_ret ); return false; } @@ -137,7 +137,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( SSL_CTX_load_verify_locations( ctx->ssl_ctx, ctx->cfg[SSL_CA_FILE], ctx->cfg[SSL_CA_PATH] ) != 1 ) { - httplib_cry( XX_httplib_fc(ctx), + httplib_cry( 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 " @@ -152,7 +152,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( use_default_verify_paths && SSL_CTX_set_default_verify_paths( ctx->ssl_ctx ) != 1 ) { - httplib_cry( XX_httplib_fc(ctx), "SSL_CTX_set_default_verify_paths error: %s", XX_httplib_ssl_error()); + httplib_cry( ctx, NULL, "SSL_CTX_set_default_verify_paths error: %s", XX_httplib_ssl_error()); return false; } @@ -167,7 +167,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( SSL_CTX_set_cipher_list( ctx->ssl_ctx, ctx->cfg[SSL_CIPHER_LIST] ) != 1 ) { - httplib_cry( XX_httplib_fc(ctx), "SSL_CTX_set_cipher_list error: %s", XX_httplib_ssl_error()); + httplib_cry( 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 35421063..2636300b 100644 --- a/src/httplib_set_uid_option.c +++ b/src/httplib_set_uid_option.c @@ -55,10 +55,10 @@ bool XX_httplib_set_uid_option( struct httplib_context *ctx ) { if ( uid == NULL ) return true; - if ( (pw = getpwnam(uid)) == NULL ) httplib_cry( XX_httplib_fc(ctx), "%s: unknown user [%s]", __func__, uid ); - else if ( setgid(pw->pw_gid) == -1 ) httplib_cry( XX_httplib_fc(ctx), "%s: setgid(%s): %s", __func__, uid, strerror(errno) ); - else if ( setgroups(0, NULL) ) httplib_cry( XX_httplib_fc(ctx), "%s: setgroups(): %s", __func__, strerror(errno) ); - else if ( setuid(pw->pw_uid) == -1 ) httplib_cry( XX_httplib_fc(ctx), "%s: setuid(%s): %s", __func__, uid, strerror(errno) ); + 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, strerror(errno) ); + else if ( setgroups(0, NULL) ) httplib_cry( ctx, NULL, "%s: setgroups(): %s", __func__, strerror(errno) ); + else if ( setuid(pw->pw_uid) == -1 ) httplib_cry( ctx, NULL, "%s: setuid(%s): %s", __func__, uid, strerror(errno) ); else return true; return false; diff --git a/src/httplib_spawn_process.c b/src/httplib_spawn_process.c index fbdc3470..c6d7ca84 100644 --- a/src/httplib_spawn_process.c +++ b/src/httplib_spawn_process.c @@ -131,7 +131,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, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); + httplib_cry( conn->ctx, conn, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); pi.hProcess = (pid_t)-1; /* @@ -162,7 +162,7 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro UNUSED_PARAMETER(envblk); - if ( conn == NULL ) return 0; + if ( conn == NULL || conn->ctx == NULL ) return 0; if ( (pid = fork()) == -1 ) { @@ -179,10 +179,10 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro * Child */ - if ( chdir( dir ) != 0 ) httplib_cry( conn, "%s: chdir(%s): %s", __func__, dir, strerror(ERRNO) ); - else if ( dup2( fdin[0], 0 ) == -1 ) httplib_cry( conn, "%s: dup2(%d, 0): %s", __func__, fdin[0], strerror(ERRNO) ); - else if ( dup2( fdout[1], 1 ) == -1 ) httplib_cry( conn, "%s: dup2(%d, 1): %s", __func__, fdout[1], strerror(ERRNO) ); - else if ( dup2( fderr[1], 2 ) == -1 ) httplib_cry( conn, "%s: dup2(%d, 2): %s", __func__, fderr[1], strerror(ERRNO) ); + if ( chdir( dir ) != 0 ) httplib_cry( conn->ctx, conn, "%s: chdir(%s): %s", __func__, dir, strerror(ERRNO) ); + else if ( dup2( fdin[0], 0 ) == -1 ) httplib_cry( conn->ctx, conn, "%s: dup2(%d, 0): %s", __func__, fdin[0], strerror(ERRNO) ); + else if ( dup2( fdout[1], 1 ) == -1 ) httplib_cry( conn->ctx, conn, "%s: dup2(%d, 1): %s", __func__, fdout[1], strerror(ERRNO) ); + else if ( dup2( fderr[1], 2 ) == -1 ) httplib_cry( conn->ctx, conn, "%s: dup2(%d, 2): %s", __func__, fderr[1], strerror(ERRNO) ); else { /* * Keep stderr and stdout in two different pipes. @@ -217,12 +217,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, "%s: execle(%s): %s", __func__, prog, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: execle(%s): %s", __func__, prog, strerror(ERRNO) ); } else { execle( interp, interp, prog, NULL, envp ); - httplib_cry( conn, "%s: execle(%s %s): %s", __func__, interp, prog, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "%s: execle(%s %s): %s", __func__, interp, prog, strerror(ERRNO) ); } } diff --git a/src/httplib_ssi.c b/src/httplib_ssi.c index 044d602b..ad027e6d 100644 --- a/src/httplib_ssi.c +++ b/src/httplib_ssi.c @@ -43,7 +43,7 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha size_t len; bool truncated; - if ( conn == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; truncated = false; @@ -98,19 +98,19 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha } else { - httplib_cry( conn, "Bad SSI #include: [%s]", tag ); + httplib_cry( conn->ctx, conn, "Bad SSI #include: [%s]", tag ); return; } if ( truncated ) { - httplib_cry( conn, "SSI #include path length overflow: [%s]", tag ); + httplib_cry( conn->ctx, conn, "SSI #include path length overflow: [%s]", tag ); return; } if ( ! XX_httplib_fopen( conn, path, "rb", &file ) ) { - httplib_cry( conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, strerror(ERRNO) ); return; } @@ -134,14 +134,14 @@ static void do_ssi_exec( struct httplib_connection *conn, char *tag ) { if ( sscanf(tag, " \"%1023[^\"]\"", cmd) != 1 ) { - httplib_cry( conn, "Bad SSI #exec: [%s]", tag ); + httplib_cry( conn->ctx, conn, "Bad SSI #exec: [%s]", tag ); } else { cmd[1023] = 0; if ( (file.fp = popen( cmd, "r" ) ) == NULL ) { - httplib_cry( conn, "Cannot SSI #exec: [%s]: %s", cmd, strerror(ERRNO) ); + httplib_cry( conn->ctx, conn, "Cannot SSI #exec: [%s]: %s", cmd, strerror(ERRNO) ); } else { @@ -174,7 +174,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st if ( include_level > 10 ) { - httplib_cry( conn, "SSI #include level is too deep (%s)", path ); + httplib_cry( conn->ctx, conn, "SSI #include level is too deep (%s)", path ); return; } @@ -212,7 +212,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, "%s: unknown SSI " "command: \"%s\"", path, buf ); + else httplib_cry( conn->ctx, conn, "%s: unknown SSI " "command: \"%s\"", path, buf ); } len = 0; @@ -231,7 +231,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st else if ( len == (int)sizeof(buf) - 2 ) { - httplib_cry( conn, "%s: SSI tag is too large", path ); + httplib_cry( 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 6ec42afc..cc877df8 100644 --- a/src/httplib_ssl_use_pem_file.c +++ b/src/httplib_ssl_use_pem_file.c @@ -39,9 +39,11 @@ int XX_httplib_ssl_use_pem_file( struct httplib_context *ctx, const char *pem ) { + if ( ctx == NULL || pem == NULL ) return 0; + if ( SSL_CTX_use_certificate_file( ctx->ssl_ctx, pem, 1 ) == 0 ) { - httplib_cry( XX_httplib_fc(ctx), "%s: cannot open certificate file %s: %s", __func__, pem, XX_httplib_ssl_error() ); + httplib_cry( ctx, NULL, "%s: cannot open certificate file %s: %s", __func__, pem, XX_httplib_ssl_error() ); return 0; } @@ -51,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( XX_httplib_fc(ctx), "%s: cannot open private key file %s: %s", __func__, pem, XX_httplib_ssl_error() ); + httplib_cry( 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( XX_httplib_fc(ctx), "%s: certificate and private key do not match: %s", __func__, pem ); + httplib_cry( 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( XX_httplib_fc(ctx), "%s: cannot use certificate chain file %s: %s", __func__, pem, XX_httplib_ssl_error() ); + httplib_cry( 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 e9dbb4a2..9934f821 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -84,7 +84,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks */ httplib_atomic_dec( & XX_httplib_sTlsInit ); - httplib_cry( XX_httplib_fc(ctx), "Cannot initialize thread local storage" ); + httplib_cry( ctx, NULL, "Cannot initialize thread local storage" ); httplib_free( ctx ); return NULL; @@ -129,7 +129,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks if ( ctx->cfg[idx] != NULL ) { - httplib_cry( XX_httplib_fc(ctx), "warning: %s: duplicate option", name ); + httplib_cry( ctx, NULL, "warning: %s: duplicate option", name ); httplib_free( ctx->cfg[idx] ); } @@ -246,7 +246,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks if ( wta != NULL ) { httplib_free( wta ); wta = NULL; } - if ( i > 0 ) httplib_cry( XX_httplib_fc( ctx ), "Cannot start worker thread %i: error %ld", i + 1, (long)ERRNO ); + if ( i > 0 ) httplib_cry( ctx, NULL, "Cannot start worker thread %i: error %ld", i + 1, (long)ERRNO ); else return cleanup( ctx, "Cannot create threads: error %ld", (long)ERRNO ); @@ -280,7 +280,7 @@ static struct httplib_context *cleanup( struct httplib_context *ctx, const char va_end( ap ); buf[sizeof(buf)-1] = 0; - httplib_cry( XX_httplib_fc( ctx ), "%s", buf ); + httplib_cry(ctx, NULL, "%s", buf ); if ( ctx != NULL ) XX_httplib_free_context( ctx ); httplib_pthread_setspecific( XX_httplib_sTlsKey, NULL ); diff --git a/src/httplib_store_body.c b/src/httplib_store_body.c index de58d8d9..b321c55c 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, "%s: Contents already consumed", __func__ ); + httplib_cry( conn->ctx, conn, "%s: Contents already consumed", __func__ ); return -11; } diff --git a/src/httplib_vsnprintf.c b/src/httplib_vsnprintf.c index 2b3f1c42..201fe42d 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; - httplib_cry( conn, "truncating vsnprintf buffer: [%.*s]", (int)((buflen > 200) ? 200 : (buflen - 1)), buf ); + if ( conn != NULL && conn->ctx != NULL ) httplib_cry( 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 d0e60b97..61efa2bb 100644 --- a/src/httplib_websocket_client_write.c +++ b/src/httplib_websocket_client_write.c @@ -44,13 +44,15 @@ int httplib_websocket_client_write( struct httplib_connection *conn, int opcode, char *masked_data; uint32_t masking_key; + if ( conn == NULL || conn->ctx == NULL ) return -1; + retval = -1; masked_data = httplib_malloc( ((dataLen + 7) / 4) * 4 ); masking_key = (uint32_t) httplib_get_random(); if ( masked_data == NULL ) { - httplib_cry( conn, "Cannot allocate buffer for masked websocket response: Out of memory" ); + httplib_cry( 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 604cb128..cea68b75 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( XX_httplib_fc(ctx), "%s", "Cannot create new connection struct, OOM" ); + if ( conn == NULL ) httplib_cry( ctx, NULL, "%s", "Cannot create new connection struct, OOM" ); else { httplib_pthread_setspecific( XX_httplib_sTlsKey, &tls );