From: Christopher Faulet Date: Mon, 1 Feb 2021 15:37:28 +0000 (+0100) Subject: MINOR: h1-htx: Update h1 parsing functions to return result as a size_t X-Git-Tag: v2.5-dev1~243 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=de471a4a8d0a7cb57c5b41b3555d004ff97ebbed;p=thirdparty%2Fhaproxy.git MINOR: h1-htx: Update h1 parsing functions to return result as a size_t h1 parsing functions (h1_parse_msg_*) returns the number of bytes parsed or 0 if nothing is parsed because an error occurred or some data are missing. But they never return negative values. Thus, instead of a signed integer, these function now return a size_t value. The H1 and FCGI muxes are updated accordingly. Note that h1_parse_msg_data() has been slightly adapted because the parsing of chunked messages still need to handle negative values when a parsing error is reported by h1_parse_chunk_size() or h1_skip_chunk_crlf(). --- diff --git a/include/haproxy/h1_htx.h b/include/haproxy/h1_htx.h index a03f66e42c..5afe53dde0 100644 --- a/include/haproxy/h1_htx.h +++ b/include/haproxy/h1_htx.h @@ -29,13 +29,13 @@ #include #include -int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, - struct buffer *srcbuf, size_t ofs, size_t max); -int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, - struct buffer *srcbuf, size_t ofs, size_t max, - struct buffer *htxbuf); -int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx, - struct buffer *srcbuf, size_t ofs, size_t max); +size_t h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, + struct buffer *srcbuf, size_t ofs, size_t max); +size_t h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, + struct buffer *srcbuf, size_t ofs, size_t max, + struct buffer *htxbuf); +size_t h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx, + struct buffer *srcbuf, size_t ofs, size_t max); /* Returns the URI of an HTX message in the most common format for a H1 peer. It * is the path part of an absolute URI when the URI was normalized, ortherwise diff --git a/src/h1_htx.c b/src/h1_htx.c index dfc6b89c9b..15eccd4a12 100644 --- a/src/h1_htx.c +++ b/src/h1_htx.c @@ -308,8 +308,8 @@ static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx * For the requests, must always be provided. For responses, may * be NULL and flags HTTP_METH_CONNECT of HTTP_METH_HEAD may be set. */ -int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, - struct buffer *srcbuf, size_t ofs, size_t max) +size_t h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, + struct buffer *srcbuf, size_t ofs, size_t max) { struct http_hdr hdrs[global.tune.max_http_hdr]; int ret = 0; @@ -381,8 +381,8 @@ int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, /* Copy data from into an DATA block in . If possible, a * zero-copy is performed. It returns the number of bytes copied. */ -static int h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs, - size_t count, struct buffer *htxbuf) +static size_t h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs, + size_t count, struct buffer *htxbuf) { struct htx *tmp_htx = *dsthtx; @@ -425,29 +425,29 @@ static int h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t o * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This * functions is responsible to update the parser state . */ -int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, - struct buffer *srcbuf, size_t ofs, size_t max, - struct buffer *htxbuf) +size_t h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, + struct buffer *srcbuf, size_t ofs, size_t max, + struct buffer *htxbuf) { - size_t total = 0; - int32_t ret = 0; + size_t sz, total = 0; + int ret = 0; if (h1m->flags & H1_MF_CLEN) { /* content-length: read only h2m->body_len */ - ret = htx_get_max_blksz(*dsthtx, max); - if ((uint64_t)ret > h1m->curr_len) - ret = h1m->curr_len; - if (ret > b_contig_data(srcbuf, ofs)) - ret = b_contig_data(srcbuf, ofs); - if (ret) { - int32_t try = ret; - - ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf); - h1m->curr_len -= ret; - max -= sizeof(struct htx_blk) + ret; - ofs += ret; - total += ret; - if (ret < try) + sz = htx_get_max_blksz(*dsthtx, max); + if (sz > h1m->curr_len) + sz = h1m->curr_len; + if (sz > b_contig_data(srcbuf, ofs)) + sz = b_contig_data(srcbuf, ofs); + if (sz) { + size_t try = sz; + + sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf); + h1m->curr_len -= sz; + max -= sizeof(struct htx_blk) + sz; + ofs += sz; + total += sz; + if (sz < try) goto end; } @@ -482,20 +482,20 @@ int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, goto end; } if (h1m->state == H1_MSG_DATA) { - ret = htx_get_max_blksz(*dsthtx, max); - if ((uint64_t)ret > h1m->curr_len) - ret = h1m->curr_len; - if (ret > b_contig_data(srcbuf, ofs)) - ret = b_contig_data(srcbuf, ofs); - if (ret) { - int32_t try = ret; - - ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf); - h1m->curr_len -= ret; - max -= sizeof(struct htx_blk) + ret; - ofs += ret; - total += ret; - if (ret < try) + sz = htx_get_max_blksz(*dsthtx, max); + if (sz > h1m->curr_len) + sz = h1m->curr_len; + if (sz > b_contig_data(srcbuf, ofs)) + sz = b_contig_data(srcbuf, ofs); + if (sz) { + size_t try = sz; + + sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf); + h1m->curr_len -= sz; + max -= sizeof(struct htx_blk) + sz; + ofs += sz; + total += sz; + if (sz < try) goto end; } if (!h1m->curr_len) { @@ -514,11 +514,11 @@ int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, } else { /* no content length, read till SHUTW */ - ret = htx_get_max_blksz(*dsthtx, max); - if (ret > b_contig_data(srcbuf, ofs)) - ret = b_contig_data(srcbuf, ofs); - if (ret) - total += h1_copy_msg_data(dsthtx, srcbuf, ofs, ret, htxbuf); + sz = htx_get_max_blksz(*dsthtx, max); + if (sz > b_contig_data(srcbuf, ofs)) + sz = b_contig_data(srcbuf, ofs); + if (sz) + total += h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, htxbuf); } end: @@ -540,8 +540,8 @@ int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This * functions is responsible to update the parser state . */ -int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx, - struct buffer *srcbuf, size_t ofs, size_t max) +size_t h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx, + struct buffer *srcbuf, size_t ofs, size_t max) { struct http_hdr hdrs[global.tune.max_http_hdr]; struct h1m tlr_h1m; diff --git a/src/mux_fcgi.c b/src/mux_fcgi.c index 14fc8b2e1b..5985c04fd8 100644 --- a/src/mux_fcgi.c +++ b/src/mux_fcgi.c @@ -3307,7 +3307,7 @@ static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_s static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, struct buffer *buf, size_t *ofs, size_t max) { - int ret; + size_t ret; TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){max}); ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max); @@ -3331,7 +3331,7 @@ static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx, struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf) { - int ret; + size_t ret; TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm, 0, (size_t[]){max}); ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); @@ -3353,7 +3353,7 @@ static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, str static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, struct buffer *buf, size_t *ofs, size_t max) { - int ret; + size_t ret; TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){max}); ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max); diff --git a/src/mux_h1.c b/src/mux_h1.c index af0247896a..0b75dbb7a8 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -1376,7 +1376,7 @@ static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *h struct buffer *buf, size_t *ofs, size_t max) { union h1_sl h1sl; - int ret = 0; + size_t ret = 0; TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){max}); @@ -1446,7 +1446,7 @@ static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf) { - int ret; + size_t ret; TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){max}); ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); @@ -1476,7 +1476,7 @@ static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, struct buffer *buf, size_t *ofs, size_t max) { - int ret; + size_t ret; TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){max}); ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);