diff --git a/include/libhttp.h b/include/libhttp.h index 27014771..93f50c28 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -164,7 +164,7 @@ struct httplib_request_info { int64_t content_length; /* Length (in bytes) of the request body, can be -1 if no length was given. */ int remote_port; /* Client's port */ - int is_ssl; /* 1 if SSL-ed, 0 if not */ + bool has_ssl; /* 1 if SSL-ed, 0 if not */ void *user_data; /* User data pointer passed to httplib_start() */ void *conn_data; /* Connection-specific user data */ @@ -444,14 +444,8 @@ httplib_get_context(const struct httplib_connection *conn); LIBHTTP_API void *httplib_get_user_data(const struct httplib_context *ctx); -/* Set user data for the current connection. */ -LIBHTTP_API void httplib_set_user_connection_data(struct httplib_connection *conn, - void *data); -/* Get user data set for the current connection. */ -LIBHTTP_API void * -httplib_get_user_connection_data(const struct httplib_connection *conn); struct httplib_option { @@ -462,12 +456,12 @@ struct httplib_option { enum { - CONFIG_TYPE_UNKNOWN = 0x0, - CONFIG_TYPE_NUMBER = 0x1, - CONFIG_TYPE_STRING = 0x2, - CONFIG_TYPE_FILE = 0x3, - CONFIG_TYPE_DIRECTORY = 0x4, - CONFIG_TYPE_BOOLEAN = 0x5, + CONFIG_TYPE_UNKNOWN = 0x0, + CONFIG_TYPE_NUMBER = 0x1, + CONFIG_TYPE_STRING = 0x2, + CONFIG_TYPE_FILE = 0x3, + CONFIG_TYPE_DIRECTORY = 0x4, + CONFIG_TYPE_BOOLEAN = 0x5, CONFIG_TYPE_EXT_PATTERN = 0x6 }; @@ -479,14 +473,14 @@ LIBHTTP_API const struct httplib_option *httplib_get_valid_options(void); struct httplib_server_ports { - int protocol; /* 1 = IPv4, 2 = IPv6, 3 = both */ - int port; /* port number */ - int is_ssl; /* https port: 0 = no, 1 = yes */ - int is_redirect; /* redirect all requests: 0 = no, 1 = yes */ - int _reserved1; - int _reserved2; - int _reserved3; - int _reserved4; + int protocol; /* 1 = IPv4, 2 = IPv6, 3 = both */ + int port; /* port number */ + bool has_ssl; /* https port: 0 = no, 1 = yes */ + bool has_redirect; /* redirect all requests: 0 = no, 1 = yes */ + int _reserved1; + int _reserved2; + int _reserved3; + int _reserved4; }; @@ -983,6 +977,7 @@ LIBHTTP_API int httplib_atomic_inc( volatile int *addr ); LIBHTTP_API int httplib_base64_encode( const unsigned char *src, int src_len, char *dst, int dst_len ); LIBHTTP_API int httplib_closedir( DIR *dir ); 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 ); LIBHTTP_API int httplib_mkdir( const char *path, int mode ); LIBHTTP_API DIR * httplib_opendir( const char *name ); @@ -1008,6 +1003,7 @@ LIBHTTP_API struct dirent * httplib_readdir( DIR *dir ); LIBHTTP_API int httplib_remove( const char *path ); LIBHTTP_API void httplib_send_file( struct httplib_connection *conn, const char *path, const char *mime_type, const char *additional_headers ); LIBHTTP_API void httplib_set_alloc_callback_func( httplib_alloc_callback_func log_func ); +LIBHTTP_API void httplib_set_user_connection_data( struct httplib_connection *conn, void *data ); 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 e2884feb..67a9c590 100644 --- a/src/httplib_accept_new_connection.c +++ b/src/httplib_accept_new_connection.c @@ -66,8 +66,8 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt XX_httplib_set_close_on_exec( so.sock, XX_httplib_fc(ctx) ); - so.is_ssl = listener->is_ssl; - so.ssl_redir = listener->ssl_redir; + so.has_ssl = listener->has_ssl; + so.has_redir = listener->has_redir; if ( getsockname( so.sock, &so.lsa.sa, &len ) != 0 ) { diff --git a/src/httplib_connect_client.c b/src/httplib_connect_client.c index 1e22bc8f..c2e531f6 100644 --- a/src/httplib_connect_client.c +++ b/src/httplib_connect_client.c @@ -114,7 +114,7 @@ static struct httplib_connection *httplib_connect_client_impl( const struct http if ( getsockname( sock, psa, &len ) != 0 ) httplib_cry(conn, "%s: getsockname() failed: %s", __func__, strerror(ERRNO) ); - conn->client.is_ssl = (use_ssl) ? 1 : 0; + conn->client.has_ssl = (use_ssl) ? true : false; httplib_pthread_mutex_init( &conn->mutex, &XX_httplib_pthread_mutex_attr ); #ifndef NO_SSL diff --git a/src/httplib_fc.c b/src/httplib_fc.c index d92f8395..3867d3c9 100644 --- a/src/httplib_fc.c +++ b/src/httplib_fc.c @@ -30,7 +30,7 @@ /* * struct httplib_connection *XX_httplib_fc( struct httplib_context *ctx ); * - * TThe function XX_httplib_fc() returns a fake connection structure specific + * The function XX_httplib_fc() returns a fake connection structure specific * for logging if a connection is not applicable at the moment of logging. */ diff --git a/src/httplib_get_first_ssl_listener_index.c b/src/httplib_get_first_ssl_listener_index.c index 7b74cb74..bd6a742b 100644 --- a/src/httplib_get_first_ssl_listener_index.c +++ b/src/httplib_get_first_ssl_listener_index.c @@ -42,7 +42,7 @@ int XX_httplib_get_first_ssl_listener_index( const struct httplib_context *ctx ) idx = -1; - if ( ctx != NULL ) for (i=0; idx == -1 && i < ctx->num_listening_sockets; i++) idx = ctx->listening_sockets[i].is_ssl ? ((int)(i)) : -1; + if ( ctx != NULL ) for (i=0; idx == -1 && i < ctx->num_listening_sockets; i++) idx = (ctx->listening_sockets[i].has_ssl) ? ((int)(i)) : -1; return idx; diff --git a/src/httplib_get_server_ports.c b/src/httplib_get_server_ports.c index 8c93f353..8c58f8f4 100644 --- a/src/httplib_get_server_ports.c +++ b/src/httplib_get_server_ports.c @@ -46,8 +46,8 @@ int httplib_get_server_ports( const struct httplib_context *ctx, int size, struc ? ntohs( ctx->listening_sockets[i].lsa.sin6.sin6_port ) : ntohs( ctx->listening_sockets[i].lsa.sin.sin_port ); - ports[cnt].is_ssl = ctx->listening_sockets[i].is_ssl; - ports[cnt].is_redirect = ctx->listening_sockets[i].ssl_redir; + ports[cnt].has_ssl = ctx->listening_sockets[i].has_ssl; + ports[cnt].has_redirect = ctx->listening_sockets[i].has_redir; if ( ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET ) ports[cnt++].protocol = 1; else if ( ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET6 ) ports[cnt++].protocol = 3; diff --git a/src/httplib_get_user_connection_data.c b/src/httplib_get_user_connection_data.c index 8b250df4..5be01c3a 100644 --- a/src/httplib_get_user_connection_data.c +++ b/src/httplib_get_user_connection_data.c @@ -20,14 +20,20 @@ * 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" -void * httplib_get_user_connection_data( const struct httplib_connection *conn ) { +/* + * void *httplib_get_user_connection_data( const struct httplib_connection *conn ); + * + * The function httplib_get_user_connection_data() returns a pointer to user + * data associated with a connection which was previously registered with a + * call to httplib_set_user_connection_data(). In case of problems the value + * NULL is returned. + */ + +LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn ) { if ( conn == NULL ) return NULL; diff --git a/src/httplib_handle_request.c b/src/httplib_handle_request.c index ec9601cd..4b81c738 100644 --- a/src/httplib_handle_request.c +++ b/src/httplib_handle_request.c @@ -105,7 +105,7 @@ void XX_httplib_handle_request( struct httplib_connection *conn ) { * 1.2. do a https redirect, if required. Do not decode URIs yet. */ - if ( ! conn->client.is_ssl && conn->client.ssl_redir ) { + if ( ! conn->client.has_ssl && conn->client.has_redir ) { ssl_index = XX_httplib_get_first_ssl_listener_index( conn->ctx ); diff --git a/src/httplib_main.h b/src/httplib_main.h index d033ea09..74fd2717 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -516,13 +516,12 @@ extern CRITICAL_SECTION global_log_file_lock; /* Describes listening socket, or socket which was accept()-ed by the master * thread and queued for future handling by the worker thread. */ struct socket { - SOCKET sock; /* Listening socket */ - union usa lsa; /* Local socket address */ - union usa rsa; /* Remote socket address */ - unsigned char is_ssl; /* Is port SSL-ed */ - unsigned char ssl_redir; /* Is port supposed to redirect everything to SSL - * port */ - unsigned char in_use; /* Is valid */ + SOCKET sock; /* Listening socket */ + union usa lsa; /* Local socket address */ + union usa rsa; /* Remote socket address */ + bool has_ssl; /* Is port SSL-ed */ + bool has_redir; /* Is port supposed to redirect everything to SSL port */ + unsigned char in_use; /* Is valid */ }; diff --git a/src/httplib_set_ports_option.c b/src/httplib_set_ports_option.c index 62d33b1e..4b99f397 100644 --- a/src/httplib_set_ports_option.c +++ b/src/httplib_set_ports_option.c @@ -82,7 +82,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { } #if !defined(NO_SSL) - if ( so.is_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 ); continue; @@ -355,8 +355,8 @@ static bool parse_port_string( const struct vec *vec, struct socket *so, int *ip } ch = vec->ptr[len]; /* Next character after the port number */ - so->is_ssl = ( ch == 's' ); - so->ssl_redir = ( ch == 'r' ); + so->has_ssl = ( ch == 's' ); + so->has_redir = ( ch == 'r' ); /* * Make sure the port is valid and vector ends with 's', 'r' or ',' diff --git a/src/httplib_set_user_connection_data.c b/src/httplib_set_user_connection_data.c index c3365fd5..6aae6732 100644 --- a/src/httplib_set_user_connection_data.c +++ b/src/httplib_set_user_connection_data.c @@ -20,9 +20,6 @@ * 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" @@ -35,7 +32,7 @@ * the data, the link between the connection and data is removed. */ -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 ) { if ( conn != NULL ) conn->request_info.conn_data = data; diff --git a/src/httplib_should_decode_url.c b/src/httplib_should_decode_url.c index 7cb8b0f7..73462e12 100644 --- a/src/httplib_should_decode_url.c +++ b/src/httplib_should_decode_url.c @@ -20,9 +20,6 @@ * 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" diff --git a/src/httplib_ssi.c b/src/httplib_ssi.c index dea05232..044d602b 100644 --- a/src/httplib_ssi.c +++ b/src/httplib_ssi.c @@ -111,19 +111,17 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha if ( ! XX_httplib_fopen( conn, path, "rb", &file ) ) { httplib_cry( conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, strerror(ERRNO) ); + return; } - else { - XX_httplib_fclose_on_exec( & file, conn ); + XX_httplib_fclose_on_exec( & file, conn ); - ssi_ext = conn->ctx->cfg[SSI_EXTENSIONS]; + ssi_ext = conn->ctx->cfg[SSI_EXTENSIONS]; - if ( ssi_ext != NULL && XX_httplib_match_prefix( ssi_ext, strlen( ssi_ext ), path ) > 0 ) send_ssi_file( conn, path, &file, include_level+1 ); - - else XX_httplib_send_file_data(conn, &file, 0, INT64_MAX); + if ( ssi_ext != NULL && XX_httplib_match_prefix( ssi_ext, strlen( ssi_ext ), path ) > 0 ) send_ssi_file( conn, path, &file, include_level+1 ); + else XX_httplib_send_file_data( conn, &file, 0, INT64_MAX ); - XX_httplib_fclose( & file ); - } + XX_httplib_fclose( & file ); } /* do_ssi_include */ diff --git a/src/httplib_worker_thread.c b/src/httplib_worker_thread.c index 43961575..604cb128 100644 --- a/src/httplib_worker_thread.c +++ b/src/httplib_worker_thread.c @@ -129,9 +129,9 @@ static void *worker_thread_run( struct worker_thread_args *thread_args ) { XX_httplib_sockaddr_to_string( conn->request_info.remote_addr, sizeof(conn->request_info.remote_addr), &conn->client.rsa ); - conn->request_info.is_ssl = conn->client.is_ssl; + conn->request_info.has_ssl = conn->client.has_ssl; - if ( conn->client.is_ssl ) { + if ( conn->client.has_ssl ) { #ifndef NO_SSL /* * HTTPS connection