[master] cbd7675a2 Avoid setting the http response twice via http_PutResponse()

Nils Goroll nils.goroll at uplex.de
Wed Oct 30 15:07:08 UTC 2019


commit cbd7675a263195ae62207d438d54e1855bfd68b8
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Oct 21 11:31:54 2019 +0200

    Avoid setting the http response twice via http_PutResponse()

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 778db6861..40f1f9185 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -628,7 +628,7 @@ double http_GetHdrQ(const struct http *hp, const char *hdr, const char *field);
 ssize_t http_GetContentLength(const struct http *hp);
 uint16_t http_GetStatus(const struct http *hp);
 int http_IsStatus(const struct http *hp, int);
-void http_SetStatus(struct http *to, uint16_t status);
+void http_SetStatus(struct http *to, uint16_t status, const char *reason);
 const char *http_GetMethod(const struct http *hp);
 int http_HdrIs(const struct http *hp, const char *hdr, const char *val);
 void http_CopyHome(const struct http *hp);
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index ec65a9cb6..5bf32db7b 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -798,10 +798,9 @@ http_IsStatus(const struct http *hp, int val)
  */
 
 void
-http_SetStatus(struct http *to, uint16_t status)
+http_SetStatus(struct http *to, uint16_t status, const char *reason)
 {
 	char buf[4];
-	const char *reason;
 	const char *sstr = NULL;
 
 	CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
@@ -813,7 +812,11 @@ http_SetStatus(struct http *to, uint16_t status)
 	status %= 1000;
 	assert(status >= 100);
 
-	reason = http_Status2Reason(status, &sstr);
+	if (reason == NULL)
+		reason = http_Status2Reason(status, &sstr);
+	else
+		(void)http_Status2Reason(status, &sstr);
+
 	if (sstr) {
 		http_SetH(to, HTTP_HDR_STATUS, sstr);
 	} else {
@@ -865,9 +868,7 @@ http_PutResponse(struct http *to, const char *proto, uint16_t status,
 	CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
 	if (proto != NULL)
 		http_SetH(to, HTTP_HDR_PROTO, proto);
-	http_SetStatus(to, status);
-	if (reason != NULL)
-		http_SetH(to, HTTP_HDR_REASON, reason);
+	http_SetStatus(to, status, reason);
 }
 
 /*--------------------------------------------------------------------
diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index 2b471930e..7ed80cfd6 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -108,7 +108,7 @@ VRT_l_##obj##_status(VRT_CTX, VCL_INT num)				\
 		VRT_fail(ctx, "illegal %s.status (%jd) (..0##)",	\
 		    #obj, (intmax_t)num);				\
 	else								\
-		http_SetStatus(ctx->http_##obj, (uint16_t)num);		\
+		http_SetStatus(ctx->http_##obj, (uint16_t)num, NULL);	\
 }
 
 #define VRT_STATUS_R(obj)						\
diff --git a/bin/varnishd/http1/cache_http1_proto.c b/bin/varnishd/http1/cache_http1_proto.c
index 31c75ed88..7fe4422db 100644
--- a/bin/varnishd/http1/cache_http1_proto.c
+++ b/bin/varnishd/http1/cache_http1_proto.c
@@ -453,7 +453,7 @@ HTTP1_DissectResponse(struct http_conn *htc, struct http *hp,
 		    (int)(htc->rxbuf_e - htc->rxbuf_b), htc->rxbuf_b);
 		assert(retval >= 100 && retval <= 999);
 		assert(retval == 503);
-		http_SetStatus(hp, 503);
+		http_SetStatus(hp, 503, NULL);
 	}
 
 	if (hp->hd[HTTP_HDR_REASON].b == NULL ||
diff --git a/bin/varnishtest/tests/c00018.vtc b/bin/varnishtest/tests/c00018.vtc
index cff0bebde..bb542b48f 100644
--- a/bin/varnishtest/tests/c00018.vtc
+++ b/bin/varnishtest/tests/c00018.vtc
@@ -125,8 +125,7 @@ logexpect l1 -v v1 -g raw {
 	expect 0 1011	RespProtocol    {^HTTP/1.1$}
 	expect 0 1011	RespStatus      {^405$}
 	expect 0 1011	RespReason      {^Method Not Allowed$}
-	# XXX dup RespReason
-	expect 1 1011	RespHeader      {^Date:}
+	expect 0 1011	RespHeader      {^Date:}
 	expect 0 1011	RespHeader      {^Server: Varnish$}
 	expect 0 1011	RespHeader      {^X-Varnish: 1011$}
 	expect * 1011	Timestamp       {^Process:}


More information about the varnish-commit mailing list