Mailing List Archive

r2680 - trunk/varnish-cache/bin/varnishtest
Author: phk
Date: 2008-06-15 15:09:21 +0200 (Sun, 15 Jun 2008)
New Revision: 2680

Modified:
trunk/varnish-cache/bin/varnishtest/vtc_http.c
Log:
Function to receive a HTTP header, as request or response.



Modified: trunk/varnish-cache/bin/varnishtest/vtc_http.c
===================================================================
--- trunk/varnish-cache/bin/varnishtest/vtc_http.c 2008-06-15 12:45:53 UTC (rev 2679)
+++ trunk/varnish-cache/bin/varnishtest/vtc_http.c 2008-06-15 13:09:21 UTC (rev 2680)
@@ -27,6 +27,7 @@
*/


+#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -44,9 +45,104 @@
#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 @@
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 @@

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