From 8e10efd027619ac65c98a217766f10a17fe03727 Mon Sep 17 00:00:00 2001 From: "roberto@quantal64" Date: Wed, 13 Jun 2012 10:53:21 +0200 Subject: [PATCH] added options for setting Expires based on REQUEST_URI and PATH_INFO --- logging.c | 29 +++++++++++++ plugins/cgi/cgi_plugin.c | 3 +- plugins/lua/lua_plugin.c | 3 +- plugins/php/php_plugin.c | 3 +- plugins/psgi/psgi_plugin.c | 3 +- plugins/python/wsgi_handlers.c | 24 +---------- plugins/rack/rack_plugin.c | 3 +- protocol.c | 74 ++++++++++++++++++++++++++++++++++ uwsgi.c | 6 +++ uwsgi.h | 6 +++ 10 files changed, 121 insertions(+), 33 deletions(-) diff --git a/logging.c b/logging.c index 97e2f92f..773b6ebb 100644 --- a/logging.c +++ b/logging.c @@ -139,10 +139,39 @@ void log_request(struct wsgi_request *wsgi_req) { char *msg4 = " via offload() "; struct uwsgi_app *wi; + int log_it = uwsgi.shared->options[UWSGI_OPTION_LOGGING]; if (wsgi_req->do_not_log) return; + if (wsgi_req->log_this) { + goto logit; + } + +/* conditional logging */ + if (uwsgi.shared->options[UWSGI_OPTION_LOG_ZERO] && wsgi_req->response_size == 0) { + goto logit; + } + if (uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW] && (uint32_t) wsgi_req_time >= uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW]) { + goto logit; + } + if (uwsgi.shared->options[UWSGI_OPTION_LOG_4xx] && (wsgi_req->status >= 400 && wsgi_req->status <= 499)) { + goto logit; + } + if (uwsgi.shared->options[UWSGI_OPTION_LOG_5xx] && (wsgi_req->status >= 500 && wsgi_req->status <= 599)) { + goto logit; + } + if (uwsgi.shared->options[UWSGI_OPTION_LOG_BIG] && (wsgi_req->response_size >= uwsgi.shared->options[UWSGI_OPTION_LOG_BIG])) { + goto logit; + } + if (uwsgi.shared->options[UWSGI_OPTION_LOG_SENDFILE] && (wsgi_req->sendfile_fd > -1 && wsgi_req->sendfile_obj == wsgi_req->async_result)) { + goto logit; + } + + if (!log_it) return; + +logit: + if (wsgi_req->app_id >= 0) { wi = &uwsgi_apps[wsgi_req->app_id]; if (wi->requests > 0) { diff --git a/plugins/cgi/cgi_plugin.c b/plugins/cgi/cgi_plugin.c index 07077e1f..4bb04dcd 100644 --- a/plugins/cgi/cgi_plugin.c +++ b/plugins/cgi/cgi_plugin.c @@ -965,8 +965,7 @@ clear2: void uwsgi_cgi_after_request(struct wsgi_request *wsgi_req) { - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) - log_request(wsgi_req); + log_request(wsgi_req); } diff --git a/plugins/lua/lua_plugin.c b/plugins/lua/lua_plugin.c index be263721..9cad54a9 100644 --- a/plugins/lua/lua_plugin.c +++ b/plugins/lua/lua_plugin.c @@ -576,8 +576,7 @@ clear2: void uwsgi_lua_after_request(struct wsgi_request *wsgi_req) { - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) - log_request(wsgi_req); + log_request(wsgi_req); } diff --git a/plugins/php/php_plugin.c b/plugins/php/php_plugin.c index 19fe6004..cd5d028a 100644 --- a/plugins/php/php_plugin.c +++ b/plugins/php/php_plugin.c @@ -911,8 +911,7 @@ secure2: void uwsgi_php_after_request(struct wsgi_request *wsgi_req) { - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) - log_request(wsgi_req); + log_request(wsgi_req); } diff --git a/plugins/psgi/psgi_plugin.c b/plugins/psgi/psgi_plugin.c index a365efa1..0bd17f2b 100644 --- a/plugins/psgi/psgi_plugin.c +++ b/plugins/psgi/psgi_plugin.c @@ -479,8 +479,7 @@ clear: void uwsgi_perl_after_request(struct wsgi_request *wsgi_req) { - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) - log_request(wsgi_req); + log_request(wsgi_req); if (wsgi_req->async_plagued) { uwsgi_log("*** psgix.harakiri.commit requested ***\n"); diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index a9fabdbe..bb818b39 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -543,29 +543,7 @@ void uwsgi_after_request_wsgi(struct wsgi_request *wsgi_req) { UWSGI_RELEASE_GIL } - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING] || wsgi_req->log_this) { - log_request(wsgi_req); - } - else { - if (uwsgi.shared->options[UWSGI_OPTION_LOG_ZERO]) { - if (wsgi_req->response_size == 0) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW]) { - if ((uint32_t) wsgi_req_time >= uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW]) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_4xx]) { - if (wsgi_req->status >= 400 && wsgi_req->status <= 499) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_5xx]) { - if (wsgi_req->status >= 500 && wsgi_req->status <= 599) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_BIG]) { - if (wsgi_req->response_size >= uwsgi.shared->options[UWSGI_OPTION_LOG_BIG]) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_SENDFILE]) { - if (wsgi_req->sendfile_fd > -1 && wsgi_req->sendfile_obj == wsgi_req->async_result) { log_request(wsgi_req); return; } - } - } + log_request(wsgi_req); } #ifdef UWSGI_SENDFILE diff --git a/plugins/rack/rack_plugin.c b/plugins/rack/rack_plugin.c index 8a21fc85..f81b9098 100644 --- a/plugins/rack/rack_plugin.c +++ b/plugins/rack/rack_plugin.c @@ -858,8 +858,7 @@ clear: void uwsgi_rack_after_request(struct wsgi_request *wsgi_req) { - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) - log_request(wsgi_req); + log_request(wsgi_req); } void uwsgi_rack_suspend(struct wsgi_request *wsgi_req) { diff --git a/protocol.c b/protocol.c index d6991d75..fd61e71a 100644 --- a/protocol.c +++ b/protocol.c @@ -114,6 +114,78 @@ void uwsgi_add_expires(struct wsgi_request *wsgi_req, char *filename, int filena } } +void uwsgi_add_expires_path_info(struct wsgi_request *wsgi_req, struct stat *st) { + + struct uwsgi_dyn_dict *udd = uwsgi.static_expires_path_info; + time_t now = wsgi_req->start_of_request.tv_sec; + // Expires+34+1 + char expires[42]; + + while(udd) { + if (uwsgi_regexp_match(udd->pattern, udd->pattern_extra, wsgi_req->path_info, wsgi_req->path_info_len) >= 0) { + int delta = uwsgi_str_num(udd->value, udd->vallen); + int size = set_http_date(now+delta, "Expires", 7, expires, 0); + if (size > 0) { + wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, expires, size); + wsgi_req->header_cnt++; + } + return; + } + udd = udd->next; + } + + udd = uwsgi.static_expires_path_info_mtime; + while(udd) { + if (uwsgi_regexp_match(udd->pattern, udd->pattern_extra, wsgi_req->path_info, wsgi_req->path_info_len) >= 0) { + int delta = uwsgi_str_num(udd->value, udd->vallen); + int size = set_http_date(st->st_mtime+delta, "Expires", 7, expires, 0); + if (size > 0) { + wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, expires, size); + wsgi_req->header_cnt++; + } + return; + } + udd = udd->next; + } +} + +void uwsgi_add_expires_uri(struct wsgi_request *wsgi_req, struct stat *st) { + + struct uwsgi_dyn_dict *udd = uwsgi.static_expires_uri; + time_t now = wsgi_req->start_of_request.tv_sec; + // Expires+34+1 + char expires[42]; + + while(udd) { + if (uwsgi_regexp_match(udd->pattern, udd->pattern_extra, wsgi_req->uri, wsgi_req->uri_len) >= 0) { + int delta = uwsgi_str_num(udd->value, udd->vallen); + int size = set_http_date(now+delta, "Expires", 7, expires, 0); + if (size > 0) { + wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, expires, size); + wsgi_req->header_cnt++; + } + return; + } + udd = udd->next; + } + + udd = uwsgi.static_expires_uri_mtime; + while(udd) { + if (uwsgi_regexp_match(udd->pattern, udd->pattern_extra, wsgi_req->uri, wsgi_req->uri_len) >= 0) { + int delta = uwsgi_str_num(udd->value, udd->vallen); + int size = set_http_date(st->st_mtime+delta, "Expires", 7, expires, 0); + if (size > 0) { + wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, expires, size); + wsgi_req->header_cnt++; + } + return; + } + udd = udd->next; + } +} + + + #endif @@ -1812,6 +1884,8 @@ int uwsgi_real_file_serve(struct wsgi_request *wsgi_req, char *real_filename, si #ifdef UWSGI_PCRE uwsgi_add_expires(wsgi_req, real_filename, real_filename_len, st); + uwsgi_add_expires_path_info(wsgi_req, st); + uwsgi_add_expires_uri(wsgi_req, st); #endif // Content-Type (if available) diff --git a/uwsgi.c b/uwsgi.c index 0d614662..177041fa 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -397,6 +397,12 @@ static struct uwsgi_option uwsgi_base_options[] = { #ifdef UWSGI_PCRE {"static-expires", required_argument, 0, "set the Expires header based on filename regexp", uwsgi_opt_add_regexp_dyn_dict, &uwsgi.static_expires, UWSGI_OPT_MIME}, {"static-expires-mtime", required_argument, 0, "set the Expires header based on filename regexp and file mtime", uwsgi_opt_add_regexp_dyn_dict, &uwsgi.static_expires_mtime, UWSGI_OPT_MIME}, + + {"static-expires-uri", required_argument, 0, "set the Expires header based on REQUEST_URI regexp", uwsgi_opt_add_regexp_dyn_dict, &uwsgi.static_expires_uri, UWSGI_OPT_MIME}, + {"static-expires-uri-mtime", required_argument, 0, "set the Expires header based on REQUEST_URI regexp and file mtime", uwsgi_opt_add_regexp_dyn_dict, &uwsgi.static_expires_uri_mtime, UWSGI_OPT_MIME}, + + {"static-expires-path-info", required_argument, 0, "set the Expires header based on PATH_INFO regexp", uwsgi_opt_add_regexp_dyn_dict, &uwsgi.static_expires_path_info, UWSGI_OPT_MIME}, + {"static-expires-path-info-mtime", required_argument, 0, "set the Expires header based on PATH_INFO regexp and file mtime", uwsgi_opt_add_regexp_dyn_dict, &uwsgi.static_expires_path_info_mtime, UWSGI_OPT_MIME}, #endif {"static-offload-to-thread", required_argument, 0, "offload static file serving to a thread (upto the specified number of threads)", uwsgi_opt_set_int, &uwsgi.static_offload_to_thread, UWSGI_OPT_MIME}, diff --git a/uwsgi.h b/uwsgi.h index 6ad887de..0a72405d 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1319,6 +1319,12 @@ struct uwsgi_server { struct uwsgi_dyn_dict *static_expires; struct uwsgi_dyn_dict *static_expires_mtime; + struct uwsgi_dyn_dict *static_expires_uri; + struct uwsgi_dyn_dict *static_expires_uri_mtime; + + struct uwsgi_dyn_dict *static_expires_path_info; + struct uwsgi_dyn_dict *static_expires_path_info_mtime; + int check_static_docroot; int static_offload_to_thread; pthread_attr_t static_offload_thread_attr;