From 820b3912609aa24a4520caa53a750782627e1ada Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 10 Dec 2018 19:16:20 +0100 Subject: [PATCH] MINOR: hpack: provide a function to encode an HTTP scheme The new function hpack_encode_scheme() supports encoding a scheme into the ":scheme" header. It knows about "https" and "http" which use a single byte, and falls back to literal encoding for other ones. --- include/common/hpack-enc.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/common/hpack-enc.h b/include/common/hpack-enc.h index a650cdbb22..831689b968 100644 --- a/include/common/hpack-enc.h +++ b/include/common/hpack-enc.h @@ -219,5 +219,23 @@ static inline int hpack_encode_method(struct buffer *out, enum http_meth_t meth, return 1; } +/* Tries to encode a :scheme pseudo-header with the scheme in , into + * the aligned buffer . Returns non-zero on success or 0 on failure + * (buffer full). Only "http" and "https" are recognized and handled as indexed + * values, others are turned into short literals. The caller is responsible for + * ensuring that the scheme is smaller than 127 bytes, and that the buffer is + * aligned. Normally the compiler will detect constant strings in the comparison + * if the code remains inlined. + */ +static inline int hpack_encode_scheme(struct buffer *out, struct ist scheme) +{ + if (out->data < out->size && isteq(scheme, ist("https"))) + out->area[out->data++] = 0x87; // indexed field : idx[07]=(":scheme", "https") + else if (out->data < out->size && isteq(scheme, ist("http"))) + out->area[out->data++] = 0x86; // indexed field : idx[06]=(":scheme", "http") + else + return hpack_encode_short_idx(out, 6, scheme); // name=":scheme" (idx 6) + return 1; +} #endif /* _COMMON_HPACK_ENC_H */