From 80bd80df8bff26f3797d437d3c817f4b03f74d23 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 26 Dec 2016 14:57:13 +0100 Subject: [PATCH] Caching now with runtime switch --- doc/api/httplib_check_feature.md | 2 -- src/httplib_check_feature.c | 3 --- src/httplib_config_options.c | 4 +--- src/httplib_handle_file_based_request.c | 10 +++------- ...ttplib_handle_not_modified_static_file_request.c | 3 --- src/httplib_is_not_modified.c | 6 +----- src/httplib_main.h | 5 +---- src/httplib_parse_date_string.c | 13 ++++--------- src/httplib_send_static_cache_header.c | 7 ------- 9 files changed, 10 insertions(+), 43 deletions(-) diff --git a/doc/api/httplib_check_feature.md b/doc/api/httplib_check_feature.md index 7fbc5e22..1f8d6694 100644 --- a/doc/api/httplib_check_feature.md +++ b/doc/api/httplib_check_feature.md @@ -25,8 +25,6 @@ The following parameter values can be used: | **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. | -| **16** | USE_WEBSOCKET | Support for web sockets. WebSockets support is available in the LibHTTP library if the proper options has been used during cimpile time. | -| **128** | NO_CACHING | *Support for caching*. The webserver will support caching, if it has not been disabled while compiling the library. | Parameter values other than the values mentioned above will give undefined results. Therefore—although the parameter values for the `httplib_check_feature()` function are effectively bitmasks, you should't assume that combining two of those values with an OR to a new value will give any meaningful results when the function returns. diff --git a/src/httplib_check_feature.c b/src/httplib_check_feature.c index 35ed9a2b..fa2e1a73 100644 --- a/src/httplib_check_feature.c +++ b/src/httplib_check_feature.c @@ -47,9 +47,6 @@ unsigned httplib_check_feature( unsigned feature ) { #if !defined(NO_CGI) | 0x0004u #endif -#if !defined(NO_CACHING) - | 0x0080u -#endif /* Set some extra bits not defined in the API documentation. * These bits may change without further notice. */ diff --git a/src/httplib_config_options.c b/src/httplib_config_options.c index a08dbfc3..64d59b33 100644 --- a/src/httplib_config_options.c +++ b/src/httplib_config_options.c @@ -70,9 +70,7 @@ struct httplib_option XX_httplib_config_options[] = { { "access_control_allow_origin", CONFIG_TYPE_STRING, "*" }, { "error_pages", CONFIG_TYPE_DIRECTORY, NULL }, { "tcp_nodelay", CONFIG_TYPE_NUMBER, "0" }, -#if !defined(NO_CACHING) - { "static_file_max_age", CONFIG_TYPE_NUMBER, "3600" }, -#endif + { "static_file_max_age", CONFIG_TYPE_NUMBER, NULL }, #if defined(__linux__) { "allow_sendfile_call", CONFIG_TYPE_BOOLEAN, "yes" }, #endif diff --git a/src/httplib_handle_file_based_request.c b/src/httplib_handle_file_based_request.c index 68c3f939..47b48522 100644 --- a/src/httplib_handle_file_based_request.c +++ b/src/httplib_handle_file_based_request.c @@ -41,6 +41,7 @@ void XX_httplib_handle_file_based_request( struct httplib_connection *conn, cons const char *cgi_ext; #endif /* ! NO_CGI */ const char *ssi_ext; + const char *max_age; if ( conn == NULL || conn->ctx == NULL ) return; @@ -48,6 +49,7 @@ void XX_httplib_handle_file_based_request( struct httplib_connection *conn, cons cgi_ext = conn->ctx->cfg[CGI_EXTENSIONS]; #endif /* ! NO_CGI */ ssi_ext = conn->ctx->cfg[SSI_EXTENSIONS]; + max_age = conn->ctx->cfg[STATIC_FILE_MAX_AGE]; if (0) { #if !defined(NO_CGI) @@ -66,17 +68,11 @@ void XX_httplib_handle_file_based_request( struct httplib_connection *conn, cons 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) } - else if ( ! conn->in_error_handler && XX_httplib_is_not_modified( conn, file ) ) { - - /* - * Send 304 "Not Modified" - this must not send any body data - */ + else if ( max_age != NULL && ! conn->in_error_handler && XX_httplib_is_not_modified( conn, file ) ) { XX_httplib_handle_not_modified_static_file_request( conn, file ); -#endif /* !NO_CACHING */ } else XX_httplib_handle_static_file_request( conn, path, file, NULL, NULL ); diff --git a/src/httplib_handle_not_modified_static_file_request.c b/src/httplib_handle_not_modified_static_file_request.c index e6f88300..bc5759b1 100644 --- a/src/httplib_handle_not_modified_static_file_request.c +++ b/src/httplib_handle_not_modified_static_file_request.c @@ -36,7 +36,6 @@ * not been changed. */ -#if !defined(NO_CACHING) void XX_httplib_handle_not_modified_static_file_request( struct httplib_connection *conn, struct file *filep ) { char date[64]; @@ -58,5 +57,3 @@ void XX_httplib_handle_not_modified_static_file_request( struct httplib_connecti httplib_printf( conn, "Last-Modified: %s\r\n" "Etag: %s\r\n" "Connection: %s\r\n" "\r\n", lm, etag, XX_httplib_suggest_connection_header( conn ) ); } /* XX_httplib_handle_not_modified_static_file_request */ - -#endif diff --git a/src/httplib_is_not_modified.c b/src/httplib_is_not_modified.c index c0349e11..67bd06cd 100644 --- a/src/httplib_is_not_modified.c +++ b/src/httplib_is_not_modified.c @@ -35,20 +35,16 @@ * be sufficient. */ -#if !defined(NO_CACHING) - bool XX_httplib_is_not_modified( const struct httplib_connection *conn, const struct file *filep ) { char etag[64]; const char *ims = httplib_get_header( conn, "If-Modified-Since" ); const char *inm = httplib_get_header( conn, "If-None-Match" ); + if ( conn == NULL || filep == NULL ) return false; XX_httplib_construct_etag( etag, sizeof(etag), filep ); - if ( filep == NULL ) return false; return (inm != NULL && ! httplib_strcasecmp( etag, inm ) ) || (ims != NULL && ( filep->last_modified <= XX_httplib_parse_date_string( ims ) ) ) ; } /* XX_httplib_is_not_modified */ - -#endif /* !NO_CACHING */ diff --git a/src/httplib_main.h b/src/httplib_main.h index 178bd9f6..66a72f46 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -432,11 +432,8 @@ enum { WEBSOCKET_ROOT, ACCESS_CONTROL_ALLOW_ORIGIN, ERROR_PAGES, - CONFIG_TCP_NODELAY, /* Prepended CONFIG_ to avoid conflict with the - * socket option typedef TCP_NODELAY. */ -#if !defined(NO_CACHING) + CONFIG_TCP_NODELAY, /* Prepended CONFIG_ to avoid conflict with the socket option typedef TCP_NODELAY. */ STATIC_FILE_MAX_AGE, -#endif #if defined(__linux__) ALLOW_SENDFILE_CALL, #endif diff --git a/src/httplib_parse_date_string.c b/src/httplib_parse_date_string.c index cc79f00b..25d8e271 100644 --- a/src/httplib_parse_date_string.c +++ b/src/httplib_parse_date_string.c @@ -27,13 +27,8 @@ #include "httplib_main.h" -#if !defined(NO_CACHING) static const char *month_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -#endif /* !NO_CACHING */ - - -#if !defined(NO_CACHING) /* Convert month to the month number. Return -1 on error, or month number */ static int get_month_index( const char *s ) { @@ -47,8 +42,10 @@ static int get_month_index( const char *s ) { return -1; } - -/* Parse UTC date-time string, and return the corresponding time_t value. */ +/* + * Parse UTC date-time string, and return the corresponding time_t value. + * This function is used in the if-modified-since calculations + */ time_t XX_httplib_parse_date_string( const char *datetime ) { char month_str[32] = {0}; @@ -85,5 +82,3 @@ time_t XX_httplib_parse_date_string( const char *datetime ) { return result; } /* XX_httplib_parse_date_string */ - -#endif /* !NO_CACHING */ diff --git a/src/httplib_send_static_cache_header.c b/src/httplib_send_static_cache_header.c index 2c7eb772..04b3facb 100644 --- a/src/httplib_send_static_cache_header.c +++ b/src/httplib_send_static_cache_header.c @@ -36,7 +36,6 @@ int XX_httplib_send_static_cache_header( struct httplib_connection *conn ) { -#if !defined(NO_CACHING) /* * Read the server config to check how long a file may be cached. * The configuration is in seconds. @@ -72,10 +71,4 @@ int XX_httplib_send_static_cache_header( struct httplib_connection *conn ) { return httplib_printf( conn, "Cache-Control: max-age=%u\r\n", (unsigned)max_age ); -#else /* NO_CACHING */ - - return XX_httplib_send_no_cache_header( conn ); - -#endif /* !NO_CACHING */ - } /* XX_httplib_send_static_cache_header */