From: phk Date: Tue, 4 Apr 2006 07:29:45 +0000 (+0000) Subject: Make the "receive a HTTP protocol header" code generic X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85c8d53a48d8fb687a7fd4955b7d87938a4ea768;p=varnish Make the "receive a HTTP protocol header" code generic git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@108 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 3a041bc3..d4bdf282 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -2,6 +2,8 @@ * $Id$ */ +struct event_base; + /* cache_acceptor.c */ void *vca_main(void *arg); void vca_retire_session(struct sess *sp); @@ -14,10 +16,14 @@ void VBE_ClosedFd(void *ptr); /* cache_httpd.c */ void HttpdAnalyze(struct sess *sp); +void HttpdGetHead(struct sess *sp, struct event_base *eb, sesscb_f *func); /* cache_main.c */ pthread_mutex_t sessmtx; +/* cache_pass.c */ +void PassSession(struct sess *sp); + /* cache_pipe.c */ void PipeSession(struct sess *sp); @@ -30,6 +36,7 @@ void VSL_Init(void); #ifdef SHMLOGHEAD_MAGIC void VSLR(enum shmlogtag tag, unsigned id, const char *b, const char *e); void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...); +#define HERE() VSL(SLT_Debug, 0, "%s(%d)", __func__, __LINE__) #endif /* cache_vcl.c */ diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c index c613b405..b6857322 100644 --- a/varnish-cache/bin/varnishd/cache_acceptor.c +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -31,43 +31,6 @@ struct sessmem { struct event e; }; -static void -http_read_f(int fd, short event, void *arg) -{ - struct sess *sp = arg; - const char *p; - int i; - - assert(VCA_RXBUFSIZE - sp->rcv_len > 0); - i = read(fd, sp->rcv + sp->rcv_len, VCA_RXBUFSIZE - sp->rcv_len); - if (i <= 0) { - VSL(SLT_SessionClose, sp->fd, "remote %d", sp->rcv_len); - event_del(sp->rd_e); - close(sp->fd); - free(sp->mem); - return; - } - - sp->rcv_len += i; - sp->rcv[sp->rcv_len] = '\0'; - - p = sp->rcv; - while (1) { - /* XXX: we could save location of all linebreaks for later */ - p = strchr(p, '\n'); - if (p == NULL) - return; - p++; - if (*p == '\r') - p++; - if (*p != '\n') - continue; - break; - } - event_del(sp->rd_e); - DealWithSession(sp); -} - static void accept_f(int fd, short event, void *arg) { @@ -100,10 +63,7 @@ accept_f(int fd, short event, void *arg) strlcat(sp->addr, ":", VCA_ADDRBUFSIZE); strlcat(sp->addr, port, VCA_ADDRBUFSIZE); VSL(SLT_SessionOpen, sp->fd, "%s", sp->addr); - event_set(sp->rd_e, sp->fd, EV_READ | EV_PERSIST, - http_read_f, sp); - event_base_set(evb, sp->rd_e); - event_add(sp->rd_e, NULL); /* XXX: timeout */ + HttpdGetHead(sp, evb, DealWithSession); } void * diff --git a/varnish-cache/bin/varnishd/cache_httpd.c b/varnish-cache/bin/varnishd/cache_httpd.c index 0694c913..d8852093 100644 --- a/varnish-cache/bin/varnishd/cache_httpd.c +++ b/varnish-cache/bin/varnishd/cache_httpd.c @@ -5,15 +5,21 @@ */ #include +#include +#include +#include #include #include #include +#include #include "libvarnish.h" #include "shmlog.h" #include "vcl_lang.h" #include "cache.h" +/*--------------------------------------------------------------------*/ + void HttpdAnalyze(struct sess *sp) { @@ -87,3 +93,55 @@ HttpdAnalyze(struct sess *sp) } } } + +/*--------------------------------------------------------------------*/ + +static void +http_read_f(int fd, short event, void *arg) +{ + struct sess *sp = arg; + const char *p; + int i; + + assert(VCA_RXBUFSIZE - sp->rcv_len > 0); + i = read(fd, sp->rcv + sp->rcv_len, VCA_RXBUFSIZE - sp->rcv_len); + if (i <= 0) { + VSL(SLT_SessionClose, sp->fd, "remote %d", sp->rcv_len); + event_del(sp->rd_e); + close(sp->fd); + free(sp->mem); + return; + } + + sp->rcv_len += i; + sp->rcv[sp->rcv_len] = '\0'; + + p = sp->rcv; + while (1) { + /* XXX: we could save location of all linebreaks for later */ + p = strchr(p, '\n'); + if (p == NULL) + return; + p++; + if (*p == '\r') + p++; + if (*p != '\n') + continue; + break; + } + event_del(sp->rd_e); + sp->sesscb(sp); +} + +/*--------------------------------------------------------------------*/ + +void +HttpdGetHead(struct sess *sp, struct event_base *eb, sesscb_f *func) +{ + + sp->sesscb = func; + assert(sp->rd_e != NULL); + event_set(sp->rd_e, sp->fd, EV_READ | EV_PERSIST, http_read_f, sp); + event_base_set(eb, sp->rd_e); + event_add(sp->rd_e, NULL); /* XXX: timeout */ +}