tizen 2.4 release
[external/nghttp2.git] / examples / deflate.c
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 #include <nghttp2/nghttp2.h>
33
34 #define MAKE_NV(K, V)                                                          \
35   {                                                                            \
36     (uint8_t *) K, (uint8_t *)V, sizeof(K) - 1, sizeof(V) - 1,                 \
37         NGHTTP2_NV_FLAG_NONE                                                   \
38   }
39
40 static void deflate(nghttp2_hd_deflater *deflater,
41                     nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
42                     size_t nvlen);
43
44 static int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
45                                 size_t inlen, int final);
46
47 int main(int argc _U_, char **argv _U_) {
48   int rv;
49   nghttp2_hd_deflater *deflater;
50   nghttp2_hd_inflater *inflater;
51   /* Define 1st header set.  This is looks like a HTTP request. */
52   nghttp2_nv nva1[] = {
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")};
63
64   rv = nghttp2_hd_deflate_new(&deflater, 4096);
65
66   if (rv != 0) {
67     fprintf(stderr, "nghttp2_hd_deflate_init failed with error: %s\n",
68             nghttp2_strerror(rv));
69     exit(EXIT_FAILURE);
70   }
71
72   rv = nghttp2_hd_inflate_new(&inflater);
73
74   if (rv != 0) {
75     fprintf(stderr, "nghttp2_hd_inflate_init failed with error: %s\n",
76             nghttp2_strerror(rv));
77     exit(EXIT_FAILURE);
78   }
79
80   /* Encode and decode 1st header set */
81   deflate(deflater, inflater, nva1, sizeof(nva1) / sizeof(nva1[0]));
82
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]));
86
87   nghttp2_hd_inflate_del(inflater);
88   nghttp2_hd_deflate_del(deflater);
89
90   return 0;
91 }
92
93 static void deflate(nghttp2_hd_deflater *deflater,
94                     nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
95                     size_t nvlen) {
96   ssize_t rv;
97   uint8_t *buf;
98   size_t buflen;
99   size_t outlen;
100   size_t i;
101   size_t sum;
102
103   sum = 0;
104
105   for (i = 0; i < nvlen; ++i) {
106     sum += nva[i].namelen + nva[i].valuelen;
107   }
108
109   printf("Input (%zu byte(s)):\n\n", sum);
110
111   for (i = 0; i < nvlen; ++i) {
112     fwrite(nva[i].name, nva[i].namelen, 1, stdout);
113     printf(": ");
114     fwrite(nva[i].value, nva[i].valuelen, 1, stdout);
115     printf("\n");
116   }
117
118   buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen);
119   buf = malloc(buflen);
120
121   rv = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, nvlen);
122
123   if (rv < 0) {
124     fprintf(stderr, "nghttp2_hd_deflate_hd() failed with error: %s\n",
125             nghttp2_strerror((int)rv));
126
127     free(buf);
128
129     exit(EXIT_FAILURE);
130   }
131
132   outlen = rv;
133
134   printf("\nDeflate (%zu byte(s), ratio %.02f):\n\n", outlen,
135          sum == 0 ? 0 : (double)outlen / sum);
136
137   for (i = 0; i < outlen; ++i) {
138     if ((i & 0x0fu) == 0) {
139       printf("%08zX: ", i);
140     }
141
142     printf("%02X ", buf[i]);
143
144     if (((i + 1) & 0x0fu) == 0) {
145       printf("\n");
146     }
147   }
148
149   printf("\n\nInflate:\n\n");
150
151   /* We pass 1 to final parameter, because buf contains whole deflated
152      header data. */
153   rv = inflate_header_block(inflater, buf, outlen, 1);
154
155   if (rv != 0) {
156     free(buf);
157
158     exit(EXIT_FAILURE);
159   }
160
161   printf("\n-----------------------------------------------------------"
162          "--------------------\n");
163
164   free(buf);
165 }
166
167 int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
168                          size_t inlen, int final) {
169   ssize_t rv;
170
171   for (;;) {
172     nghttp2_nv nv;
173     int inflate_flags = 0;
174     size_t proclen;
175
176     rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, in, inlen, final);
177
178     if (rv < 0) {
179       fprintf(stderr, "inflate failed with error code %zd", rv);
180       return -1;
181     }
182
183     proclen = rv;
184
185     in += proclen;
186     inlen -= proclen;
187
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");
193     }
194
195     if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
196       nghttp2_hd_inflate_end_headers(inflater);
197       break;
198     }
199
200     if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
201       break;
202     }
203   }
204
205   return 0;
206 }