]> err.no Git - varnish/commitdiff
Function to receive a HTTP header, as request or response.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sun, 15 Jun 2008 13:09:21 +0000 (13:09 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sun, 15 Jun 2008 13:09:21 +0000 (13:09 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2680 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishtest/vtc_http.c

index acb613a0dd88c592611ba6bd51885abe7e4f57c7..38388f4194a198c593d42a64883946baea4707dd 100644 (file)
@@ -27,6 +27,7 @@
  */
 
 
+#include <poll.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -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');