[master] c65bf8e Include the Host header if not present

Federico G. Schwindt fgsch at lodoss.net
Thu Mar 29 04:27:12 UTC 2018


commit c65bf8e335e30a6e2dd5d720c62c0d457ebbe634
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Thu Mar 29 00:09:16 2018 -0300

    Include the Host header if not present
    
    Enforced for HTTP/1.1 requests. To disable it use -nohost.

diff --git a/bin/varnishtest/tests/r01847.vtc b/bin/varnishtest/tests/r01847.vtc
index ac764ee..8249cd2 100644
--- a/bin/varnishtest/tests/r01847.vtc
+++ b/bin/varnishtest/tests/r01847.vtc
@@ -24,7 +24,7 @@ client c1 {
 	expect resp.http.rxhost == www.example.com
 	expect resp.http.rxurl == /bar
 
-	txreq -url https://www.example.com/bar -hdr "Host: ${localhost}"
+	txreq -url https://www.example.com/bar
 	rxresp
 	expect resp.http.rxhost == "${localhost}"
 	expect resp.http.rxurl == https://www.example.com/bar
diff --git a/bin/varnishtest/tests/u00003.vtc b/bin/varnishtest/tests/u00003.vtc
index 197e5a8..e18d9b9 100644
--- a/bin/varnishtest/tests/u00003.vtc
+++ b/bin/varnishtest/tests/u00003.vtc
@@ -32,9 +32,9 @@ shell {
 delay 1
 
 client c1 {
-	txreq -url /1?foo=bar -hdr "authorization: basic dXNlcjpwYXNz" -hdr "Host: ${localhost}"
+	txreq -url /1?foo=bar -hdr "authorization: basic dXNlcjpwYXNz"
 	rxresp
-	txreq -url /1?foo=bar -hdr "baz: qux" -hdr "Host: ${localhost}"
+	txreq -url /1?foo=bar -hdr "baz: qux"
 	rxresp
 } -run
 
@@ -44,7 +44,7 @@ shell "mv ${tmpdir}/ncsa.log ${tmpdir}/ncsa.old.log"
 shell "kill -HUP `cat ${tmpdir}/ncsa.pid`"
 
 client c1 {
-	txreq -url /2 -hdr "Host: ${localhost}"
+	txreq -url /2
 	rxresp
 } -run
 
diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index 1f72492..ab0ff6e 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -172,7 +172,7 @@ macro_undef(struct vtclog *vl, const char *instance, const char *name)
 	AZ(pthread_mutex_unlock(&macro_mtx));
 }
 
-static char *
+char *
 macro_get(const char *b, const char *e)
 {
 	struct macro *m;
diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index 8878e1d..f5b8e35 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -114,6 +114,7 @@ void macro_undef(struct vtclog *vl, const char *instance, const char *name);
 void macro_def(struct vtclog *vl, const char *instance, const char *name,
     const char *fmt, ...)
     v_printflike_(4, 5);
+char *macro_get(const char *, const char *);
 struct vsb *macro_expand(struct vtclog *vl, const char *text);
 struct vsb *macro_expandf(struct vtclog *vl, const char *, ...)
     v_printflike_(2, 3);
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index e5857e7..0634d66 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -832,11 +832,12 @@ gzip_body(const struct http *hp, const char *txt, char **body, int *bodylen)
 
 static char* const *
 http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
-    char* body)
+    char *body, unsigned nohost)
 {
 	int bodylen = 0;
 	char *b, *c;
 	char *nullbody;
+	char *m;
 	int nolen = 0;
 	int l;
 
@@ -846,7 +847,11 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
 	for (; *av != NULL; av++) {
 		if (!strcmp(*av, "-nolen")) {
 			nolen = 1;
+		} else if (!strcmp(*av, "-nohost")) {
+			nohost = 1;
 		} else if (!strcmp(*av, "-hdr")) {
+			if (!strncasecmp(av[1], "Host:", 5))
+				nohost = 1;
 			VSB_printf(hp->vsb, "%s%s", av[1], nl);
 			av++;
 		} else if (!strcmp(*av, "-hdrlen")) {
@@ -904,6 +909,12 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
 		} else
 			break;
 	}
+	if (!nohost) {
+		m = macro_get("localhost", NULL);
+		AN(m);
+		VSB_printf(hp->vsb, "Host: %s%s", m, nl);
+		free(m);
+	}
 	if (body != NULL && !nolen)
 		VSB_printf(hp->vsb, "Content-Length: %d%s", bodylen, nl);
 	VSB_cat(hp->vsb, nl);
@@ -944,8 +955,11 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
  *         These three switches can appear in any order but must come before the
  *         following ones.
  *
+ *         \-nohost
+ *                 Don't include a Host header in the request.
+ *
  *         \-nolen
- *                 Don't include a Content-Length header in the response.
+ *                 Don't include a Content-Length header.
  *
  *         \-hdr STRING
  *                 Add STRING as a header, it must follow this format:
@@ -1019,7 +1033,7 @@ cmd_http_txresp(CMD_ARGS)
 	/* send a "Content-Length: 0" header unless something else happens */
 	REPLACE(body, "");
 
-	av = http_tx_parse_args(av, vl, hp, body);
+	av = http_tx_parse_args(av, vl, hp, body, 1);
 	if (*av != NULL)
 		vtc_fatal(hp->vl, "Unknown http txresp spec: %s\n", *av);
 
@@ -1207,6 +1221,7 @@ cmd_http_txreq(CMD_ARGS)
 	const char *url = "/";
 	const char *proto = "HTTP/1.1";
 	const char *up = NULL;
+	unsigned nohost;
 
 	(void)cmd;
 	(void)vl;
@@ -1240,7 +1255,8 @@ cmd_http_txreq(CMD_ARGS)
 				"Upgrade: h2c%s"
 				"HTTP2-Settings: %s%s", nl, nl, up, nl);
 
-	av = http_tx_parse_args(av, vl, hp, NULL);
+	nohost = strcasecmp(proto, "HTTP/1.1") != 0;
+	av = http_tx_parse_args(av, vl, hp, NULL, nohost);
 	if (*av != NULL)
 		vtc_fatal(hp->vl, "Unknown http txreq spec: %s\n", *av);
 	http_write(hp, 4, "txreq");


More information about the varnish-commit mailing list