summaryrefslogtreecommitdiffstats
path: root/src/comp_helper.c
blob: 98db08a487af67fd4538494dda74bb9ef50399ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * nghttp2 - HTTP/2 C Library
 *
 * Copyright (c) 2013 Tatsuhiro Tsujikawa
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "comp_helper.h"
#include <string.h>

static void dump_val(json_t *jent, const char *key, uint8_t *val, size_t len) {
  json_object_set_new(jent, key, json_pack("s#", val, len));
}

#define NGHTTP2_HD_ENTRY_OVERHEAD 32

json_t *dump_deflate_header_table(nghttp2_hd_deflater *deflater) {
  json_t *obj, *entries;
  size_t i;
  size_t len = nghttp2_hd_deflate_get_num_table_entries(deflater);

  obj = json_object();
  entries = json_array();
  /* The first index of dynamic table is 62 */
  for (i = 62; i <= len; ++i) {
    const nghttp2_nv *nv = nghttp2_hd_deflate_get_table_entry(deflater, i);
    json_t *outent = json_object();
    json_object_set_new(outent, "index", json_integer((json_int_t)i));
    dump_val(outent, "name", nv->name, nv->namelen);
    dump_val(outent, "value", nv->value, nv->valuelen);
    json_object_set_new(outent, "size",
                        json_integer((json_int_t)(nv->namelen + nv->valuelen +
                                                  NGHTTP2_HD_ENTRY_OVERHEAD)));
    json_array_append_new(entries, outent);
  }
  json_object_set_new(obj, "entries", entries);
  json_object_set_new(
      obj, "size",
      json_integer(
          (json_int_t)nghttp2_hd_deflate_get_dynamic_table_size(deflater)));
  json_object_set_new(
      obj, "max_size",
      json_integer(
          (json_int_t)nghttp2_hd_deflate_get_max_dynamic_table_size(deflater)));

  return obj;
}

json_t *dump_inflate_header_table(nghttp2_hd_inflater *inflater) {
  json_t *obj, *entries;
  size_t i;
  size_t len = nghttp2_hd_inflate_get_num_table_entries(inflater);

  obj = json_object();
  entries = json_array();
  /* The first index of dynamic table is 62 */
  for (i = 62; i <= len; ++i) {
    const nghttp2_nv *nv = nghttp2_hd_inflate_get_table_entry(inflater, i);
    json_t *outent = json_object();
    json_object_set_new(outent, "index", json_integer((json_int_t)i));
    dump_val(outent, "name", nv->name, nv->namelen);
    dump_val(outent, "value", nv->value, nv->valuelen);
    json_object_set_new(outent, "size",
                        json_integer((json_int_t)(nv->namelen + nv->valuelen +
                                                  NGHTTP2_HD_ENTRY_OVERHEAD)));
    json_array_append_new(entries, outent);
  }
  json_object_set_new(obj, "entries", entries);
  json_object_set_new(
      obj, "size",
      json_integer(
          (json_int_t)nghttp2_hd_inflate_get_dynamic_table_size(inflater)));
  json_object_set_new(
      obj, "max_size",
      json_integer(
          (json_int_t)nghttp2_hd_inflate_get_max_dynamic_table_size(inflater)));

  return obj;
}

json_t *dump_header(const uint8_t *name, size_t namelen, const uint8_t *value,
                    size_t valuelen) {
  json_t *nv_pair = json_object();
  char *cname = malloc(namelen + 1);
  if (cname == NULL) {
    return NULL;
  }
  memcpy(cname, name, namelen);
  cname[namelen] = '\0';
  json_object_set_new(nv_pair, cname, json_pack("s#", value, valuelen));
  free(cname);
  return nv_pair;
}

json_t *dump_headers(const nghttp2_nv *nva, size_t nvlen) {
  json_t *headers;
  size_t i;

  headers = json_array();
  for (i = 0; i < nvlen; ++i) {
    json_array_append_new(headers, dump_header(nva[i].name, nva[i].namelen,
                                               nva[i].value, nva[i].valuelen));
  }
  return headers;
}

void output_json_header(void) {
  printf("{\n"
         "  \"cases\":\n"
         "  [\n");
}

void output_json_footer(void) {
  printf("  ]\n"
         "}\n");
}