]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: mux-h1: Be able to define the length of a chunk size when it is prepended
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 25 Jan 2024 13:47:19 +0000 (14:47 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 7 Feb 2024 14:04:34 +0000 (15:04 +0100)
It is now possible to impose the length to represent the chunk size in the
function used to prepended the chunk size in a buffer (so before the chunk
itself). It is thus possible to reserve a specific space for an unknown
chunk size and padding it with leading '0' to use all the space and avoid
holes.

src/mux_h1.c

index 20f2019dadc1ff539272c6dc367c4d0538d63d95..0db232934f5018fa0df734f5ced2e6a332698c2e 100644 (file)
@@ -1445,21 +1445,33 @@ static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
                            &ctx, h1_show_error_snapshot);
 }
 
-/* Emit the chunksize followed by a CRLF in front of data of the buffer
+/* Emit the chunk size <chksz> followed by a CRLF in front of data of the buffer
  * <buf>. It goes backwards and starts with the byte before the buffer's
  * head. The caller is responsible for ensuring there is enough room left before
- * the buffer's head for the string.
+ * the buffer's head for the string. if <length> is greater than 0, it
+ * represents the expected total length of the chunk size, including the
+ * CRLF. So it will be padded with 0 to resepct this length. It is the caller
+ * responsibility to pass the right value. if <length> is set to 0 (or less that
+ * the smallest size to represent the chunk size), it is ignored.
  */
-static void h1_prepend_chunk_size(struct buffer *buf, size_t chksz)
+static void h1_prepend_chunk_size(struct buffer *buf, size_t chksz, size_t length)
 {
        char *beg, *end;
 
        beg = end = b_head(buf);
        *--beg = '\n';
        *--beg = '\r';
+       if (length)
+               length -= 2;
        do {
                *--beg = hextab[chksz & 0xF];
+               if (length)
+                       --length;
        } while (chksz >>= 4);
+       while (length) {
+               *--beg = '0';
+               --length;
+       }
        buf->head -= (end - beg);
        b_add(buf, end - beg);
 }
@@ -2640,7 +2652,7 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf,
                                /* Because chunk meta-data are prepended, the chunk size of the current chunk
                                 * must be handled before the end of the previous chunk.
                                 */
-                               h1_prepend_chunk_size(&h1c->obuf, h1m->curr_len);
+                               h1_prepend_chunk_size(&h1c->obuf, h1m->curr_len, 0);
                                if (h1m->state == H1_MSG_CHUNK_CRLF)
                                        h1_prepend_chunk_crlf(&h1c->obuf);