1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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.
24 */
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif /* !HAVE_CONFIG_H */
28
29 #include <stdio.h>
30 #include <string.h>
31
32 #define NGHTTP2_NO_SSIZE_T
33 #include <nghttp2/nghttp2.h>
34
35 #define MAKE_NV(K, V) \
36 { \
37 (uint8_t *)K, (uint8_t *)V, sizeof(K) - 1, \
38 sizeof(V) - 1, NGHTTP2_NV_FLAG_NONE, \
39 }
40
41 static void deflate(nghttp2_hd_deflater *deflater,
42 nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
43 size_t nvlen);
44
45 static int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
46 size_t inlen, int final);
47
main(void)48 int main(void) {
49 int rv;
50 nghttp2_hd_deflater *deflater;
51 nghttp2_hd_inflater *inflater;
52 /* Define 1st header set. This is looks like a HTTP request. */
53 nghttp2_nv nva1[] = {
54 MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
55 MAKE_NV(":path", "/"), MAKE_NV("user-agent", "libnghttp2"),
56 MAKE_NV("accept-encoding", "gzip, deflate")};
57 /* Define 2nd header set */
58 nghttp2_nv nva2[] = {MAKE_NV(":scheme", "https"),
59 MAKE_NV(":authority", "example.org"),
60 MAKE_NV(":path", "/stylesheet/style.css"),
61 MAKE_NV("user-agent", "libnghttp2"),
62 MAKE_NV("accept-encoding", "gzip, deflate"),
63 MAKE_NV("referer", "https://example.org")};
64
65 rv = nghttp2_hd_deflate_new(&deflater, 4096);
66
67 if (rv != 0) {
68 fprintf(stderr, "nghttp2_hd_deflate_init failed with error: %s\n",
69 nghttp2_strerror(rv));
70 exit(EXIT_FAILURE);
71 }
72
73 rv = nghttp2_hd_inflate_new(&inflater);
74
75 if (rv != 0) {
76 fprintf(stderr, "nghttp2_hd_inflate_init failed with error: %s\n",
77 nghttp2_strerror(rv));
78 exit(EXIT_FAILURE);
79 }
80
81 /* Encode and decode 1st header set */
82 deflate(deflater, inflater, nva1, sizeof(nva1) / sizeof(nva1[0]));
83
84 /* Encode and decode 2nd header set, using differential encoding
85 using state after encoding 1st header set. */
86 deflate(deflater, inflater, nva2, sizeof(nva2) / sizeof(nva2[0]));
87
88 nghttp2_hd_inflate_del(inflater);
89 nghttp2_hd_deflate_del(deflater);
90
91 return 0;
92 }
93
deflate(nghttp2_hd_deflater * deflater,nghttp2_hd_inflater * inflater,const nghttp2_nv * const nva,size_t nvlen)94 static void deflate(nghttp2_hd_deflater *deflater,
95 nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
96 size_t nvlen) {
97 nghttp2_ssize rv;
98 uint8_t *buf;
99 size_t buflen;
100 size_t outlen;
101 size_t i;
102 size_t sum;
103
104 sum = 0;
105
106 for (i = 0; i < nvlen; ++i) {
107 sum += nva[i].namelen + nva[i].valuelen;
108 }
109
110 printf("Input (%zu byte(s)):\n\n", sum);
111
112 for (i = 0; i < nvlen; ++i) {
113 fwrite(nva[i].name, 1, nva[i].namelen, stdout);
114 printf(": ");
115 fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
116 printf("\n");
117 }
118
119 buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen);
120 buf = malloc(buflen);
121
122 rv = nghttp2_hd_deflate_hd2(deflater, buf, buflen, nva, nvlen);
123
124 if (rv < 0) {
125 fprintf(stderr, "nghttp2_hd_deflate_hd2() failed with error: %s\n",
126 nghttp2_strerror((int)rv));
127
128 free(buf);
129
130 exit(EXIT_FAILURE);
131 }
132
133 outlen = (size_t)rv;
134
135 printf("\nDeflate (%zu byte(s), ratio %.02f):\n\n", outlen,
136 sum == 0 ? 0 : (double)outlen / (double)sum);
137
138 for (i = 0; i < outlen; ++i) {
139 if ((i & 0x0fu) == 0) {
140 printf("%08zX: ", i);
141 }
142
143 printf("%02X ", buf[i]);
144
145 if (((i + 1) & 0x0fu) == 0) {
146 printf("\n");
147 }
148 }
149
150 printf("\n\nInflate:\n\n");
151
152 /* We pass 1 to final parameter, because buf contains whole deflated
153 header data. */
154 rv = inflate_header_block(inflater, buf, outlen, 1);
155
156 if (rv != 0) {
157 free(buf);
158
159 exit(EXIT_FAILURE);
160 }
161
162 printf("\n-----------------------------------------------------------"
163 "--------------------\n");
164
165 free(buf);
166 }
167
inflate_header_block(nghttp2_hd_inflater * inflater,uint8_t * in,size_t inlen,int final)168 int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
169 size_t inlen, int final) {
170 nghttp2_ssize rv;
171
172 for (;;) {
173 nghttp2_nv nv;
174 int inflate_flags = 0;
175 size_t proclen;
176
177 rv =
178 nghttp2_hd_inflate_hd3(inflater, &nv, &inflate_flags, in, inlen, final);
179
180 if (rv < 0) {
181 fprintf(stderr, "inflate failed with error code %td", rv);
182 return -1;
183 }
184
185 proclen = (size_t)rv;
186
187 in += proclen;
188 inlen -= proclen;
189
190 if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
191 fwrite(nv.name, 1, nv.namelen, stderr);
192 fprintf(stderr, ": ");
193 fwrite(nv.value, 1, nv.valuelen, stderr);
194 fprintf(stderr, "\n");
195 }
196
197 if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
198 nghttp2_hd_inflate_end_headers(inflater);
199 break;
200 }
201
202 if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
203 break;
204 }
205 }
206
207 return 0;
208 }
209