]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 4563: duplicate code in httpMakeVaryMark
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 17 Aug 2016 05:10:37 +0000 (17:10 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 17 Aug 2016 05:10:37 +0000 (17:10 +1200)
src/http.cc

index 1dd1e6d4321b02c76eb2f8584bff743ba0336fd6..80bac019a96e13b018bae55c93757b01251deada 100644 (file)
@@ -572,43 +572,27 @@ HttpStateData::cacheableReply()
     /* NOTREACHED */
 }
 
-/*
- * For Vary, store the relevant request headers as
- * virtual headers in the reply
- * Returns an empty SBuf if the variance cannot be stored
- */
-SBuf
-httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
+/// assemble a variant key (vary-mark) from the given Vary header and HTTP request
+static void
+assembleVaryKey(String &vary, SBuf &vstr, const HttpRequest &request)
 {
-    String vary, hdr;
-    const char *pos = NULL;
-    const char *item;
-    const char *value;
-    int ilen;
-    SBuf vstr;
     static const SBuf asterisk("*");
-
-    vary = reply->header.getList(HDR_VARY);
+    const char *pos = nullptr;
+    const char *item = nullptr;
+    int ilen = 0;
 
     while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
-        char *name = (char *)xmalloc(ilen + 1);
-        xstrncpy(name, item, ilen + 1);
-        Tolower(name);
-
-        if (strcmp(name, "*") == 0) {
-            /* Can not handle "Vary: *" withtout ETag support */
-            safe_free(name);
+        SBuf name(item, ilen);
+        if (name == asterisk) {
             vstr.clear();
             break;
         }
-
+        name.toLower();
         if (!vstr.isEmpty())
             vstr.append(", ", 2);
         vstr.append(name);
-        hdr = request->header.getByName(name);
-        safe_free(name);
-        value = hdr.termedBuf();
-
+        String hdr(request.header.getByName(name.c_str()));
+        const char *value = hdr.termedBuf();
         if (value) {
             value = rfc1738_escape_part(value);
             vstr.append("=\"", 2);
@@ -618,43 +602,26 @@ httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
 
         hdr.clean();
     }
+}
 
-    vary.clean();
-#if X_ACCELERATOR_VARY
-
-    pos = NULL;
-    vary = reply->header.getList(HDR_X_ACCELERATOR_VARY);
-
-    while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
-        char *name = (char *)xmalloc(ilen + 1);
-        xstrncpy(name, item, ilen + 1);
-        Tolower(name);
-
-        if (strcmp(name, "*") == 0) {
-            /* Can not handle "Vary: *" withtout ETag support */
-            safe_free(name);
-            vstr.clear();
-            break;
-        }
-
-        if (!vstr.isEmpty())
-            vstr.append(", ", 2);
-        vstr.append(name);
-        hdr = request->header.getByName(name);
-        safe_free(name);
-        value = hdr.termedBuf();
-
-        if (value) {
-            value = rfc1738_escape_part(value);
-            vstr.append("=\"", 2);
-            vstr.append(value);
-            vstr.append("\"", 1);
-        }
+/*
+ * For Vary, store the relevant request headers as
+ * virtual headers in the reply
+ * Returns an empty SBuf if the variance cannot be stored
+ */
+SBuf
+httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
+{
+    SBuf vstr;
+    String vary;
 
-        hdr.clean();
-    }
+    vary = reply->header.getList(HDR_VARY);
+    assembleVaryKey(vary, vstr, *request);
 
+#if X_ACCELERATOR_VARY
     vary.clean();
+    vary = reply->header.getList(HDR_X_ACCELERATOR_VARY);
+    assembleVaryKey(vary, vstr, *request);
 #endif
 
     debugs(11, 3, vstr);