[master] acd0cd4a8 Convert body_status from enum to const struct *

Poul-Henning Kamp phk at FreeBSD.org
Fri Feb 21 08:34:06 UTC 2020


commit acd0cd4a81e93ab76162178ddb41acd4e0bf4018
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri Feb 21 06:55:58 2020 +0000

    Convert body_status from enum to const struct *

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index e2274dd92..2b36acc0b 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -57,11 +57,18 @@
 
 /*--------------------------------------------------------------------*/
 
-enum body_status {
-#define BODYSTATUS(U,l)	BS_##U,
-#include "tbl/body_status.h"
+struct body_status {
+	const char		*name;
+	int			nbr;
+	int			avail;
+	int			length_known;
 };
 
+#define BODYSTATUS(U, l, n, a, k) extern const struct body_status BS_##U[1];
+#include "tbl/body_status.h"
+
+typedef const struct body_status *body_status_t;
+
 /*--------------------------------------------------------------------*/
 
 enum req_body_state_e {
diff --git a/bin/varnishd/cache/cache_fetch.c b/bin/varnishd/cache/cache_fetch.c
index 06bd94ddc..df00367e0 100644
--- a/bin/varnishd/cache/cache_fetch.c
+++ b/bin/varnishd/cache/cache_fetch.c
@@ -640,7 +640,7 @@ vbf_stp_fetch(struct worker *wrk, struct busyobj *bo)
 	}
 
 	VSLb(bo->vsl, SLT_Fetch_Body, "%u %s %s",
-	    bo->htc->body_status, body_status_2str(bo->htc->body_status),
+	    bo->htc->body_status->nbr, bo->htc->body_status->name,
 	    bo->do_stream ? "stream" : "-");
 
 	if (bo->htc->body_status != BS_NONE) {
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 2297121e0..91069f103 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -40,6 +40,16 @@
 #include "vct.h"
 #include "vtim.h"
 
+#define BODYSTATUS(U, l, n, a, k)				\
+	const struct body_status BS_##U[1] = {{			\
+		.name = #l,					\
+		.nbr = n,					\
+		.avail = a,					\
+		.length_known = k				\
+	}};
+#include "tbl/body_status.h"
+
+
 #define HTTPH(a, b, c) char b[] = "*" a ":";
 #include "tbl/http_headers.h"
 
diff --git a/bin/varnishd/cache/cache_panic.c b/bin/varnishd/cache/cache_panic.c
index d5031b131..81987f89e 100644
--- a/bin/varnishd/cache/cache_panic.c
+++ b/bin/varnishd/cache/cache_panic.c
@@ -71,19 +71,6 @@ static void pan_req(struct vsb *, const struct req *);
 
 /*--------------------------------------------------------------------*/
 
-const char *
-body_status_2str(enum body_status e)
-{
-	switch (e) {
-#define BODYSTATUS(U,l)	case BS_##U: return (#l);
-#include "tbl/body_status.h"
-	default:
-		return ("?");
-	}
-}
-
-/*--------------------------------------------------------------------*/
-
 static const char *
 reqbody_status_2str(enum req_body_state_e e)
 {
@@ -204,7 +191,7 @@ pan_htc(struct vsb *vsb, const struct http_conn *htc)
 	VSB_printf(vsb, "content_length = %jd,\n",
 	    (intmax_t)htc->content_length);
 	VSB_printf(vsb, "body_status = %s,\n",
-	    body_status_2str(htc->body_status));
+	    htc->body_status ? htc->body_status->name : "NULL");
 	VSB_printf(vsb, "first_byte_timeout = %f,\n",
 	    htc->first_byte_timeout);
 	VSB_printf(vsb, "between_bytes_timeout = %f,\n",
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index 73c758f23..e7fc7515f 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -67,7 +67,7 @@ struct http_conn {
 
 	int			*rfd;
 	enum sess_close		doclose;
-	enum body_status	body_status;
+	body_status_t		body_status;
 	struct ws		*ws;
 	char			*rxbuf_b;
 	char			*rxbuf_e;
@@ -335,7 +335,6 @@ void ObjUnsubscribeEvents(uintptr_t *);
 /* cache_panic.c */
 void PAN_Init(void);
 int PAN_already(struct vsb *, const void *);
-const char *body_status_2str(enum body_status e);
 const char *sess_close_2str(enum sess_close sc, int want_desc);
 
 /* cache_pool.c */
diff --git a/bin/varnishd/http1/cache_http1_fsm.c b/bin/varnishd/http1/cache_http1_fsm.c
index 81ab2e3f6..bbce1b4fe 100644
--- a/bin/varnishd/http1/cache_http1_fsm.c
+++ b/bin/varnishd/http1/cache_http1_fsm.c
@@ -151,16 +151,9 @@ http1_req_body(struct req *req)
 {
 
 	CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
-	switch (req->htc->body_status) {
-	case BS_EOF:
-	case BS_LENGTH:
-	case BS_CHUNKED:
-		if (V1F_Setup_Fetch(req->vfc, req->htc) != 0)
-			req->req_body_status = REQ_BODY_FAIL;
-		break;
-	default:
-		break;
-	}
+	if (req->htc->body_status->avail &&
+	    V1F_Setup_Fetch(req->vfc, req->htc) != 0)
+		req->req_body_status = REQ_BODY_FAIL;
 }
 
 static void
@@ -282,22 +275,16 @@ http1_dissect(struct worker *wrk, struct req *req)
 
 	assert (req->req_body_status == REQ_BODY_INIT);
 
-	switch (req->htc->body_status) {
-	case BS_CHUNKED:
+	if (req->htc->body_status == BS_CHUNKED)
 		req->req_body_status = REQ_BODY_WITHOUT_LEN;
-		break;
-	case BS_LENGTH:
+	else if (req->htc->body_status == BS_LENGTH)
 		req->req_body_status = REQ_BODY_WITH_LEN;
-		break;
-	case BS_NONE:
+	else if (req->htc->body_status == BS_NONE)
 		req->req_body_status = REQ_BODY_NONE;
-		break;
-	case BS_EOF:
+	else if (req->htc->body_status == BS_EOF)
 		req->req_body_status = REQ_BODY_WITHOUT_LEN;
-		break;
-	default:
+	else
 		WRONG("Unknown req_body_status situation");
-	}
 	return (0);
 }
 
diff --git a/bin/varnishd/http1/cache_http1_proto.c b/bin/varnishd/http1/cache_http1_proto.c
index 935e70e47..f30c02561 100644
--- a/bin/varnishd/http1/cache_http1_proto.c
+++ b/bin/varnishd/http1/cache_http1_proto.c
@@ -301,7 +301,7 @@ http1_splitline(struct http *hp, struct http_conn *htc, const int *hf,
 
 /*--------------------------------------------------------------------*/
 
-static enum body_status
+static body_status_t
 http1_body_status(const struct http *hp, struct http_conn *htc, int request)
 {
 	ssize_t cl;
diff --git a/bin/varnishd/http1/cache_http1_vfp.c b/bin/varnishd/http1/cache_http1_vfp.c
index 30d879ec2..7e4782a15 100644
--- a/bin/varnishd/http1/cache_http1_vfp.c
+++ b/bin/varnishd/http1/cache_http1_vfp.c
@@ -265,31 +265,26 @@ V1F_Setup_Fetch(struct vfp_ctx *vfc, struct http_conn *htc)
 	CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
 
-	switch (htc->body_status) {
-	case BS_EOF:
+	if (htc->body_status == BS_EOF) {
 		assert(htc->content_length == -1);
 		vfe = VFP_Push(vfc, &v1f_eof);
 		if (vfe == NULL)
 			return (ENOSPC);
 		vfe->priv2 = 0;
-		break;
-	case BS_LENGTH:
+	} else if (htc->body_status == BS_LENGTH) {
 		assert(htc->content_length > 0);
 		vfe = VFP_Push(vfc, &v1f_straight);
 		if (vfe == NULL)
 			return (ENOSPC);
 		vfe->priv2 = htc->content_length;
-		break;
-	case BS_CHUNKED:
+	} else if (htc->body_status == BS_CHUNKED) {
 		assert(htc->content_length == -1);
 		vfe = VFP_Push(vfc, &v1f_chunked);
 		if (vfe == NULL)
 			return (ENOSPC);
 		vfe->priv2 = -1;
-		break;
-	default:
+	} else {
 		WRONG("Wrong body_status");
-		break;
 	}
 	vfe->priv1 = htc;
 	return (0);
diff --git a/include/tbl/body_status.h b/include/tbl/body_status.h
index 4ab48f1a3..878248d72 100644
--- a/include/tbl/body_status.h
+++ b/include/tbl/body_status.h
@@ -32,11 +32,12 @@
 
 /*lint -save -e525 -e539 */
 
-BODYSTATUS(NONE, none)
-BODYSTATUS(ERROR, error)
-BODYSTATUS(CHUNKED, chunked)
-BODYSTATUS(LENGTH, length)
-BODYSTATUS(EOF, eof)
+/*         Upper        lower         nbr,	avail   len_known */
+BODYSTATUS(NONE,	none,		0,	0,	1)
+BODYSTATUS(ERROR,	error,		1,	0,	0)
+BODYSTATUS(CHUNKED,	chunked,	2,	1,	0)
+BODYSTATUS(LENGTH,	length,		3,	1,	1)
+BODYSTATUS(EOF,		eof,		4,	1,	0)
 #undef BODYSTATUS
 
 /*lint -restore */


More information about the varnish-commit mailing list