[7.3] ca88d4346 hpack: Turn header validation state into an enum

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Oct 24 15:08:14 UTC 2023


commit ca88d4346555c2e9e6f9a255ba93e6332ed4a6e7
Author: Walid Boudebouda <walid.boudebouda at gmail.com>
Date:   Fri Sep 8 16:55:18 2023 +0200

    hpack: Turn header validation state into an enum
    
    Signed-off-by: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>

diff --git a/bin/varnishd/http2/cache_http2_hpack.c b/bin/varnishd/http2/cache_http2_hpack.c
index b05ab31e0..7d9625b41 100644
--- a/bin/varnishd/http2/cache_http2_hpack.c
+++ b/bin/varnishd/http2/cache_http2_hpack.c
@@ -44,6 +44,12 @@ static h2_error
 h2h_checkhdr(const struct http *hp, const char *b, size_t namelen, size_t len)
 {
 	const char *p;
+	enum {
+		FLD_NAME_FIRST,
+		FLD_NAME,
+		FLD_VALUE_FIRST,
+		FLD_VALUE
+	} state;
 
 	CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
 	AN(b);
@@ -60,15 +66,15 @@ h2h_checkhdr(const struct http *hp, const char *b, size_t namelen, size_t len)
 	// VSLb(hp->vsl, SLT_Debug, "CHDR [%.*s] [%.*s]",
 	//     (int)namelen, b, (int)(len - namelen), b + namelen);
 
-	int state = 0;
+	state = FLD_NAME_FIRST;
 	for (p = b; p < b + namelen - 2; p++) {
 		switch(state) {
-		case 0:	/* First char of field */
-			state = 1;
+		case FLD_NAME_FIRST:
+			state = FLD_NAME;
 			if (*p == ':')
 				break;
 			/* FALL_THROUGH */
-		case 1: /* field name */
+		case FLD_NAME:
 			if (*p <= 0x20 || *p >= 0x7f) {
 				VSLb(hp->vsl, SLT_BogoHeader,
 				    "Illegal field header name (control): %.*s",
@@ -93,19 +99,19 @@ h2h_checkhdr(const struct http *hp, const char *b, size_t namelen, size_t len)
 		}
 	}
 
-	state = 2;
+	state = FLD_VALUE_FIRST;
 	for (p = b + namelen; p < b + len; p++) {
 		switch(state) {
-		case 2: /* First char of field */
+		case FLD_VALUE_FIRST:
 			if (*p == ' ' || *p == 0x09) {
 				VSLb(hp->vsl, SLT_BogoHeader,
 				    "Illegal field value start %.*s",
 				    (int)(len > 20 ? 20 : len), b);
 				return (H2SE_PROTOCOL_ERROR);
 			}
-			state = 3;
+			state = FLD_VALUE;
 			/* FALL_THROUGH */
-		case 3: /* field value character */
+		case FLD_VALUE:
 			if (*p != 0x09 && (*p < 0x20 || *p == 0x7f)) {
 				VSLb(hp->vsl, SLT_BogoHeader,
 				    "Illegal field value (control) %.*s",
@@ -117,7 +123,7 @@ h2h_checkhdr(const struct http *hp, const char *b, size_t namelen, size_t len)
 			WRONG("http2 field value validation state");
 		}
 	}
-	if (state == 3 && b[len - 1] <= 0x20) {
+	if (state == FLD_VALUE && b[len - 1] <= 0x20) {
 		VSLb(hp->vsl, SLT_BogoHeader,
 		    "Illegal field value (end) %.*s",
 		    (int)(len > 20 ? 20 : len), b);


More information about the varnish-commit mailing list