added options for setting Expires based on REQUEST_URI and PATH_INFO

This commit is contained in:
roberto@quantal64
2012-06-13 10:53:21 +02:00
parent 13c112430d
commit 8e10efd027
10 changed files with 121 additions and 33 deletions
+29
View File
@@ -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) {
+1 -2
View File
@@ -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);
}
+1 -2
View File
@@ -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);
}
+1 -2
View File
@@ -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);
}
+1 -2
View File
@@ -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");
+1 -23
View File
@@ -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
+1 -2
View File
@@ -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) {
+74
View File
@@ -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)
+6
View File
@@ -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},
+6
View File
@@ -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;