From: Willy Tarreau Date: Mon, 10 Dec 2018 18:26:51 +0000 (+0100) Subject: MINOR: hpack: provide a function to encode an HTTP path X-Git-Tag: v1.9-dev11~117 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eaeeb68f235061486c6511ccbed4bf7d1000c62c;p=thirdparty%2Fhaproxy.git MINOR: hpack: provide a function to encode an HTTP path The new function hpack_encode_path() supports encoding a path into the ":path" header. It knows about "/" and "/index.html" which use a single byte, and falls back to literal encoding for other ones, with a fast path for short paths < 127 bytes. --- diff --git a/include/common/hpack-enc.h b/include/common/hpack-enc.h index 831689b968..4ab38bf5f0 100644 --- a/include/common/hpack-enc.h +++ b/include/common/hpack-enc.h @@ -238,4 +238,25 @@ static inline int hpack_encode_scheme(struct buffer *out, struct ist scheme) return 1; } +/* Tries to encode a :path pseudo-header with the path in , into the + * aligned buffer . Returns non-zero on success or 0 on failure (buffer + * full). The well-known values "/" and "/index.html" are recognized, and other + * ones are handled as literals. The caller is responsible for ensuring 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_path(struct buffer *out, struct ist path) +{ + if (out->data < out->size && isteq(path, ist("/"))) + out->area[out->data++] = 0x84; // indexed field : idx[04]=(":path", "/") + else if (out->data < out->size && isteq(path, ist("/index.html"))) + out->area[out->data++] = 0x85; // indexed field : idx[05]=(":path", "/index.html") + else if (path.len < 127) + return hpack_encode_short_idx(out, 4, path); // name=":path" (idx 4) + else + return hpack_encode_long_idx(out, 4, path); // name=":path" (idx 4) + return 1; +} + + #endif /* _COMMON_HPACK_ENC_H */