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().
This commit is contained in:
Christopher Faulet 2021-02-01 16:37:28 +01:00
parent 3e6690a555
commit de471a4a8d
4 changed files with 55 additions and 55 deletions

View File

@ -29,13 +29,13 @@
#include <haproxy/h1.h>
#include <haproxy/htx.h>
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

View File

@ -308,8 +308,8 @@ static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx
* For the requests, <h1sl> must always be provided. For responses, <h1sl> may
* be NULL and <h1m> 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 <srbuf> into an DATA block in <dsthtx>. 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 <h1m>.
*/
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;
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;
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 = 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;
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;
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 = 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 <h1m>.
*/
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;

View File

@ -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);

View File

@ -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);