xref: /third_party/nghttp2/src/comp_helper.c (revision 2c593315)
1/*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2013 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#include "comp_helper.h"
26#include <string.h>
27
28static void dump_val(json_t *jent, const char *key, uint8_t *val, size_t len) {
29  json_object_set_new(jent, key, json_pack("s#", val, len));
30}
31
32#define NGHTTP2_HD_ENTRY_OVERHEAD 32
33
34json_t *dump_deflate_header_table(nghttp2_hd_deflater *deflater) {
35  json_t *obj, *entries;
36  size_t i;
37  size_t len = nghttp2_hd_deflate_get_num_table_entries(deflater);
38
39  obj = json_object();
40  entries = json_array();
41  /* The first index of dynamic table is 62 */
42  for (i = 62; i <= len; ++i) {
43    const nghttp2_nv *nv = nghttp2_hd_deflate_get_table_entry(deflater, i);
44    json_t *outent = json_object();
45    json_object_set_new(outent, "index", json_integer((json_int_t)i));
46    dump_val(outent, "name", nv->name, nv->namelen);
47    dump_val(outent, "value", nv->value, nv->valuelen);
48    json_object_set_new(outent, "size",
49                        json_integer((json_int_t)(nv->namelen + nv->valuelen +
50                                                  NGHTTP2_HD_ENTRY_OVERHEAD)));
51    json_array_append_new(entries, outent);
52  }
53  json_object_set_new(obj, "entries", entries);
54  json_object_set_new(
55      obj, "size",
56      json_integer(
57          (json_int_t)nghttp2_hd_deflate_get_dynamic_table_size(deflater)));
58  json_object_set_new(
59      obj, "max_size",
60      json_integer(
61          (json_int_t)nghttp2_hd_deflate_get_max_dynamic_table_size(deflater)));
62
63  return obj;
64}
65
66json_t *dump_inflate_header_table(nghttp2_hd_inflater *inflater) {
67  json_t *obj, *entries;
68  size_t i;
69  size_t len = nghttp2_hd_inflate_get_num_table_entries(inflater);
70
71  obj = json_object();
72  entries = json_array();
73  /* The first index of dynamic table is 62 */
74  for (i = 62; i <= len; ++i) {
75    const nghttp2_nv *nv = nghttp2_hd_inflate_get_table_entry(inflater, i);
76    json_t *outent = json_object();
77    json_object_set_new(outent, "index", json_integer((json_int_t)i));
78    dump_val(outent, "name", nv->name, nv->namelen);
79    dump_val(outent, "value", nv->value, nv->valuelen);
80    json_object_set_new(outent, "size",
81                        json_integer((json_int_t)(nv->namelen + nv->valuelen +
82                                                  NGHTTP2_HD_ENTRY_OVERHEAD)));
83    json_array_append_new(entries, outent);
84  }
85  json_object_set_new(obj, "entries", entries);
86  json_object_set_new(
87      obj, "size",
88      json_integer(
89          (json_int_t)nghttp2_hd_inflate_get_dynamic_table_size(inflater)));
90  json_object_set_new(
91      obj, "max_size",
92      json_integer(
93          (json_int_t)nghttp2_hd_inflate_get_max_dynamic_table_size(inflater)));
94
95  return obj;
96}
97
98json_t *dump_header(const uint8_t *name, size_t namelen, const uint8_t *value,
99                    size_t valuelen) {
100  json_t *nv_pair = json_object();
101  char *cname = malloc(namelen + 1);
102  if (cname == NULL) {
103    return NULL;
104  }
105  memcpy(cname, name, namelen);
106  cname[namelen] = '\0';
107  json_object_set_new(nv_pair, cname, json_pack("s#", value, valuelen));
108  free(cname);
109  return nv_pair;
110}
111
112json_t *dump_headers(const nghttp2_nv *nva, size_t nvlen) {
113  json_t *headers;
114  size_t i;
115
116  headers = json_array();
117  for (i = 0; i < nvlen; ++i) {
118    json_array_append_new(headers, dump_header(nva[i].name, nva[i].namelen,
119                                               nva[i].value, nva[i].valuelen));
120  }
121  return headers;
122}
123
124void output_json_header(void) {
125  printf("{\n"
126         "  \"cases\":\n"
127         "  [\n");
128}
129
130void output_json_footer(void) {
131  printf("  ]\n"
132         "}\n");
133}
134