[master] a91d3f2cd Polish the body-gzippery a bit.

Poul-Henning Kamp phk at FreeBSD.org
Tue May 9 07:53:08 UTC 2023


commit a91d3f2cdb225bd1681cbb2545fd954b9cf68b35
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue May 9 07:52:10 2023 +0000

    Polish the body-gzippery a bit.

diff --git a/bin/varnishtest/vtc_gzip.c b/bin/varnishtest/vtc_gzip.c
index f38699b33..e2b210885 100644
--- a/bin/varnishtest/vtc_gzip.c
+++ b/bin/varnishtest/vtc_gzip.c
@@ -61,15 +61,18 @@ APOS(ssize_t sz)
 	return (sz);
 }
 
+/**********************************************************************
+ * GUNZIPery
+ */
+
 static struct vsb *
-vtc_gzip_vsb(struct vtclog *vl, int fatal, int gzip_level, const struct vsb *vin, int *residual)
+vtc_gunzip_vsb(struct vtclog *vl, int fatal, const struct vsb *vin)
 {
 	z_stream vz;
 	struct vsb *vout;
 	int i;
 	char buf[BUFSIZ];
 
-	AN(residual);
 	memset(&vz, 0, sizeof vz);
 	vout = VSB_new_auto();
 	AN(vout);
@@ -77,120 +80,123 @@ vtc_gzip_vsb(struct vtclog *vl, int fatal, int gzip_level, const struct vsb *vin
 	vz.next_in = (void*)VSB_data(vin);
 	vz.avail_in = APOS(VSB_len(vin));
 
-	assert(Z_OK == deflateInit2(&vz,
-	    gzip_level, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
+	assert(Z_OK == inflateInit2(&vz, 31));
 
 	do {
 		vz.next_out = (void*)buf;
 		vz.avail_out = sizeof buf;
-		i = deflate(&vz, Z_FINISH);
+		i = inflate(&vz, Z_FINISH);
 		if (vz.avail_out != sizeof buf)
 			VSB_bcat(vout, buf, sizeof buf - vz.avail_out);
 	} while (i == Z_OK || i == Z_BUF_ERROR);
 	if (i != Z_STREAM_END)
 		vtc_log(vl, fatal,
-		    "Gzip error = %d (%s) in:%jd out:%jd len:%zd",
+		    "Gunzip error = %d (%s) in:%jd out:%jd len:%zd",
 		    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out,
 		    VSB_len(vin));
 	AZ(VSB_finish(vout));
 #ifdef VGZ_EXTENSIONS
-	*residual = vz.stop_bit & 7;
 	vtc_report_gz_bits(vl, &vz);
-#else
-	*residual = 0;
 #endif
-	assert(Z_OK == deflateEnd(&vz));
+	assert(Z_OK == inflateEnd(&vz));
 	return (vout);
 }
 
-static void
-vtc_gzip(struct http *hp, const char *input, char **body, long *bodylen)
+void
+vtc_gunzip(struct http *hp, char *body, long *bodylen)
 {
 	struct vsb *vin, *vout;
-	int res;
+
+	AN(body);
+	if (body[0] != (char)0x1f || body[1] != (char)0x8b)
+		vtc_log(hp->vl, hp->fatal,
+		    "Gunzip error: body lacks gzip magic");
 
 	vin = VSB_new_auto();
 	AN(vin);
-	VSB_bcat(vin, input, strlen(input));
+	VSB_bcat(vin, body, *bodylen);
 	AZ(VSB_finish(vin));
-	vout = vtc_gzip_vsb(hp->vl, hp->fatal, hp->gziplevel, vin, &res);
+	vout = vtc_gunzip_vsb(hp->vl, hp->fatal, vin);
 	VSB_destroy(&vin);
 
-#ifdef VGZ_EXTENSIONS
-	if (hp->gzipresidual >= 0 && hp->gzipresidual != res)
-		vtc_log(hp->vl, hp->fatal,
-		    "Wrong gzip residual got %d wanted %d",
-		    res, hp->gzipresidual);
-#endif
-	*body = malloc(APOS(VSB_len(vout) + 1));
-	AN(*body);
-	memcpy(*body, VSB_data(vout), APOS(VSB_len(vout) + 1));
+	memcpy(body, VSB_data(vout), APOS(VSB_len(vout) + 1));
 	*bodylen = APOS(VSB_len(vout));
 	VSB_destroy(&vout);
 	vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen);
-	vtc_dump(hp->vl, 4, "body", *body, *bodylen);
+	vtc_dump(hp->vl, 4, "body", body, *bodylen);
 	bprintf(hp->bodylen, "%ld", *bodylen);
 }
 
-static struct vsb *
-vtc_gunzip_vsb(struct vtclog *vl, int fatal, const struct vsb *vin)
+/**********************************************************************
+ * GZIPery
+ */
+
+static int
+vtc_gzip_chunk(z_stream *vz, struct vsb *vout, const void *in, size_t inlen, int flush)
 {
-	z_stream vz;
-	struct vsb *vout;
 	int i;
 	char buf[BUFSIZ];
 
+	vz->next_in = TRUST_ME(in);
+	vz->avail_in = APOS(inlen);
+	do {
+		vz->next_out = (void*)buf;
+		vz->avail_out = sizeof buf;
+		i = deflate(vz, flush);
+		if (vz->avail_out != sizeof buf)
+			VSB_bcat(vout, buf, sizeof buf - vz->avail_out);
+	} while (i == Z_OK || i == Z_BUF_ERROR);
+	vz->next_out = NULL;
+	vz->avail_out = 0;
+	vz->next_in = NULL;
+	AZ(vz->avail_in);
+	vz->avail_in = 0;
+	return (i);
+}
+
+static void
+vtc_gzip(struct http *hp, const void *input, char **body, long *bodylen)
+{
+	struct vsb *vout;
+	int i, res;
+	z_stream vz;
+
 	memset(&vz, 0, sizeof vz);
 	vout = VSB_new_auto();
 	AN(vout);
 
-	vz.next_in = (void*)VSB_data(vin);
-	vz.avail_in = APOS(VSB_len(vin));
-
-	assert(Z_OK == inflateInit2(&vz, 31));
+	assert(Z_OK == deflateInit2(&vz,
+	    hp->gziplevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
 
-	do {
-		vz.next_out = (void*)buf;
-		vz.avail_out = sizeof buf;
-		i = inflate(&vz, Z_FINISH);
-		if (vz.avail_out != sizeof buf)
-			VSB_bcat(vout, buf, sizeof buf - vz.avail_out);
-	} while (i == Z_OK || i == Z_BUF_ERROR);
-	if (i != Z_STREAM_END)
-		vtc_log(vl, fatal,
-		    "Gunzip error = %d (%s) in:%jd out:%jd len:%zd",
+	i = vtc_gzip_chunk(&vz, vout, input, strlen(input), Z_FINISH);
+	if (i != Z_STREAM_END) {
+		vtc_log(hp->vl, hp->fatal,
+		    "Gzip error = %d (%s) in:%jd out:%jd len:%zd",
 		    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out,
-		    VSB_len(vin));
+		    strlen(input));
+	}
 	AZ(VSB_finish(vout));
 #ifdef VGZ_EXTENSIONS
-	vtc_report_gz_bits(vl, &vz);
+	res = vz.stop_bit & 7;
+	vtc_report_gz_bits(hp->vl, &vz);
+#else
+	res = 0;
 #endif
-	assert(Z_OK == inflateEnd(&vz));
-	return (vout);
-}
-
-void
-vtc_gunzip(struct http *hp, char *body, long *bodylen)
-{
-	struct vsb *vin, *vout;
+	assert(Z_OK == deflateEnd(&vz));
 
-	AN(body);
-	if (body[0] != (char)0x1f || body[1] != (char)0x8b)
+#ifdef VGZ_EXTENSIONS
+	if (hp->gzipresidual >= 0 && hp->gzipresidual != res)
 		vtc_log(hp->vl, hp->fatal,
-		    "Gunzip error: body lacks gzip magic");
-
-	vin = VSB_new_auto();
-	AN(vin);
-	VSB_bcat(vin, body, *bodylen);
-	AZ(VSB_finish(vin));
-	vout = vtc_gunzip_vsb(hp->vl, hp->fatal, vin);
-	VSB_destroy(&vin);
-
-	memcpy(body, VSB_data(vout), APOS(VSB_len(vout) + 1));
+		    "Wrong gzip residual got %d wanted %d",
+		    res, hp->gzipresidual);
+#endif
+	*body = malloc(APOS(VSB_len(vout) + 1));
+	AN(*body);
+	memcpy(*body, VSB_data(vout), APOS(VSB_len(vout) + 1));
 	*bodylen = APOS(VSB_len(vout));
 	VSB_destroy(&vout);
 	vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen);
-	vtc_dump(hp->vl, 4, "body", body, *bodylen);
+	vtc_dump(hp->vl, 4, "body", *body, *bodylen);
 	bprintf(hp->bodylen, "%ld", *bodylen);
 }
 


More information about the varnish-commit mailing list