From ef40077cd779e3a68323d50617cac4d80a63fdd5 Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 11 Jul 2008 21:02:05 +0000 Subject: [PATCH] Add a backend property ".host_header" to set default content of Host: header if client didn't provide one. If .host_header is not specified, it defaults to .host. Add testcase for this logic. (Incidental change: Use "interval" instead of "rate" for backend pollers.) git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2938 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- .../bin/varnishd/cache_backend_cfg.c | 2 +- .../bin/varnishtest/tests/v00008.vtc | 44 +++++++++++++++ varnish-cache/include/vrt.h | 10 ++-- varnish-cache/lib/libvcl/vcc_backend.c | 55 ++++++++++++++----- varnish-cache/lib/libvcl/vcc_fixed_token.c | 10 ++-- 5 files changed, 95 insertions(+), 26 deletions(-) create mode 100644 varnish-cache/bin/varnishtest/tests/v00008.vtc diff --git a/varnish-cache/bin/varnishd/cache_backend_cfg.c b/varnish-cache/bin/varnishd/cache_backend_cfg.c index 0a24bb64..8d05076a 100644 --- a/varnish-cache/bin/varnishd/cache_backend_cfg.c +++ b/varnish-cache/bin/varnishd/cache_backend_cfg.c @@ -219,7 +219,7 @@ VBE_AddBackend(struct cli *cli, const struct vrt_backend *vb) */ REPLACE(b->ident, vb->ident); REPLACE(b->vcl_name, vb->vcl_name); - REPLACE(b->hosthdr, vb->hostname); + REPLACE(b->hosthdr, vb->hosthdr); b->connect_timeout = vb->connect_timeout; diff --git a/varnish-cache/bin/varnishtest/tests/v00008.vtc b/varnish-cache/bin/varnishtest/tests/v00008.vtc new file mode 100644 index 00000000..84e9e88a --- /dev/null +++ b/varnish-cache/bin/varnishtest/tests/v00008.vtc @@ -0,0 +1,44 @@ +# $Id$ + +test "Test host header specification" + +server s1 { + rxreq + expect req.url == "/foo" + expect req.http.host == "snafu" + txresp -body "foo1" + + rxreq + expect req.url == "/bar" + expect req.http.host == "127.0.0.1" + txresp -body "foo1" +} -start + +varnish v1 -vcl+backend { } -start + +client c1 { + txreq -url "/foo" -hdr "Host: snafu" + rxresp + txreq -url "/bar" + rxresp +} -run + +server s2 -listen 127.0.0.1:9180 { + rxreq + expect req.url == "/barf" + expect req.http.host == "FOObar" + txresp -body "foo1" +} -start + +varnish v1 -vcl { + backend b1 { + .host = "127.0.0.1"; + .port = "9180"; + .host_header = "FOObar"; + } +} + +client c1 { + txreq -url "/barf" + rxresp +} -run diff --git a/varnish-cache/include/vrt.h b/varnish-cache/include/vrt.h index e7290dcb..7ef1a4b7 100644 --- a/varnish-cache/include/vrt.h +++ b/varnish-cache/include/vrt.h @@ -48,21 +48,21 @@ struct sockaddr; struct vrt_backend_probe { char *request; double timeout; - double rate; + double interval; }; /* * A backend is a host+port somewhere on the network */ struct vrt_backend { - char *portname; - char *hostname; + char *vcl_name; + char *ident; + + char *hosthdr; const unsigned char *ipv4_sockaddr; const unsigned char *ipv6_sockaddr; - char *vcl_name; - char *ident; double connect_timeout; struct vrt_backend_probe probe; }; diff --git a/varnish-cache/lib/libvcl/vcc_backend.c b/varnish-cache/lib/libvcl/vcc_backend.c index d8367071..0f43b548 100644 --- a/varnish-cache/lib/libvcl/vcc_backend.c +++ b/varnish-cache/lib/libvcl/vcc_backend.c @@ -336,7 +336,12 @@ vcc_ParseProbe(struct tokenlist *tl) struct token *t_field; struct token *t_did = NULL; - fs = vcc_FldSpec(tl, "?url", "?request", "?timeout", "?rate", NULL); + fs = vcc_FldSpec(tl, + "?url", + "?request", + "?timeout", + "?interval", + NULL); ExpectErr(tl, '{'); vcc_NextToken(tl); @@ -376,8 +381,8 @@ vcc_ParseProbe(struct tokenlist *tl) vcc_TimeVal(tl); ERRCHK(tl); Fb(tl, 0, ",\n"); - } else if (vcc_IdIs(t_field, "rate")) { - Fb(tl, 0, "\t\t.rate = "); + } else if (vcc_IdIs(t_field, "interval")) { + Fb(tl, 0, "\t\t.interval = "); vcc_TimeVal(tl); ERRCHK(tl); Fb(tl, 0, ",\n"); @@ -421,12 +426,17 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name, const struct token *t_first; struct token *t_host = NULL; struct token *t_port = NULL; + struct token *t_hosthdr = NULL; const char *ep; struct fld_spec *fs; struct vsb *vsb; fs = vcc_FldSpec(tl, - "!host", "?port", "?connect_timeout", "?probe", NULL); + "!host", + "?port", + "?host_header", + "?connect_timeout", + "?probe", NULL); t_first = tl->t; ExpectErr(tl, '{'); @@ -439,6 +449,11 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name, const *nbh = tl->nbackend_host++; Fb(tl, 0, "\nstatic const struct vrt_backend bh_%d = {\n", *nbh); + Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(name)); + if (serial) + Fb(tl, 0, "[%d]", serial); + Fb(tl, 0, "\",\n"); + /* Check for old syntax */ if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) { vsb_printf(tl->sb, @@ -471,6 +486,13 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name, const vcc_NextToken(tl); ExpectErr(tl, ';'); vcc_NextToken(tl); + } else if (vcc_IdIs(t_field, "host_header")) { + ExpectErr(tl, CSTR); + assert(tl->t->dec != NULL); + t_hosthdr = tl->t; + vcc_NextToken(tl); + ExpectErr(tl, ';'); + vcc_NextToken(tl); } else if (vcc_IdIs(t_field, "connect_timeout")) { Fb(tl, 0, "\t.connect_timeout = "); vcc_TimeVal(tl); @@ -499,9 +521,6 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name, const vcc_ErrWhere(tl, t_host); return; } - Fb(tl, 0, "\t.hostname = "); - EncToken(tl->fb, t_host); - Fb(tl, 0, ",\n"); /* Check that the portname makes sense */ if (t_port != NULL) { @@ -512,22 +531,28 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name, const vcc_ErrWhere(tl, t_port); return; } - Fb(tl, 0, "\t.portname = "); - EncToken(tl->fb, t_port); - Fb(tl, 0, ",\n"); Emit_Sockaddr(tl, t_host, t_port->dec); } else { - Fb(tl, 0, "\t.portname = \"80\",\n"); Emit_Sockaddr(tl, t_host, "80"); } ERRCHK(tl); ExpectErr(tl, '}'); + + /* We have parsed it all, emit the ident string */ vcc_EmitBeIdent(tl->fb, name, qual, serial, t_first, tl->t); - Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(name)); - if (serial) - Fb(tl, 0, "[%d]", serial); - Fb(tl, 0, "\"\n};\n"); + + /* Emit the hosthdr field, fall back to .host if not specified */ + Fb(tl, 0, "\t.hosthdr = "); + if (t_hosthdr != NULL) + EncToken(tl->fb, t_hosthdr); + else + EncToken(tl->fb, t_host); + Fb(tl, 0, ",\n"); + + /* Close the struct */ + Fb(tl, 0, "};\n"); + vcc_NextToken(tl); tl->fb = NULL; diff --git a/varnish-cache/lib/libvcl/vcc_fixed_token.c b/varnish-cache/lib/libvcl/vcc_fixed_token.c index 905124d1..b0d87486 100644 --- a/varnish-cache/lib/libvcl/vcc_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcc_fixed_token.c @@ -386,21 +386,21 @@ vcl_output_lang_h(struct vsb *sb) vsb_cat(sb, "struct vrt_backend_probe {\n"); vsb_cat(sb, " char *request;\n"); vsb_cat(sb, " double timeout;\n"); - vsb_cat(sb, " double rate;\n"); + vsb_cat(sb, " double interval;\n"); vsb_cat(sb, "};\n"); vsb_cat(sb, "\n"); vsb_cat(sb, "/*\n"); vsb_cat(sb, " * A backend is a host+port somewhere on the network\n"); vsb_cat(sb, " */\n"); vsb_cat(sb, "struct vrt_backend {\n"); - vsb_cat(sb, " char *portname;\n"); - vsb_cat(sb, " char *hostname;\n"); + vsb_cat(sb, " char *vcl_name;\n"); + vsb_cat(sb, " char *ident;\n"); + vsb_cat(sb, "\n"); + vsb_cat(sb, " char *hosthdr;\n"); vsb_cat(sb, "\n"); vsb_cat(sb, " const unsigned char *ipv4_sockaddr;\n"); vsb_cat(sb, " const unsigned char *ipv6_sockaddr;\n"); vsb_cat(sb, "\n"); - vsb_cat(sb, " char *vcl_name;\n"); - vsb_cat(sb, " char *ident;\n"); vsb_cat(sb, " double connect_timeout;\n"); vsb_cat(sb, " struct vrt_backend_probe probe;\n"); vsb_cat(sb, "};\n"); -- 2.39.5