]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hpack: provide a function to encode a long indexed header
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 06:44:19 +0000 (07:44 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 08:07:01 +0000 (09:07 +0100)
For long header values whose index is known, hpack_encodde_long_idx()
may now be used. This function emits the short index and follows with
the header's value.

include/common/hpack-enc.h

index a0fc0f37c548a4ddf82609977b99906553595c38..4e60104bd477bc522e413f8c3a26709ce4c804e2 100644 (file)
@@ -29,6 +29,7 @@
 #define _COMMON_HPACK_ENC_H
 
 #include <stdint.h>
+#include <string.h>
 #include <common/buf.h>
 #include <common/config.h>
 #include <common/ist.h>
@@ -97,4 +98,29 @@ static inline int hpack_encode_short_idx(struct buffer *out, int idx, struct ist
        return 1;
 }
 
+/* Tries to encode header field index <idx> with long value <val> into the
+ * aligned buffer <out>. Returns non-zero on success, 0 on failure (buffer
+ * full). The caller is responsible for ensuring <idx> is lower than 64 (static
+ * list only), and that the buffer is aligned (head==0).
+ */
+static inline int hpack_encode_long_idx(struct buffer *out, int idx, struct ist val)
+{
+       int len = out->data;
+
+       if (!hpack_len_to_bytes(val.len) ||
+           1 + len + hpack_len_to_bytes(val.len) + val.len > out->size)
+               return 0;
+
+       /* emit literal with indexing (7541#6.2.1) :
+        * [ 0 | 1 | Index (6+) ]
+        */
+       out->area[len++] = idx | 0x40;
+       len = hpack_encode_len(out->area, len, val.len);
+       memcpy(out->area + len, val.ptr, val.len);
+       len += val.len;
+
+       out->data = len;
+       return 1;
+}
+
 #endif /* _COMMON_HPACK_ENC_H */