From fe0b6dfad7fbb0e64ddd1676f5fef05534e6d4f5 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 26 Dec 2016 21:25:49 +0100 Subject: [PATCH] Removed NO_FILES compile option --- doc/api/httplib_check_feature.md | 1 - include/libhttp.h | 2 - src/httplib_delete_file.c | 11 +++-- src/httplib_forward_body_data.c | 4 +- src/httplib_handle_propfind.c | 5 +- src/httplib_handle_request.c | 66 ++++----------------------- src/httplib_interpret_uri.c | 11 +---- src/httplib_is_authorized_for_put.c | 7 +-- src/httplib_main.h | 8 ++-- src/httplib_mkcol.c | 6 +-- src/httplib_prepare_cgi_environment.c | 13 +++--- src/httplib_put_file.c | 7 +-- src/httplib_send_options.c | 7 +-- src/httplib_ssi.c | 7 ++- src/httplib_start.c | 4 -- src/httplib_substitute_index_file.c | 4 +- 16 files changed, 41 insertions(+), 122 deletions(-) diff --git a/doc/api/httplib_check_feature.md b/doc/api/httplib_check_feature.md index 1f8d6694..9e3645a2 100644 --- a/doc/api/httplib_check_feature.md +++ b/doc/api/httplib_check_feature.md @@ -22,7 +22,6 @@ The following parameter values can be used: | Value | Compilation option | Description | | :---: | :---: | :--- | -| **1** | NO_FILES | *Able to serve files*. If this feature is available, the webserver is able to serve files directly from a directory tree. | | **2** | NO_SSL | *Support for HTTPS*. If this feature is available, the webserver van use encryption in the client-server connection. SSLv2, SSLv3, TLSv1.0, TLSv1.1 and TLSv1.2 are supported depending on the SSL library LibHTTP has been compiled with, but which protocols are used effectively when the server is running is dependent on the options used when the server is started. | | **4** | NO_CGI | *Support for CGI*. If this feature is available, external CGI scripts can be called by the webserver. | diff --git a/include/libhttp.h b/include/libhttp.h index e719af50..fee8fba8 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -930,10 +930,8 @@ LIBHTTP_API int httplib_get_response(struct httplib_connection *conn, char *ebuf /* Check which features where set when LibHTTP has been compiled. Parameters: feature: specifies which feature should be checked - 1 serve files (NO_FILES not set) 2 support HTTPS (NO_SSL not set) 4 support CGI (NO_CGI not set) - 128 support caching (NO_CACHING not set) The result is undefined for all other feature values. Return: diff --git a/src/httplib_delete_file.c b/src/httplib_delete_file.c index 68ac2440..fa67fbb3 100644 --- a/src/httplib_delete_file.c +++ b/src/httplib_delete_file.c @@ -34,12 +34,17 @@ * connection. */ -#if !defined(NO_FILES) - void XX_httplib_delete_file( struct httplib_connection *conn, const char *path ) { struct de de; + if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) { + + XX_httplib_send_http_error( conn, 405, "Error: File delete operations are not supported" ); + return; + } + memset( &de.file, 0, sizeof(de.file) ); if ( ! XX_httplib_stat( conn, path, &de.file ) ) { @@ -106,5 +111,3 @@ void XX_httplib_delete_file( struct httplib_connection *conn, const char *path ) else XX_httplib_send_http_error( conn, 423, "Error: Cannot delete file\nremove(%s): %s", path, strerror(ERRNO) ); } /* XX_httplib_delete_file */ - -#endif /* !NO_FILES */ diff --git a/src/httplib_forward_body_data.c b/src/httplib_forward_body_data.c index 94d6d841..13dd4613 100644 --- a/src/httplib_forward_body_data.c +++ b/src/httplib_forward_body_data.c @@ -34,7 +34,6 @@ * client. The function returns true if successful, and false otherwise. */ -#if !defined(NO_CGI) || !defined(NO_FILES) bool XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ) { const char *expect; @@ -52,7 +51,7 @@ bool XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SO 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"); + expect = httplib_get_header( conn, "Expect" ); if ( fp == NULL ) { @@ -139,4 +138,3 @@ bool XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SO return success; } /* XX_httplib_forward_body_data */ -#endif diff --git a/src/httplib_handle_propfind.c b/src/httplib_handle_propfind.c index 3d653483..f97c4f74 100644 --- a/src/httplib_handle_propfind.c +++ b/src/httplib_handle_propfind.c @@ -29,8 +29,6 @@ #include "httplib_string.h" #include "httplib_utils.h" -#if !defined(NO_FILES) - /* * static void print_props( struct httplib_connection *conn, const char *uri, struct file *filep ); * @@ -106,6 +104,7 @@ void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *pa const char *edl; if ( conn == NULL || conn->ctx == NULL || path == NULL || filep == NULL ) return; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return; depth = httplib_get_header( conn, "Depth" ); curtime = time( NULL ); @@ -139,5 +138,3 @@ void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *pa conn->num_bytes_sent += httplib_printf( conn, "%s\n", "" ); } /* XX_httplib_handle_propfind */ - -#endif diff --git a/src/httplib_handle_request.c b/src/httplib_handle_request.c index 7512aaed..ca2c6755 100644 --- a/src/httplib_handle_request.c +++ b/src/httplib_handle_request.c @@ -60,14 +60,12 @@ void XX_httplib_handle_request( struct httplib_connection *conn ) { httplib_authorization_handler auth_handler; void *auth_callback_data; const char *edl; + time_t curtime; + char date[64]; union { const char * con; char * var; } ptr; -#if !defined(NO_FILES) - time_t curtime = time( NULL ); - char date[64]; -#endif if ( conn == NULL ) return; @@ -86,6 +84,7 @@ void XX_httplib_handle_request( struct httplib_connection *conn ) { auth_handler = NULL; auth_callback_data = NULL; path[0] = 0; + curtime = time( NULL ); if ( ri == NULL ) return; @@ -272,11 +271,8 @@ no_callback_resource: * 6.2.1. thus, the server must have real files */ -#if defined(NO_FILES) - if (1) { -#else if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) { -#endif + /* * This server does not have any real files, thus the * PUT/DELETE methods are not valid. @@ -286,7 +282,6 @@ no_callback_resource: return; } -#if !defined(NO_FILES) /* * 6.2.2. Check if put authorization for static files is * available. @@ -296,8 +291,6 @@ no_callback_resource: XX_httplib_send_authorization_request( conn ); return; } -#endif - } else { @@ -394,25 +387,12 @@ no_callback_resource: return; } - else - -#if defined(NO_FILES) - - /* - * 9a. In case the server uses only callbacks, this uri is - * unknown. - * Then, all request handling ends here. - */ - - XX_httplib_send_http_error( conn, 404, "%s", "Not Found" ); - -#else /* - * 9b. This request is either for a static file or resource handled + * 9. This request is either for a static file or resource handled * by a script file. Thus, a DOCUMENT_ROOT must exist. */ - if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) { + else if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) { XX_httplib_send_http_error( conn, 404, "%s", "Not Found" ); return; @@ -434,35 +414,9 @@ no_callback_resource: if ( is_put_or_delete_request ) { - /* - * 11.1. PUT method - */ - - if ( ! strcmp( ri->request_method, "PUT" ) ) { - - XX_httplib_put_file( conn, path ); - return; - } - - /* - * 11.2. DELETE method - */ - - if ( ! strcmp( ri->request_method, "DELETE" ) ) { - - XX_httplib_delete_file( conn, path ); - return; - } - - /* - * 11.3. MKCOL method - */ - - if ( ! strcmp( ri->request_method, "MKCOL" ) ) { - - XX_httplib_mkcol( conn, path ); - return; - } + if ( ! strcmp( ri->request_method, "PUT" ) ) { XX_httplib_put_file( conn, path ); return; } + if ( ! strcmp( ri->request_method, "DELETE" ) ) { XX_httplib_delete_file( conn, path ); return; } + if ( ! strcmp( ri->request_method, "MKCOL" ) ) { XX_httplib_mkcol( conn, path ); return; } /* * 11.4. PATCH method @@ -575,8 +529,6 @@ no_callback_resource: XX_httplib_handle_file_based_request( conn, path, &file ); -#endif /* !defined(NO_FILES) */ - #if 0 /* * Perform redirect and auth checks before calling begin_request() handler. diff --git a/src/httplib_interpret_uri.c b/src/httplib_interpret_uri.c index 18a742a6..6247ea0b 100644 --- a/src/httplib_interpret_uri.c +++ b/src/httplib_interpret_uri.c @@ -48,7 +48,6 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, /* TODO (high): Restructure this function */ -#if !defined(NO_FILES) const char *uri; const char *root; const char *rewrite; @@ -62,9 +61,6 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, char *p; const char *cgi_ext; #endif /* !NO_CGI */ -#else /* NO_FILES */ - UNUSED_PARAMETER( filename_buf_len ); -#endif /* NO_FILES */ if ( conn == NULL || conn->ctx == NULL || filep == NULL ) return; @@ -79,7 +75,7 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, *is_put_or_delete_request = XX_httplib_is_put_or_delete_method( conn ); *is_websocket_request = XX_httplib_is_websocket_protocol( conn ); -#if !defined(NO_FILES) + if ( *is_websocket_request && conn->ctx->cfg[WEBSOCKET_ROOT] != NULL ) root = conn->ctx->cfg[WEBSOCKET_ROOT]; /* @@ -230,11 +226,8 @@ void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, } } #endif /* !defined(NO_CGI) */ -#endif /* !defined(NO_FILES) */ return; -#if !defined(NO_FILES) - /* * Reset all outputs */ @@ -249,6 +242,4 @@ interpret_cleanup: *is_websocket_request = false; *is_put_or_delete_request = false; -#endif /* !defined(NO_FILES) */ - } /* XX_httplib_interpret_uri */ diff --git a/src/httplib_is_authorized_for_put.c b/src/httplib_is_authorized_for_put.c index d531c0bd..984f46e9 100644 --- a/src/httplib_is_authorized_for_put.c +++ b/src/httplib_is_authorized_for_put.c @@ -24,8 +24,6 @@ #include "httplib_main.h" -#if !defined(NO_FILES) - /* * bool XX_httplib_is_authorized_for_put( struct httplib_connection *conn ); * @@ -40,7 +38,8 @@ bool XX_httplib_is_authorized_for_put( struct httplib_connection *conn ) { const char *passfile; bool ret; - if ( conn == NULL || conn->ctx == NULL ) return 0; + if ( conn == NULL || conn->ctx == NULL ) return false; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return false; passfile = conn->ctx->cfg[PUT_DELETE_PASSWORDS_FILE]; @@ -55,5 +54,3 @@ bool XX_httplib_is_authorized_for_put( struct httplib_connection *conn ) { return false; } /* XX_httplib_is_authorized_for_put */ - -#endif diff --git a/src/httplib_main.h b/src/httplib_main.h index d0fdf8e2..32afdc64 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -514,10 +514,10 @@ struct httplib_workerTLS { extern CRITICAL_SECTION global_log_file_lock; #endif -#define PASSWORDS_FILE_NAME ".htpasswd" -#define CGI_ENVIRONMENT_SIZE (4096) -#define MAX_CGI_ENVIR_VARS (256) -#define MG_BUF_LEN (8192) +#define PASSWORDS_FILE_NAME ".htpasswd" +#define CGI_ENVIRONMENT_SIZE (4096) +#define MAX_CGI_ENVIR_VARS (256) +#define MG_BUF_LEN (8192) /* * TODO: LJB: Move to test functions diff --git a/src/httplib_mkcol.c b/src/httplib_mkcol.c index a6a58fac..f2cacc77 100644 --- a/src/httplib_mkcol.c +++ b/src/httplib_mkcol.c @@ -36,7 +36,6 @@ * location specificied by the request URI. */ -#if !defined(NO_FILES) void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { int rc; @@ -45,7 +44,8 @@ void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { char date[64]; time_t curtime; - if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return; curtime = time( NULL ); @@ -98,5 +98,3 @@ void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { } } /* XX_httplib_mkcol */ - -#endif /* !NO_FILES */ diff --git a/src/httplib_prepare_cgi_environment.c b/src/httplib_prepare_cgi_environment.c index 09fade4a..a16e812b 100644 --- a/src/httplib_prepare_cgi_environment.c +++ b/src/httplib_prepare_cgi_environment.c @@ -49,7 +49,7 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const int i; bool truncated; - if ( conn == NULL || conn->ctx == NULL || prog == NULL || env == NULL ) return; + if ( conn == NULL || conn->ctx == NULL || prog == NULL || env == NULL || conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return; env->conn = conn; env->buflen = CGI_ENVIRONMENT_SIZE; @@ -60,9 +60,9 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const env->var = httplib_malloc( env->buflen * sizeof(char *) ); 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() ); + XX_httplib_addenv( env, "SERVER_ROOT=%s", conn->ctx->cfg[DOCUMENT_ROOT] ); + 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 @@ -91,9 +91,8 @@ void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const XX_httplib_addenv( env, "SCRIPT_FILENAME=%s", prog ); - 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 ); + 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" ); diff --git a/src/httplib_put_file.c b/src/httplib_put_file.c index 2ff010f6..8cdc5a8a 100644 --- a/src/httplib_put_file.c +++ b/src/httplib_put_file.c @@ -35,8 +35,6 @@ * a remote client. */ -#if !defined(NO_FILES) - void XX_httplib_put_file( struct httplib_connection *conn, const char *path ) { struct file file = STRUCT_FILE_INITIALIZER; @@ -47,7 +45,8 @@ void XX_httplib_put_file( struct httplib_connection *conn, const char *path ) { char date[64]; time_t curtime; - if ( conn == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return; curtime = time( NULL ); @@ -198,5 +197,3 @@ void XX_httplib_put_file( struct httplib_connection *conn, const char *path ) { XX_httplib_fclose( & file ); } /* XX_httplib_put_file */ - -#endif /* !NO_FILES */ diff --git a/src/httplib_send_options.c b/src/httplib_send_options.c index 2a03d9e2..d5533af2 100644 --- a/src/httplib_send_options.c +++ b/src/httplib_send_options.c @@ -35,14 +35,13 @@ * client can use to connect to the server. */ -#if !defined(NO_FILES) - void XX_httplib_send_options( struct httplib_connection *conn ) { char date[64]; time_t curtime; - if ( conn == NULL ) return; + if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return; curtime = time( NULL ); conn->status_code = 200; @@ -62,5 +61,3 @@ void XX_httplib_send_options( struct httplib_connection *conn ) { XX_httplib_suggest_connection_header( conn ) ); } /* XX_httplib_send_options */ - -#endif diff --git a/src/httplib_ssi.c b/src/httplib_ssi.c index ad027e6d..5a42c936 100644 --- a/src/httplib_ssi.c +++ b/src/httplib_ssi.c @@ -32,7 +32,7 @@ static void send_ssi_file(struct httplib_connection *, const char *, struct file *, int); -static void do_ssi_include(struct httplib_connection *conn, const char *ssi, char *tag, int include_level) { +static void do_ssi_include( struct httplib_connection *conn, const char *ssi, char *tag, int include_level ) { char file_name[MG_BUF_LEN]; char path[512]; @@ -43,7 +43,7 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha size_t len; bool truncated; - if ( conn == NULL || conn->ctx == NULL ) return; + if ( conn == NULL || conn->ctx == NULL || conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return; truncated = false; @@ -60,8 +60,7 @@ static void do_ssi_include(struct httplib_connection *conn, const char *ssi, cha */ file_name[511] = 0; - if ( conn->ctx->cfg[DOCUMENT_ROOT] != NULL ) doc_root = conn->ctx->cfg[DOCUMENT_ROOT]; - else doc_root = ""; + doc_root = conn->ctx->cfg[DOCUMENT_ROOT]; XX_httplib_snprintf( conn, &truncated, path, sizeof(path), "%s/%s", doc_root, file_name ); diff --git a/src/httplib_start.c b/src/httplib_start.c index b67d9a14..7bdb1171 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -146,10 +146,6 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks if ( ctx->cfg[i] == NULL && default_value != NULL ) ctx->cfg[i] = httplib_strdup( default_value ); } -#if defined(NO_FILES) - if ( ctx->cfg[DOCUMENT_ROOT] != NULL ) return cleanup( ctx, "Document root must not be set" ); -#endif - XX_httplib_get_system_name( & ctx->systemName ); /* diff --git a/src/httplib_substitute_index_file.c b/src/httplib_substitute_index_file.c index a0d9cf2d..2540dc8e 100644 --- a/src/httplib_substitute_index_file.c +++ b/src/httplib_substitute_index_file.c @@ -37,8 +37,6 @@ * located, it's stats are returnd in stp. */ -#if !defined(NO_FILES) - int XX_httplib_substitute_index_file( struct httplib_connection *conn, char *path, size_t path_len, struct file *filep ) { const char *list; @@ -48,6 +46,7 @@ int XX_httplib_substitute_index_file( struct httplib_connection *conn, char *pat bool found; if ( conn == NULL || conn->ctx == NULL || path == NULL ) return 0; + if ( conn->ctx->cfg[DOCUMENT_ROOT] == NULL ) return 0; list = conn->ctx->cfg[INDEX_FILES]; n = strlen( path ); @@ -106,4 +105,3 @@ int XX_httplib_substitute_index_file( struct httplib_connection *conn, char *pat return found; } /* XX_httplib_substitute_index_file */ -#endif