[master] 4fac23d Unify the task of gunzip'ing a chunk of data and putting it in a buf to be flushed to WRW.

Poul-Henning Kamp phk at varnish-cache.org
Wed Mar 30 12:01:05 CEST 2011


commit 4fac23da161b8885f4f4c35876ac30b971015d2a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Mar 29 12:00:15 2011 +0000

    Unify the task of gunzip'ing a chunk of data and putting it in
    a buf to be flushed to WRW.

diff --git a/bin/varnishd/cache.h b/bin/varnishd/cache.h
index 9bb84e6..337f3ee 100644
--- a/bin/varnishd/cache.h
+++ b/bin/varnishd/cache.h
@@ -689,6 +689,8 @@ int VGZ_Gzip(struct vgz *, const void **, size_t *len, enum vgz_flag);
 int VGZ_Gunzip(struct vgz *, const void **, size_t *len);
 void VGZ_Destroy(struct vgz **);
 void VGZ_UpdateObj(const struct vgz*, struct object *);
+int VGZ_WrwGunzip(struct sess *, struct vgz *, void *ibuf, ssize_t ibufl,
+    char *obuf, ssize_t obufl, ssize_t *obufp);
 
 /* Return values */
 #define VGZ_ERROR	-1
diff --git a/bin/varnishd/cache_esi_deliver.c b/bin/varnishd/cache_esi_deliver.c
index aec0961..b074cd5 100644
--- a/bin/varnishd/cache_esi_deliver.c
+++ b/bin/varnishd/cache_esi_deliver.c
@@ -320,21 +320,9 @@ ESI_Deliver(struct sess *sp)
 				 * A gzip'ed VEC, but ungzip'ed ESI response
 				 */
 				AN(vgz);
-				VGZ_Ibuf(vgz, st->ptr + off, l);
-				do {
-					VGZ_Obuf(vgz, obuf + obufl,
-					    sizeof obuf - obufl);
-					i = VGZ_Gunzip(vgz, &dp, &dl);
-					assert(i >= VGZ_OK);
-					obufl += dl;
-					assert(obufl <= sizeof obuf);
-					if (obufl == sizeof obuf ||
-					    i == VGZ_STUCK) {
-						WRW_Write(sp->wrk, obuf, obufl);
-						WRW_Flush(sp->wrk);
-						obufl = 0;
-					}
-				} while (!VGZ_IbufEmpty(vgz));
+				i = VGZ_WrwGunzip(sp, vgz,
+					st->ptr + off, l,
+					obuf, sizeof obuf, &obufl);
 				assert (i == VGZ_OK || i == VGZ_END);
 			} else {
 				/*
diff --git a/bin/varnishd/cache_gzip.c b/bin/varnishd/cache_gzip.c
index 69fdb44..0222cfd 100644
--- a/bin/varnishd/cache_gzip.c
+++ b/bin/varnishd/cache_gzip.c
@@ -350,6 +350,40 @@ VGZ_Gzip(struct vgz *vg, const void **pptr, size_t *plen, enum vgz_flag flags)
 	return (-1);
 }
 
+/*--------------------------------------------------------------------
+ * Gunzip ibuf into outb, if it runs full, emit it with WRW.
+ * Leave flushing to caller, more data may be coming.
+ */
+
+int
+VGZ_WrwGunzip(struct sess *sp, struct vgz *vg, void *ibuf, ssize_t ibufl,
+    char *obuf, ssize_t obufl, ssize_t *obufp)
+{
+	int i;
+	size_t dl;
+	const void *dp;
+
+	CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
+	VGZ_Ibuf(vg, ibuf, ibufl);
+	VGZ_Obuf(vg, obuf + *obufp, ibufl - *obufp);
+	do {
+		i = VGZ_Gunzip(vg, &dp, &dl);
+		if (i < VGZ_OK) {
+			/* XXX: VSL ? */
+			return (-1);
+		}
+		*obufp += dl;
+		if (obufl == *obufp || i == VGZ_STUCK) {
+			WRW_Write(sp->wrk, obuf, *obufp);
+			if (WRW_Flush(sp->wrk))
+				return (-1);
+			*obufp = 0;
+			VGZ_Obuf(vg, obuf + *obufp, ibufl - *obufp);
+		}
+	} while (!VGZ_IbufEmpty(vg));
+	return (i);
+}
+
 /*--------------------------------------------------------------------*/
 
 void
diff --git a/bin/varnishd/cache_response.c b/bin/varnishd/cache_response.c
index c97ffea..87f6f6f 100644
--- a/bin/varnishd/cache_response.c
+++ b/bin/varnishd/cache_response.c
@@ -245,15 +245,15 @@ res_WriteGunzipObj(struct sess *sp)
 	struct storage *st;
 	unsigned u = 0;
 	struct vgz *vg;
-	const void *dp;
 	char obuf[params->gzip_stack_buffer];
-	size_t dl;
+	ssize_t obufl = 0;
 	int i;
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 
 	vg = VGZ_NewUngzip(sp, "U D -");
 
+	VGZ_Obuf(vg, obuf, sizeof obuf);
 	VTAILQ_FOREACH(st, &sp->obj->store, list) {
 		CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 		CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
@@ -262,17 +262,14 @@ res_WriteGunzipObj(struct sess *sp)
 		sp->acct_tmp.bodybytes += st->len;	/* XXX ? */
 		VSC_main->n_objwrite++;
 
-		VGZ_Ibuf(vg, st->ptr, st->len);
-		do {
-			VGZ_Obuf(vg, obuf, sizeof obuf);
-			i = VGZ_Gunzip(vg, &dp, &dl);
-			assert(i >= VGZ_OK);
-			if (dl != 0) {
-				(void)WRW_Write(sp->wrk, dp, dl);
-				if (WRW_Flush(sp->wrk))
-					break;
-			}
-		} while (!VGZ_IbufEmpty(vg));
+		i = VGZ_WrwGunzip(sp, vg,
+		    st->ptr, st->len,
+		    obuf, sizeof obuf, &obufl);
+		/* XXX: error check */
+	}
+	if (obufl) {
+		(void)WRW_Write(sp->wrk, obuf, obufl);
+		WRW_Flush(sp->wrk);
 	}
 	VGZ_Destroy(&vg);
 	assert(u == sp->obj->len);



More information about the varnish-commit mailing list