From 96e5ae9b8daa7fdb22a6e82b8fc1965dde8624e1 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 31 Jul 2006 07:26:25 +0000 Subject: [PATCH] Introduce http_SetHeader() for setting a http header to a const string, no need to waste time printf'ing in this case, and no need to waste workspace. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@572 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 1 + varnish-cache/bin/varnishd/cache_http.c | 17 ++++++++++++++++- varnish-cache/bin/varnishd/cache_response.c | 19 +++++++------------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 1d7a0b2e..97c7658a 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -328,6 +328,7 @@ void http_CopyReq(int fd, struct http *to, struct http *fm); void http_CopyResp(int fd, struct http *to, struct http *fm); void http_FilterHeader(int fd, struct http *to, struct http *fm, unsigned how); void http_PrintfHeader(int fd, struct http *to, const char *fmt, ...); +void http_SetHeader(int fd, struct http *to, unsigned n, const char *hdr); int http_IsHdr(struct http_hdr *hh, char *hdr); void http_Setup(struct http *ht, void *space, unsigned len); int http_GetHdr(struct http *hp, const char *hdr, char **ptr); diff --git a/varnish-cache/bin/varnishd/cache_http.c b/varnish-cache/bin/varnishd/cache_http.c index e1beeca0..c9240f70 100644 --- a/varnish-cache/bin/varnishd/cache_http.c +++ b/varnish-cache/bin/varnishd/cache_http.c @@ -484,7 +484,9 @@ http_RecvHead(struct http *hp, int fd, struct event_base *eb, http_callback_f *f AZ(event_add(&hp->ev, NULL)); /* XXX: timeout */ } -/*--------------------------------------------------------------------*/ +/*-------------------------------------------------------------------- + * Copy HTTP headers into malloc'ed space. + */ void http_CopyHttp(struct http *to, struct http *fm) @@ -614,6 +616,19 @@ http_FilterHeader(int fd, struct http *to, struct http *fm, unsigned how) /*--------------------------------------------------------------------*/ +void +http_SetHeader(int fd, struct http *to, unsigned n, const char *hdr) +{ + + if (n == 0) + n = to->nhd++; + to->hd[n].b = (void*)(uintptr_t)hdr; + to->hd[n].e = strchr(hdr, '\0'); + VSLH(SLT_TxHeader, fd, to, n); +} + +/*--------------------------------------------------------------------*/ + void http_PrintfHeader(int fd, struct http *to, const char *fmt, ...) { diff --git a/varnish-cache/bin/varnishd/cache_response.c b/varnish-cache/bin/varnishd/cache_response.c index 5311ddf5..0430bf45 100644 --- a/varnish-cache/bin/varnishd/cache_response.c +++ b/varnish-cache/bin/varnishd/cache_response.c @@ -84,21 +84,16 @@ res_do_304(struct sess *sp, char *p) sp->http->f = sp->http->v; - sp->http->nhd = HTTP_HDR_PROTO; - http_PrintfHeader(sp->fd, sp->http, "HTTP/1.1"); - - sp->http->nhd = HTTP_HDR_STATUS; - http_PrintfHeader(sp->fd, sp->http, "304"); - - sp->http->nhd = HTTP_HDR_RESPONSE; - http_PrintfHeader(sp->fd, sp->http, "Not Modified"); + http_SetHeader(sp->fd, sp->http, HTTP_HDR_PROTO, "HTTP/1.1"); + http_SetHeader(sp->fd, sp->http, HTTP_HDR_STATUS, "304"); + http_SetHeader(sp->fd, sp->http, HTTP_HDR_RESPONSE, "Not Modified"); sp->http->nhd = HTTP_HDR_FIRST; + http_SetHeader(sp->fd, sp->http, 0, "Via: 1.1 varnish"); http_PrintfHeader(sp->fd, sp->http, "X-Varnish: %u", sp->xid); - http_PrintfHeader(sp->fd, sp->http, "Via: 1.1 varnish"); http_PrintfHeader(sp->fd, sp->http, "Last-Modified: %s", p); if (sp->doclose != NULL) - http_PrintfHeader(sp->fd, sp->http, "Connection: close"); + http_SetHeader(sp->fd, sp->http, 0, "Connection: close"); WRK_Reset(sp->wrk, &sp->fd); http_Write(sp->wrk, sp->http, 1); if (WRK_Flush(sp->wrk)) @@ -155,9 +150,9 @@ RES_WriteObj(struct sess *sp) http_PrintfHeader(sp->fd, sp->http, "X-Varnish: %u", sp->xid); http_PrintfHeader(sp->fd, sp->http, "Age: %u", sp->obj->age + sp->t_req - sp->obj->entered); - http_PrintfHeader(sp->fd, sp->http, "Via: 1.1 varnish"); + http_SetHeader(sp->fd, sp->http, 0, "Via: 1.1 varnish"); if (sp->doclose != NULL) - http_PrintfHeader(sp->fd, sp->http, "Connection: close"); + http_SetHeader(sp->fd, sp->http, 0, "Connection: close"); WRK_Reset(sp->wrk, &sp->fd); sp->wrk->acct.hdrbytes += http_Write(sp->wrk, sp->http, 1); -- 2.39.5