From 30eb809fdbf7782057e6063c5de9768e674b3516 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 11 Dec 2018 06:16:45 +0100 Subject: [PATCH] MINOR: hpack: provide a function to encode a short indexed header 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 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/common/hpack-enc.h b/include/common/hpack-enc.h index f04b78c244..a0fc0f37c5 100644 --- a/include/common/hpack-enc.h +++ b/include/common/hpack-enc.h @@ -78,4 +78,23 @@ static inline int hpack_encode_len(char *out, int pos, int len) return pos; } +/* Tries to encode header field index with short value into the + * aligned buffer . Returns non-zero on success, 0 on failure (buffer + * full). The caller is responsible for ensuring that the length of is + * strictly lower than 127, and that 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 */