2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #endif /* !HAVE_CONFIG_H */
32 #include <nghttp2/nghttp2.h>
34 #define MAKE_NV(K, V) \
36 (uint8_t *) K, (uint8_t *)V, sizeof(K) - 1, sizeof(V) - 1, \
37 NGHTTP2_NV_FLAG_NONE \
40 static void deflate(nghttp2_hd_deflater *deflater,
41 nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
44 static int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
45 size_t inlen, int final);
47 int main(int argc _U_, char **argv _U_) {
49 nghttp2_hd_deflater *deflater;
50 nghttp2_hd_inflater *inflater;
51 /* Define 1st header set. This is looks like a HTTP request. */
53 MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
54 MAKE_NV(":path", "/"), MAKE_NV("user-agent", "libnghttp2"),
55 MAKE_NV("accept-encoding", "gzip, deflate")};
56 /* Define 2nd header set */
57 nghttp2_nv nva2[] = {MAKE_NV(":scheme", "https"),
58 MAKE_NV(":authority", "example.org"),
59 MAKE_NV(":path", "/stylesheet/style.css"),
60 MAKE_NV("user-agent", "libnghttp2"),
61 MAKE_NV("accept-encoding", "gzip, deflate"),
62 MAKE_NV("referer", "https://example.org")};
64 rv = nghttp2_hd_deflate_new(&deflater, 4096);
67 fprintf(stderr, "nghttp2_hd_deflate_init failed with error: %s\n",
68 nghttp2_strerror(rv));
72 rv = nghttp2_hd_inflate_new(&inflater);
75 fprintf(stderr, "nghttp2_hd_inflate_init failed with error: %s\n",
76 nghttp2_strerror(rv));
80 /* Encode and decode 1st header set */
81 deflate(deflater, inflater, nva1, sizeof(nva1) / sizeof(nva1[0]));
83 /* Encode and decode 2nd header set, using differential encoding
84 using state after encoding 1st header set. */
85 deflate(deflater, inflater, nva2, sizeof(nva2) / sizeof(nva2[0]));
87 nghttp2_hd_inflate_del(inflater);
88 nghttp2_hd_deflate_del(deflater);
93 static void deflate(nghttp2_hd_deflater *deflater,
94 nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
105 for (i = 0; i < nvlen; ++i) {
106 sum += nva[i].namelen + nva[i].valuelen;
109 printf("Input (%zu byte(s)):\n\n", sum);
111 for (i = 0; i < nvlen; ++i) {
112 fwrite(nva[i].name, nva[i].namelen, 1, stdout);
114 fwrite(nva[i].value, nva[i].valuelen, 1, stdout);
118 buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen);
119 buf = malloc(buflen);
121 rv = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, nvlen);
124 fprintf(stderr, "nghttp2_hd_deflate_hd() failed with error: %s\n",
125 nghttp2_strerror((int)rv));
134 printf("\nDeflate (%zu byte(s), ratio %.02f):\n\n", outlen,
135 sum == 0 ? 0 : (double)outlen / sum);
137 for (i = 0; i < outlen; ++i) {
138 if ((i & 0x0fu) == 0) {
139 printf("%08zX: ", i);
142 printf("%02X ", buf[i]);
144 if (((i + 1) & 0x0fu) == 0) {
149 printf("\n\nInflate:\n\n");
151 /* We pass 1 to final parameter, because buf contains whole deflated
153 rv = inflate_header_block(inflater, buf, outlen, 1);
161 printf("\n-----------------------------------------------------------"
162 "--------------------\n");
167 int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
168 size_t inlen, int final) {
173 int inflate_flags = 0;
176 rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, in, inlen, final);
179 fprintf(stderr, "inflate failed with error code %zd", rv);
188 if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
189 fwrite(nv.name, nv.namelen, 1, stderr);
190 fprintf(stderr, ": ");
191 fwrite(nv.value, nv.valuelen, 1, stderr);
192 fprintf(stderr, "\n");
195 if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
196 nghttp2_hd_inflate_end_headers(inflater);
200 if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {