From e70499a60bc1995052381c9772b802da167f85b3 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 31 Jul 2006 21:04:43 +0000 Subject: [PATCH] Add http_ClrHeader() and cure an unintended bug-oid its use exposes: we checked if the request is a GET long after we should have. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@577 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 2 ++ varnish-cache/bin/varnishd/cache_center.c | 1 + varnish-cache/bin/varnishd/cache_fetch.c | 4 +--- varnish-cache/bin/varnishd/cache_http.c | 14 ++++++++++++++ varnish-cache/bin/varnishd/cache_pass.c | 3 +-- varnish-cache/bin/varnishd/cache_response.c | 15 +++++---------- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 38724939..ec20ad7b 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -244,6 +244,7 @@ struct sess { enum step step; unsigned handling; + unsigned char wantbody; TAILQ_ENTRY(sess) list; @@ -322,6 +323,7 @@ void HSH_Init(void); /* cache_http.c */ void HTTP_Init(void); +void http_ClrHeader(struct http *to); void http_CopyHttp(struct http *to, struct http *fm); unsigned http_Write(struct worker *w, struct http *hp, int resp); void http_GetReq(int fd, struct http *to, struct http *fm); diff --git a/varnish-cache/bin/varnishd/cache_center.c b/varnish-cache/bin/varnishd/cache_center.c index 93369c03..6690afa4 100644 --- a/varnish-cache/bin/varnishd/cache_center.c +++ b/varnish-cache/bin/varnishd/cache_center.c @@ -543,6 +543,7 @@ cnt_recv(struct sess *sp) switch(sp->handling) { case VCL_RET_LOOKUP: /* XXX: discard req body, if any */ + sp->wantbody = !strcmp(sp->http->hd[HTTP_HDR_REQ].b, "GET"); sp->step = STP_LOOKUP; return (0); case VCL_RET_PIPE: diff --git a/varnish-cache/bin/varnishd/cache_fetch.c b/varnish-cache/bin/varnishd/cache_fetch.c index ead22c10..b5964edb 100644 --- a/varnish-cache/bin/varnishd/cache_fetch.c +++ b/varnish-cache/bin/varnishd/cache_fetch.c @@ -228,8 +228,7 @@ FetchBody(struct sess *sp) * The actual headers to reply with are built later on over in * cache_response.c */ - sp->http->f = sp->http->v; - sp->http->nhd = HTTP_HDR_FIRST; + http_ClrHeader(sp->http); sp->http->objlog = 1; /* log as SLT_ObjHeader */ http_CopyResp(sp->fd, sp->http, vc->http); http_FilterHeader(sp->fd, sp->http, vc->http, HTTPH_A_INS); @@ -247,7 +246,6 @@ FetchBody(struct sess *sp) cls = 0; sp->http->objlog = 0; http_CopyHttp(&sp->obj->http, sp->http); - sp->http->f = sp->http->v; if (http_GetHdr(vc->http, H_Connection, &b) && !strcasecmp(b, "close")) cls = 1; diff --git a/varnish-cache/bin/varnishd/cache_http.c b/varnish-cache/bin/varnishd/cache_http.c index 8f319b41..f2a1d69a 100644 --- a/varnish-cache/bin/varnishd/cache_http.c +++ b/varnish-cache/bin/varnishd/cache_http.c @@ -628,10 +628,23 @@ http_FilterHeader(int fd, struct http *to, struct http *fm, unsigned how) /*--------------------------------------------------------------------*/ +void +http_ClrHeader(struct http *to) +{ + + CHECK_OBJ_NOTNULL(to, HTTP_MAGIC); + to->f = to->v; + to->nhd = HTTP_HDR_FIRST; + memset(to->hd, 0, sizeof to->hd); +} + +/*--------------------------------------------------------------------*/ + void http_SetHeader(int fd, struct http *to, const char *hdr) { + CHECK_OBJ_NOTNULL(to, HTTP_MAGIC); to->hd[to->nhd].b = (void*)(uintptr_t)hdr; to->hd[to->nhd].e = strchr(hdr, '\0'); assert(to->hd[to->nhd].e != NULL); @@ -647,6 +660,7 @@ http_PrintfHeader(int fd, struct http *to, const char *fmt, ...) va_list ap; unsigned l, n; + CHECK_OBJ_NOTNULL(to, HTTP_MAGIC); va_start(ap, fmt); l = to->e - to->f; n = vsnprintf(to->f, l, fmt, ap); diff --git a/varnish-cache/bin/varnishd/cache_pass.c b/varnish-cache/bin/varnishd/cache_pass.c index 62440fe0..f92b1cb5 100644 --- a/varnish-cache/bin/varnishd/cache_pass.c +++ b/varnish-cache/bin/varnishd/cache_pass.c @@ -148,8 +148,7 @@ PassBody(struct sess *sp) vc = sp->vbc; assert(vc != NULL); - sp->http->f = sp->http->v; - sp->http->nhd = HTTP_HDR_FIRST; + http_ClrHeader(sp->http); http_CopyResp(sp->fd, sp->http, vc->http); http_FilterHeader(sp->fd, sp->http, vc->http, HTTPH_A_PASS); http_PrintfHeader(sp->fd, sp->http, "X-Varnish: %u", sp->xid); diff --git a/varnish-cache/bin/varnishd/cache_response.c b/varnish-cache/bin/varnishd/cache_response.c index c9d049f0..7b64b6f5 100644 --- a/varnish-cache/bin/varnishd/cache_response.c +++ b/varnish-cache/bin/varnishd/cache_response.c @@ -79,14 +79,10 @@ static void res_do_304(struct sess *sp, char *p) { - VSL(SLT_Status, sp->fd, "%u", 304); VSL(SLT_Length, sp->fd, "%u", 0); - sp->http->f = sp->http->v; - + http_ClrHeader(sp->http); http_SetResp(sp->fd, sp->http, "HTTP/1.1", "304", "Not Modified"); - - sp->http->nhd = HTTP_HDR_FIRST; http_SetHeader(sp->fd, sp->http, "Via: 1.1 varnish"); http_PrintfHeader(sp->fd, sp->http, "X-Varnish: %u", sp->xid); http_PrintfHeader(sp->fd, sp->http, "Last-Modified: %s", p); @@ -135,15 +131,14 @@ RES_WriteObj(struct sess *sp) if (sp->obj->response == 200 && sp->http->conds && res_do_conds(sp)) return; - VSL(SLT_Status, sp->fd, "%u", sp->obj->response); VSL(SLT_Length, sp->fd, "%u", sp->obj->len); - sp->http->f = sp->http->v; - sp->http->nhd = HTTP_HDR_FIRST; + http_ClrHeader(sp->http); http_CopyResp(sp->fd, sp->http, &sp->obj->http); http_FilterHeader(sp->fd, sp->http, &sp->obj->http, HTTPH_A_DELIVER); if (sp->xid != sp->obj->xid) - http_PrintfHeader(sp->fd, sp->http, "X-Varnish: %u %u", sp->xid, sp->obj->xid); + http_PrintfHeader(sp->fd, sp->http, + "X-Varnish: %u %u", sp->xid, sp->obj->xid); else http_PrintfHeader(sp->fd, sp->http, "X-Varnish: %u", sp->xid); http_PrintfHeader(sp->fd, sp->http, "Age: %u", @@ -155,7 +150,7 @@ RES_WriteObj(struct sess *sp) sp->wrk->acct.hdrbytes += http_Write(sp->wrk, sp->http, 1); /* XXX: conditional request handling */ - if (!strcmp(sp->http->hd[HTTP_HDR_REQ].b, "GET")) { + if (sp->wantbody) { TAILQ_FOREACH(st, &sp->obj->store, list) { assert(st->stevedore != NULL); u += st->len; -- 2.39.5