From 2ded4a3228d44c4f43fc250b748c59a774d962e0 Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 4 Apr 2006 08:20:24 +0000 Subject: [PATCH] Add HTTP response headers and processing. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@111 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 2 +- varnish-cache/bin/varnishd/cache_httpd.c | 84 +++++++++++++++------- varnish-cache/bin/varnishd/cache_pass.c | 5 +- varnish-cache/bin/varnishd/cache_pipe.c | 1 - varnish-cache/bin/varnishd/cache_pool.c | 2 +- varnish-cache/include/http_headers.h | 57 +++++++++------ varnish-cache/include/shmlog_tags.h | 2 + varnish-cache/include/vcl_lang.h | 2 + varnish-cache/lib/libvcl/vcl_fixed_token.c | 59 +++++++++------ 9 files changed, 137 insertions(+), 77 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index d4bdf282..ce776c93 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -15,7 +15,7 @@ void VBE_Pass(struct sess *sp); void VBE_ClosedFd(void *ptr); /* cache_httpd.c */ -void HttpdAnalyze(struct sess *sp); +void HttpdAnalyze(struct sess *sp, int rr); void HttpdGetHead(struct sess *sp, struct event_base *eb, sesscb_f *func); /* cache_main.c */ diff --git a/varnish-cache/bin/varnishd/cache_httpd.c b/varnish-cache/bin/varnishd/cache_httpd.c index d8852093..5b4bbd7b 100644 --- a/varnish-cache/bin/varnishd/cache_httpd.c +++ b/varnish-cache/bin/varnishd/cache_httpd.c @@ -21,7 +21,7 @@ /*--------------------------------------------------------------------*/ void -HttpdAnalyze(struct sess *sp) +HttpdAnalyze(struct sess *sp, int rr) { char *p, *q, *r; @@ -29,37 +29,68 @@ HttpdAnalyze(struct sess *sp) memset(&sp->http, 0, sizeof sp->http); - /* First, isolate and possibly identify request type */ - sp->http.req = sp->rcv; - for (p = sp->rcv; isalpha(*p); p++) - ; - VSLR(SLT_Request, sp->fd, sp->http.req, p); - *p++ = '\0'; + if (rr == 1) { + /* First, isolate and possibly identify request type */ + sp->http.req = sp->rcv; + for (p = sp->rcv; isalpha(*p); p++) + ; + VSLR(SLT_Request, sp->fd, sp->http.req, p); + *p++ = '\0'; - /* Next find the URI */ - while (isspace(*p)) - p++; - sp->http.url = p; - while (!isspace(*p)) - p++; - VSLR(SLT_URL, sp->fd, sp->http.url, p); - *p++ = '\0'; + /* Next find the URI */ + while (isspace(*p)) + p++; + sp->http.url = p; + while (!isspace(*p)) + p++; + VSLR(SLT_URL, sp->fd, sp->http.url, p); + *p++ = '\0'; - /* Finally, look for protocol, if any */ - while (isspace(*p) && *p != '\n') + /* Finally, look for protocol, if any */ + while (isspace(*p) && *p != '\n') + p++; + sp->http.proto = p; + if (*p != '\n') { + while (!isspace(*p)) + p++; + } + VSLR(SLT_Protocol, sp->fd, sp->http.proto, p); + *p++ = '\0'; + + while (isspace(*p) && *p != '\n') + p++; p++; - sp->http.proto = p; - if (*p != '\n') { + } else { + /* First, protocol */ + sp->http.proto = sp->rcv; + p = sp->rcv; while (!isspace(*p)) p++; - } - VSLR(SLT_Protocol, sp->fd, sp->http.proto, p); - *p++ = '\0'; + *p++ = '\0'; + VSLR(SLT_Protocol, sp->fd, sp->http.proto, p); - while (isspace(*p) && *p != '\n') + /* Next find the status */ + while (isspace(*p)) + p++; + sp->http.status = p; + while (!isspace(*p)) + p++; + VSLR(SLT_Status, sp->fd, sp->http.status, p); + *p++ = '\0'; + + /* Next find the response */ + while (isspace(*p)) + p++; + sp->http.response = p; + while (*p != '\n') + p++; + for (q = p; q > sp->http.response && isspace(q[-1]); q--) + continue; + *q = '\0'; + VSLR(SLT_Response, sp->fd, sp->http.response, q); p++; + } - p++; if (*p == '\r') p++; @@ -81,7 +112,10 @@ HttpdAnalyze(struct sess *sp) continue; \ } -#define HTTPH(a, b, c, d, e, f, g) W(a ":", b, p, q, sp) +#define HTTPH(a, b, c, d, e, f, g) \ + if (c & rr) { \ + W(a ":", b, p, q, sp); \ + } #include "http_headers.h" #undef HTTPH #undef W diff --git a/varnish-cache/bin/varnishd/cache_pass.c b/varnish-cache/bin/varnishd/cache_pass.c index 183482ed..0aa4c510 100644 --- a/varnish-cache/bin/varnishd/cache_pass.c +++ b/varnish-cache/bin/varnishd/cache_pass.c @@ -25,7 +25,7 @@ PassReturn(struct sess *sp) { HERE(); - HttpdAnalyze(sp); + HttpdAnalyze(sp, 2); } /*--------------------------------------------------------------------*/ @@ -52,7 +52,7 @@ PassSession(struct sess *sp) sbuf_cat(sb, "\r\n"); #define HTTPH(a, b, c, d, e, f, g) \ do { \ - if (c && sp->http.b != NULL) { \ + if (d && sp->http.b != NULL) { \ sbuf_cat(sb, a ": "); \ sbuf_cat(sb, sp->http.b); \ sbuf_cat(sb, "\r\n"); \ @@ -62,7 +62,6 @@ PassSession(struct sess *sp) #undef HTTPH sbuf_cat(sb, "\r\n"); sbuf_finish(sb); - printf("REQ: <%s>\n", sbuf_data(sb)); i = write(fd, sbuf_data(sb), sbuf_len(sb)); assert(i == sbuf_len(sb)); diff --git a/varnish-cache/bin/varnishd/cache_pipe.c b/varnish-cache/bin/varnishd/cache_pipe.c index 0da549de..80eae135 100644 --- a/varnish-cache/bin/varnishd/cache_pipe.c +++ b/varnish-cache/bin/varnishd/cache_pipe.c @@ -75,7 +75,6 @@ PipeSession(struct sess *sp) #undef HTTPH sbuf_cat(sb, "\r\n"); sbuf_finish(sb); - printf("REQ: <%s>\n", sbuf_data(sb)); i = write(fd, sbuf_data(sb), sbuf_len(sb)); assert(i == sbuf_len(sb)); diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 4ce35172..a1c30203 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -31,7 +31,7 @@ CacheWorker(void *priv __unused) sp->vcl = GetVCL(); AZ(pthread_mutex_unlock(&sessmtx)); - HttpdAnalyze(sp); + HttpdAnalyze(sp, 1); sp->backend = sp->vcl->default_backend; /* Call the VCL program */ diff --git a/varnish-cache/include/http_headers.h b/varnish-cache/include/http_headers.h index 4092208d..e04e9b6a 100644 --- a/varnish-cache/include/http_headers.h +++ b/varnish-cache/include/http_headers.h @@ -3,8 +3,8 @@ * * a Http header name * b session field name - * c PassThrough handling (0=remove, 1=pass) - * d unused + * c Request(1)/Response(2) bitfield + * d Pass header * e unused * f unused * g unused @@ -12,24 +12,35 @@ * a b c d e f g *-------------------------------------------------------------------- */ -HTTPH("Accept-Charset", H_Accept_Charset, 1, 0, 0, 0, 0) -HTTPH("Accept-Encoding", H_Accept_Encoding, 1, 0, 0, 0, 0) -HTTPH("Accept-Language", H_Accept_Language, 1, 0, 0, 0, 0) -HTTPH("Accept", H_Accept, 1, 0, 0, 0, 0) -HTTPH("Authorization", H_Authorization, 1, 0, 0, 0, 0) -HTTPH("Connection", H_Connection, 0, 0, 0, 0, 0) -HTTPH("Expect", H_Expect, 1, 0, 0, 0, 0) -HTTPH("From", H_From, 1, 0, 0, 0, 0) -HTTPH("Host", H_Host, 1, 0, 0, 0, 0) -HTTPH("If-Match", H_If_Match, 1, 0, 0, 0, 0) -HTTPH("If-Modified-Since", H_If_Modified_Since, 1, 0, 0, 0, 0) -HTTPH("If-None-Match", H_If_None_Match, 1, 0, 0, 0, 0) -HTTPH("If-Range", H_If_Range, 1, 0, 0, 0, 0) -HTTPH("If-Unmodified-Since", H_If_Unmodifed_Since, 1, 0, 0, 0, 0) -HTTPH("Keep-Alive", H_Keep_Alive, 0, 0, 0, 0, 0) -HTTPH("Max-Forwards", H_Max_Forwards, 1, 0, 0, 0, 0) -HTTPH("Proxy-Authorization", H_Proxy_Authorization, 1, 0, 0, 0, 0) -HTTPH("Range", H_Range, 1, 0, 0, 0, 0) -HTTPH("Referer", H_Referer, 1, 0, 0, 0, 0) -HTTPH("TE", H_TE, 1, 0, 0, 0, 0) -HTTPH("User-Agent", H_User_Agent, 1, 0, 0, 0, 0) +HTTPH("Connection", H_Connection, 3, 0, 0, 0, 0) +HTTPH("Keep-Alive", H_Keep_Alive, 3, 0, 0, 0, 0) + +HTTPH("Accept-Charset", H_Accept_Charset, 1, 1, 0, 0, 0) +HTTPH("Accept-Encoding", H_Accept_Encoding, 1, 1, 0, 0, 0) +HTTPH("Accept-Language", H_Accept_Language, 1, 1, 0, 0, 0) +HTTPH("Accept", H_Accept, 1, 1, 0, 0, 0) +HTTPH("Authorization", H_Authorization, 1, 1, 0, 0, 0) +HTTPH("Expect", H_Expect, 1, 1, 0, 0, 0) +HTTPH("From", H_From, 1, 1, 0, 0, 0) +HTTPH("Host", H_Host, 1, 1, 0, 0, 0) +HTTPH("If-Match", H_If_Match, 1, 1, 0, 0, 0) +HTTPH("If-Modified-Since", H_If_Modified_Since, 1, 1, 0, 0, 0) +HTTPH("If-None-Match", H_If_None_Match, 1, 1, 0, 0, 0) +HTTPH("If-Range", H_If_Range, 1, 1, 0, 0, 0) +HTTPH("If-Unmodified-Since", H_If_Unmodifed_Since, 1, 1, 0, 0, 0) +HTTPH("Max-Forwards", H_Max_Forwards, 1, 1, 0, 0, 0) +HTTPH("Proxy-Authorization", H_Proxy_Authorization, 1, 1, 0, 0, 0) +HTTPH("Range", H_Range, 1, 1, 0, 0, 0) +HTTPH("Referer", H_Referer, 1, 1, 0, 0, 0) +HTTPH("TE", H_TE, 1, 1, 0, 0, 0) +HTTPH("User-Agent", H_User_Agent, 1, 1, 0, 0, 0) + +HTTPH("Server", H_Server, 2, 0, 0, 0, 0) +HTTPH("Content-Type", H_Content_Type, 2, 0, 0, 0, 0) +HTTPH("Date", H_Date, 2, 0, 0, 0, 0) +HTTPH("Last-Modified", H_Last_Modified, 2, 0, 0, 0, 0) +HTTPH("Accept-Ranges", H_Accept_Ranges, 2, 0, 0, 0, 0) +HTTPH("Content-Length", H_Content_Length, 2, 0, 0, 0, 0) +HTTPH("Cache-Control", H_Cache_Control, 2, 0, 0, 0, 0) +HTTPH("Vary", H_Vary, 2, 0, 0, 0, 0) +HTTPH("Expires", H_Expires, 2, 0, 0, 0, 0) diff --git a/varnish-cache/include/shmlog_tags.h b/varnish-cache/include/shmlog_tags.h index 727b2b2d..89785c06 100644 --- a/varnish-cache/include/shmlog_tags.h +++ b/varnish-cache/include/shmlog_tags.h @@ -12,6 +12,8 @@ SLTM(SessionOpen) SLTM(SessionClose) SLTM(ClientAddr) SLTM(Request) +SLTM(Response) +SLTM(Status) SLTM(URL) SLTM(Protocol) SLTM(HD_Unknown) diff --git a/varnish-cache/include/vcl_lang.h b/varnish-cache/include/vcl_lang.h index 1f6f29dd..ee80734f 100644 --- a/varnish-cache/include/vcl_lang.h +++ b/varnish-cache/include/vcl_lang.h @@ -31,6 +31,8 @@ struct httphdr { const char *req; const char *url; const char *proto; + const char *status; + const char *response; #define HTTPH(a, b, c, d, e, f, g) const char *b; #include #undef HTTPH diff --git a/varnish-cache/lib/libvcl/vcl_fixed_token.c b/varnish-cache/lib/libvcl/vcl_fixed_token.c index fc0b8f75..7a991d04 100644 --- a/varnish-cache/lib/libvcl/vcl_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcl_fixed_token.c @@ -412,14 +412,16 @@ vcl_output_lang_h(FILE *f) fputs(" const char *req;\n", f); fputs(" const char *url;\n", f); fputs(" const char *proto;\n", f); + fputs(" const char *status;\n", f); + fputs(" const char *response;\n", f); fputs("#define HTTPH(a, b, c, d, e, f, g) const char *b;\n", f); fputs("/*\n", f); fputs(" * $Id$\n", f); fputs(" *\n", f); fputs(" * a Http header name\n", f); fputs(" * b session field name\n", f); - fputs(" * c PassThrough handling (0=remove, 1=pass)\n", f); - fputs(" * d unused\n", f); + fputs(" * c Request(1)/Response(2) bitfield\n", f); + fputs(" * d Pass header\n", f); fputs(" * e unused\n", f); fputs(" * f unused\n", f); fputs(" * g unused\n", f); @@ -427,27 +429,38 @@ vcl_output_lang_h(FILE *f) fputs(" * a b c d e f g \n", f); fputs(" *--------------------------------------------------------------------\n", f); fputs(" */\n", f); - fputs("HTTPH(\"Accept-Charset\", H_Accept_Charset, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Accept-Encoding\", H_Accept_Encoding, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Accept-Language\", H_Accept_Language, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Accept\", H_Accept, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Authorization\", H_Authorization, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Connection\", H_Connection, 0, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Expect\", H_Expect, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"From\", H_From, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Host\", H_Host, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"If-Match\", H_If_Match, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"If-Modified-Since\", H_If_Modified_Since, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"If-None-Match\", H_If_None_Match, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"If-Range\", H_If_Range, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"If-Unmodified-Since\", H_If_Unmodifed_Since, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Keep-Alive\", H_Keep_Alive, 0, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Max-Forwards\", H_Max_Forwards, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Proxy-Authorization\", H_Proxy_Authorization, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Range\", H_Range, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"Referer\", H_Referer, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"TE\", H_TE, 1, 0, 0, 0, 0)\n", f); - fputs("HTTPH(\"User-Agent\", H_User_Agent, 1, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Connection\", H_Connection, 3, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Keep-Alive\", H_Keep_Alive, 3, 0, 0, 0, 0)\n", f); + fputs("\n", f); + fputs("HTTPH(\"Accept-Charset\", H_Accept_Charset, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Accept-Encoding\", H_Accept_Encoding, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Accept-Language\", H_Accept_Language, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Accept\", H_Accept, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Authorization\", H_Authorization, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Expect\", H_Expect, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"From\", H_From, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Host\", H_Host, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"If-Match\", H_If_Match, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"If-Modified-Since\", H_If_Modified_Since, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"If-None-Match\", H_If_None_Match, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"If-Range\", H_If_Range, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"If-Unmodified-Since\", H_If_Unmodifed_Since, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Max-Forwards\", H_Max_Forwards, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Proxy-Authorization\", H_Proxy_Authorization, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Range\", H_Range, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"Referer\", H_Referer, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"TE\", H_TE, 1, 1, 0, 0, 0)\n", f); + fputs("HTTPH(\"User-Agent\", H_User_Agent, 1, 1, 0, 0, 0)\n", f); + fputs("\n", f); + fputs("HTTPH(\"Server\", H_Server, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Content-Type\", H_Content_Type, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Date\", H_Date, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Last-Modified\", H_Last_Modified, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Accept-Ranges\", H_Accept_Ranges, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Content-Length\", H_Content_Length, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Cache-Control\", H_Cache_Control, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Vary\", H_Vary, 2, 0, 0, 0, 0)\n", f); + fputs("HTTPH(\"Expires\", H_Expires, 2, 0, 0, 0, 0)\n", f); fputs("#undef HTTPH\n", f); fputs(" const char *uhdr[VCA_UNKNOWNHDR];\n", f); fputs(" unsigned nuhdr;\n", f); -- 2.39.5