From dd90a57a729153e2caaed4f81c8944f2c42e3cb8 Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 15 Feb 2008 08:54:20 +0000 Subject: [PATCH] Determine our backend (using the director) before we filter the req into the bereq, in order to be able to assign a default Host: header if there is none. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2464 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 2 ++ varnish-cache/bin/varnishd/cache_backend.c | 32 ++++++++++++++++++++-- varnish-cache/bin/varnishd/cache_center.c | 3 ++ varnish-cache/bin/varnishd/cache_http.c | 12 +++----- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index dce0f8eb..1234aaff 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -415,6 +415,8 @@ void VBE_free_bereq(struct bereq *bereq); void VBE_DropRef(struct backend *); struct backend *VBE_AddBackend(struct cli *cli, const struct vrt_backend *vb); void VBE_UpdateHealth(const struct sess *sp, const struct vbe_conn *, int); +void VBE_AddHostHeader(struct sess *sp); +void VBE_SelectBackend(struct sess *sp); /* cache_ban.c */ void AddBan(const char *, int hash); diff --git a/varnish-cache/bin/varnishd/cache_backend.c b/varnish-cache/bin/varnishd/cache_backend.c index 81820e58..2e407252 100644 --- a/varnish-cache/bin/varnishd/cache_backend.c +++ b/varnish-cache/bin/varnishd/cache_backend.c @@ -122,6 +122,21 @@ static VTAILQ_HEAD(,bereq) bereq_head = VTAILQ_HEAD_INITIALIZER(bereq_head); static VTAILQ_HEAD(, backend) backends = VTAILQ_HEAD_INITIALIZER(backends); +/*-------------------------------------------------------------------- + * Create default Host: header for backend request + */ +void +VBE_AddHostHeader(struct sess *sp) +{ + + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + CHECK_OBJ_NOTNULL(sp->bereq, BEREQ_MAGIC); + CHECK_OBJ_NOTNULL(sp->bereq->http, HTTP_MAGIC); + CHECK_OBJ_NOTNULL(sp->backend, BACKEND_MAGIC); + http_PrintfHeader(sp->wrk, sp->fd, sp->bereq->http, + "Host: %s", sp->backend->vrt->hostname); +} + /*-------------------------------------------------------------------- * Attempt to connect to a given addrinfo entry. * @@ -406,17 +421,28 @@ bes_conn_try(const struct sess *sp, struct backend *bp) /*--------------------------------------------------------------------*/ -struct vbe_conn * -VBE_GetFd(struct sess *sp) +void +VBE_SelectBackend(struct sess *sp) { struct backend *bp; - struct vbe_conn *vc; CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC); bp = sp->director->choose(sp); CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC); sp->backend = bp; +} + +/*--------------------------------------------------------------------*/ + +struct vbe_conn * +VBE_GetFd(struct sess *sp) +{ + struct backend *bp; + struct vbe_conn *vc; + + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + bp = sp->backend; /* first look for vbe_conn's we can recycle */ while (1) { diff --git a/varnish-cache/bin/varnishd/cache_center.c b/varnish-cache/bin/varnishd/cache_center.c index a30d2dea..94b52f1a 100644 --- a/varnish-cache/bin/varnishd/cache_center.c +++ b/varnish-cache/bin/varnishd/cache_center.c @@ -617,6 +617,7 @@ cnt_miss(struct sess *sp) CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC); CHECK_OBJ_NOTNULL(sp->vcl, VCL_CONF_MAGIC); + VBE_SelectBackend(sp); http_FilterHeader(sp, HTTPH_R_FETCH); VCL_miss_method(sp); if (sp->handling == VCL_RET_ERROR) { @@ -680,6 +681,7 @@ cnt_pass(struct sess *sp) CHECK_OBJ_NOTNULL(sp->vcl, VCL_CONF_MAGIC); AZ(sp->obj); + VBE_SelectBackend(sp); http_FilterHeader(sp, HTTPH_R_PASS); VCL_pass_method(sp); @@ -729,6 +731,7 @@ cnt_pipe(struct sess *sp) CHECK_OBJ_NOTNULL(sp->vcl, VCL_CONF_MAGIC); sp->wrk->acct.pipe++; + VBE_SelectBackend(sp); http_FilterHeader(sp, HTTPH_R_PIPE); VCL_pipe_method(sp); diff --git a/varnish-cache/bin/varnishd/cache_http.c b/varnish-cache/bin/varnishd/cache_http.c index ea7e4db7..c5e2a517 100644 --- a/varnish-cache/bin/varnishd/cache_http.c +++ b/varnish-cache/bin/varnishd/cache_http.c @@ -649,15 +649,11 @@ http_FilterHeader(struct sess *sp, unsigned how) http_PrintfHeader(sp->wrk, sp->fd, hp, "X-Forwarded-for: %s", sp->addr); - /* XXX: This really ought to go into the default VCL */ - if (!http_GetHdr(hp, H_Host, &b)) { -#if 0 - http_PrintfHeader(sp->wrk, sp->fd, hp, "Host: %s", - sp->backend->method->gethostname(sp->backend)); -#endif - INCOMPL(); - } sp->bereq = bereq; + + /* XXX: This possibly ought to go into the default VCL */ + if (!http_GetHdr(hp, H_Host, &b)) + VBE_AddHostHeader(sp); } /*-------------------------------------------------------------------- -- 2.39.5