[master] 62080bc Move interpretation of Content-Length to cache_http.c since HTTP/2 will also use it.

Poul-Henning Kamp phk at FreeBSD.org
Mon Sep 8 10:01:55 CEST 2014


commit 62080bc09286e7ebee31e0aceb34dca8dba398a4
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Sep 8 08:01:22 2014 +0000

    Move interpretation of Content-Length to cache_http.c since HTTP/2
    will also use it.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 4c0e6d2..b422d9b 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -966,6 +966,7 @@ int http_GetHdrToken(const struct http *hp, const char *hdr,
 int http_GetHdrField(const struct http *hp, const char *hdr,
     const char *field, char **ptr);
 double http_GetHdrQ(const struct http *hp, const char *hdr, const char *field);
+ssize_t http_GetContentLength(const struct http *hp);
 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);
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 177919e..7e2474c 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -525,6 +525,35 @@ http_GetHdrField(const struct http *hp, const char *hdr,
 
 /*--------------------------------------------------------------------*/
 
+ssize_t
+http_GetContentLength(const struct http *hp)
+{
+	ssize_t cl, cll;
+	char *b;
+
+	CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
+
+	if (!http_GetHdr(hp, H_Content_Length, &b))
+		return (-1);
+	cl = 0;
+	if (!vct_isdigit(*b))
+		return (-2);
+	for (;vct_isdigit(*b); b++) {
+		cll = cl;
+		cl *= 10;
+		cl += *b - '0';
+		if (cll != cl / 10)
+			return (-2);
+	}
+	while (vct_islws(*b))
+		b++;
+	if (*b != '\0')
+		return (-2);
+	return (cl);
+}
+
+/*--------------------------------------------------------------------*/
+
 int
 http_HdrIs(const struct http *hp, const char *hdr, const char *val)
 {
diff --git a/bin/varnishd/cache/cache_rfc2616.c b/bin/varnishd/cache/cache_rfc2616.c
index b7737bc..131df15 100644
--- a/bin/varnishd/cache/cache_rfc2616.c
+++ b/bin/varnishd/cache/cache_rfc2616.c
@@ -35,7 +35,6 @@
 #include "cache.h"
 
 #include "vtim.h"
-#include "vct.h"
 
 /*--------------------------------------------------------------------
  * TTL and Age calculation in Varnish
@@ -190,7 +189,7 @@ RFC2616_Body(struct busyobj *bo, struct dstat *stats)
 {
 	struct http *hp;
 	char *b;
-	ssize_t cll;
+	ssize_t cl;
 
 	hp = bo->beresp;
 
@@ -243,37 +242,20 @@ RFC2616_Body(struct busyobj *bo, struct dstat *stats)
 		return (BS_ERROR);
 	}
 
-	if (http_GetHdr(hp, H_Content_Length, &b)) {
-		bo->content_length = 0;
-		if (!vct_isdigit(*b)) {
-			VSLb(bo->vsl, SLT_Error, "Empty Content-Length:");
-			stats->fetch_bad++;
-			return (BS_ERROR);
-		}
-		for (;vct_isdigit(*b); b++) {
-			cll = bo->content_length;
-			bo->content_length *= 10;
-			bo->content_length += *b - '0';
-			if (cll > bo->content_length) {
-				VSLb(bo->vsl, SLT_Error,
-				    "Content-Length: too large");
-				stats->fetch_bad++;
-				return (BS_ERROR);
-			}
-		}
-		while (vct_islws(*b))
-			b++;
-		if (*b != '\0') {
-			VSLb(bo->vsl, SLT_Error,
-			    "Illegal Content-Length: (0x%02x)", *b);
-			stats->fetch_bad++;
-			return (BS_ERROR);
-		}
+	cl = http_GetContentLength(hp);
+	if (cl == -2) {
+		VSLb(bo->vsl, SLT_Error, "Bad Content-Length:");
+		stats->fetch_bad++;
+		return (BS_ERROR);
+	}
+	if (cl > 0) {
+		stats->fetch_length++;
+		bo->content_length = cl;
+		return (BS_LENGTH);
+	}
+	if (cl == 0) {
 		stats->fetch_length++;
-		if (bo->content_length == 0)
-			return (BS_NONE);
-		else
-			return (BS_LENGTH);
+		return (BS_NONE);
 	}
 
 	if (http_HdrIs(hp, H_Connection, "keep-alive")) {



More information about the varnish-commit mailing list