[master] e68610a43 http: New http_GetContentRange() helper

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Aug 31 10:23:06 UTC 2021


commit e68610a43878505e459f983f2794813cc20902ae
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Thu Feb 25 08:39:17 2021 +0100

    http: New http_GetContentRange() helper

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 35000cf6d..31fdd4d1f 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -606,6 +606,7 @@ int http_GetHdrField(const struct http *hp, hdr_t,
     const char *field, const char **ptr);
 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);
 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 bc30d4a66..7819c383d 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -864,6 +864,70 @@ http_GetContentLength(const struct http *hp)
 	return (cl);
 }
 
+ssize_t
+http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi)
+{
+	ssize_t tmp, cl;
+	const char *b;
+
+	CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
+
+	if (lo == NULL)
+		lo = &tmp;
+	if (hi == NULL)
+		hi = &tmp;
+
+	*lo = *hi = -1;
+
+	if (!http_GetHdr(hp, H_Content_Range, &b))
+		return (-1);
+
+	if (strncasecmp("bytes", b, strlen("bytes")))
+		return (-1);		// Unknown range unit, ignore
+	b += strlen("bytes");
+
+	if (!vct_islws(*b))
+		return (-1);		// Unknown range unit, ignore
+	while (vct_islws(*b))
+		b++;
+	if (*b == '*') {		// Content-Range: bytes */123
+		*lo = *hi = -1;
+		b++;
+	} else {			// Content-Range: bytes 1-2/3
+		*lo = http_parse_uint(b, &b);
+		if (*lo < 0)
+			return (-2);
+		if (*b != '-')
+			return (-2);
+		*hi = http_parse_uint(b + 1, &b);
+		if (*hi < 0)
+			return (-2);
+	}
+	if (*b != '/')
+		return (-2);
+	if (b[1] == '*') {		// Content-Range: bytes 1-2/*
+		cl = -1;
+		b += 2;
+	} else {
+		cl = http_parse_uint(b + 1, &b);
+		if (cl <= 0)
+			return (-2);
+	}
+	while (vct_islws(*b))
+		b++;
+	if (*b != '\0')
+		return (-2);
+	if (*lo > *hi)
+		return (-2);
+	assert(cl >= -1);
+	if (cl == -1)
+		return (-1);
+	if (*lo >= cl || *hi >= cl)
+		return (-2);
+	AN(cl);
+	return (cl);
+}
+
 /*--------------------------------------------------------------------
  */
 


More information about the varnish-commit mailing list