1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-06 05:02:40 +03:00

Removed httplib_fc fake context

This commit is contained in:
Lammert Bies
2016-12-26 19:25:35 +01:00
parent f2a03be9db
commit 8118c908eb
44 changed files with 156 additions and 221 deletions

View File

@@ -199,7 +199,6 @@ OBJLIST = \
${OBJDIR}httplib_discard_unread_request_data${OBJEXT} \ ${OBJDIR}httplib_discard_unread_request_data${OBJEXT} \
${OBJDIR}httplib_download${OBJEXT} \ ${OBJDIR}httplib_download${OBJEXT} \
${OBJDIR}httplib_event_queue${OBJEXT} \ ${OBJDIR}httplib_event_queue${OBJEXT} \
${OBJDIR}httplib_fc${OBJEXT} \
${OBJDIR}httplib_fclose${OBJEXT} \ ${OBJDIR}httplib_fclose${OBJEXT} \
${OBJDIR}httplib_fclose_on_exec${OBJEXT} \ ${OBJDIR}httplib_fclose_on_exec${OBJEXT} \
${OBJDIR}httplib_fgets${OBJEXT} \ ${OBJDIR}httplib_fgets${OBJEXT} \
@@ -560,10 +559,6 @@ ${OBJDIR}httplib_event_queue${OBJEXT} : ${SRCDIR}httplib_event_queue.c \
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.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 \ ${OBJDIR}httplib_fclose${OBJEXT} : ${SRCDIR}httplib_fclose.c \
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h

View File

@@ -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); 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 /* httplib_request_handler
Called when a new request comes in. This callback is URI based 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], ...); 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. */ /* 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_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_base64_encode( const unsigned char *src, int src_len, char *dst, int dst_len );
LIBHTTP_API int httplib_closedir( DIR *dir ); 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 uint64_t httplib_get_random( void );
LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn ); LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn );
LIBHTTP_API int httplib_kill( pid_t pid, int sig_num ); 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_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_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_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 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 const char * httplib_strcasestr( const char *big_str, const char *small_str );
LIBHTTP_API char * httplib_strdup( const char *str ); LIBHTTP_API char * httplib_strdup( const char *str );

View File

@@ -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 )) ) { 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 ); 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 ); closesocket( so.sock );
so.sock = INVALID_SOCKET; 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 * 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_ssl = listener->has_ssl;
so.has_redir = listener->has_redir; so.has_redir = listener->has_redir;
if ( getsockname( so.sock, &so.lsa.sa, &len ) != 0 ) { 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 ) { 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 ) { 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) );
} }
} }

View File

@@ -36,6 +36,10 @@
* a CGI script is called. The environment variable has the form * a CGI script is called. The environment variable has the form
* VARIABLE=VALUE\0 an is appended to the buffer. This function assumes that * VARIABLE=VALUE\0 an is appended to the buffer. This function assumes that
* env != NULL and also fmt != NULL. * 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) #if !defined(NO_CGI)
@@ -48,6 +52,8 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) {
char *added; char *added;
va_list ap; va_list ap;
if ( env == NULL || env->conn == NULL || env->conn->ctx == NULL ) return;
/* /*
* Calculate how much space is left in the buffer * 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 * 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; return;
} }
@@ -127,7 +133,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) {
if ( space < 2 ) { 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; return;
} }

View File

@@ -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. * 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))) #elif defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 0)))

View File

@@ -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 ) { 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; return -1;
} }

View File

@@ -53,7 +53,7 @@ bool XX_httplib_check_authorization( struct httplib_connection *conn, const char
if ( truncated || ! XX_httplib_fopen( conn, fname, "r", &file ) ) { 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; break;
} }

View File

@@ -44,7 +44,7 @@ void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ) {
int error_code; int error_code;
socklen_t opt_len; socklen_t opt_len;
if ( conn == NULL ) return; if ( conn == NULL || conn->ctx == NULL ) return;
error_code = 0; error_code = 0;
opt_len = sizeof(error_code); opt_len = sizeof(error_code);
@@ -66,7 +66,7 @@ void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ) {
else { else {
if ( setsockopt( conn->client.sock, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger) ) != 0 ) { 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) );
} }
} }

View File

@@ -112,7 +112,7 @@ static struct httplib_connection *httplib_connect_client_impl( const struct http
conn->client.sock = sock; conn->client.sock = sock;
conn->client.lsa = sa; 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; conn->client.has_ssl = (use_ssl) ? true : false;
httplib_pthread_mutex_init( &conn->mutex, &XX_httplib_pthread_mutex_attr ); httplib_pthread_mutex_init( &conn->mutex, &XX_httplib_pthread_mutex_attr );

View File

@@ -121,7 +121,7 @@ int XX_httplib_connect_socket( struct httplib_context *ctx, const char *host, in
return 0; 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 == 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; if ( ip_ver == 6 && connect( *sock, (struct sockaddr *)&sa->sin6, sizeof(sa->sin6) ) == 0 ) return 1;

View File

@@ -29,13 +29,13 @@
#include "httplib_ssl.h" #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 * The function httplib_cry() prints a formatted error message to the opened
* error log stream. * 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 buf[MG_BUF_LEN];
char src_addr[IP_ADDR_STR_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 ); va_end( ap );
buf[sizeof(buf)-1] = 0; 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. * 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. * 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; else fi.fp = NULL;

View File

@@ -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 */

View File

@@ -36,7 +36,7 @@ void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *c
#else #else
if ( fcntl( fileno( filep->fp ), F_SETFD, FD_CLOEXEC) != 0 ) { 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 #endif

View File

@@ -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. * 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. * Return at least a category according to RFC 2616 Section 10.

View File

@@ -63,7 +63,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char
struct file fout = STRUCT_FILE_INITIALIZER; struct file fout = STRUCT_FILE_INITIALIZER;
pid_t pid = (pid_t)-1; pid_t pid = (pid_t)-1;
if ( conn == NULL ) return; if ( conn == NULL || conn->ctx == NULL ) return;
in = NULL; in = NULL;
out = NULL; out = NULL;
@@ -84,7 +84,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char
if ( truncated ) { 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" ); XX_httplib_send_http_error( conn, 500, "Error: %s", "CGI path too long" );
goto done; 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 ) { if ( pipe(fdin) != 0 || pipe(fdout) != 0 || pipe(fderr) != 0 ) {
status = strerror( ERRNO ); 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 ); XX_httplib_send_http_error( conn, 500, "Error: Cannot create CGI pipe: %s", status );
goto done; goto done;
@@ -116,7 +116,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char
if ( pid == (pid_t)-1 ) { if ( pid == (pid_t)-1 ) {
status = strerror(ERRNO); 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 ); XX_httplib_send_http_error( conn, 500, "Error: Cannot spawn CGI process [%s]: %s", prog, status );
goto done; 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 * 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)fdin[0], conn->ctx ); /* stdin read */
XX_httplib_set_close_on_exec( (SOCKET)fdout[1], conn ); /* stdout write */ XX_httplib_set_close_on_exec( (SOCKET)fdout[1], conn->ctx ); /* stdout write */
XX_httplib_set_close_on_exec( (SOCKET)fderr[1], conn ); /* stderr write */ XX_httplib_set_close_on_exec( (SOCKET)fderr[1], conn->ctx ); /* stderr write */
XX_httplib_set_close_on_exec( (SOCKET)fdin[1], conn ); /* stdin write */ XX_httplib_set_close_on_exec( (SOCKET)fdin[1], conn->ctx ); /* stdin write */
XX_httplib_set_close_on_exec( (SOCKET)fdout[0], conn ); /* stdout read */ XX_httplib_set_close_on_exec( (SOCKET)fdout[0], conn->ctx ); /* stdout read */
XX_httplib_set_close_on_exec( (SOCKET)fderr[0], conn ); /* stderr read */ XX_httplib_set_close_on_exec( (SOCKET)fderr[0], conn->ctx ); /* stderr read */
/* /*
* Parent closes only one side of the pipes. * 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 ) { if ( (in = fdopen( fdin[1], "wb" )) == NULL ) {
status = strerror(ERRNO); 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 ); XX_httplib_send_http_error( conn, 500, "Error: CGI can not open fdin\nfopen: %s", status );
goto done; 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 ) { if ( (out = fdopen( fdout[0], "rb" )) == NULL ) {
status = strerror(ERRNO); 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 ); XX_httplib_send_http_error( conn, 500, "Error: CGI can not open fdout\nfopen: %s", status );
goto done; 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 ) { if ( (err = fdopen( fderr[0], "rb" )) == NULL ) {
status = strerror(ERRNO); 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 ); XX_httplib_send_http_error( conn, 500, "Error: CGI can not open fdout\nfopen: %s", status );
goto done; goto done;
@@ -193,7 +193,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char
* Error sending the body data * 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; goto done;
} }
@@ -221,7 +221,7 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char
if ( buf == NULL ) { if ( buf == NULL ) {
XX_httplib_send_http_error( conn, 500, "Error: Not enough memory for CGI buffer (%u bytes)", (unsigned int)buflen ); 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; goto done;
} }
@@ -238,12 +238,12 @@ void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char
if ( i > 0 ) { 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 ); XX_httplib_send_http_error( conn, 500, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf );
} }
else { 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, XX_httplib_send_http_error( conn,
500, 500,

View File

@@ -55,7 +55,7 @@ static int url_encoded_field_found(const struct httplib_connection *conn,
* Log error message and skip this field. * 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; 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 ) { 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; 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 ) { 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; 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. * 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; 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); size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
if ((n != (size_t)vallen) || (ferror(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 ); fclose( fstore.fp );
fstore.fp = NULL; fstore.fp = NULL;
XX_httplib_remove_bad_file( conn, path ); XX_httplib_remove_bad_file( conn, path );
@@ -303,14 +303,14 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
} }
else { 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 ); XX_httplib_remove_bad_file( conn, path );
} }
fstore.fp = NULL; 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; if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL;
file_size = 0; 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; get_block = 0;
@@ -456,7 +456,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
if ( fstore.fp ) { if ( fstore.fp ) {
size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp); size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
if ((n != (size_t)vallen) || (ferror(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); fclose(fstore.fp);
fstore.fp = NULL; fstore.fp = NULL;
XX_httplib_remove_bad_file(conn, path); XX_httplib_remove_bad_file(conn, path);
@@ -507,7 +507,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
} }
else { 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 ); 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; if ( XX_httplib_fopen( conn, path, "wb", &fstore ) == 0 ) fstore.fp = NULL;
file_size = 0; 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; get_block = 0;
@@ -753,7 +753,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
if ( n != towrite || ferror( fstore.fp ) ) { 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 ); fclose( fstore.fp );
fstore.fp = NULL; fstore.fp = NULL;
XX_httplib_remove_bad_file( conn, path ); 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 ) ) { 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 ); fclose( fstore.fp );
fstore.fp = NULL; fstore.fp = NULL;
XX_httplib_remove_bad_file( conn, path ); XX_httplib_remove_bad_file( conn, path );
@@ -833,7 +833,7 @@ int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_
} }
else { 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 ); XX_httplib_remove_bad_file( conn, path );
} }
fstore.fp = NULL; fstore.fp = NULL;

View File

@@ -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" ); 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; return;

View File

@@ -78,7 +78,7 @@ int XX_httplib_initialize_ssl( struct httplib_context *ctx ) {
if ( (XX_httplib_ssl_mutexes = httplib_malloc( size )) == NULL ) { 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; return 0;
} }

View File

@@ -38,7 +38,7 @@ bool XX_httplib_is_file_in_memory( const struct httplib_connection *conn, const
size_t size; size_t size;
if ( conn == NULL || filep == NULL ) return false; if ( conn == NULL || conn->ctx == NULL || filep == NULL ) return false;
size = 0; size = 0;

View File

@@ -82,7 +82,7 @@ void *XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, st
if ( dll_handle == NULL ) { 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; return NULL;
} }
@@ -109,7 +109,7 @@ void *XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, st
if ( u.fp == NULL ) { 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 ); dlclose( dll_handle );
return NULL; return NULL;

View File

@@ -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_delete_file( struct httplib_connection *conn, const char *path );
void XX_httplib_dir_scan_callback( struct de *de, void *data ); void XX_httplib_dir_scan_callback( struct de *de, void *data );
void XX_httplib_discard_unread_request_data( struct httplib_connection *conn ); 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 ); int XX_httplib_fclose( struct file *filep );
void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *conn ); 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 ); 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_static_cache_header( struct httplib_connection *conn );
int XX_httplib_send_websocket_handshake( struct httplib_connection *conn, const char *websock_key ); int XX_httplib_send_websocket_handshake( struct httplib_connection *conn, const char *websock_key );
int XX_httplib_set_acl_option( struct httplib_context *ctx ); 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 ); 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 ); 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 ); int XX_httplib_set_non_blocking_mode( SOCKET sock );

View File

@@ -45,7 +45,7 @@ void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) {
char date[64]; char date[64];
time_t curtime; time_t curtime;
if ( conn == NULL ) return; if ( conn == NULL || conn->ctx == NULL ) return;
curtime = time( NULL ); 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 ) ) { 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 ) { if ( de.file.last_modified ) {

View File

@@ -49,7 +49,7 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const
int i; int i;
bool truncated; 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->conn = conn;
env->buflen = CGI_ENVIRONMENT_SIZE; 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 ); XX_httplib_snprintf( conn, &truncated, http_var_name, sizeof(http_var_name), "HTTP_%s", conn->request_info.http_headers[i].name );
if ( truncated ) { 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; continue;
} }

View File

@@ -91,7 +91,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct
XX_httplib_fclose( &fp ); 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; continue;
} }
@@ -100,7 +100,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct
* future) * 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; continue;
} }
@@ -108,7 +108,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct
if ( workdata->f_domain == NULL ) { 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; continue;
} }
@@ -119,7 +119,7 @@ bool XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct
if ( workdata->f_ha1 == NULL ) { 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; continue;
} }

View File

@@ -140,7 +140,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock
* connection * connection
*/ */
httplib_cry( conn, "websocket out of memory; closing connection" ); httplib_cry( conn->ctx, conn, "websocket out of memory; closing connection" );
break; break;
} }
} }
@@ -185,7 +185,7 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock
if (error) { if (error) {
httplib_cry(conn, "Websocket pull failed; closing connection"); httplib_cry( conn->ctx, conn, "Websocket pull failed; closing connection" );
break; break;
} }

View File

@@ -49,9 +49,11 @@ int XX_httplib_refresh_trust( struct httplib_connection *conn ) {
char *pem; char *pem;
int should_verify_peer; 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 ( pem == NULL && conn->ctx->callbacks.init_ssl == NULL ) return 0;
if ( stat( pem, &cert_buf ) != -1 ) t = (long int)cert_buf.st_mtime; 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 ) { 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_CTX_load_verify_locations error: %s "
"ssl_verify_peer requires setting " "ssl_verify_peer requires setting "
"either ssl_ca_path or ssl_ca_file. Is any of them " "either ssl_ca_path or ssl_ca_file. Is any of them "

View File

@@ -38,6 +38,6 @@ void XX_httplib_remove_bad_file( const struct httplib_connection *conn, const ch
int r = httplib_remove( path ); 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 */ } /* XX_httplib_remove_bad_file */

View File

@@ -84,7 +84,7 @@ int XX_httplib_remove_directory( struct httplib_connection *conn, const char *di
if ( ! XX_httplib_stat( conn, path, & de.file ) ) { 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; ok = 0;
} }
if ( de.file.membuf == NULL ) { if ( de.file.membuf == NULL ) {

View File

@@ -65,7 +65,7 @@ int XX_httplib_scan_directory( struct httplib_connection *conn, const char *dir,
if ( ! XX_httplib_stat( conn, path, &de.file ) ) { 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; de.file_name = dp->d_name;

View File

@@ -49,7 +49,7 @@ void XX_httplib_send_file_data( struct httplib_connection *conn, struct file *fi
const char *asc; const char *asc;
#endif /* __linux__ */ #endif /* __linux__ */
if ( filep == NULL || conn == NULL ) return; if ( filep == NULL || conn == NULL || conn->ctx == NULL ) return;
/* /*
* Sanity check the offset * Sanity check the offset
@@ -142,7 +142,7 @@ void XX_httplib_send_file_data( struct httplib_connection *conn, struct file *fi
#endif #endif
if ( offset > 0 && fseeko( filep->fp, offset, SEEK_SET ) != 0 ) { 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." ); XX_httplib_send_http_error( conn, 500, "%s", "Error: Unable to access file at requested position." );
} }

View File

@@ -27,36 +27,25 @@
#include "httplib_main.h" #include "httplib_main.h"
#if defined(_WIN32)
/* /*
* conn parameter may be NULL * 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 ); SetHandleInformation( (HANDLE)(intptr_t)sock, HANDLE_FLAG_INHERIT, 0 );
#endif
} /* XX_httplib_set_close_on_exec */ #else /* _WIN32 */
#else
/*
* conn may be NULL
*/
void XX_httplib_set_close_on_exec( SOCKET fd, struct httplib_connection *conn ) {
if ( fcntl( fd, F_SETFD, FD_CLOEXEC ) != 0 ) { 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 */

View File

@@ -43,9 +43,9 @@ int XX_httplib_set_gpass_option( struct httplib_context *ctx ) {
path = ctx->cfg[GLOBAL_PASSWORDS_FILE]; 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 0;
} }
return 1; return 1;

View File

@@ -139,7 +139,7 @@ void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri,
if ( tmp_rh == NULL ) { if ( tmp_rh == NULL ) {
httplib_unlock_context( ctx ); 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; return;
} }
@@ -150,7 +150,7 @@ void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri,
httplib_unlock_context( ctx ); httplib_unlock_context( ctx );
httplib_free( tmp_rh ); 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; return;
} }

View File

@@ -72,7 +72,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
if ( ! parse_port_string( & vec, & so, & ip_version ) ) { 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", "%.*s: invalid port spec (entry %i). Expecting list of: %s",
(int)vec.len, (int)vec.len,
vec.ptr, vec.ptr,
@@ -84,14 +84,14 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
#if !defined(NO_SSL) #if !defined(NO_SSL)
if ( so.has_ssl && ctx->ssl_ctx == NULL ) { 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; continue;
} }
#endif /* ! NO_SLL */ #endif /* ! NO_SLL */
if ( ( so.sock = socket( so.lsa.sa.sa_family, SOCK_STREAM, 6 ) ) == INVALID_SOCKET ) { 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; continue;
} }
@@ -115,7 +115,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
* Set reuse option, but don't abort on errors. * 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 */ #else /* _WIN32 */
if ( setsockopt( so.sock, SOL_SOCKET, SO_REUSEADDR, (SOCK_OPT_TYPE)&on, sizeof(on) ) != 0 ) { 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. * 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 */ #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. * 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 ) { 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 ); closesocket( so.sock );
so.sock = INVALID_SOCKET; so.sock = INVALID_SOCKET;
continue; continue;
@@ -162,7 +162,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
if ( bind( so.sock, &so.lsa.sa, len ) != 0 ) { 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 ); closesocket( so.sock );
so.sock = INVALID_SOCKET; so.sock = INVALID_SOCKET;
continue; continue;
@@ -170,13 +170,13 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
} }
else { 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; continue;
} }
if ( listen( so.sock, SOMAXCONN ) != 0 ) { 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 ); closesocket( so.sock );
so.sock = INVALID_SOCKET; so.sock = INVALID_SOCKET;
continue; 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 ) { if ( getsockname( so.sock, &(usa.sa), &len ) != 0 || usa.sa.sa_family != so.lsa.sa.sa_family ) {
int err = (int)ERRNO; 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 ); closesocket( so.sock );
so.sock = INVALID_SOCKET; so.sock = INVALID_SOCKET;
continue; continue;
@@ -202,7 +202,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
if ( ptr != NULL ) ctx->listening_sockets = ptr; if ( ptr != NULL ) ctx->listening_sockets = ptr;
else { else {
httplib_cry( XX_httplib_fc(ctx), "%s", "Out of memory" ); httplib_cry( ctx, NULL, "%s", "Out of memory" );
closesocket( so.sock ); closesocket( so.sock );
so.sock = INVALID_SOCKET; so.sock = INVALID_SOCKET;
continue; continue;
@@ -213,14 +213,14 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) {
if ( pfd != NULL ) ctx->listening_socket_fds = pfd; if ( pfd != NULL ) ctx->listening_socket_fds = pfd;
else { else {
httplib_cry( XX_httplib_fc(ctx), "%s", "Out of memory" ); httplib_cry( ctx, NULL, "%s", "Out of memory" );
closesocket( so.sock ); closesocket( so.sock );
so.sock = INVALID_SOCKET; so.sock = INVALID_SOCKET;
continue; 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->listening_sockets[ctx->num_listening_sockets] = so;
ctx->num_listening_sockets++; ctx->num_listening_sockets++;

View File

@@ -78,7 +78,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) {
ctx->ssl_ctx = SSL_CTX_new( SSLv23_server_method() ); ctx->ssl_ctx = SSL_CTX_new( SSLv23_server_method() );
if ( ctx->ssl_ctx == NULL ) { 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; return false;
} }
@@ -105,7 +105,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) {
if ( callback_ret < 0 ) { 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; 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 ) { 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_CTX_load_verify_locations error: %s "
"ssl_verify_peer requires setting " "ssl_verify_peer requires setting "
"either ssl_ca_path or ssl_ca_file. Is any of them " "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 ) { 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; 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 ) { 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());
} }
} }

View File

@@ -55,10 +55,10 @@ bool XX_httplib_set_uid_option( struct httplib_context *ctx ) {
if ( uid == NULL ) return true; if ( uid == NULL ) return true;
if ( (pw = getpwnam(uid)) == NULL ) httplib_cry( XX_httplib_fc(ctx), "%s: unknown user [%s]", __func__, uid ); if ( (pw = getpwnam(uid)) == NULL ) httplib_cry( ctx, NULL, "%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 ( setgid(pw->pw_gid) == -1 ) httplib_cry( ctx, NULL, "%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 ( setgroups(0, NULL) ) httplib_cry( ctx, NULL, "%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) ); else if ( setuid(pw->pw_uid) == -1 ) httplib_cry( ctx, NULL, "%s: setuid(%s): %s", __func__, uid, strerror(errno) );
else return true; else return true;
return false; return false;

View File

@@ -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 ) { 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; 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); UNUSED_PARAMETER(envblk);
if ( conn == NULL ) return 0; if ( conn == NULL || conn->ctx == NULL ) return 0;
if ( (pid = fork()) == -1 ) { if ( (pid = fork()) == -1 ) {
@@ -179,10 +179,10 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro
* Child * Child
*/ */
if ( chdir( dir ) != 0 ) httplib_cry( conn, "%s: chdir(%s): %s", __func__, dir, 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, "%s: dup2(%d, 0): %s", __func__, fdin[0], 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, "%s: dup2(%d, 1): %s", __func__, fdout[1], 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, "%s: dup2(%d, 2): %s", __func__, fderr[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 { else {
/* /*
* Keep stderr and stdout in two different pipes. * 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 ) { if ( interp == NULL ) {
execle( prog, prog, NULL, envp ); 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 { else {
execle( interp, interp, prog, NULL, envp ); 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) );
} }
} }

View File

@@ -43,7 +43,7 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha
size_t len; size_t len;
bool truncated; bool truncated;
if ( conn == NULL ) return; if ( conn == NULL || conn->ctx == NULL ) return;
truncated = false; truncated = false;
@@ -98,19 +98,19 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha
} }
else { else {
httplib_cry( conn, "Bad SSI #include: [%s]", tag ); httplib_cry( conn->ctx, conn, "Bad SSI #include: [%s]", tag );
return; return;
} }
if ( truncated ) { 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; return;
} }
if ( ! XX_httplib_fopen( conn, path, "rb", &file ) ) { 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; return;
} }
@@ -134,14 +134,14 @@ static void do_ssi_exec( struct httplib_connection *conn, char *tag ) {
if ( sscanf(tag, " \"%1023[^\"]\"", cmd) != 1 ) { 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 { else {
cmd[1023] = 0; cmd[1023] = 0;
if ( (file.fp = popen( cmd, "r" ) ) == NULL ) { 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 { else {
@@ -174,7 +174,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st
if ( include_level > 10 ) { 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; return;
} }
@@ -212,7 +212,7 @@ static void send_ssi_file( struct httplib_connection *conn, const char *path, st
do_ssi_exec(conn, buf + 9); do_ssi_exec(conn, buf + 9);
#endif /* !NO_POPEN */ #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; 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 ) { 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; len = 0;
} }

View File

@@ -39,9 +39,11 @@
int XX_httplib_ssl_use_pem_file( struct httplib_context *ctx, const char *pem ) { 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 ) { 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; 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 ) { 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; return 0;
} }
if ( SSL_CTX_check_private_key( ctx->ssl_ctx ) == 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; return 0;
} }
if ( SSL_CTX_use_certificate_chain_file( ctx->ssl_ctx, pem ) == 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; return 0;
} }

View File

@@ -84,7 +84,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks
*/ */
httplib_atomic_dec( & XX_httplib_sTlsInit ); 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 ); httplib_free( ctx );
return NULL; return NULL;
@@ -129,7 +129,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks
if ( ctx->cfg[idx] != NULL ) { 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] ); 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 ( 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 ); 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 ); va_end( ap );
buf[sizeof(buf)-1] = 0; 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 ); if ( ctx != NULL ) XX_httplib_free_context( ctx );
httplib_pthread_setspecific( XX_httplib_sTlsKey, NULL ); httplib_pthread_setspecific( XX_httplib_sTlsKey, NULL );

View File

@@ -47,7 +47,7 @@ int64_t httplib_store_body( struct httplib_connection *conn, const char *path )
if ( conn->consumed_content != 0 ) { 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; return -11;
} }

View File

@@ -60,7 +60,7 @@ void XX_httplib_vsnprintf( const struct httplib_connection *conn, bool *truncate
else { else {
if ( truncated != NULL ) *truncated = true; 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; n = (int)buflen - 1;
} }
buf[n] = '\0'; buf[n] = '\0';

View File

@@ -44,13 +44,15 @@ int httplib_websocket_client_write( struct httplib_connection *conn, int opcode,
char *masked_data; char *masked_data;
uint32_t masking_key; uint32_t masking_key;
if ( conn == NULL || conn->ctx == NULL ) return -1;
retval = -1; retval = -1;
masked_data = httplib_malloc( ((dataLen + 7) / 4) * 4 ); masked_data = httplib_malloc( ((dataLen + 7) / 4) * 4 );
masking_key = (uint32_t) httplib_get_random(); masking_key = (uint32_t) httplib_get_random();
if ( masked_data == NULL ) { 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; return -1;
} }

View File

@@ -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) */ 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 ); 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 { else {
httplib_pthread_setspecific( XX_httplib_sTlsKey, &tls ); httplib_pthread_setspecific( XX_httplib_sTlsKey, &tls );