From cd1306d72a3ca3ba6f8ff3ddbadfdbbc6340667e Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 3 Jul 2007 20:38:38 +0000 Subject: [PATCH] Emply the new vbe_bereq structure for holding backend request and reply. There should be no functional change as a result of this. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1632 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 9 ++- varnish-cache/bin/varnishd/cache_backend.c | 31 ++++------ varnish-cache/bin/varnishd/cache_fetch.c | 66 +++++++++++++--------- varnish-cache/bin/varnishd/cache_http.c | 64 ++++++++------------- varnish-cache/bin/varnishd/cache_pipe.c | 27 +++++---- 5 files changed, 95 insertions(+), 102 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index f8f4c830..cbf5c2e4 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -188,7 +188,8 @@ struct bereq { unsigned magic; #define BEREQ_MAGIC 0x3b6d250c TAILQ_ENTRY(bereq) list; - struct ws ws[1]; + void *space; + unsigned len; struct http http[1]; }; @@ -198,9 +199,6 @@ struct vbe_conn { TAILQ_ENTRY(vbe_conn) list; struct backend *backend; int fd; - struct http *bereq; - struct http *beresp; - struct http http_mem[2]; }; /* Storage -----------------------------------------------------------*/ @@ -407,7 +405,6 @@ void HSH_Init(void); const char *http_StatusMessage(int); 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(struct worker *w, int fd, struct http *to, struct http *fm); void http_CopyReq(struct worker *w, int fd, struct http *to, struct http *fm); @@ -433,6 +430,8 @@ int http_RecvHead(struct http *hp, int fd); int http_DissectRequest(struct worker *w, struct http *sp, int fd); int http_DissectResponse(struct worker *w, struct http *sp, int fd); void http_DoConnection(struct sess *sp); +void http_CopyHome(struct http *hp); + #define HTTPH(a, b, c, d, e, f, g) extern char b[]; #include "http_headers.h" diff --git a/varnish-cache/bin/varnishd/cache_backend.c b/varnish-cache/bin/varnishd/cache_backend.c index 5061b81d..fce74820 100644 --- a/varnish-cache/bin/varnishd/cache_backend.c +++ b/varnish-cache/bin/varnishd/cache_backend.c @@ -81,22 +81,25 @@ struct bereq * vbe_new_bereq(void) { struct bereq *bereq; - volatile unsigned space; + volatile unsigned len; LOCK(&vbemtx); bereq = TAILQ_FIRST(&bereq_head); if (bereq != NULL) TAILQ_REMOVE(&bereq_head, bereq, list); UNLOCK(&vbemtx); - if (bereq == NULL) { - space = params->mem_workspace; - bereq = calloc(sizeof *bereq + space, 1); + if (bereq != NULL) { + CHECK_OBJ(bereq, BEREQ_MAGIC); + } else { + len = params->mem_workspace; + bereq = calloc(sizeof *bereq + len, 1); if (bereq == NULL) return (NULL); bereq->magic = BEREQ_MAGIC; - WS_Init(bereq->ws, bereq + 1, space); + bereq->space = bereq + 1; + bereq->len = len; } - WS_Reset(bereq->ws); + http_Setup(bereq->http, bereq->space, bereq->len); return (bereq); } @@ -107,6 +110,7 @@ void vbe_free_bereq(struct bereq *bereq) { + CHECK_OBJ_NOTNULL(bereq, BEREQ_MAGIC); LOCK(&vbemtx); TAILQ_INSERT_HEAD(&bereq_head, bereq, list); UNLOCK(&vbemtx); @@ -118,22 +122,13 @@ static struct vbe_conn * vbe_new_conn(void) { struct vbe_conn *vbc; - unsigned char *p; - volatile unsigned space; - space = params->mem_workspace; - vbc = calloc(sizeof *vbc + space * 2, 1); + vbc = calloc(sizeof *vbc, 1); if (vbc == NULL) return (NULL); VSL_stats->n_vbe_conn++; vbc->magic = VBE_CONN_MAGIC; - vbc->bereq = &vbc->http_mem[0]; - vbc->beresp = &vbc->http_mem[1]; vbc->fd = -1; - p = (void *)(vbc + 1); - http_Setup(vbc->bereq, p, space); - p += space; - http_Setup(vbc->beresp, p, space); return (vbc); } @@ -375,8 +370,6 @@ VBE_ClosedFd(struct worker *w, struct vbe_conn *vc, int already) AZ(close(vc->fd)); vc->fd = -1; vc->backend = NULL; - WS_Reset(vc->bereq->ws); - WS_Reset(vc->beresp->ws); LOCK(&vbemtx); TAILQ_INSERT_HEAD(&vbe_head, vc, list); VSL_stats->backend_unused++; @@ -393,8 +386,6 @@ VBE_RecycleFd(struct worker *w, struct vbe_conn *vc) assert(vc->fd >= 0); AN(vc->backend); WSL(w, SLT_BackendReuse, vc->fd, "%s", vc->backend->vcl_name); - WS_Reset(vc->bereq->ws); - WS_Reset(vc->beresp->ws); LOCK(&vbemtx); VSL_stats->backend_recycle++; TAILQ_INSERT_HEAD(&vc->backend->connlist, vc, list); diff --git a/varnish-cache/bin/varnishd/cache_fetch.c b/varnish-cache/bin/varnishd/cache_fetch.c index 327d094d..d4dd3f4d 100644 --- a/varnish-cache/bin/varnishd/cache_fetch.c +++ b/varnish-cache/bin/varnishd/cache_fetch.c @@ -260,8 +260,10 @@ Fetch(struct sess *sp) char *b; int cls; int body = 1; /* XXX */ - struct http *hp; + struct http *hp, *hp2; struct storage *st; + struct bereq *bereq; + int len; CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); CHECK_OBJ_NOTNULL(sp->wrk, WORKER_MAGIC); @@ -275,20 +277,23 @@ Fetch(struct sess *sp) if (vc == NULL) return (1); - http_ClrHeader(vc->bereq); - vc->bereq->logtag = HTTP_Tx; - http_GetReq(w, vc->fd, vc->bereq, sp->http); - http_FilterHeader(w, vc->fd, vc->bereq, sp->http, HTTPH_R_FETCH); - http_PrintfHeader(w, vc->fd, vc->bereq, "X-Varnish: %u", sp->xid); - http_PrintfHeader(w, vc->fd, vc->bereq, + bereq = vbe_new_bereq(); + AN(bereq); + hp = bereq->http; + hp->logtag = HTTP_Tx; + + http_GetReq(w, vc->fd, hp, sp->http); + http_FilterHeader(w, vc->fd, hp, sp->http, HTTPH_R_FETCH); + http_PrintfHeader(w, vc->fd, hp, "X-Varnish: %u", sp->xid); + http_PrintfHeader(w, vc->fd, hp, "X-Forwarded-for: %s", sp->addr); - if (!http_GetHdr(vc->bereq, H_Host, &b)) { - http_PrintfHeader(w, vc->fd, vc->bereq, "Host: %s", + if (!http_GetHdr(hp, H_Host, &b)) { + http_PrintfHeader(w, vc->fd, hp, "Host: %s", sp->backend->hostname); } WRK_Reset(w, &vc->fd); - http_Write(w, vc->bereq, 0); + http_Write(w, hp, 0); if (WRK_Flush(w)) { /* XXX: cleanup */ return (1); @@ -298,11 +303,11 @@ Fetch(struct sess *sp) CHECK_OBJ_NOTNULL(sp->wrk, WORKER_MAGIC); CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC); - if (http_RecvHead(vc->bereq, vc->fd)) { + if (http_RecvHead(hp, vc->fd)) { /* XXX: cleanup */ return (1); } - if (http_DissectResponse(sp->wrk, vc->bereq, vc->fd)) { + if (http_DissectResponse(sp->wrk, hp, vc->fd)) { /* XXX: cleanup */ return (1); } @@ -315,23 +320,31 @@ Fetch(struct sess *sp) assert(sp->obj->busy != 0); - if (http_GetHdr(vc->bereq, H_Last_Modified, &b)) + if (http_GetHdr(hp, H_Last_Modified, &b)) sp->obj->last_modified = TIM_parse(b); - hp = vc->beresp; - http_ClrHeader(hp); - hp->logtag = HTTP_Obj; - http_CopyResp(sp->wrk, sp->fd, hp, vc->bereq); - http_FilterHeader(sp->wrk, sp->fd, hp, vc->bereq, HTTPH_A_INS); + /* Filter into object */ + hp2 = &sp->obj->http; + len = hp->rx_e - hp->rx_s; + len += 256; /* margin for content-length etc */ + + b = malloc(len); + AN(b); + http_Setup(hp2, b, len); + + hp2->logtag = HTTP_Obj; + http_CopyResp(sp->wrk, sp->fd, hp2, hp); + http_FilterHeader(sp->wrk, sp->fd, hp2, hp, HTTPH_A_INS); + http_CopyHome(hp2); if (body) { - if (http_GetHdr(vc->bereq, H_Content_Length, &b)) - cls = fetch_straight(sp, vc->fd, vc->bereq, b); - else if (http_HdrIs(vc->bereq, H_Transfer_Encoding, "chunked")) - cls = fetch_chunked(sp, vc->fd, vc->bereq); + if (http_GetHdr(hp, H_Content_Length, &b)) + cls = fetch_straight(sp, vc->fd, hp, b); + else if (http_HdrIs(hp, H_Transfer_Encoding, "chunked")) + cls = fetch_chunked(sp, vc->fd, hp); else - cls = fetch_eof(sp, vc->fd, vc->bereq); - http_PrintfHeader(sp->wrk, sp->fd, hp, + cls = fetch_eof(sp, vc->fd, hp); + http_PrintfHeader(sp->wrk, sp->fd, hp2, "Content-Length: %u", sp->obj->len); } else cls = 0; @@ -357,15 +370,14 @@ Fetch(struct sess *sp) assert(uu == sp->obj->len); } - http_CopyHttp(&sp->obj->http, hp); - - if (http_GetHdr(vc->bereq, H_Connection, &b) && !strcasecmp(b, "close")) + if (http_GetHdr(hp, H_Connection, &b) && !strcasecmp(b, "close")) cls = 1; if (cls) VBE_ClosedFd(sp->wrk, vc, 0); else VBE_RecycleFd(sp->wrk, vc); + vbe_free_bereq(bereq); return (0); } diff --git a/varnish-cache/bin/varnishd/cache_http.c b/varnish-cache/bin/varnishd/cache_http.c index 3a3969b6..244ec3b0 100644 --- a/varnish-cache/bin/varnishd/cache_http.c +++ b/varnish-cache/bin/varnishd/cache_http.c @@ -678,46 +678,6 @@ http_RecvHead(struct http *hp, int fd) return (i); } -/*-------------------------------------------------------------------- - * Copy HTTP headers into malloc'ed space. - */ - -void -http_CopyHttp(struct http *to, struct http *fm) -{ - unsigned u, l; - char *p; - - CHECK_OBJ_NOTNULL(to, HTTP_MAGIC); - CHECK_OBJ_NOTNULL(fm, HTTP_MAGIC); - l = 0; - for (u = 0; u < fm->nhd; u++) { - if (fm->hd[u].b == NULL) - continue; - AN(fm->hd[u].e); - l += (fm->hd[u].e - fm->hd[u].b) + 1; - } - p = malloc(l); - XXXAN(p); - WS_Init(to->ws, p, l); - WS_Reserve(to->ws, 0); - for (u = 0; u < fm->nhd; u++) { - if (fm->hd[u].b == NULL) - continue; - AN(fm->hd[u].e); - assert(*fm->hd[u].e == '\0'); - l = fm->hd[u].e - fm->hd[u].b; - assert(l == strlen(fm->hd[u].b)); - memcpy(p, fm->hd[u].b, l); - to->hd[u].b = p; - to->hd[u].e = p + l; - *to->hd[u].e = '\0'; - p += l + 1; - } - /* XXX: Leave to->ws reserved for now */ - to->nhd = fm->nhd; -} - /*--------------------------------------------------------------------*/ static void @@ -834,6 +794,30 @@ http_FilterHeader(struct worker *w, int fd, struct http *to, struct http *fm, un } } +/*-------------------------------------------------------------------- + * This function copies any header fields which reference foreign + * storage into our own WS. + */ + +void +http_CopyHome(struct http *hp) +{ + unsigned u, l; + char *p; + + for (u = 0; u < hp->nhd; u++) { + if (hp->hd[u].b == NULL) + continue; + if (hp->hd[u].b >= hp->ws->s && hp->hd[u].e <= hp->ws->e) + continue; + l = hp->hd[u].e - hp->hd[u].b; + p = WS_Alloc(hp->ws, l + 1); + memcpy(p, hp->hd[u].b, l + 1); + hp->hd[u].b = p; + hp->hd[u].e = p + l; + } +} + /*--------------------------------------------------------------------*/ void diff --git a/varnish-cache/bin/varnishd/cache_pipe.c b/varnish-cache/bin/varnishd/cache_pipe.c index 01c4d42e..383a2533 100644 --- a/varnish-cache/bin/varnishd/cache_pipe.c +++ b/varnish-cache/bin/varnishd/cache_pipe.c @@ -78,7 +78,8 @@ PipeSession(struct sess *sp) char *b, *e; struct worker *w; struct pollfd fds[2]; - char wsspc[8192]; + struct bereq *bereq; + struct http *hp; int i; CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); @@ -88,19 +89,21 @@ PipeSession(struct sess *sp) vc = VBE_GetFd(sp); if (vc == NULL) return; - w->bereq->logtag = HTTP_Tx; - http_Setup(w->bereq, wsspc, sizeof wsspc); + bereq = vbe_new_bereq(); + AN(bereq); + hp = bereq->http; + hp->logtag = HTTP_Tx; - http_CopyReq(w, vc->fd, w->bereq, sp->http); - http_FilterHeader(w, vc->fd, w->bereq, sp->http, HTTPH_R_PIPE); - http_PrintfHeader(w, vc->fd, w->bereq, "X-Varnish: %u", sp->xid); - http_PrintfHeader(w, vc->fd, w->bereq, + http_CopyReq(w, vc->fd, hp, sp->http); + http_FilterHeader(w, vc->fd, hp, sp->http, HTTPH_R_PIPE); + http_PrintfHeader(w, vc->fd, hp, "X-Varnish: %u", sp->xid); + http_PrintfHeader(w, vc->fd, hp, "X-Forwarded-for: %s", sp->addr); /* XXX: does this belong in VCL ? */ - if (!http_GetHdr(w->bereq, H_Host, &b)) { - http_PrintfHeader(w, vc->fd, w->bereq, "Host: %s", + if (!http_GetHdr(hp, H_Host, &b)) { + http_PrintfHeader(w, vc->fd, hp, "Host: %s", sp->backend->hostname); } @@ -110,7 +113,7 @@ PipeSession(struct sess *sp) INCOMPL(); WRK_Reset(w, &vc->fd); - http_Write(w, w->bereq, 0); + http_Write(w, hp, 0); if (http_GetTail(sp->http, 0, &b, &e) && b != e) WRK_Write(w, b, e - b); @@ -121,6 +124,10 @@ PipeSession(struct sess *sp) return; } + vbe_free_bereq(bereq); + bereq = NULL; + hp = NULL; + clock_gettime(CLOCK_REALTIME, &sp->t_resp); memset(fds, 0, sizeof fds); -- 2.39.5