Mailing List Archive

r1632 - trunk/varnish-cache/bin/varnishd
Author: phk
Date: 2007-07-03 22:38:38 +0200 (Tue, 03 Jul 2007)
New Revision: 1632

Modified:
trunk/varnish-cache/bin/varnishd/cache.h
trunk/varnish-cache/bin/varnishd/cache_backend.c
trunk/varnish-cache/bin/varnishd/cache_fetch.c
trunk/varnish-cache/bin/varnishd/cache_http.c
trunk/varnish-cache/bin/varnishd/cache_pipe.c
Log:
Emply the new vbe_bereq structure for holding backend request and reply.

There should be no functional change as a result of this.



Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h 2007-07-03 19:32:21 UTC (rev 1631)
+++ trunk/varnish-cache/bin/varnishd/cache.h 2007-07-03 20:38:38 UTC (rev 1632)
@@ -188,7 +188,8 @@
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 @@
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 @@
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,7 +430,9 @@
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"
#undef HTTPH

Modified: trunk/varnish-cache/bin/varnishd/cache_backend.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend.c 2007-07-03 19:32:21 UTC (rev 1631)
+++ trunk/varnish-cache/bin/varnishd/cache_backend.c 2007-07-03 20:38:38 UTC (rev 1632)
@@ -81,22 +81,25 @@
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 @@
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 @@
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 @@
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 @@
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);

Modified: trunk/varnish-cache/bin/varnishd/cache_fetch.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_fetch.c 2007-07-03 19:32:21 UTC (rev 1631)
+++ trunk/varnish-cache/bin/varnishd/cache_fetch.c 2007-07-03 20:38:38 UTC (rev 1632)
@@ -260,8 +260,10 @@
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 @@
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 @@
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 @@

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 @@
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);
}

Modified: trunk/varnish-cache/bin/varnishd/cache_http.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_http.c 2007-07-03 19:32:21 UTC (rev 1631)
+++ trunk/varnish-cache/bin/varnishd/cache_http.c 2007-07-03 20:38:38 UTC (rev 1632)
@@ -678,46 +678,6 @@
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 @@
}
}

+/*--------------------------------------------------------------------
+ * 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

Modified: trunk/varnish-cache/bin/varnishd/cache_pipe.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_pipe.c 2007-07-03 19:32:21 UTC (rev 1631)
+++ trunk/varnish-cache/bin/varnishd/cache_pipe.c 2007-07-03 20:38:38 UTC (rev 1632)
@@ -78,7 +78,8 @@
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 @@
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 @@
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 @@
return;
}

+ vbe_free_bereq(bereq);
+ bereq = NULL;
+ hp = NULL;
+
clock_gettime(CLOCK_REALTIME, &sp->t_resp);

memset(fds, 0, sizeof fds);