[master] 095d05d Generalize the transmit code, so it can be used for both req.* and resp.*

Poul-Henning Kamp phk at varnish-cache.org
Fri Jan 25 09:47:08 CET 2013


commit 095d05d94cf0b62e87d105acb28f8b44aca1a31a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri Jan 25 08:46:32 2013 +0000

    Generalize the transmit code, so it can be used for both req.* and
    resp.*
    
    Submitted by: jonathan.huot at thomsonreuters.com

diff --git a/bin/varnishtest/tests/a00011.vtc b/bin/varnishtest/tests/a00011.vtc
index 9e07f83..7b71d0d 100644
--- a/bin/varnishtest/tests/a00011.vtc
+++ b/bin/varnishtest/tests/a00011.vtc
@@ -2,12 +2,18 @@ varnishtest "test vtc gzip support"
 
 server s1 {
 	rxreq
+	expect req.http.content-length == "26"
+	expect req.bodylen == "26"
+	gunzip
+	expect req.bodylen == "3"
+	expect req.http.content-encoding == "gzip"
 	txresp -gzipbody FOO
 } -start
 
 client c1 -connect ${s1_sock} {
-	txreq 
+	txreq -gzipbody FOO
 	rxresp
+	expect resp.http.content-length == "26"
 	expect resp.bodylen == "26"
 	gunzip
 	expect resp.bodylen == "3"
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index a7c128c..6a204dc 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -562,13 +562,14 @@ cmd_http_gunzip_body(CMD_ARGS)
 	char *p;
 	unsigned l;
 
+	(void)av;
 	(void)cmd;
 	(void)vl;
 	CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
-	ONLY_CLIENT(hp, av);
 
 	memset(&vz, 0, sizeof vz);
 
+	AN(hp->body);
 	if (hp->body[0] != (char)0x1f || hp->body[1] != (char)0x8b)
 		vtc_log(hp->vl, hp->fatal,
 		    "Gunzip error: Body lacks gzip magics");
@@ -650,53 +651,22 @@ gzip_body(const struct http *hp, const char *txt, char **body, int *bodylen)
 }
 
 /**********************************************************************
- * Transmit a response
+ * Handle common arguments of a transmited request or response
  */
 
-static void
-cmd_http_txresp(CMD_ARGS)
+static char* const *
+http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
+    char* body)
 {
-	struct http *hp;
-	const char *proto = "HTTP/1.1";
-	const char *status = "200";
-	const char *msg = "Ok";
 	int bodylen = 0;
 	char *b, *c;
-	char *body = NULL, *nullbody;
+	char *nullbody = NULL;
 	int nolen = 0;
 
-
-	(void)cmd;
 	(void)vl;
-	CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
-	ONLY_SERVER(hp, av);
-	assert(!strcmp(av[0], "txresp"));
-	av++;
-
-	VSB_clear(hp->vsb);
-
-	/* send a "Content-Length: 0" header unless something else happens */
-	REPLACE(body, "");
 	nullbody = body;
 
 	for(; *av != NULL; av++) {
-		if (!strcmp(*av, "-proto")) {
-			proto = av[1];
-			av++;
-		} else if (!strcmp(*av, "-status")) {
-			status = av[1];
-			av++;
-		} else if (!strcmp(*av, "-msg")) {
-			msg = av[1];
-			av++;
-			continue;
-		} else
-			break;
-	}
-
-	VSB_printf(hp->vsb, "%s %s %s%s", proto, status, msg, nl);
-
-	for(; *av != NULL; av++) {
 		if (!strcmp(*av, "-nolen")) {
 			nolen = 1;
 		} else if (!strcmp(*av, "-hdr")) {
@@ -750,13 +720,60 @@ cmd_http_txresp(CMD_ARGS)
 		} else
 			break;
 	}
-	if (*av != NULL)
-		vtc_log(hp->vl, 0, "Unknown http txresp spec: %s\n", *av);
 	if (body != NULL && !nolen)
 		VSB_printf(hp->vsb, "Content-Length: %d%s", bodylen, nl);
 	VSB_cat(hp->vsb, nl);
 	if (body != NULL)
 		VSB_bcat(hp->vsb, body, bodylen);
+	return av;
+}
+
+/**********************************************************************
+ * Transmit a response
+ */
+
+static void
+cmd_http_txresp(CMD_ARGS)
+{
+	struct http *hp;
+	const char *proto = "HTTP/1.1";
+	const char *status = "200";
+	const char *msg = "Ok";
+	char* body = NULL;
+
+	(void)cmd;
+	(void)vl;
+	CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
+	ONLY_SERVER(hp, av);
+	assert(!strcmp(av[0], "txresp"));
+	av++;
+
+	VSB_clear(hp->vsb);
+
+	for(; *av != NULL; av++) {
+		if (!strcmp(*av, "-proto")) {
+			proto = av[1];
+			av++;
+		} else if (!strcmp(*av, "-status")) {
+			status = av[1];
+			av++;
+		} else if (!strcmp(*av, "-msg")) {
+			msg = av[1];
+			av++;
+			continue;
+		} else
+			break;
+	}
+
+	VSB_printf(hp->vsb, "%s %s %s%s", proto, status, msg, nl);
+
+	/* send a "Content-Length: 0" header unless something else happens */
+	REPLACE(body, "");
+
+	av = http_tx_parse_args(av, vl, hp, body);
+	if (*av != NULL)
+		vtc_log(hp->vl, 0, "Unknown http txresp spec: %s\n", *av);
+
 	http_write(hp, 4, "txresp");
 }
 
@@ -780,6 +797,7 @@ cmd_http_rxreq(CMD_ARGS)
 		vtc_log(hp->vl, 0, "Unknown http rxreq spec: %s\n", *av);
 	http_rxhdr(hp);
 	http_splitheader(hp, 1);
+	hp->body = hp->rxbuf + hp->prxbuf;
 	http_swallow_body(hp, hp->req, 0);
 	vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
 }
@@ -851,7 +869,6 @@ cmd_http_txreq(CMD_ARGS)
 	const char *req = "GET";
 	const char *url = "/";
 	const char *proto = "HTTP/1.1";
-	const char *body = NULL;
 
 	(void)cmd;
 	(void)vl;
@@ -876,35 +893,10 @@ cmd_http_txreq(CMD_ARGS)
 			break;
 	}
 	VSB_printf(hp->vsb, "%s %s %s%s", req, url, proto, nl);
-	for(; *av != NULL; av++) {
-		if (!strcmp(*av, "-hdr")) {
-			VSB_printf(hp->vsb, "%s%s", av[1], nl);
-			av++;
-		} else
-			break;
-	}
-	for(; *av != NULL; av++) {
-		if (!strcmp(*av, "-body")) {
-			AZ(body);
-			body = av[1];
-			av++;
-		} else if (!strcmp(*av, "-bodylen")) {
-			AZ(body);
-			body = synth_body(av[1], 0);
-			av++;
-		} else
-			break;
-	}
+
+	av = http_tx_parse_args(av, vl, hp, NULL);
 	if (*av != NULL)
 		vtc_log(hp->vl, 0, "Unknown http txreq spec: %s\n", *av);
-	if (body != NULL)
-		VSB_printf(hp->vsb, "Content-Length: %ju%s",
-		    (uintmax_t)strlen(body), nl);
-	VSB_cat(hp->vsb, nl);
-	if (body != NULL) {
-		VSB_cat(hp->vsb, body);
-		VSB_cat(hp->vsb, nl);
-	}
 	http_write(hp, 4, "txreq");
 }
 



More information about the varnish-commit mailing list