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.
This commit is contained in:
Willy Tarreau 2018-12-11 06:16:45 +01:00
parent bad0a381d3
commit 30eb809fdb
1 changed files with 19 additions and 0 deletions

View 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 */