1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-09 03:22:45 +03:00

Caching now with runtime switch

This commit is contained in:
Lammert Bies
2016-12-26 14:57:13 +01:00
parent f2d9e614a3
commit 80bd80df8b
9 changed files with 10 additions and 43 deletions

View File

@@ -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.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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