diff options
author | Igor Sysoev <igor@sysoev.ru> | 2008-11-27 17:40:35 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2008-11-27 17:40:35 +0300 |
commit | 9ad0c07bd947fcf47483f21147a0d511d4e2fe38 (patch) | |
tree | ab4f555100df0fc725c6119af25e8c3e5a0a4ed8 | |
parent | fe3fe0e3bfbcfadce5ce7b41a244b8c77abca942 (diff) |
r2163, r2164, r2165 merge:
*) ngx_next_time()
*) expires daily time
-rw-r--r-- | src/core/ngx_times.c | 39 | ||||
-rw-r--r-- | src/core/ngx_times.h | 3 | ||||
-rw-r--r-- | src/http/modules/ngx_http_headers_filter_module.c | 39 |
3 files changed, 74 insertions, 7 deletions
diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c index c1798b5bc..99b25813b 100644 --- a/src/core/ngx_times.c +++ b/src/core/ngx_times.c @@ -291,3 +291,42 @@ ngx_gmtime(time_t t, ngx_tm_t *tp) tp->ngx_tm_year = (ngx_tm_year_t) year; tp->ngx_tm_wday = (ngx_tm_wday_t) wday; } + + +time_t +ngx_next_time(time_t when) +{ + time_t now, next; + struct tm tm; + + now = ngx_time(); + + ngx_libc_localtime(now, &tm); + + tm.tm_hour = (int) (when / 3600); + when %= 3600; + tm.tm_min = (int) (when / 60); + tm.tm_sec = (int) (when % 60); + + next = mktime(&tm); + + if (next == -1) { + return -1; + } + + if (next - now > 0) { + return next; + } + + tm.tm_mday++; + + /* mktime() should normalize a date (Jan 32, etc) */ + + next = mktime(&tm); + + if (next != -1) { + return next; + } + + return -1; +} diff --git a/src/core/ngx_times.h b/src/core/ngx_times.h index 6e7ab638c..8363ca136 100644 --- a/src/core/ngx_times.h +++ b/src/core/ngx_times.h @@ -25,6 +25,9 @@ u_char *ngx_http_time(u_char *buf, time_t t); u_char *ngx_http_cookie_time(u_char *buf, time_t t); void ngx_gmtime(time_t t, ngx_tm_t *tp); +time_t ngx_next_time(time_t when); +#define ngx_next_time_n "mktime()" + extern volatile ngx_time_t *ngx_cached_time; diff --git a/src/http/modules/ngx_http_headers_filter_module.c b/src/http/modules/ngx_http_headers_filter_module.c index ad9467ba9..9adc04dd1 100644 --- a/src/http/modules/ngx_http_headers_filter_module.c +++ b/src/http/modules/ngx_http_headers_filter_module.c @@ -36,6 +36,7 @@ struct ngx_http_header_val_s { #define NGX_HTTP_EXPIRES_MAX 2 #define NGX_HTTP_EXPIRES_ACCESS 3 #define NGX_HTTP_EXPIRES_MODIFIED 4 +#define NGX_HTTP_EXPIRES_DAILY 5 typedef struct { @@ -187,7 +188,7 @@ static ngx_int_t ngx_http_set_expires(ngx_http_request_t *r, ngx_http_headers_conf_t *conf) { size_t len; - time_t since; + time_t now, expires_time, max_age; ngx_uint_t i; ngx_table_elt_t *expires, *cc, **ccp; @@ -279,16 +280,24 @@ ngx_http_set_expires(ngx_http_request_t *r, ngx_http_headers_conf_t *conf) return NGX_OK; } + now = ngx_time(); + if (conf->expires == NGX_HTTP_EXPIRES_ACCESS || r->headers_out.last_modified_time == -1) { - since = ngx_time(); + expires_time = now + conf->expires_time; + max_age = conf->expires_time; + + } else if (conf->expires == NGX_HTTP_EXPIRES_DAILY) { + expires_time = ngx_next_time(conf->expires_time); + max_age = expires_time - now; } else { - since = r->headers_out.last_modified_time; + expires_time = r->headers_out.last_modified_time + conf->expires_time; + max_age = expires_time - now; } - ngx_http_time(expires->value.data, since + conf->expires_time); + ngx_http_time(expires->value.data, expires_time); if (conf->expires_time < 0) { cc->value.len = sizeof("no-cache") - 1; @@ -303,8 +312,7 @@ ngx_http_set_expires(ngx_http_request_t *r, ngx_http_headers_conf_t *conf) return NGX_ERROR; } - cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T", - since + conf->expires_time - ngx_time()) + cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T", max_age) - cc->value.data; return NGX_OK; @@ -514,7 +522,18 @@ ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) n = 2; } - if (value[n].data[0] == '+') { + if (value[n].data[0] == '@') { + value[n].data++; + value[n].len--; + minus = 0; + + if (hcf->expires == NGX_HTTP_EXPIRES_MODIFIED) { + return "daily time can not be used with \"modified\" parameter"; + } + + hcf->expires = NGX_HTTP_EXPIRES_DAILY; + + } else if (value[n].data[0] == '+') { value[n].data++; value[n].len--; minus = 0; @@ -534,6 +553,12 @@ ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return "invalid value"; } + if (hcf->expires == NGX_HTTP_EXPIRES_DAILY + && hcf->expires_time > 24 * 60 * 60) + { + return "daily time value must be less than 24 hours"; + } + if (hcf->expires_time == NGX_PARSE_LARGE_TIME) { return "value must be less than 68 years"; } |