Commit c65bf8e3 authored by Federico G. Schwindt's avatar Federico G. Schwindt

Include the Host header if not present

Enforced for HTTP/1.1 requests. To disable it use -nohost.
parent 074d66ad
......@@ -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
......
......@@ -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
......
......@@ -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;
......
......@@ -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);
......
......@@ -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");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment