From aaa12c1686d12bc2ead07e4ceef6ac36ec9b699e Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Wed, 17 Aug 2016 17:10:37 +1200 Subject: [PATCH] Bug 4563: duplicate code in httpMakeVaryMark --- src/http.cc | 87 +++++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 60 deletions(-) diff --git a/src/http.cc b/src/http.cc index 1dd1e6d432..80bac019a9 100644 --- a/src/http.cc +++ b/src/http.cc @@ -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); -- 2.47.2