]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: h1-htx: Update h1 parsing functions to return result as a size_t
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 1 Feb 2021 15:37:28 +0000 (16:37 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 25 May 2021 08:41:50 +0000 (10:41 +0200)
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().

include/haproxy/h1_htx.h
src/h1_htx.c
src/mux_fcgi.c
src/mux_h1.c

index a03f66e42c2921851c84c3800909eb8a7e0841c3..5afe53dde0eb9eb088d12c0f7a69910516d15899 100644 (file)
 #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
index dfc6b89c9bacd4c6760e301ff7ab29785997e557..15eccd4a12c51f9f79bfbd6a17595f9d6de50b9a 100644 (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;
-
-                       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 <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;
index 14fc8b2e1b095ea06693cef0e28837baf77b073c..5985c04fd8a3a73241911863420ea9aaacb9e3a2 100644 (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);
index af0247896a52f6f97c230fc44f771d8cdd89a2da..0b75dbb7a86514d6e0cee8370190409950add3c3 100644 (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);