From: phk Date: Sun, 15 Jun 2008 13:09:21 +0000 (+0000) Subject: Function to receive a HTTP header, as request or response. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f8d52a66db387632315e540ef922f2d5a62340e6;p=varnish Function to receive a HTTP header, as request or response. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2680 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishtest/vtc_http.c b/varnish-cache/bin/varnishtest/vtc_http.c index acb613a0..38388f41 100644 --- a/varnish-cache/bin/varnishtest/vtc_http.c +++ b/varnish-cache/bin/varnishtest/vtc_http.c @@ -27,6 +27,7 @@ */ +#include #include #include #include @@ -44,8 +45,103 @@ struct http { #define HTTP_MAGIC 0x2f02169c int fd; int client; + int timeout; + + int nrxbuf; + char *rxbuf; + + char *req; + char *resp; }; +/********************************************************************** + * Receive a HTTP protocol header + */ + +static void +http_rxhdr(struct http *hp) +{ + int l, n, i; + struct pollfd pfd[1]; + char *p; + + CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); + hp->rxbuf = malloc(hp->nrxbuf); /* XXX */ + AN(hp->rxbuf); + l = 0; + while (1) { + pfd[0].fd = hp->fd; + pfd[0].events = POLLRDNORM; + pfd[0].revents = 0; + i = poll(pfd, 1, hp->timeout); + assert(i > 0); + assert(l < hp->nrxbuf); + n = read(hp->fd, hp->rxbuf + l, 1); + assert(n == 1); + l += n; + hp->rxbuf[l] = '\0'; + assert(n > 0); + p = hp->rxbuf + l - 1; + i = 0; + for (i = 0; p > hp->rxbuf; p--) { + if (*p != '\n') + break; + if (p - 1 > hp->rxbuf && p[-1] == '\r') + p--; + if (++i == 2) + break; + } + if (i == 2) + break; + } +printf("<<<%s>>>\n", hp->rxbuf); +} + + +/********************************************************************** + * Receive a response + */ + +static void +cmd_http_rxresp(char **av, void *priv) +{ + struct http *hp; + + CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); + AN(hp->client); + assert(!strcmp(av[0], "rxresponse")); + av++; + + for(; *av != NULL; av++) { + fprintf(stderr, "Unknown http rxresp spec: %s\n", *av); + exit (1); + } + http_rxhdr(hp); + hp->resp = hp->rxbuf; +} + +/********************************************************************** + * Receive a request + */ + +static void +cmd_http_rxreq(char **av, void *priv) +{ + struct http *hp; + + CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); + AZ(hp->client); + assert(!strcmp(av[0], "rxreq")); + av++; + + for(; *av != NULL; av++) { + fprintf(stderr, "Unknown http rxreq spec: %s\n", *av); + exit (1); + } + http_rxhdr(hp); + hp->req = hp->rxbuf; +} + /********************************************************************** * Transmit a request */ @@ -98,7 +194,7 @@ cmd_http_txreq(char **av, void *priv) av++; continue; } - fprintf(stderr, "Unknown http spec: %s\n", *av); + fprintf(stderr, "Unknown http txreq spec: %s\n", *av); exit (1); } if (dohdr == 0) { @@ -120,9 +216,9 @@ cmd_http_txreq(char **av, void *priv) static struct cmds http_cmds[] = { { "txreq", cmd_http_txreq }, - { "rxreq", cmd_dump }, + { "rxreq", cmd_http_rxreq }, { "txresponse", cmd_dump }, - { "rxresponse", cmd_dump }, + { "rxresponse", cmd_http_rxresp }, { "expect", cmd_dump }, { NULL, NULL } }; @@ -136,6 +232,8 @@ http_process(const char *spec, int sock, int client) ALLOC_OBJ(hp, HTTP_MAGIC); hp->fd = sock; hp->client = client; + hp->timeout = 1000; + hp->nrxbuf = 8192; s = strdup(spec + 1); q = strchr(s, '\0');