[PATCH 2/2] Add iteration methods for packed headers and use them in HTTP_GetHdrPack

Martin Blix Grydeland martin at varnish-software.com
Fri Sep 4 12:57:53 CEST 2015


---
 bin/varnishd/cache/cache.h      |  2 ++
 bin/varnishd/cache/cache_http.c | 69 ++++++++++++++++++++++++++++++-----------
 2 files changed, 53 insertions(+), 18 deletions(-)

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 648dad4..fca8fb7 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -831,6 +831,8 @@ void http_CollectHdr(struct http *hp, const char *hdr);
 void http_VSL_log(const struct http *hp);
 void HTTP_Merge(struct worker *, struct objcore *, struct http *to);
 uint16_t HTTP_GetStatusPack(struct worker *, struct objcore *oc);
+const char *HTTP_FirstHdrPack(struct worker *, struct objcore *);
+const char *HTTP_NextHdrPack(const char *);
 const char *HTTP_GetHdrPack(struct worker *, struct objcore *,
     const char *hdr);
 enum sess_close http_DoConnection(struct http *hp);
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index a3aeba3..268b5b3 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -891,6 +891,38 @@ HTTP_GetStatusPack(struct worker *wrk, struct objcore *oc)
 
 /*--------------------------------------------------------------------*/
 
+/* Get the first packed header */
+const char *
+HTTP_FirstHdrPack(struct worker *wrk, struct objcore *oc)
+{
+	const char *ptr;
+
+	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
+	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+
+	ptr = ObjGetattr(wrk, oc, OA_HEADERS, NULL);
+	AN(ptr);
+	ptr += 4;	/* Skip nhd and status */
+	ptr = strchr(ptr, '\0') + 1; /* Skip PROTO */
+	ptr = strchr(ptr, '\0') + 1; /* Skip STATUS */
+	ptr = strchr(ptr, '\0') + 1; /* Skip REASON */
+	if (*ptr == '\0')
+		return (NULL);
+	return (ptr);
+}
+
+/* Get the next packed header */
+const char *
+HTTP_NextHdrPack(const char *prev)
+{
+
+	AN(prev);
+	prev = strchr(prev, '\0') + 1;
+	if (*prev == '\0')
+		return (NULL);
+	return (prev);
+}
+
 const char *
 HTTP_GetHdrPack(struct worker *wrk, struct objcore *oc, const char *hdr)
 {
@@ -907,33 +939,34 @@ HTTP_GetHdrPack(struct worker *wrk, struct objcore *oc, const char *hdr)
 	assert(hdr[l] == ':');
 	hdr++;
 
-	ptr = ObjGetattr(wrk, oc, OA_HEADERS, NULL);
-	AN(ptr);
+	if (hdr[0] == ':') {
+		/* Special cases */
+		ptr = ObjGetattr(wrk, oc, OA_HEADERS, NULL);
+		AN(ptr);
+		ptr += 4;	/* Skip nhd and status */
 
-	/* Skip nhd and status */
-	ptr += 4;
-	VSL(SLT_Debug, 0, "%d %s", __LINE__, ptr);
-
-	/* Skip PROTO, STATUS and REASON */
-	if (!strcmp(hdr, ":proto:"))
-		return (ptr);
-	ptr = strchr(ptr, '\0') + 1;
-	if (!strcmp(hdr, ":status:"))
-		return (ptr);
-	ptr = strchr(ptr, '\0') + 1;
-	if (!strcmp(hdr, ":reason:"))
-		return (ptr);
-	ptr = strchr(ptr, '\0') + 1;
+		if (!strcmp(hdr, ":proto:"))
+			return (ptr);
+		ptr = strchr(ptr, '\0') + 1;
+		if (!strcmp(hdr, ":status:"))
+			return (ptr);
+		ptr = strchr(ptr, '\0') + 1;
+		if (!strcmp(hdr, ":reason:"))
+			return (ptr);
+		WRONG("Unknown magic packed header");
+	}
 
-	while (*ptr != '\0') {
+	for (ptr = HTTP_FirstHdrPack(wrk, oc);
+	     ptr != NULL;
+	     ptr = HTTP_NextHdrPack(ptr)) {
 		if (!strncasecmp(ptr, hdr, l)) {
 			ptr += l;
 			while (vct_islws(*ptr))
 				ptr++;
 			return (ptr);
 		}
-		ptr = strchr(ptr, '\0') + 1;
 	}
+
 	return (NULL);
 }
 
-- 
2.1.4




More information about the varnish-dev mailing list