diff --git a/src/httplib_accept_new_connection.c b/src/httplib_accept_new_connection.c index 7ddae044..e2884feb 100644 --- a/src/httplib_accept_new_connection.c +++ b/src/httplib_accept_new_connection.c @@ -97,7 +97,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt * are relatively small (eg. less than 1400 bytes). */ - if ( ctx != NULL && ctx->config[CONFIG_TCP_NODELAY] != NULL && ! strcmp(ctx->config[CONFIG_TCP_NODELAY], "1" ) ) { + if ( ctx != NULL && ctx->cfg[CONFIG_TCP_NODELAY] != NULL && ! strcmp(ctx->cfg[CONFIG_TCP_NODELAY], "1" ) ) { if ( XX_httplib_set_tcp_nodelay( so.sock, 1 ) != 0 ) { @@ -105,8 +105,8 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct htt } } - if ( ctx != NULL && ctx->config[REQUEST_TIMEOUT] ) timeout = atoi( ctx->config[REQUEST_TIMEOUT] ); - else timeout = -1; + if ( ctx != NULL && ctx->cfg[REQUEST_TIMEOUT] != NULL ) timeout = atoi( ctx->cfg[REQUEST_TIMEOUT] ); + else timeout = -1; if ( timeout > 0 ) XX_httplib_set_sock_timeout( so.sock, timeout ); diff --git a/src/httplib_authorize.c b/src/httplib_authorize.c index 0b7efbb3..d4555960 100644 --- a/src/httplib_authorize.c +++ b/src/httplib_authorize.c @@ -45,7 +45,7 @@ bool XX_httplib_authorize( struct httplib_connection *conn, struct file *filep ) workdata.conn = conn; if ( ! XX_httplib_parse_auth_header( conn, buf, sizeof(buf), &workdata.ah ) ) return false; - workdata.domain = conn->ctx->config[AUTHENTICATION_DOMAIN]; + workdata.domain = conn->ctx->cfg[AUTHENTICATION_DOMAIN]; return XX_httplib_read_auth_file( filep, &workdata ); diff --git a/src/httplib_check_acl.c b/src/httplib_check_acl.c index cc7eaec2..f83e712f 100644 --- a/src/httplib_check_acl.c +++ b/src/httplib_check_acl.c @@ -47,11 +47,7 @@ int XX_httplib_check_acl( struct httplib_context *ctx, uint32_t remote_ip ) { if ( ctx == NULL ) return -1; - list = ctx->config[ACCESS_CONTROL_LIST]; - - /* - * If any ACL is set, deny by default - */ + list = ctx->cfg[ACCESS_CONTROL_LIST]; if ( list == NULL ) allowed = '+'; else allowed = '-'; diff --git a/src/httplib_check_authorization.c b/src/httplib_check_authorization.c index 3ddb63a9..e2b7e6d8 100644 --- a/src/httplib_check_authorization.c +++ b/src/httplib_check_authorization.c @@ -43,7 +43,7 @@ bool XX_httplib_check_authorization( struct httplib_connection *conn, const char authorized = true; - list = conn->ctx->config[PROTECT_URI]; + list = conn->ctx->cfg[PROTECT_URI]; while ( (list = XX_httplib_next_option( list, &uri_vec, &filename_vec )) != NULL ) { diff --git a/src/httplib_cry.c b/src/httplib_cry.c index d7beade9..81531a33 100644 --- a/src/httplib_cry.c +++ b/src/httplib_cry.c @@ -58,9 +58,9 @@ void httplib_cry( const struct httplib_connection *conn, const char *fmt, ... ) if ( conn->ctx->callbacks.log_message == NULL || conn->ctx->callbacks.log_message( conn, buf ) == 0 ) { - if ( conn->ctx->config[ERROR_LOG_FILE] != NULL ) { + if ( conn->ctx->cfg[ERROR_LOG_FILE] != NULL ) { - if ( XX_httplib_fopen( conn, conn->ctx->config[ERROR_LOG_FILE], "a+", &fi ) == 0 ) fi.fp = NULL; + if ( XX_httplib_fopen( conn, conn->ctx->cfg[ERROR_LOG_FILE], "a+", &fi ) == 0 ) fi.fp = NULL; } else fi.fp = NULL; diff --git a/src/httplib_forward_body_data.c b/src/httplib_forward_body_data.c index 53cd9f32..fade0ddf 100644 --- a/src/httplib_forward_body_data.c +++ b/src/httplib_forward_body_data.c @@ -49,8 +49,8 @@ int XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SOC if ( conn == NULL || conn->ctx == NULL ) return 0; success = 0; - timeout = -1.0; - if ( conn->ctx->config[REQUEST_TIMEOUT] != NULL ) timeout = atof( conn->ctx->config[REQUEST_TIMEOUT] ) / 1000.0; + if ( conn->ctx->cfg[REQUEST_TIMEOUT] != NULL ) timeout = atof( conn->ctx->cfg[REQUEST_TIMEOUT] ) / 1000.0; + else timeout = -1.0; expect = httplib_get_header(conn, "Expect"); diff --git a/src/httplib_free_context.c b/src/httplib_free_context.c index bfac57fc..8e3b9b73 100644 --- a/src/httplib_free_context.c +++ b/src/httplib_free_context.c @@ -78,11 +78,10 @@ void XX_httplib_free_context( struct httplib_context *ctx ) { for (i = 0; i < NUM_OPTIONS; i++) { - if (ctx->config[i] != NULL) { -#if defined(_MSC_VER) -#pragma warning(suppress : 6001) -#endif - httplib_free( ctx->config[i] ); + if (ctx->cfg[i] != NULL) { + + httplib_free( ctx->cfg[i] ); + ctx->cfg[i] = NULL; } } @@ -90,13 +89,13 @@ void XX_httplib_free_context( struct httplib_context *ctx ) { * Deallocate request handlers */ - while ( ctx->handlers ) { + while ( ctx->handlers != NULL ) { tmp_rh = ctx->handlers; ctx->handlers = tmp_rh->next; httplib_free( tmp_rh->uri ); - httplib_free( tmp_rh ); + if ( tmp_rh != NULL ) httplib_free( tmp_rh ); } #ifndef NO_SSL @@ -105,7 +104,11 @@ void XX_httplib_free_context( struct httplib_context *ctx ) { * Deallocate SSL context */ - if ( ctx->ssl_ctx != NULL ) SSL_CTX_free( ctx->ssl_ctx ); + if ( ctx->ssl_ctx != NULL ) { + + SSL_CTX_free( ctx->ssl_ctx ); + ctx->ssl_ctx = NULL; + } #endif /* !NO_SSL */ @@ -113,7 +116,11 @@ void XX_httplib_free_context( struct httplib_context *ctx ) { * Deallocate worker thread ID array */ - if ( ctx->workerthreadids != NULL ) httplib_free( ctx->workerthreadids ); + if ( ctx->workerthreadids != NULL ) { + + httplib_free( ctx->workerthreadids ); + ctx->workerthreadids = NULL; + } /* * Deallocate the tls variable diff --git a/src/httplib_get_mime_type.c b/src/httplib_get_mime_type.c index 5b91695e..80ab6318 100644 --- a/src/httplib_get_mime_type.c +++ b/src/httplib_get_mime_type.c @@ -47,7 +47,7 @@ void XX_httplib_get_mime_type( struct httplib_context *ctx, const char *path, st * override default mime types. */ - list = ctx->config[EXTRA_MIME_TYPES]; + list = ctx->cfg[EXTRA_MIME_TYPES]; while ( (list = XX_httplib_next_option( list, &ext_vec, &mime_vec )) != NULL ) { diff --git a/src/httplib_get_option.c b/src/httplib_get_option.c index 70225a45..d39ec6a5 100644 --- a/src/httplib_get_option.c +++ b/src/httplib_get_option.c @@ -45,8 +45,8 @@ const char *httplib_get_option( const struct httplib_context *ctx, const char *n i = XX_httplib_get_option_index( name ); if ( i == -1 ) return NULL; - if ( ctx == NULL || ctx->config[i] == NULL ) return ""; + if ( ctx == NULL || ctx->cfg[i] == NULL ) return ""; - return ctx->config[i]; + return ctx->cfg[i]; } /* httplib_get_option */ diff --git a/src/httplib_get_rel_url_at_current_server.c b/src/httplib_get_rel_url_at_current_server.c index c74b829f..0d4509fe 100644 --- a/src/httplib_get_rel_url_at_current_server.c +++ b/src/httplib_get_rel_url_at_current_server.c @@ -55,7 +55,7 @@ const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const st * DNS is case insensitive, so use case insensitive string compare here */ - server_domain = conn->ctx->config[AUTHENTICATION_DOMAIN]; + server_domain = conn->ctx->cfg[AUTHENTICATION_DOMAIN]; if ( server_domain == NULL ) return 0; server_domain_len = strlen( server_domain ); diff --git a/src/httplib_get_response.c b/src/httplib_get_response.c index 1b67b394..76a94b95 100644 --- a/src/httplib_get_response.c +++ b/src/httplib_get_response.c @@ -32,7 +32,12 @@ * int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout ); * * The function httplib_get_response() tries to get a response from a remote - * peer. + * peer. This function does some dirty action by temporarily replacing the + * contect of the connection with a copy. The only thing which is changed in + * the copy is the timeout value which is set according to the timeout as it + * was passed as a parameter to the function call. After the call to the + * function XX_httplib_getreq() has finished, the old context is put back in + * place. */ int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout ) { @@ -45,22 +50,31 @@ int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t eb if ( conn == NULL ) return -1; + /* + * Replace the connection context with a copy of it where the timeout + * value is changed to a parameter passed value. + */ + octx = conn->ctx; rctx = *(conn->ctx); if ( timeout >= 0 ) { XX_httplib_snprintf( conn, NULL, txt, sizeof(txt), "%i", timeout ); - rctx.config[REQUEST_TIMEOUT] = txt; + rctx.cfg[REQUEST_TIMEOUT] = txt; XX_httplib_set_sock_timeout( conn->client.sock, timeout ); } - else rctx.config[REQUEST_TIMEOUT] = NULL; + else rctx.cfg[REQUEST_TIMEOUT] = NULL; conn->ctx = &rctx; ret = XX_httplib_getreq( conn, ebuf, ebuf_len, &err ); conn->ctx = octx; + /* + * End of dirty context swap code. + */ + /* * TODO: 1) uri is deprecated; * 2) here, ri.uri is the http response code diff --git a/src/httplib_handle_file_based_request.c b/src/httplib_handle_file_based_request.c index 97483189..68c3f939 100644 --- a/src/httplib_handle_file_based_request.c +++ b/src/httplib_handle_file_based_request.c @@ -37,13 +37,23 @@ void XX_httplib_handle_file_based_request( struct httplib_connection *conn, const char *path, struct file *file ) { +#if !defined(NO_CGI) + const char *cgi_ext; +#endif /* ! NO_CGI */ + const char *ssi_ext; + if ( conn == NULL || conn->ctx == NULL ) return; +#if !defined(NO_CGI) + cgi_ext = conn->ctx->cfg[CGI_EXTENSIONS]; +#endif /* ! NO_CGI */ + ssi_ext = conn->ctx->cfg[SSI_EXTENSIONS]; + if (0) { #if !defined(NO_CGI) } - else if ( XX_httplib_match_prefix( conn->ctx->config[CGI_EXTENSIONS], strlen(conn->ctx->config[CGI_EXTENSIONS]), path) > 0 ) { + else if ( cgi_ext != NULL && XX_httplib_match_prefix( cgi_ext, strlen( cgi_ext ), path ) > 0 ) { /* * CGI scripts may support all HTTP methods @@ -53,7 +63,7 @@ void XX_httplib_handle_file_based_request( struct httplib_connection *conn, cons #endif /* !NO_CGI */ } - else if ( XX_httplib_match_prefix( conn->ctx->config[SSI_EXTENSIONS], strlen( conn->ctx->config[SSI_EXTENSIONS] ), path ) > 0 ) { + else if ( ssi_ext != NULL && XX_httplib_match_prefix( ssi_ext, strlen( ssi_ext ), path ) > 0 ) { XX_httplib_handle_ssi_file_request( conn, path, file ); #if !defined(NO_CACHING) diff --git a/src/httplib_handle_propfind.c b/src/httplib_handle_propfind.c index 71b08fed..59f79697 100644 --- a/src/httplib_handle_propfind.c +++ b/src/httplib_handle_propfind.c @@ -103,6 +103,7 @@ void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *pa const char *depth; char date[64]; time_t curtime; + const char *edl; if ( conn == NULL || conn->ctx == NULL || path == NULL || filep == NULL ) return; @@ -129,7 +130,8 @@ void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *pa * If it is a directory, print directory entries too if Depth is not 0 */ - if ( filep && filep->is_directory && !httplib_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes") && (depth == NULL || strcmp(depth, "0") != 0)) { + edl = conn->ctx->cfg[ENABLE_DIRECTORY_LISTING]; + if ( filep && filep->is_directory && edl != NULL && ! httplib_strcasecmp( edl, "yes" ) && ( depth == NULL || strcmp( depth, "0" ) != 0 ) ) { XX_httplib_scan_directory( conn, path, conn, &print_dav_dir_entry ); } diff --git a/src/httplib_handle_request.c b/src/httplib_handle_request.c index 388bf97b..35234ab5 100644 --- a/src/httplib_handle_request.c +++ b/src/httplib_handle_request.c @@ -150,7 +150,7 @@ void XX_httplib_handle_request( struct httplib_connection *conn ) { * 3. if this ip has limited speed, set it for this connection */ - conn->throttle = XX_httplib_set_throttle( conn->ctx->config[THROTTLE], XX_httplib_get_remote_ip( conn ), ri->local_uri ); + conn->throttle = XX_httplib_set_throttle( conn->ctx->cfg[THROTTLE], XX_httplib_get_remote_ip( conn ), ri->local_uri ); /* * 4. call a "handle everything" callback, if registered @@ -274,7 +274,7 @@ no_callback_resource: #if defined(NO_FILES) if (1) { #else - if ( conn->ctx->config[DOCUMENT_ROOT] == NULL ) { + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) { #endif /* * This server does not have any real files, thus the @@ -416,7 +416,7 @@ no_callback_resource: * by a script file. Thus, a DOCUMENT_ROOT must exist. */ - if ( conn->ctx->config[DOCUMENT_ROOT] == NULL ) { + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) { XX_httplib_send_http_error( conn, 404, "%s", "Not Found" ); return; @@ -568,7 +568,10 @@ no_callback_resource: * 14.2. no substitute file */ - if ( ! httplib_strcasecmp( conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes" ) ) XX_httplib_handle_directory_request( conn, path ); + if ( conn->ctx->cfg[ENABLE_DIRECTORY_LISTING] != NULL && ! httplib_strcasecmp( conn->ctx->cfg[ENABLE_DIRECTORY_LISTING], "yes" ) ) { + + XX_httplib_handle_directory_request( conn, path ); + } else XX_httplib_send_http_error( conn, 403, "%s", "Error: Directory listing denied" ); return; diff --git a/src/httplib_handle_static_file_request.c b/src/httplib_handle_static_file_request.c index 58cbc544..c847f5e4 100644 --- a/src/httplib_handle_static_file_request.c +++ b/src/httplib_handle_static_file_request.c @@ -151,7 +151,7 @@ void XX_httplib_handle_static_file_request( struct httplib_connection *conn, con */ cors1 = "Access-Control-Allow-Origin: "; - cors2 = conn->ctx->config[ACCESS_CONTROL_ALLOW_ORIGIN]; + cors2 = ( conn->ctx->cfg[ACCESS_CONTROL_ALLOW_ORIGIN] != NULL ) ? conn->ctx->cfg[ACCESS_CONTROL_ALLOW_ORIGIN] : ""; cors3 = "\r\n"; } else { diff --git a/src/httplib_interpret_uri.c b/src/httplib_interpret_uri.c index 82c2d215..11647ee2 100644 --- a/src/httplib_interpret_uri.c +++ b/src/httplib_interpret_uri.c @@ -60,6 +60,7 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, bool truncated; #if !defined(NO_CGI) char *p; + const char *cgi_ext; #endif /* !NO_CGI */ #else /* NO_FILES */ UNUSED_PARAMETER( filename_buf_len ); @@ -68,7 +69,7 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, if ( conn == NULL || conn->ctx == NULL || filep == NULL ) return; uri = conn->request_info.local_uri; - root = conn->ctx->config[DOCUMENT_ROOT]; + root = conn->ctx->cfg[DOCUMENT_ROOT]; memset( filep, 0, sizeof(*filep) ); @@ -80,7 +81,7 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, #if defined(USE_WEBSOCKET) *is_websocket_request = XX_httplib_is_websocket_protocol( conn ); #if !defined(NO_FILES) - if ( *is_websocket_request && conn->ctx->config[WEBSOCKET_ROOT] ) root = conn->ctx->config[WEBSOCKET_ROOT]; + if ( *is_websocket_request && conn->ctx->cfg[WEBSOCKET_ROOT] != NULL ) root = conn->ctx->cfg[WEBSOCKET_ROOT]; #endif /* !NO_FILES */ #else /* USE_WEBSOCKET */ *is_websocket_request = false; @@ -112,7 +113,7 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, if ( truncated ) goto interpret_cleanup; - rewrite = conn->ctx->config[REWRITE]; + rewrite = conn->ctx->cfg[REWRITE]; while ( (rewrite = XX_httplib_next_option( rewrite, &a, &b )) != NULL ) { @@ -139,11 +140,9 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, * File exists. Check if it is a script type. */ - if (0 -#if !defined(NO_CGI) - || XX_httplib_match_prefix(conn->ctx->config[CGI_EXTENSIONS], strlen(conn->ctx->config[CGI_EXTENSIONS]), filename) > 0 -#endif - ) { + if ( conn->ctx->cfg[CGI_EXTENSIONS] != NULL && + XX_httplib_match_prefix(conn->ctx->cfg[CGI_EXTENSIONS], strlen(conn->ctx->cfg[CGI_EXTENSIONS]), filename) > 0 ) { + /* * The request addresses a CGI script or a Lua script. The URI * corresponds to the script itself (like /path/script.cgi), @@ -210,13 +209,10 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, if ( *p == '/' ) { - *p = '\0'; + *p = '\0'; + cgi_ext = conn->ctx->cfg[CGI_EXTENSIONS]; - if ((0 -#if !defined(NO_CGI) - || XX_httplib_match_prefix(conn->ctx->config[CGI_EXTENSIONS], strlen(conn->ctx->config[CGI_EXTENSIONS]), filename) > 0 -#endif - ) && XX_httplib_stat(conn, filename, filep)) { + if ( cgi_ext != NULL && XX_httplib_match_prefix( cgi_ext, strlen( cgi_ext ), filename ) > 0 && XX_httplib_stat( conn, filename, filep ) ) { /* * Shift PATH_INFO block one character right, e.g. diff --git a/src/httplib_is_authorized_for_put.c b/src/httplib_is_authorized_for_put.c index 674e29d0..dae970e6 100644 --- a/src/httplib_is_authorized_for_put.c +++ b/src/httplib_is_authorized_for_put.c @@ -37,7 +37,7 @@ int XX_httplib_is_authorized_for_put( struct httplib_connection *conn ) { if ( conn == NULL || conn->ctx == NULL ) return 0; - passfile = conn->ctx->config[PUT_DELETE_PASSWORDS_FILE]; + passfile = conn->ctx->cfg[PUT_DELETE_PASSWORDS_FILE]; ret = 0; if ( passfile != NULL && XX_httplib_fopen( conn, passfile, "r", &file ) ) { diff --git a/src/httplib_log_access.c b/src/httplib_log_access.c index 3cafc00c..a1b7f560 100644 --- a/src/httplib_log_access.c +++ b/src/httplib_log_access.c @@ -50,9 +50,9 @@ void XX_httplib_log_access( const struct httplib_connection *conn ) { if ( conn == NULL || conn->ctx == NULL ) return; - if ( conn->ctx->config[ACCESS_LOG_FILE] != NULL ) { + if ( conn->ctx->cfg[ACCESS_LOG_FILE] != NULL ) { - if ( XX_httplib_fopen( conn, conn->ctx->config[ACCESS_LOG_FILE], "a+", &fi ) == 0 ) fi.fp = NULL; + if ( XX_httplib_fopen( conn, conn->ctx->cfg[ACCESS_LOG_FILE], "a+", &fi ) == 0 ) fi.fp = NULL; } else fi.fp = NULL; diff --git a/src/httplib_main.h b/src/httplib_main.h index eca413ed..5bc5651e 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -567,8 +567,8 @@ struct httplib_context { volatile int stop_flag; /* Should we stop event loop */ SSL_CTX *ssl_ctx; /* SSL context */ - char *config[NUM_OPTIONS]; /* LibHTTP configuration parameters */ - struct httplib_callbacks callbacks; /* User-defined callback function */ + char *cfg[NUM_OPTIONS]; /* LibHTTP configuration parameters */ + struct httplib_callbacks callbacks; /* User-defined callback function */ void *user_data; /* User-defined data */ int context_type; /* 1 = server context, 2 = client context */ @@ -845,7 +845,7 @@ void XX_httplib_log_access( const struct httplib_connection *conn ); LIBHTTP_THREAD XX_httplib_master_thread( void *thread_func_param ); int XX_httplib_match_prefix(const char *pattern, size_t pattern_len, const char *str); void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ); -int XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ); +bool XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ); const char * XX_httplib_next_option( const char *list, struct vec *val, struct vec *eq_val ); void XX_httplib_open_auth_file( struct httplib_connection *conn, const char *path, struct file *filep ); int XX_httplib_parse_auth_header( struct httplib_connection *conn, char *buf, size_t buf_size, struct ah *ah ); diff --git a/src/httplib_must_hide_file.c b/src/httplib_must_hide_file.c index bee73a7e..935e0f11 100644 --- a/src/httplib_must_hide_file.c +++ b/src/httplib_must_hide_file.c @@ -27,16 +27,26 @@ #include "httplib_main.h" -int XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ) { +/* + * bool XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ); + * + * The function XX_httplib_must_hide_file() returns true, if a file must be + * hidden from browsing by the remote client. A used provided list of file + * patterns to hide is used. Password files are always hidden, independent of + * the patterns defined by the user. + */ + +bool XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ) { const char *pw_pattern; const char *pattern; - if ( conn == NULL || conn->ctx == NULL ) return 0; + if ( conn == NULL || conn->ctx == NULL ) return false; pw_pattern = "**" PASSWORDS_FILE_NAME "$"; - pattern = conn->ctx->config[HIDE_FILES]; + pattern = conn->ctx->cfg[HIDE_FILES]; - return XX_httplib_match_prefix( pw_pattern, strlen(pw_pattern), path) > 0 || (pattern != NULL && XX_httplib_match_prefix(pattern, strlen(pattern), path) > 0); + return ( pw_pattern != NULL && XX_httplib_match_prefix( pw_pattern, strlen( pw_pattern ), path ) > 0 ) || + ( pattern != NULL && XX_httplib_match_prefix( pattern, strlen( pattern ), path ) > 0 ); } /* XX_httplib_must_hide_file */ diff --git a/src/httplib_open_auth_file.c b/src/httplib_open_auth_file.c index 07fb0200..27f673c4 100644 --- a/src/httplib_open_auth_file.c +++ b/src/httplib_open_auth_file.c @@ -41,7 +41,7 @@ void XX_httplib_open_auth_file( struct httplib_connection *conn, const char *pat if ( conn == NULL || conn->ctx == NULL ) return; - gpass = conn->ctx->config[GLOBAL_PASSWORDS_FILE]; + gpass = conn->ctx->cfg[GLOBAL_PASSWORDS_FILE]; if ( gpass != NULL ) { diff --git a/src/httplib_prepare_cgi_environment.c b/src/httplib_prepare_cgi_environment.c index 35a61be4..4f8b2cca 100644 --- a/src/httplib_prepare_cgi_environment.c +++ b/src/httplib_prepare_cgi_environment.c @@ -60,10 +60,10 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const env->varused = 0; env->var = httplib_malloc( env->buflen * sizeof(char *) ); - XX_httplib_addenv( env, "SERVER_NAME=%s", conn->ctx->config[AUTHENTICATION_DOMAIN] ); - XX_httplib_addenv( env, "SERVER_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT] ); - XX_httplib_addenv( env, "DOCUMENT_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT] ); - XX_httplib_addenv( env, "SERVER_SOFTWARE=%s/%s", "LibHTTP", httplib_version() ); + if ( conn->ctx->cfg[AUTHENTICATION_DOMAIN] != NULL ) XX_httplib_addenv( env, "SERVER_NAME=%s", conn->ctx->cfg[AUTHENTICATION_DOMAIN] ); + if ( conn->ctx->cfg[DOCUMENT_ROOT] != NULL ) XX_httplib_addenv( env, "SERVER_ROOT=%s", conn->ctx->cfg[DOCUMENT_ROOT] ); + if ( conn->ctx->cfg[DOCUMENT_ROOT] != NULL ) XX_httplib_addenv( env, "DOCUMENT_ROOT=%s", conn->ctx->cfg[DOCUMENT_ROOT] ); + XX_httplib_addenv( env, "SERVER_SOFTWARE=%s/%s", "LibHTTP", httplib_version() ); /* * Prepare the environment block @@ -92,10 +92,11 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const XX_httplib_addenv( env, "SCRIPT_FILENAME=%s", prog ); - if ( conn->path_info == NULL ) XX_httplib_addenv( env, "PATH_TRANSLATED=%s", conn->ctx->config[DOCUMENT_ROOT] ); - else XX_httplib_addenv( env, "PATH_TRANSLATED=%s%s", conn->ctx->config[DOCUMENT_ROOT], conn->path_info ); + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) XX_httplib_addenv( env, "PATH_TRANSLATED=" ); + else if ( conn->path_info == NULL ) XX_httplib_addenv( env, "PATH_TRANSLATED=%s", conn->ctx->cfg[DOCUMENT_ROOT] ); + else XX_httplib_addenv( env, "PATH_TRANSLATED=%s%s", conn->ctx->cfg[DOCUMENT_ROOT], conn->path_info ); - XX_httplib_addenv(env, "HTTPS=%s", (conn->ssl == NULL) ? "off" : "on"); + XX_httplib_addenv( env, "HTTPS=%s", (conn->ssl == NULL) ? "off" : "on" ); if ( (s = httplib_get_header( conn, "Content-Type" ) ) != NULL ) XX_httplib_addenv( env, "CONTENT_TYPE=%s", s ); if ( conn->request_info.query_string != NULL ) XX_httplib_addenv( env, "QUERY_STRING=%s", conn->request_info.query_string ); @@ -160,7 +161,7 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const * Add user-specified variables */ - s = conn->ctx->config[CGI_ENVIRONMENT]; + s = conn->ctx->cfg[CGI_ENVIRONMENT]; while ( (s = XX_httplib_next_option( s, &var_vec, NULL )) != NULL ) { diff --git a/src/httplib_process_new_connection.c b/src/httplib_process_new_connection.c index 86cc8f2d..cdef4da5 100644 --- a/src/httplib_process_new_connection.c +++ b/src/httplib_process_new_connection.c @@ -54,7 +54,7 @@ void XX_httplib_process_new_connection( struct httplib_connection *conn ) { if ( conn == NULL || conn->ctx == NULL ) return; ri = & conn->request_info; - keep_alive_enabled = ! strcmp( conn->ctx->config[ENABLE_KEEP_ALIVE], "yes" ); + keep_alive_enabled = ( conn->ctx->cfg[ENABLE_KEEP_ALIVE] != NULL && ! strcmp( conn->ctx->cfg[ENABLE_KEEP_ALIVE], "yes" ) ); /* * Important: on new connection, reset the receiving buffer. Credit diff --git a/src/httplib_pull_all.c b/src/httplib_pull_all.c index 54259380..2f2d6ade 100644 --- a/src/httplib_pull_all.c +++ b/src/httplib_pull_all.c @@ -36,9 +36,9 @@ int XX_httplib_pull_all( FILE *fp, struct httplib_connection *conn, char *buf, i if ( conn == NULL || conn->ctx == NULL ) return 0; nread = 0; - timeout = -1.0; - if ( conn->ctx->config[REQUEST_TIMEOUT] != NULL ) timeout = atof( conn->ctx->config[REQUEST_TIMEOUT] ) / 1000.0; + if ( conn->ctx->cfg[REQUEST_TIMEOUT] != NULL ) timeout = atof( conn->ctx->cfg[REQUEST_TIMEOUT] ) / 1000.0; + else timeout = -1.0; while ( len > 0 && conn->ctx->stop_flag == 0 ) { diff --git a/src/httplib_push_all.c b/src/httplib_push_all.c index d7a1b1f1..0f8ea884 100644 --- a/src/httplib_push_all.c +++ b/src/httplib_push_all.c @@ -166,8 +166,8 @@ int64_t XX_httplib_push_all( struct httplib_context *ctx, FILE *fp, SOCKET sock, nwritten = 0; - if ( ctx->config[REQUEST_TIMEOUT] != NULL ) timeout = atoi( ctx->config[REQUEST_TIMEOUT] ) / 1000.0; - else timeout = -1.0; + if ( ctx->cfg[REQUEST_TIMEOUT] != NULL ) timeout = atoi( ctx->cfg[REQUEST_TIMEOUT] ) / 1000.0; + else timeout = -1.0; while ( len > 0 && ctx->stop_flag == 0 ) { diff --git a/src/httplib_read_request.c b/src/httplib_read_request.c index 24b61831..685f6cc9 100644 --- a/src/httplib_read_request.c +++ b/src/httplib_read_request.c @@ -52,16 +52,8 @@ int XX_httplib_read_request( FILE *fp, struct httplib_connection *conn, char *bu memset( & last_action_time, 0, sizeof(last_action_time) ); - if ( conn->ctx->config[REQUEST_TIMEOUT] != NULL ) { - - /* - * value of request_timeout is in seconds, config in milliseconds - */ - - request_timeout = atof( conn->ctx->config[REQUEST_TIMEOUT] ) / 1000.0; - } - - else request_timeout = -1.0; + if ( conn->ctx->cfg[REQUEST_TIMEOUT] != NULL ) request_timeout = atof( conn->ctx->cfg[REQUEST_TIMEOUT] ) / 1000.0; + else request_timeout = -1.0; request_len = XX_httplib_get_request_len( buf, *nread ); diff --git a/src/httplib_read_websocket.c b/src/httplib_read_websocket.c index 58ead58e..8a7c1e79 100644 --- a/src/httplib_read_websocket.c +++ b/src/httplib_read_websocket.c @@ -79,11 +79,11 @@ void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websock unsigned char mop; /* mask flag and opcode */ double timeout; - timeout = -1.0; data = mem; + timeout = -1.0; - if ( conn->ctx->config[WEBSOCKET_TIMEOUT] ) timeout = atoi( conn->ctx->config[WEBSOCKET_TIMEOUT] ) / 1000.0; - if ( timeout <= 0.0 && conn->ctx->config[REQUEST_TIMEOUT] ) timeout = atoi( conn->ctx->config[REQUEST_TIMEOUT] ) / 1000.0; + if ( conn->ctx->cfg[WEBSOCKET_TIMEOUT] != NULL ) timeout = atof( conn->ctx->cfg[WEBSOCKET_TIMEOUT] ) / 1000.0; + if ( timeout <= 0.0 && conn->ctx->cfg[REQUEST_TIMEOUT] != NULL ) timeout = atof( conn->ctx->cfg[REQUEST_TIMEOUT] ) / 1000.0; XX_httplib_set_thread_name( "wsock" ); diff --git a/src/httplib_refresh_trust.c b/src/httplib_refresh_trust.c index 03e3650f..93e7b786 100644 --- a/src/httplib_refresh_trust.c +++ b/src/httplib_refresh_trust.c @@ -51,7 +51,7 @@ int XX_httplib_refresh_trust( struct httplib_connection *conn ) { p_reload_lock = & reload_lock; - pem = conn->ctx->config[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; @@ -61,14 +61,11 @@ int XX_httplib_refresh_trust( struct httplib_connection *conn ) { data_check = t; - should_verify_peer = conn->ctx->config[SSL_DO_VERIFY_PEER] != NULL && ! httplib_strcasecmp( conn->ctx->config[SSL_DO_VERIFY_PEER], "yes" ); + should_verify_peer = conn->ctx->cfg[SSL_DO_VERIFY_PEER] != NULL && ! httplib_strcasecmp( conn->ctx->cfg[SSL_DO_VERIFY_PEER], "yes" ); if ( should_verify_peer ) { - char *ca_path = conn->ctx->config[SSL_CA_PATH]; - char *ca_file = conn->ctx->config[SSL_CA_FILE]; - - if ( SSL_CTX_load_verify_locations( conn->ctx->ssl_ctx, ca_file, 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), "SSL_CTX_load_verify_locations error: %s " diff --git a/src/httplib_send_authorization_request.c b/src/httplib_send_authorization_request.c index 72251f8f..7ec1d09c 100644 --- a/src/httplib_send_authorization_request.c +++ b/src/httplib_send_authorization_request.c @@ -34,6 +34,7 @@ void XX_httplib_send_authorization_request( struct httplib_connection *conn ) { char date[64]; time_t curtime; uint64_t nonce; + const char *auth_domain; if ( conn == NULL || conn->ctx == NULL ) return; @@ -46,12 +47,15 @@ void XX_httplib_send_authorization_request( struct httplib_connection *conn ) { ++conn->ctx->nonce_count; httplib_pthread_mutex_unlock( & conn->ctx->nonce_mutex ); - nonce ^= conn->ctx->auth_nonce_mask; + nonce ^= conn->ctx->auth_nonce_mask; conn->status_code = 401; conn->must_close = 1; XX_httplib_gmt_time_string( date, sizeof(date), &curtime ); + if ( conn->ctx->cfg[AUTHENTICATION_DOMAIN] != NULL ) auth_domain = conn->ctx->cfg[AUTHENTICATION_DOMAIN]; + else auth_domain = "example.com"; + httplib_printf( conn, "HTTP/1.1 401 Unauthorized\r\n" ); XX_httplib_send_no_cache_header( conn ); httplib_printf( conn, @@ -62,7 +66,7 @@ void XX_httplib_send_authorization_request( struct httplib_connection *conn ) { "nonce=\"%" UINT64_FMT "\"\r\n\r\n", date, XX_httplib_suggest_connection_header(conn), - conn->ctx->config[AUTHENTICATION_DOMAIN], + auth_domain, nonce ); } /* XX_httplib_send_authorization_request */ diff --git a/src/httplib_send_file.c b/src/httplib_send_file.c index 1a604e8a..5b5c3dff 100644 --- a/src/httplib_send_file.c +++ b/src/httplib_send_file.c @@ -37,6 +37,7 @@ void httplib_send_file( struct httplib_connection *conn, const char *path, const char *mime_type, const char *additional_headers ) { struct file file = STRUCT_FILE_INITIALIZER; + const char *edl; if ( XX_httplib_stat( conn, path, &file ) ) { @@ -44,8 +45,9 @@ void httplib_send_file( struct httplib_connection *conn, const char *path, const if ( conn == NULL ) return; - if ( ! httplib_strcasecmp( conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes" ) ) XX_httplib_handle_directory_request( conn, path ); - + edl = conn->ctx->cfg[ENABLE_DIRECTORY_LISTING]; + + if ( edl != NULL && ! httplib_strcasecmp( edl, "yes" ) ) XX_httplib_handle_directory_request( conn, path ); else XX_httplib_send_http_error( conn, 403, "%s", "Error: Directory listing denied" ); } diff --git a/src/httplib_send_http_error.c b/src/httplib_send_http_error.c index 48fcb2ff..3646ddfe 100644 --- a/src/httplib_send_http_error.c +++ b/src/httplib_send_http_error.c @@ -63,8 +63,8 @@ void XX_httplib_send_http_error( struct httplib_connection *conn, int status, co * Send user defined error pages, if defined */ - error_handler = conn->ctx->config[ERROR_PAGES]; - error_page_file_ext = conn->ctx->config[INDEX_FILES]; + error_handler = conn->ctx->cfg[ERROR_PAGES]; + error_page_file_ext = conn->ctx->cfg[INDEX_FILES]; page_handler_found = 0; if ( error_handler != NULL ) { @@ -106,7 +106,9 @@ void XX_httplib_send_http_error( struct httplib_connection *conn, int status, co */ len = (int)strlen( buf ); - tstr = strchr( error_page_file_ext, '.' ); + + if ( error_page_file_ext != NULL ) tstr = strchr( error_page_file_ext, '.' ); + else tstr = NULL; while ( tstr ) { diff --git a/src/httplib_send_static_cache_header.c b/src/httplib_send_static_cache_header.c index a5e8553a..2c7eb772 100644 --- a/src/httplib_send_static_cache_header.c +++ b/src/httplib_send_static_cache_header.c @@ -44,7 +44,8 @@ int XX_httplib_send_static_cache_header( struct httplib_connection *conn ) { int max_age; - max_age = atoi( conn->ctx->config[STATIC_FILE_MAX_AGE] ); + if ( conn->ctx->cfg[STATIC_FILE_MAX_AGE] != NULL ) max_age = atoi( conn->ctx->cfg[STATIC_FILE_MAX_AGE] ); + else max_age = -1; if ( max_age <= 0 ) { diff --git a/src/httplib_set_gpass_option.c b/src/httplib_set_gpass_option.c index 383fc5c8..05d18c3f 100644 --- a/src/httplib_set_gpass_option.c +++ b/src/httplib_set_gpass_option.c @@ -41,7 +41,7 @@ int XX_httplib_set_gpass_option( struct httplib_context *ctx ) { if ( ctx == NULL ) return 0; - path = ctx->config[GLOBAL_PASSWORDS_FILE]; + path = ctx->cfg[GLOBAL_PASSWORDS_FILE]; if ( path != NULL && ! XX_httplib_stat( XX_httplib_fc(ctx), path, &file ) ) { diff --git a/src/httplib_set_ports_option.c b/src/httplib_set_ports_option.c index 5e254dfb..f805d5f6 100644 --- a/src/httplib_set_ports_option.c +++ b/src/httplib_set_ports_option.c @@ -65,7 +65,7 @@ int XX_httplib_set_ports_option( struct httplib_context *ctx ) { memset( & usa, 0, sizeof(usa) ); len = sizeof(usa); - list = ctx->config[LISTENING_PORTS]; + list = ctx->cfg[LISTENING_PORTS]; while ( (list = XX_httplib_next_option( list, &vec, NULL )) != NULL ) { diff --git a/src/httplib_set_ssl_option.c b/src/httplib_set_ssl_option.c index 5c6417b6..663487a7 100644 --- a/src/httplib_set_ssl_option.c +++ b/src/httplib_set_ssl_option.c @@ -43,8 +43,6 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { const char *pem; int callback_ret; int should_verify_peer; - const char *ca_path; - const char *ca_file; int use_default_verify_paths; int verify_depth; time_t now_rt; @@ -58,7 +56,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( ctx == NULL ) return false; now_rt = time( NULL ); - pem = ctx->config[ SSL_CERTIFICATE ]; + pem = ctx->cfg[ SSL_CERTIFICATE ]; if ( pem == NULL && ctx->callbacks.init_ssl == NULL ) return true; @@ -86,7 +84,8 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { SSL_CTX_clear_options( ctx->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 ); - protocol_ver = atoi( ctx->config[ SSL_PROTOCOL_VERSION ] ); + if ( ctx->cfg[SSL_PROTOCOL_VERSION] != NULL ) protocol_ver = atoi( ctx->cfg[SSL_PROTOCOL_VERSION] ); + else protocol_ver = 0; SSL_CTX_set_options( ctx->ssl_ctx, XX_httplib_ssl_get_protocol( protocol_ver ) ); SSL_CTX_set_options( ctx->ssl_ctx, SSL_OP_SINGLE_DH_USE ); @@ -121,7 +120,9 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { md5_append( & md5state, (const md5_byte_t *)&now_rt, sizeof(now_rt) ); clock_gettime( CLOCK_MONOTONIC, &now_mt ); md5_append( & md5state, (const md5_byte_t *)&now_mt, sizeof(now_mt) ); - md5_append( & md5state, (const md5_byte_t *)ctx->config[ LISTENING_PORTS ], strlen( ctx->config[LISTENING_PORTS ] ) ); + + if ( ctx->cfg[LISTENING_PORTS] != NULL ) md5_append( & md5state, (const md5_byte_t *)ctx->cfg[LISTENING_PORTS], strlen( ctx->cfg[LISTENING_PORTS] ) ); + md5_append( & md5state, (const md5_byte_t *)ctx, sizeof(*ctx) ); md5_finish( & md5state, ssl_context_id ); @@ -129,15 +130,12 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { if ( pem != NULL && ! XX_httplib_ssl_use_pem_file( ctx, pem ) ) return false; - should_verify_peer = ctx->config[SSL_DO_VERIFY_PEER] != NULL && ! httplib_strcasecmp( ctx->config[SSL_DO_VERIFY_PEER], "yes" ); - use_default_verify_paths = ctx->config[SSL_DEFAULT_VERIFY_PATHS] != NULL && ! httplib_strcasecmp( ctx->config[SSL_DEFAULT_VERIFY_PATHS], "yes" ); + should_verify_peer = ctx->cfg[SSL_DO_VERIFY_PEER] != NULL && ! httplib_strcasecmp( ctx->cfg[SSL_DO_VERIFY_PEER], "yes" ); + use_default_verify_paths = ctx->cfg[SSL_DEFAULT_VERIFY_PATHS] != NULL && ! httplib_strcasecmp( ctx->cfg[SSL_DEFAULT_VERIFY_PATHS], "yes" ); if ( should_verify_peer ) { - ca_path = ctx->config[SSL_CA_PATH]; - ca_file = ctx->config[SSL_CA_FILE]; - - if ( SSL_CTX_load_verify_locations( ctx->ssl_ctx, ca_file, 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), "SSL_CTX_load_verify_locations error: %s " @@ -158,16 +156,16 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) { return false; } - if ( ctx->config[SSL_VERIFY_DEPTH] != NULL ) { + if ( ctx->cfg[SSL_VERIFY_DEPTH] != NULL ) { - verify_depth = atoi( ctx->config[SSL_VERIFY_DEPTH] ); + verify_depth = atoi( ctx->cfg[SSL_VERIFY_DEPTH] ); SSL_CTX_set_verify_depth( ctx->ssl_ctx, verify_depth ); } } - if ( ctx->config[SSL_CIPHER_LIST] != NULL ) { + if ( ctx->cfg[SSL_CIPHER_LIST] != NULL ) { - if ( SSL_CTX_set_cipher_list( ctx->ssl_ctx, ctx->config[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()); } diff --git a/src/httplib_set_uid_option.c b/src/httplib_set_uid_option.c index 86dd6999..565d7dd8 100644 --- a/src/httplib_set_uid_option.c +++ b/src/httplib_set_uid_option.c @@ -43,7 +43,7 @@ int XX_httplib_set_uid_option( struct httplib_context *ctx ) { if ( ctx == NULL ) return 0; - uid = ctx->config[RUN_AS_USER]; + uid = ctx->cfg[RUN_AS_USER]; if ( uid == NULL ) return 1; diff --git a/src/httplib_should_decode_url.c b/src/httplib_should_decode_url.c index 633e011e..7cb8b0f7 100644 --- a/src/httplib_should_decode_url.c +++ b/src/httplib_should_decode_url.c @@ -29,9 +29,9 @@ bool XX_httplib_should_decode_url( const struct httplib_connection *conn ) { - if ( conn == NULL || conn->ctx == NULL ) return false; - if ( conn->ctx->config[DECODE_URL] == NULL ) return false; + if ( conn == NULL || conn->ctx == NULL ) return false; + if ( conn->ctx->cfg[DECODE_URL] == NULL ) return false; - return ( ! httplib_strcasecmp( conn->ctx->config[DECODE_URL], "yes" ) ); + return ( ! httplib_strcasecmp( conn->ctx->cfg[DECODE_URL], "yes" ) ); } /* XX_httplib_should_decode_url */ diff --git a/src/httplib_should_keep_alive.c b/src/httplib_should_keep_alive.c index d922d35c..a6e55549 100644 --- a/src/httplib_should_keep_alive.c +++ b/src/httplib_should_keep_alive.c @@ -48,12 +48,12 @@ bool XX_httplib_should_keep_alive( const struct httplib_connection *conn ) { http_version = conn->request_info.http_version; header = httplib_get_header( conn, "Connection" ); - if ( conn->must_close ) return false; - if ( conn->internal_error ) return false; - if ( conn->status_code == 401 ) return false; - if ( conn->ctx->config[ENABLE_KEEP_ALIVE] != NULL && httplib_strcasecmp( conn->ctx->config[ENABLE_KEEP_ALIVE], "yes" ) ) return false; - if ( header != NULL && ! XX_httplib_header_has_option( header, "keep-alive" ) ) return false; - if ( header == NULL && http_version != NULL && strcmp( http_version, "1.1" ) ) return false; + if ( conn->must_close ) return false; + if ( conn->internal_error ) return false; + if ( conn->status_code == 401 ) return false; + if ( conn->ctx->cfg[ENABLE_KEEP_ALIVE] != NULL && httplib_strcasecmp( conn->ctx->cfg[ENABLE_KEEP_ALIVE], "yes" ) ) return false; + if ( header != NULL && ! XX_httplib_header_has_option( header, "keep-alive" ) ) return false; + if ( header == NULL && http_version != NULL && strcmp( http_version, "1.1" ) ) return false; return true; diff --git a/src/httplib_spawn_process.c b/src/httplib_spawn_process.c index e02de066..68d1014f 100644 --- a/src/httplib_spawn_process.c +++ b/src/httplib_spawn_process.c @@ -212,7 +212,7 @@ pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *pro signal( SIGCHLD, SIG_DFL ); - interp = conn->ctx->config[CGI_INTERPRETER]; + interp = conn->ctx->cfg[CGI_INTERPRETER]; if ( interp == NULL ) { diff --git a/src/httplib_ssi.c b/src/httplib_ssi.c index 45b80ba4..f6e2d81b 100644 --- a/src/httplib_ssi.c +++ b/src/httplib_ssi.c @@ -36,6 +36,8 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha char file_name[MG_BUF_LEN]; char path[512]; + const char *doc_root; + const char *ssi_ext; char *p; struct file file = STRUCT_FILE_INITIALIZER; size_t len; @@ -58,7 +60,10 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha */ file_name[511] = 0; - XX_httplib_snprintf( conn, &truncated, path, sizeof(path), "%s/%s", conn->ctx->config[DOCUMENT_ROOT], file_name ); + if ( conn->ctx->cfg[DOCUMENT_ROOT] != NULL ) doc_root = conn->ctx->cfg[DOCUMENT_ROOT]; + else doc_root = ""; + + XX_httplib_snprintf( conn, &truncated, path, sizeof(path), "%s/%s", doc_root, file_name ); } @@ -111,10 +116,9 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha else { XX_httplib_fclose_on_exec( & file, conn ); - if ( XX_httplib_match_prefix( conn->ctx->config[SSI_EXTENSIONS], strlen( conn->ctx->config[SSI_EXTENSIONS] ), path) > 0 ) { + ssi_ext = conn->ctx->cfg[SSI_EXTENSIONS]; - send_ssi_file(conn, path, &file, include_level + 1); - } + 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); @@ -285,7 +289,7 @@ void XX_httplib_handle_ssi_file_request( struct httplib_connection *conn, const */ cors1 = "Access-Control-Allow-Origin: "; - cors2 = conn->ctx->config[ACCESS_CONTROL_ALLOW_ORIGIN]; + cors2 = ( conn->ctx->cfg[ACCESS_CONTROL_ALLOW_ORIGIN] != NULL ) ? conn->ctx->cfg[ACCESS_CONTROL_ALLOW_ORIGIN] : ""; cors3 = "\r\n"; } diff --git a/src/httplib_sslize.c b/src/httplib_sslize.c index 626b8fb7..4737a22d 100644 --- a/src/httplib_sslize.c +++ b/src/httplib_sslize.c @@ -45,7 +45,7 @@ int XX_httplib_sslize( struct httplib_connection *conn, SSL_CTX *s, int (*func)( if ( conn == NULL ) return 0; - short_trust = ( conn->ctx->config[SSL_SHORT_TRUST] != NULL && ! httplib_strcasecmp( conn->ctx->config[SSL_SHORT_TRUST], "yes" ) ); + short_trust = ( conn->ctx->cfg[SSL_SHORT_TRUST] != NULL && ! httplib_strcasecmp( conn->ctx->cfg[SSL_SHORT_TRUST], "yes" ) ); if ( short_trust ) { diff --git a/src/httplib_start.c b/src/httplib_start.c index b2711348..50584556 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -157,13 +157,13 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks return NULL; } - if ( ctx->config[idx] != NULL ) { + if ( ctx->cfg[idx] != NULL ) { httplib_cry( XX_httplib_fc(ctx), "warning: %s: duplicate option", name ); - httplib_free( ctx->config[idx] ); + httplib_free( ctx->cfg[idx] ); } - ctx->config[idx] = httplib_strdup( value ); + ctx->cfg[idx] = httplib_strdup( value ); } /* @@ -173,11 +173,11 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks for (i=0; XX_httplib_config_options[i].name != NULL; i++) { default_value = XX_httplib_config_options[i].default_value; - if ( ctx->config[i] == NULL && default_value != NULL ) ctx->config[i] = httplib_strdup( default_value ); + if ( ctx->cfg[i] == NULL && default_value != NULL ) ctx->cfg[i] = httplib_strdup( default_value ); } #if defined(NO_FILES) - if ( ctx->config[DOCUMENT_ROOT] != NULL ) { + if ( ctx->cfg[DOCUMENT_ROOT] != NULL ) { httplib_cry( XX_httplib_fc( ctx ), "%s", "Document root must not be set" ); XX_httplib_free_context( ctx ); @@ -221,7 +221,16 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks #endif /* !_WIN32 */ - workerthreadcount = atoi( ctx->config[NUM_THREADS] ); + if ( ctx->cfg[NUM_THREADS] == NULL ) { + + httplib_cry( XX_httplib_fc(ctx), "No worker thread number specified" ); + XX_httplib_free_context( ctx ); + httplib_pthread_setspecific( XX_httplib_sTlsKey, NULL ); + + return NULL; + } + + workerthreadcount = atoi( ctx->cfg[NUM_THREADS] ); if ( workerthreadcount > MAX_WORKER_THREADS ) { @@ -232,7 +241,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks return NULL; } - if (workerthreadcount > 0) { + if ( workerthreadcount > 0 ) { ctx->cfg_worker_threads = ((unsigned int)(workerthreadcount)); ctx->workerthreadids = httplib_calloc( ctx->cfg_worker_threads, sizeof(pthread_t) ); diff --git a/src/httplib_substitute_index_file.c b/src/httplib_substitute_index_file.c index 37512515..a0d9cf2d 100644 --- a/src/httplib_substitute_index_file.c +++ b/src/httplib_substitute_index_file.c @@ -49,7 +49,7 @@ int XX_httplib_substitute_index_file( struct httplib_connection *conn, char *pat if ( conn == NULL || conn->ctx == NULL || path == NULL ) return 0; - list = conn->ctx->config[INDEX_FILES]; + list = conn->ctx->cfg[INDEX_FILES]; n = strlen( path ); found = false;