]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hpack: provide a function to encode a short indexed header
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 05:16:45 +0000 (06:16 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 08:06:46 +0000 (09:06 +0100)
Most direct calls to HPACK functions are made to encode short header
fields like methods, schemes or statuses, whose lengths and indexes
are known. Let's have a small function to do this.

include/common/hpack-enc.h

index f04b78c244a3a4a7038a4bf18c61c70175dce73d..a0fc0f37c548a4ddf82609977b99906553595c38 100644 (file)
@@ -78,4 +78,23 @@ static inline int hpack_encode_len(char *out, int pos, int len)
        return pos;
 }
 
+/* Tries to encode header field index <idx> with short value <val> into the
+ * aligned buffer <out>. Returns non-zero on success, 0 on failure (buffer
+ * full). The caller is responsible for ensuring that the length of <val> is
+ * strictly lower than 127, and that <idx> is lower than 64 (static list only),
+ * and that the buffer is aligned (head==0).
+ */
+static inline int hpack_encode_short_idx(struct buffer *out, int idx, struct ist val)
+{
+       if (out->data + 2 + val.len > out->size)
+               return 0;
+
+       /* literal header field with incremental indexing */
+       out->area[out->data++] = idx | 0x40;
+       out->area[out->data++] = val.len;
+       ist2bin(&out->area[out->data], val);
+       out->data += val.len;
+       return 1;
+}
+
 #endif /* _COMMON_HPACK_ENC_H */