[3.0] 9703116 Prompted by HTTPbis and FlexeLint in unison, go over the nhttp and response code stuff and fully incorporate the fact that they are both 16 unsigned now.

Tollef Fog Heen tfheen at varnish-cache.org
Wed Aug 17 11:28:46 CEST 2011


commit 970311694e70d2d67d0227238f200f2150b7958a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Aug 15 08:37:38 2011 +0000

    Prompted by HTTPbis and FlexeLint in unison, go over the nhttp and
    response code stuff and fully incorporate the fact that they are both
    16 unsigned now.
    
    No functional changes.

diff --git a/bin/varnishd/cache.h b/bin/varnishd/cache.h
index ef8435c..0b5ad8e 100644
--- a/bin/varnishd/cache.h
+++ b/bin/varnishd/cache.h
@@ -739,21 +739,21 @@ int VGZ_WrwGunzip(const struct sess *, struct vgz *, const void *ibuf,
 /* cache_http.c */
 unsigned HTTP_estimate(unsigned nhttp);
 void HTTP_Copy(struct http *to, const struct http * const fm);
-struct http *HTTP_create(void *p, unsigned nhttp);
+struct http *HTTP_create(void *p, uint16_t nhttp);
 const char *http_StatusMessage(unsigned);
-unsigned http_EstimateWS(const struct http *fm, unsigned how, unsigned *nhd);
+unsigned http_EstimateWS(const struct http *fm, unsigned how, uint16_t *nhd);
 void HTTP_Init(void);
 void http_ClrHeader(struct http *to);
 unsigned http_Write(struct worker *w, const struct http *hp, int resp);
 void http_CopyResp(struct http *to, const struct http *fm);
-void http_SetResp(struct http *to, const char *proto, int status,
+void http_SetResp(struct http *to, const char *proto, uint16_t status,
     const char *response);
 void http_FilterFields(struct worker *w, int fd, struct http *to,
     const struct http *fm, unsigned how);
 void http_FilterHeader(const struct sess *sp, unsigned how);
 void http_PutProtocol(struct worker *w, int fd, const struct http *to,
     const char *protocol);
-void http_PutStatus(struct http *to, int status);
+void http_PutStatus(struct http *to, uint16_t status);
 void http_PutResponse(struct worker *w, int fd, const struct http *to,
     const char *response);
 void http_PrintfHeader(struct worker *w, int fd, struct http *to,
@@ -768,11 +768,11 @@ int http_GetHdrData(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);
-int http_GetStatus(const struct http *hp);
+uint16_t http_GetStatus(const struct http *hp);
 const char *http_GetReq(const struct http *hp);
 int http_HdrIs(const struct http *hp, const char *hdr, const char *val);
-int http_DissectRequest(struct sess *sp);
-int http_DissectResponse(struct worker *w, const struct http_conn *htc,
+uint16_t http_DissectRequest(struct sess *sp);
+uint16_t http_DissectResponse(struct worker *w, const struct http_conn *htc,
     struct http *sp);
 const char *http_DoConnection(const struct http *hp);
 void http_CopyHome(struct worker *w, int fd, const struct http *hp);
diff --git a/bin/varnishd/cache_center.c b/bin/varnishd/cache_center.c
index f5d2957..42d4b3b 100644
--- a/bin/varnishd/cache_center.c
+++ b/bin/varnishd/cache_center.c
@@ -417,10 +417,10 @@ cnt_error(struct sess *sp)
 		/* XXX: 1024 is a pure guess */
 		EXP_Clr(&w->exp);
 		sp->obj = STV_NewObject(sp, NULL, 1024, &w->exp,
-		     params->http_max_hdr);
+		     (uint16_t)params->http_max_hdr);
 		if (sp->obj == NULL) 
 			sp->obj = STV_NewObject(sp, TRANSIENT_STORAGE,
-			    1024, &w->exp, params->http_max_hdr);
+			    1024, &w->exp, (uint16_t)params->http_max_hdr);
 		if (sp->obj == NULL) {
 			sp->doclose = "Out of objects";
 			sp->step = STP_DONE;
@@ -637,7 +637,8 @@ cnt_fetchbody(struct sess *sp)
 	int i;
 	struct http *hp, *hp2;
 	char *b;
-	unsigned l, nhttp;
+	uint16_t nhttp;
+	unsigned l;
 	struct vsb *vary = NULL;
 	int varyl = 0, pass;
 
@@ -1411,7 +1412,7 @@ DOT start -> recv [style=bold,color=green]
 static int
 cnt_start(struct sess *sp)
 {
-	int done;
+	uint16_t done;
 	char *p;
 	const char *r = "HTTP/1.1 100 Continue\r\n\r\n";
 
@@ -1439,7 +1440,7 @@ cnt_start(struct sess *sp)
 	done = http_DissectRequest(sp);
 
 	/* If we could not even parse the request, just close */
-	if (done < 0) {
+	if (done == 400) {
 		sp->step = STP_DONE;
 		vca_close_session(sp, "junk");
 		return (0);
diff --git a/bin/varnishd/cache_http.c b/bin/varnishd/cache_http.c
index 2141cc4..7fe0a8a 100644
--- a/bin/varnishd/cache_http.c
+++ b/bin/varnishd/cache_http.c
@@ -118,7 +118,7 @@ HTTP_estimate(unsigned nhttp)
 }
 
 struct http *
-HTTP_create(void *p, unsigned nhttp)
+HTTP_create(void *p, uint16_t nhttp)
 {
 	struct http *hp;
 
@@ -135,7 +135,7 @@ HTTP_create(void *p, unsigned nhttp)
 void
 http_Setup(struct http *hp, struct ws *ws)
 {
-	unsigned shd;
+	uint16_t shd;
 	txt *hd;
 	unsigned char *hdf;
 
@@ -425,6 +425,7 @@ http_DoConnection(const struct http *hp)
 		return (NULL);
 	}
 	ret = NULL;
+	AN(p);
 	for (; *p; p++) {
 		if (vct_issp(*p))
 			continue;
@@ -463,7 +464,7 @@ http_HdrIs(const struct http *hp, const char *hdr, const char *val)
 
 /*--------------------------------------------------------------------*/
 
-int
+uint16_t
 http_GetStatus(const struct http *hp)
 {
 
@@ -483,7 +484,7 @@ http_GetReq(const struct http *hp)
  * Detect conditionals (headers which start with '^[Ii][Ff]-')
  */
 
-static int
+static uint16_t
 http_dissect_hdrs(struct worker *w, struct http *hp, int fd, char *p,
     const struct http_conn *htc)
 {
@@ -557,7 +558,7 @@ http_dissect_hdrs(struct worker *w, struct http *hp, int fd, char *p,
  * Deal with first line of HTTP protocol message.
  */
 
-static int
+static uint16_t
 http_splitline(struct worker *w, int fd, struct http *hp,
     const struct http_conn *htc, int h1, int h2, int h3)
 {
@@ -577,7 +578,7 @@ http_splitline(struct worker *w, int fd, struct http *hp,
 	q = p;
 	for (; !vct_issp(*p); p++) {
 		if (vct_isctl(*p))
-			return (-1);
+			return (400);
 	}
 	hp->hd[h1].b = q;
 	hp->hd[h1].e = p;
@@ -585,14 +586,14 @@ http_splitline(struct worker *w, int fd, struct http *hp,
 	/* Skip SP */
 	for (; vct_issp(*p); p++) {
 		if (vct_isctl(*p))
-			return (-1);
+			return (400);
 	}
 
 	/* Second field cannot contain LWS or CTL */
 	q = p;
 	for (; !vct_islws(*p); p++) {
 		if (vct_isctl(*p))
-			return (-1);
+			return (400);
 	}
 	hp->hd[h2].b = q;
 	hp->hd[h2].e = p;
@@ -603,7 +604,7 @@ http_splitline(struct worker *w, int fd, struct http *hp,
 	/* Skip SP */
 	for (; vct_issp(*p); p++) {
 		if (vct_isctl(*p))
-			return (-1);
+			return (400);
 	}
 
 	/* Third field is optional and cannot contain CTL */
@@ -611,7 +612,7 @@ http_splitline(struct worker *w, int fd, struct http *hp,
 	if (!vct_iscrlf(*p)) {
 		for (; !vct_iscrlf(*p); p++)
 			if (!vct_issep(*p) && vct_isctl(*p))
-				return (-1);
+				return (400);
 	}
 	hp->hd[h3].b = q;
 	hp->hd[h3].e = p;
@@ -650,12 +651,12 @@ http_ProtoVer(struct http *hp)
 
 /*--------------------------------------------------------------------*/
 
-int
+uint16_t
 http_DissectRequest(struct sess *sp)
 {
 	struct http_conn *htc;
 	struct http *hp;
-	int i;
+	uint16_t retval;
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	htc = sp->htc;
@@ -665,23 +666,26 @@ http_DissectRequest(struct sess *sp)
 
 	hp->logtag = HTTP_Rx;
 
-	i = http_splitline(sp->wrk, sp->fd, hp, htc,
+	retval = http_splitline(sp->wrk, sp->fd, hp, htc,
 	    HTTP_HDR_REQ, HTTP_HDR_URL, HTTP_HDR_PROTO);
-	if (i != 0) {
+	if (retval != 0) {
 		WSPR(sp, SLT_HttpGarbage, htc->rxbuf);
-		return (i);
+		return (retval);
 	}
 	http_ProtoVer(hp);
-	return (i);
+	return (retval);
 }
 
 /*--------------------------------------------------------------------*/
 
-int
+uint16_t
 http_DissectResponse(struct worker *w, const struct http_conn *htc,
     struct http *hp)
 {
-	int i = 0;
+	int j;
+	uint16_t retval = 0;
+	char *p;
+
 
 	CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
 	CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
@@ -689,23 +693,33 @@ http_DissectResponse(struct worker *w, const struct http_conn *htc,
 
 	if (http_splitline(w, htc->fd, hp, htc,
 	    HTTP_HDR_PROTO, HTTP_HDR_STATUS, HTTP_HDR_RESPONSE))
-		i = 503;
+		retval = 503;
 
-	if (i == 0 && memcmp(hp->hd[HTTP_HDR_PROTO].b, "HTTP/1.", 7))
-		i = 503;
+	if (retval == 0 && memcmp(hp->hd[HTTP_HDR_PROTO].b, "HTTP/1.", 7))
+		retval = 503;
 
-	if (i == 0 && Tlen(hp->hd[HTTP_HDR_STATUS]) != 3)
-		i = 503;
+	if (retval == 0 && Tlen(hp->hd[HTTP_HDR_STATUS]) != 3)
+		retval = 503;
 
-	if (i == 0) {
-		hp->status = strtoul(hp->hd[HTTP_HDR_STATUS].b, NULL, 10);
-		if (hp->status < 100 || hp->status > 999)
-			i = 503;
+	if (retval == 0) {
+		hp->status = 0;
+		p = hp->hd[HTTP_HDR_STATUS].b;
+		for (j = 100; j != 0; j /= 10) {
+			if (!vct_isdigit(*p)) {
+				retval = 503;
+				break;
+			}
+			hp->status += (uint16_t)(j * (*p - '0'));
+			p++;
+		}
+		if (*p != '\0')
+			retval = 503;
 	}
 
-	if (i != 0) {
+	if (retval != 0) {
 		WSLR(w, SLT_HttpGarbage, htc->fd, htc->rxbuf);
-		hp->status = i;
+		assert(retval >= 100 && retval <= 999);
+		hp->status = retval;
 	} else {
 		http_ProtoVer(hp);
 	}
@@ -718,7 +732,7 @@ http_DissectResponse(struct worker *w, const struct http_conn *htc,
 		hp->hd[HTTP_HDR_RESPONSE].e =
 		    strchr(hp->hd[HTTP_HDR_RESPONSE].b, '\0');
 	}
-	return (i);
+	return (retval);
 }
 
 /*--------------------------------------------------------------------*/
@@ -763,12 +777,13 @@ http_CopyResp(struct http *to, const struct http *fm)
 }
 
 void
-http_SetResp(struct http *to, const char *proto, int status,
+http_SetResp(struct http *to, const char *proto, uint16_t status,
     const char *response)
 {
 
 	CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
 	http_SetH(to, HTTP_HDR_PROTO, proto);
+	assert(status >= 100 && status <= 999);
 	to->status = status;
 	http_SetH(to, HTTP_HDR_RESPONSE, response);
 }
@@ -798,7 +813,7 @@ http_copyheader(struct worker *w, int fd, struct http *to,
  */
 
 unsigned
-http_EstimateWS(const struct http *fm, unsigned how, unsigned *nhd)
+http_EstimateWS(const struct http *fm, unsigned how, uint16_t *nhd)
 {
 	unsigned u, l;
 
@@ -967,10 +982,10 @@ http_PutProtocol(struct worker *w, int fd, const struct http *to,
 }
 
 void
-http_PutStatus(struct http *to, int status)
+http_PutStatus(struct http *to, uint16_t status)
 {
 
-	assert(status >= 0 && status <= 999);
+	assert(status >= 100 && status <= 999);
 	to->status = status;
 }
 
@@ -1011,7 +1026,7 @@ http_PrintfHeader(struct worker *w, int fd, struct http *to,
 void
 http_Unset(struct http *hp, const char *hdr)
 {
-	unsigned u, v;
+	uint16_t u, v;
 
 	for (v = u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
 		if (hp->hd[u].b == NULL)
diff --git a/bin/varnishd/cache_pool.c b/bin/varnishd/cache_pool.c
index 603ba83..0875e07 100644
--- a/bin/varnishd/cache_pool.c
+++ b/bin/varnishd/cache_pool.c
@@ -116,7 +116,7 @@ WRK_SumStat(struct worker *w)
 
 static void *
 wrk_thread_real(struct wq *qp, unsigned shm_workspace, unsigned sess_workspace,
-    unsigned nhttp, unsigned http_space, unsigned siov)
+    uint16_t nhttp, unsigned http_space, unsigned siov)
 {
 	struct worker *w, ww;
 	uint32_t wlog[shm_workspace / 4];
@@ -218,12 +218,13 @@ static void *
 wrk_thread(void *priv)
 {
 	struct wq *qp;
-	unsigned nhttp;
+	uint16_t nhttp;
 	unsigned siov;
 
 	CAST_OBJ_NOTNULL(qp, priv, WQ_MAGIC);
+	assert(params->http_max_hdr <= 65535);
 	/* We need to snapshot these two for consistency */
-	nhttp = params->http_max_hdr;
+	nhttp = (uint16_t)params->http_max_hdr;
 	siov = nhttp * 2;
 	if (siov > IOV_MAX)
 		siov = IOV_MAX;
diff --git a/bin/varnishd/cache_session.c b/bin/varnishd/cache_session.c
index 87e05c7..af422a6 100644
--- a/bin/varnishd/cache_session.c
+++ b/bin/varnishd/cache_session.c
@@ -101,8 +101,8 @@ ses_sm_alloc(void)
 {
 	struct sessmem *sm;
 	unsigned char *p, *q;
-	volatile unsigned nws;
-	volatile unsigned nhttp;
+	unsigned nws;
+	uint16_t nhttp;
 	unsigned l, hl;
 
 	if (VSC_C_main->n_sess_mem >= params->max_sess)
@@ -113,7 +113,7 @@ ses_sm_alloc(void)
 	 * view of the value.
 	 */
 	nws = params->sess_workspace;
-	nhttp = params->http_max_hdr;
+	nhttp = (uint16_t)params->http_max_hdr;
 	hl = HTTP_estimate(nhttp);
 	l = sizeof *sm + nws + 2 * hl;
 	p = malloc(l);
diff --git a/bin/varnishd/cache_vrt.c b/bin/varnishd/cache_vrt.c
index 65b7522..c17c361 100644
--- a/bin/varnishd/cache_vrt.c
+++ b/bin/varnishd/cache_vrt.c
@@ -61,7 +61,9 @@ VRT_error(struct sess *sp, unsigned code, const char *reason)
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	WSL(sp->wrk, SLT_Debug, 0, "VCL_error(%u, %s)", code, reason ?
 	    reason : "(null)");
-	sp->err_code = code ? code : 503;
+	if (code < 100 || code > 999)
+		code = 503;
+	sp->err_code = (uint16_t)code;
 	sp->err_reason = reason ? reason : http_StatusMessage(sp->err_code);
 }
 
diff --git a/bin/varnishd/cache_vrt_var.c b/bin/varnishd/cache_vrt_var.c
index 1ee011b..2054953 100644
--- a/bin/varnishd/cache_vrt_var.c
+++ b/bin/varnishd/cache_vrt_var.c
@@ -102,7 +102,7 @@ VRT_l_##obj##_status(const struct sess *sp, int num)		\
 {								\
 								\
 	assert(num >= 100 && num <= 999);			\
-	http->status = num;					\
+	http->status = (uint16_t)num;				\
 }								\
 								\
 int								\
diff --git a/bin/varnishd/mgt_param.c b/bin/varnishd/mgt_param.c
index 3126744..7bfa707 100644
--- a/bin/varnishd/mgt_param.c
+++ b/bin/varnishd/mgt_param.c
@@ -549,7 +549,7 @@ static const struct parspec input_parspec[] = {
 		"how much of that the request is allowed to take up.",
 		0,
 		"32768", "bytes" },
-	{ "http_max_hdr", tweak_uint, &master.http_max_hdr, 32, UINT_MAX,
+	{ "http_max_hdr", tweak_uint, &master.http_max_hdr, 32, 65535,
 		"Maximum number of HTTP headers we will deal with in "
 		"client request or backend reponses.  "
 		"Note that the first line occupies five header fields.\n"
diff --git a/bin/varnishd/stevedore.c b/bin/varnishd/stevedore.c
index 533874e..1dad892 100644
--- a/bin/varnishd/stevedore.c
+++ b/bin/varnishd/stevedore.c
@@ -199,7 +199,7 @@ stv_alloc(const struct sess *sp, size_t size)
 struct stv_objsecrets {
 	unsigned	magic;
 #define STV_OBJ_SECRETES_MAGIC	0x78c87247
-	unsigned	nhttp;
+	uint16_t	nhttp;
 	unsigned	lhttp;
 	unsigned	wsl;
 	struct exp	*exp;
@@ -294,7 +294,7 @@ stv_default_allocobj(struct stevedore *stv, struct sess *sp, unsigned ltot,
 
 struct object *
 STV_NewObject(struct sess *sp, const char *hint, unsigned wsl, struct exp *ep,
-    unsigned nhttp)
+    uint16_t nhttp)
 {
 	struct object *o;
 	struct stevedore *stv;
diff --git a/bin/varnishd/stevedore.h b/bin/varnishd/stevedore.h
index 5a5cc4b..0ccf834 100644
--- a/bin/varnishd/stevedore.h
+++ b/bin/varnishd/stevedore.h
@@ -91,7 +91,7 @@ struct object *STV_MkObject(struct sess *sp, void *ptr, unsigned ltot,
     const struct stv_objsecrets *soc);
 
 struct object *STV_NewObject(struct sess *sp, const char *hint, unsigned len,
-    struct exp *, unsigned nhttp);
+    struct exp *, uint16_t nhttp);
 struct storage *STV_alloc(const struct sess *sp, size_t size);
 void STV_trim(struct storage *st, size_t size);
 void STV_free(struct storage *st);



More information about the varnish-commit mailing list