[nginx] Cache: normalization of some Vary headers.

Maxim Dounin mdounin at mdounin.ru
Mon Oct 27 18:20:16 UTC 2014


details:   http://hg.nginx.org/nginx/rev/ee9230cd4bda
branches:  
changeset: 5881:ee9230cd4bda
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Mon Oct 27 21:14:12 2014 +0300
description:
Cache: normalization of some Vary headers.

Spaces in Accept-Charset, Accept-Encoding, and Accept-Language headers
are now ignored.  As per syntax of these headers spaces can only appear
in places where they are optional.

diffstat:

 src/http/ngx_http_file_cache.c |  68 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 66 insertions(+), 2 deletions(-)

diffs (88 lines):

diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -951,10 +951,34 @@ static void
 ngx_http_file_cache_vary_header(ngx_http_request_t *r, ngx_md5_t *md5,
     ngx_str_t *name)
 {
-    ngx_uint_t        i;
+    size_t            len;
+    u_char           *p, *start, *last;
+    ngx_uint_t        i, multiple, normalize;
     ngx_list_part_t  *part;
     ngx_table_elt_t  *header;
 
+    multiple = 0;
+    normalize = 0;
+
+    if (name->len == sizeof("Accept-Charset") - 1
+        && ngx_strncasecmp(name->data, (u_char *) "Accept-Charset",
+                           sizeof("Accept-Charset") - 1) == 0)
+    {
+        normalize = 1;
+
+    } else if (name->len == sizeof("Accept-Encoding") - 1
+        && ngx_strncasecmp(name->data, (u_char *) "Accept-Encoding",
+                           sizeof("Accept-Encoding") - 1) == 0)
+    {
+        normalize = 1;
+
+    } else if (name->len == sizeof("Accept-Language") - 1
+        && ngx_strncasecmp(name->data, (u_char *) "Accept-Language",
+                           sizeof("Accept-Language") - 1) == 0)
+    {
+        normalize = 1;
+    }
+
     part = &r->headers_in.headers.part;
     header = part->elts;
 
@@ -982,7 +1006,47 @@ ngx_http_file_cache_vary_header(ngx_http
             continue;
         }
 
-        ngx_md5_update(md5, header[i].value.data, header[i].value.len);
+        if (!normalize) {
+
+            if (multiple) {
+                ngx_md5_update(md5, (u_char *) ",", sizeof(",") - 1);
+            }
+
+            ngx_md5_update(md5, header[i].value.data, header[i].value.len);
+
+            multiple = 1;
+
+            continue;
+        }
+
+        /* normalize spaces */
+
+        p = header[i].value.data;
+        start = p;
+        last = p + header[i].value.len;
+
+        while (p < last) {
+
+            while (p < last && (*p == ' ' || *p == ',')) { p++; }
+
+            start = p;
+
+            while (p < last && *p != ',' && *p != ' ') { p++; }
+
+            len = p - start;
+
+            if (len == 0) {
+                break;
+            }
+
+            if (multiple) {
+                ngx_md5_update(md5, (u_char *) ",", sizeof(",") - 1);
+            }
+
+            ngx_md5_update(md5, start, len);
+
+            multiple = 1;
+        }
     }
 }
 



More information about the nginx-devel mailing list