[master] e8f56d916 range: Move more parsing logic to http_GetRange()

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Jan 18 06:44:05 UTC 2023


commit e8f56d916ce9eb568fd1ff983eb84b55276a3dea
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed Oct 26 14:38:52 2022 +0200

    range: Move more parsing logic to http_GetRange()

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index f1d107c15..a0e141a48 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -640,7 +640,8 @@ int http_GetHdrField(const struct http *hp, hdr_t,
 double http_GetHdrQ(const struct http *hp, hdr_t, const char *field);
 ssize_t http_GetContentLength(const struct http *hp);
 ssize_t http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi);
-const char * http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi);
+const char * http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi,
+    ssize_t len);
 uint16_t http_GetStatus(const struct http *hp);
 int http_IsStatus(const struct http *hp, int);
 void http_SetStatus(struct http *to, uint16_t status, const char *reason);
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 77120bf3f..63818eaad 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -958,7 +958,7 @@ http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi)
 }
 
 const char *
-http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi)
+http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi, ssize_t len)
 {
 	ssize_t tmp_lo, tmp_hi;
 	const char *b, *t;
@@ -1003,6 +1003,26 @@ http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi)
 		b++;
 	if (*b != '\0')
 		return ("Trailing stuff");
+
+	assert(*lo >= -1);
+	assert(*hi >= -1);
+
+	if (len <= 0)
+		return (NULL);			// Allow 200 response
+
+	if (*lo < 0) {
+		assert(*hi > 0);
+		*lo = len - *hi;
+		if (*lo < 0)
+			*lo = 0;
+		*hi = len - 1;
+	} else if (len >= 0 && (*hi >= len || *hi < 0)) {
+		*hi = len - 1;
+	}
+
+	if (*lo >= len)
+		return ("low range beyond object");
+
 	return (NULL);
 }
 
diff --git a/bin/varnishd/cache/cache_range.c b/bin/varnishd/cache/cache_range.c
index ac1a45bc6..dc1aa73f3 100644
--- a/bin/varnishd/cache/cache_range.c
+++ b/bin/varnishd/cache/cache_range.c
@@ -108,25 +108,13 @@ vrg_dorange(struct req *req, void **priv)
 	struct vrg_priv *vrg_priv;
 	const char *err;
 
-	err = http_GetRange(req->http, &low, &high);
+	err = http_GetRange(req->http, &low, &high, req->resp_len);
 	if (err != NULL)
 		return (err);
 
-	assert(low >= -1);
-	assert(high >= -1);
-
-	if (low < 0) {
-		if (req->resp_len < 0 || high < 0)
-			return (NULL);		// Allow 200 response
-		assert(high > 0);
-		low = req->resp_len - high;
-		if (low < 0)
-			low = 0;
-		high = req->resp_len - 1;
-	} else if (req->resp_len >= 0 && (high >= req->resp_len || high < 0))
-		high = req->resp_len - 1;
-	else if (high < 0)
-		return (NULL);			// Allow 200 response
+	if (low < 0 || high < 0)
+		return (NULL);		// Allow 200 response
+
 	/*
 	 * else (bo != NULL) {
 	 *    We assume that the client knows what it's doing and trust
@@ -134,9 +122,6 @@ vrg_dorange(struct req *req, void **priv)
 	 * }
 	 */
 
-	if (req->resp_len >= 0 && low >= req->resp_len)
-		return ("low range beyond object");
-
 	if (req->resp_len >= 0) {
 		http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd",
 		    (intmax_t)low, (intmax_t)high, (intmax_t)req->resp_len);
@@ -268,7 +253,7 @@ VRG_CheckBo(struct busyobj *bo)
 	if (!cache_param->http_range_support)
 		return (0);
 
-	err = http_GetRange(bo->bereq0, &rlo, &rhi);
+	err = http_GetRange(bo->bereq0, &rlo, &rhi, -1);
 	clen = http_GetContentLength(bo->beresp);
 	crlen = http_GetContentRange(bo->beresp, &crlo, &crhi);
 
diff --git a/include/vrt.h b/include/vrt.h
index d2af82cc7..e65e59737 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -58,6 +58,7 @@
  * binary/load-time compatible, increment MAJOR version
  *
  * NEXT (2023-03-15)
+ *	[cache.h] http_GetRange() changed
  * 16.0 (2022-09-15)
  *	VMOD C-prototypes moved into JSON
  *	VRT_AddVDP() deprecated


More information about the varnish-commit mailing list