[4.0] 36f01d5 Add a VDP_pretend_gzip for use with synth bodies in ESI includes with gzip

Martin Blix Grydeland martin at varnish-software.com
Wed Mar 11 14:35:11 CET 2015


commit 36f01d5b81ae449ecd532f1eafae32a11e8231c3
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Wed Mar 11 14:29:02 2015 +0100

    Add a VDP_pretend_gzip for use with synth bodies in ESI includes with
    gzip
    
    Varnishtest:
    NUL terminate the ungzip'ed body so we can expect on it.
    
    Fixes #1688

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 660d6b7..9727a52 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -982,6 +982,7 @@ enum vgzret_e VGZ_Gzip(struct vgz *, const void **, size_t *len, enum vgz_flag);
 enum vgzret_e VGZ_Gunzip(struct vgz *, const void **, size_t *len);
 enum vgzret_e VGZ_Destroy(struct vgz **);
 void VGZ_UpdateObj(const struct vgz*, struct object *);
+vdp_bytes VDP_pretend_gzip;
 vdp_bytes VDP_gunzip;
 
 int VGZ_WrwInit(struct vgz *vg);
diff --git a/bin/varnishd/cache/cache_gzip.c b/bin/varnishd/cache/cache_gzip.c
index bdbf229..670704a 100644
--- a/bin/varnishd/cache/cache_gzip.c
+++ b/bin/varnishd/cache/cache_gzip.c
@@ -43,6 +43,7 @@
 
 #include "cache.h"
 
+#include "vend.h"
 #include "vgz.h"
 
 struct vgz {
@@ -280,6 +281,47 @@ VGZ_WrwInit(struct vgz *vg)
 }
 
 /*--------------------------------------------------------------------
+ * VDP for pretend gzip
+ */
+
+int __match_proto__(vdp_bytes)
+VDP_pretend_gzip(struct req *req, enum vdp_action act, const void *ptr,
+    ssize_t len)
+{
+	uint8_t buf[5];
+	const uint8_t *p;
+	uint16_t lx;
+
+	CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
+	CHECK_OBJ_NOTNULL(req->wrk, WORKER_MAGIC);
+
+	if (len == 0) {
+		assert(act > VDP_NULL);
+		return (VDP_bytes(req, act, ptr, len));
+	}
+
+	lx = 65535;
+	p = ptr;
+	while (len > 0) {
+		if (lx > len)
+			lx = (uint16_t)len;
+		buf[0] = 0;
+		vle16enc(buf + 1, lx);
+		vle16enc(buf + 3, ~lx);
+		if (VDP_bytes(req, VDP_NULL, buf, sizeof buf))
+			return (-1);
+		if (VDP_bytes(req, VDP_FLUSH, p, lx))
+			return (-1);
+		req->crc = crc32(req->crc, p, lx);
+		req->l_crc += lx;
+		len -= lx;
+		assert(len >= 0);
+		p += lx;
+	}
+	return (0);
+}
+
+/*--------------------------------------------------------------------
  * VDP for gunzip'ing
  */
 
diff --git a/bin/varnishd/cache/cache_http1_deliver.c b/bin/varnishd/cache/cache_http1_deliver.c
index e0a9618..090d83d 100644
--- a/bin/varnishd/cache/cache_http1_deliver.c
+++ b/bin/varnishd/cache/cache_http1_deliver.c
@@ -394,6 +394,9 @@ V1D_Deliver_Synth(struct req *req)
 	req->vdps[0] = v1d_bytes;
 	req->vdp_nxt = 0;
 
+	if (req->gzip_resp)
+		req->vdps[++req->vdp_nxt] = VDP_pretend_gzip;
+
 	WRW_Reserve(req->wrk, &req->sp->fd, req->vsl, req->t_prev);
 
 	/*
@@ -411,11 +414,6 @@ V1D_Deliver_Synth(struct req *req)
 
 	if (!req->wantbody) {
 		/* This was a HEAD or conditional request */
-#if 0
-	XXX: Missing pretend GZIP for esi-children
-	} else if (req->res_mode & RES_ESI_CHILD && req->gzip_resp) {
-		ESI_DeliverChild(req);
-#endif
 	} else {
 		(void)VDP_bytes(req, VDP_FLUSH, VSB_data(req->synth_body),
 		    VSB_len(req->synth_body));
diff --git a/bin/varnishtest/tests/r01688.vtc b/bin/varnishtest/tests/r01688.vtc
new file mode 100644
index 0000000..fd0373b
--- /dev/null
+++ b/bin/varnishtest/tests/r01688.vtc
@@ -0,0 +1,45 @@
+varnishtest "ESI-included, compressed synthetic responses"
+
+server s1 {
+	rxreq
+	expect req.url == "/bar"
+	txresp -gzipbody {<XYZ><esi:include src="/foo"/></XYZ>}
+	rxreq
+	expect req.url == "/baz"
+	txresp -gzipbody {<XYZ><esi:include src="/quux"/></XYZ>}
+} -start
+
+varnish v1 -vcl+backend {
+	sub vcl_recv {
+	  if (req.url == "/foo" || req.url == "/quux") {
+	    return(synth(998, "included synthetic reponse"));
+	  }
+	}
+
+	sub vcl_synth {
+	  if (resp.status == 998) {
+	    synthetic("this is the body of an included synthetic response");
+	    return(deliver);
+	  }
+	}
+
+	sub vcl_backend_response {
+	  set beresp.do_esi = true;
+	}
+} -start
+
+client c1 {
+	txreq -url /bar
+	rxresp
+	expect resp.status == 200
+	delay .1
+	expect resp.body == "<XYZ>this is the body of an included synthetic response</XYZ>"
+
+	txreq -url /baz -hdr "Accept-Encoding: gzip"
+	timeout 2
+	rxresp
+	expect resp.status == 200
+	expect resp.http.Content-Encoding == "gzip"
+	gunzip
+	expect resp.body == "<XYZ>this is the body of an included synthetic response</XYZ>"
+} -run
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index 8283c68..579b981 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -630,6 +630,7 @@ cmd_http_gunzip_body(CMD_ARGS)
 
 	assert(Z_OK == inflateInit2(&vz, 31));
 	i = inflate(&vz, Z_FINISH);
+	assert(vz.total_out < l);
 	hp->bodyl = vz.total_out;
 	memcpy(hp->body, p, hp->bodyl);
 	free(p);
@@ -650,6 +651,7 @@ cmd_http_gunzip_body(CMD_ARGS)
 		    "Gunzip error = %d (%s) in:%jd out:%jd",
 		    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out);
 	assert(Z_OK == inflateEnd(&vz));
+	hp->body[hp->bodyl] = '\0';
 }
 
 /**********************************************************************



More information about the varnish-commit mailing list