changeset 2353:640e41cd0742 stable-0.6

r2163, r2164, r2165 merge: *) ngx_next_time() *) expires daily time
author Igor Sysoev <igor@sysoev.ru>
date Thu, 27 Nov 2008 14:40:35 +0000
parents 4521f4615506
children 7f6f4568574d
files src/core/ngx_times.c src/core/ngx_times.h src/http/modules/ngx_http_headers_filter_module.c
diffstat 3 files changed, 74 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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;
+}
--- 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_
 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;
 
--- 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 
         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 
         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,
         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,
         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";
     }