12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2014 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifdef HAVE_CONFIG_H
262c593315Sopenharmony_ci#  include <config.h>
272c593315Sopenharmony_ci#endif /* !HAVE_CONFIG_H */
282c593315Sopenharmony_ci
292c593315Sopenharmony_ci#include <stdio.h>
302c593315Sopenharmony_ci#include <string.h>
312c593315Sopenharmony_ci
322c593315Sopenharmony_ci#include <nghttp2/nghttp2.h>
332c593315Sopenharmony_ci
342c593315Sopenharmony_ci#define MAKE_NV(K, V)                                                          \
352c593315Sopenharmony_ci  {                                                                            \
362c593315Sopenharmony_ci    (uint8_t *)K, (uint8_t *)V, sizeof(K) - 1, sizeof(V) - 1,                  \
372c593315Sopenharmony_ci        NGHTTP2_NV_FLAG_NONE                                                   \
382c593315Sopenharmony_ci  }
392c593315Sopenharmony_ci
402c593315Sopenharmony_cistatic void deflate(nghttp2_hd_deflater *deflater,
412c593315Sopenharmony_ci                    nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
422c593315Sopenharmony_ci                    size_t nvlen);
432c593315Sopenharmony_ci
442c593315Sopenharmony_cistatic int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
452c593315Sopenharmony_ci                                size_t inlen, int final);
462c593315Sopenharmony_ci
472c593315Sopenharmony_ciint main(void) {
482c593315Sopenharmony_ci  int rv;
492c593315Sopenharmony_ci  nghttp2_hd_deflater *deflater;
502c593315Sopenharmony_ci  nghttp2_hd_inflater *inflater;
512c593315Sopenharmony_ci  /* Define 1st header set.  This is looks like a HTTP request. */
522c593315Sopenharmony_ci  nghttp2_nv nva1[] = {
532c593315Sopenharmony_ci      MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
542c593315Sopenharmony_ci      MAKE_NV(":path", "/"), MAKE_NV("user-agent", "libnghttp2"),
552c593315Sopenharmony_ci      MAKE_NV("accept-encoding", "gzip, deflate")};
562c593315Sopenharmony_ci  /* Define 2nd header set */
572c593315Sopenharmony_ci  nghttp2_nv nva2[] = {MAKE_NV(":scheme", "https"),
582c593315Sopenharmony_ci                       MAKE_NV(":authority", "example.org"),
592c593315Sopenharmony_ci                       MAKE_NV(":path", "/stylesheet/style.css"),
602c593315Sopenharmony_ci                       MAKE_NV("user-agent", "libnghttp2"),
612c593315Sopenharmony_ci                       MAKE_NV("accept-encoding", "gzip, deflate"),
622c593315Sopenharmony_ci                       MAKE_NV("referer", "https://example.org")};
632c593315Sopenharmony_ci
642c593315Sopenharmony_ci  rv = nghttp2_hd_deflate_new(&deflater, 4096);
652c593315Sopenharmony_ci
662c593315Sopenharmony_ci  if (rv != 0) {
672c593315Sopenharmony_ci    fprintf(stderr, "nghttp2_hd_deflate_init failed with error: %s\n",
682c593315Sopenharmony_ci            nghttp2_strerror(rv));
692c593315Sopenharmony_ci    exit(EXIT_FAILURE);
702c593315Sopenharmony_ci  }
712c593315Sopenharmony_ci
722c593315Sopenharmony_ci  rv = nghttp2_hd_inflate_new(&inflater);
732c593315Sopenharmony_ci
742c593315Sopenharmony_ci  if (rv != 0) {
752c593315Sopenharmony_ci    fprintf(stderr, "nghttp2_hd_inflate_init failed with error: %s\n",
762c593315Sopenharmony_ci            nghttp2_strerror(rv));
772c593315Sopenharmony_ci    exit(EXIT_FAILURE);
782c593315Sopenharmony_ci  }
792c593315Sopenharmony_ci
802c593315Sopenharmony_ci  /* Encode and decode 1st header set */
812c593315Sopenharmony_ci  deflate(deflater, inflater, nva1, sizeof(nva1) / sizeof(nva1[0]));
822c593315Sopenharmony_ci
832c593315Sopenharmony_ci  /* Encode and decode 2nd header set, using differential encoding
842c593315Sopenharmony_ci     using state after encoding 1st header set. */
852c593315Sopenharmony_ci  deflate(deflater, inflater, nva2, sizeof(nva2) / sizeof(nva2[0]));
862c593315Sopenharmony_ci
872c593315Sopenharmony_ci  nghttp2_hd_inflate_del(inflater);
882c593315Sopenharmony_ci  nghttp2_hd_deflate_del(deflater);
892c593315Sopenharmony_ci
902c593315Sopenharmony_ci  return 0;
912c593315Sopenharmony_ci}
922c593315Sopenharmony_ci
932c593315Sopenharmony_cistatic void deflate(nghttp2_hd_deflater *deflater,
942c593315Sopenharmony_ci                    nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
952c593315Sopenharmony_ci                    size_t nvlen) {
962c593315Sopenharmony_ci  ssize_t rv;
972c593315Sopenharmony_ci  uint8_t *buf;
982c593315Sopenharmony_ci  size_t buflen;
992c593315Sopenharmony_ci  size_t outlen;
1002c593315Sopenharmony_ci  size_t i;
1012c593315Sopenharmony_ci  size_t sum;
1022c593315Sopenharmony_ci
1032c593315Sopenharmony_ci  sum = 0;
1042c593315Sopenharmony_ci
1052c593315Sopenharmony_ci  for (i = 0; i < nvlen; ++i) {
1062c593315Sopenharmony_ci    sum += nva[i].namelen + nva[i].valuelen;
1072c593315Sopenharmony_ci  }
1082c593315Sopenharmony_ci
1092c593315Sopenharmony_ci  printf("Input (%zu byte(s)):\n\n", sum);
1102c593315Sopenharmony_ci
1112c593315Sopenharmony_ci  for (i = 0; i < nvlen; ++i) {
1122c593315Sopenharmony_ci    fwrite(nva[i].name, 1, nva[i].namelen, stdout);
1132c593315Sopenharmony_ci    printf(": ");
1142c593315Sopenharmony_ci    fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
1152c593315Sopenharmony_ci    printf("\n");
1162c593315Sopenharmony_ci  }
1172c593315Sopenharmony_ci
1182c593315Sopenharmony_ci  buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen);
1192c593315Sopenharmony_ci  buf = malloc(buflen);
1202c593315Sopenharmony_ci
1212c593315Sopenharmony_ci  rv = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, nvlen);
1222c593315Sopenharmony_ci
1232c593315Sopenharmony_ci  if (rv < 0) {
1242c593315Sopenharmony_ci    fprintf(stderr, "nghttp2_hd_deflate_hd() failed with error: %s\n",
1252c593315Sopenharmony_ci            nghttp2_strerror((int)rv));
1262c593315Sopenharmony_ci
1272c593315Sopenharmony_ci    free(buf);
1282c593315Sopenharmony_ci
1292c593315Sopenharmony_ci    exit(EXIT_FAILURE);
1302c593315Sopenharmony_ci  }
1312c593315Sopenharmony_ci
1322c593315Sopenharmony_ci  outlen = (size_t)rv;
1332c593315Sopenharmony_ci
1342c593315Sopenharmony_ci  printf("\nDeflate (%zu byte(s), ratio %.02f):\n\n", outlen,
1352c593315Sopenharmony_ci         sum == 0 ? 0 : (double)outlen / (double)sum);
1362c593315Sopenharmony_ci
1372c593315Sopenharmony_ci  for (i = 0; i < outlen; ++i) {
1382c593315Sopenharmony_ci    if ((i & 0x0fu) == 0) {
1392c593315Sopenharmony_ci      printf("%08zX: ", i);
1402c593315Sopenharmony_ci    }
1412c593315Sopenharmony_ci
1422c593315Sopenharmony_ci    printf("%02X ", buf[i]);
1432c593315Sopenharmony_ci
1442c593315Sopenharmony_ci    if (((i + 1) & 0x0fu) == 0) {
1452c593315Sopenharmony_ci      printf("\n");
1462c593315Sopenharmony_ci    }
1472c593315Sopenharmony_ci  }
1482c593315Sopenharmony_ci
1492c593315Sopenharmony_ci  printf("\n\nInflate:\n\n");
1502c593315Sopenharmony_ci
1512c593315Sopenharmony_ci  /* We pass 1 to final parameter, because buf contains whole deflated
1522c593315Sopenharmony_ci     header data. */
1532c593315Sopenharmony_ci  rv = inflate_header_block(inflater, buf, outlen, 1);
1542c593315Sopenharmony_ci
1552c593315Sopenharmony_ci  if (rv != 0) {
1562c593315Sopenharmony_ci    free(buf);
1572c593315Sopenharmony_ci
1582c593315Sopenharmony_ci    exit(EXIT_FAILURE);
1592c593315Sopenharmony_ci  }
1602c593315Sopenharmony_ci
1612c593315Sopenharmony_ci  printf("\n-----------------------------------------------------------"
1622c593315Sopenharmony_ci         "--------------------\n");
1632c593315Sopenharmony_ci
1642c593315Sopenharmony_ci  free(buf);
1652c593315Sopenharmony_ci}
1662c593315Sopenharmony_ci
1672c593315Sopenharmony_ciint inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
1682c593315Sopenharmony_ci                         size_t inlen, int final) {
1692c593315Sopenharmony_ci  ssize_t rv;
1702c593315Sopenharmony_ci
1712c593315Sopenharmony_ci  for (;;) {
1722c593315Sopenharmony_ci    nghttp2_nv nv;
1732c593315Sopenharmony_ci    int inflate_flags = 0;
1742c593315Sopenharmony_ci    size_t proclen;
1752c593315Sopenharmony_ci
1762c593315Sopenharmony_ci    rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, in, inlen, final);
1772c593315Sopenharmony_ci
1782c593315Sopenharmony_ci    if (rv < 0) {
1792c593315Sopenharmony_ci      fprintf(stderr, "inflate failed with error code %zd", rv);
1802c593315Sopenharmony_ci      return -1;
1812c593315Sopenharmony_ci    }
1822c593315Sopenharmony_ci
1832c593315Sopenharmony_ci    proclen = (size_t)rv;
1842c593315Sopenharmony_ci
1852c593315Sopenharmony_ci    in += proclen;
1862c593315Sopenharmony_ci    inlen -= proclen;
1872c593315Sopenharmony_ci
1882c593315Sopenharmony_ci    if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
1892c593315Sopenharmony_ci      fwrite(nv.name, 1, nv.namelen, stderr);
1902c593315Sopenharmony_ci      fprintf(stderr, ": ");
1912c593315Sopenharmony_ci      fwrite(nv.value, 1, nv.valuelen, stderr);
1922c593315Sopenharmony_ci      fprintf(stderr, "\n");
1932c593315Sopenharmony_ci    }
1942c593315Sopenharmony_ci
1952c593315Sopenharmony_ci    if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
1962c593315Sopenharmony_ci      nghttp2_hd_inflate_end_headers(inflater);
1972c593315Sopenharmony_ci      break;
1982c593315Sopenharmony_ci    }
1992c593315Sopenharmony_ci
2002c593315Sopenharmony_ci    if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
2012c593315Sopenharmony_ci      break;
2022c593315Sopenharmony_ci    }
2032c593315Sopenharmony_ci  }
2042c593315Sopenharmony_ci
2052c593315Sopenharmony_ci  return 0;
2062c593315Sopenharmony_ci}
207