2013-11-10 12:46:18 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-11-10 12:46:18 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-01-08 15:30:02 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <config.h>
|
2014-05-14 16:22:23 +02:00
|
|
|
#endif // HAVE_CONFIG_H
|
2014-01-08 15:30:02 +01:00
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
2013-10-21 17:42:46 +02:00
|
|
|
#include <getopt.h>
|
2014-03-30 15:41:02 +02:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2013-10-21 17:42:46 +02:00
|
|
|
#include <assert.h>
|
2014-03-30 15:41:02 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
2013-10-21 17:42:46 +02:00
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
2015-10-21 17:57:00 +02:00
|
|
|
#include <nghttp2/nghttp2.h>
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2015-10-17 16:34:05 +02:00
|
|
|
#include "template.h"
|
2015-10-21 17:57:00 +02:00
|
|
|
#include "comp_helper.h"
|
2015-10-17 16:34:05 +02:00
|
|
|
|
2015-10-17 04:52:33 +02:00
|
|
|
namespace nghttp2 {
|
|
|
|
|
2017-11-23 06:19:12 +01:00
|
|
|
typedef struct {
|
|
|
|
int dump_header_table;
|
|
|
|
} inflate_config;
|
2013-10-26 16:31:41 +02:00
|
|
|
|
|
|
|
static inflate_config config;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static uint8_t to_ud(char c) {
|
|
|
|
if (c >= 'A' && c <= 'Z') {
|
2013-10-21 17:42:46 +02:00
|
|
|
return c - 'A' + 10;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (c >= 'a' && c <= 'z') {
|
2013-10-21 17:42:46 +02:00
|
|
|
return c - 'a' + 10;
|
|
|
|
} else {
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void decode_hex(uint8_t *dest, const char *src, size_t len) {
|
2013-10-21 17:42:46 +02:00
|
|
|
size_t i;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < len; i += 2) {
|
2013-10-21 17:42:46 +02:00
|
|
|
*dest++ = to_ud(src[i]) << 4 | to_ud(src[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void to_json(nghttp2_hd_inflater *inflater, json_t *headers,
|
|
|
|
json_t *wire, int seq, size_t old_settings_table_size) {
|
2014-03-30 15:41:02 +02:00
|
|
|
auto obj = json_object();
|
2013-10-21 17:42:46 +02:00
|
|
|
json_object_set_new(obj, "seq", json_integer(seq));
|
2014-01-11 07:31:36 +01:00
|
|
|
json_object_set(obj, "wire", wire);
|
2014-01-16 15:41:13 +01:00
|
|
|
json_object_set(obj, "headers", headers);
|
2015-10-21 17:57:00 +02:00
|
|
|
auto max_dyn_table_size =
|
|
|
|
nghttp2_hd_inflate_get_max_dynamic_table_size(inflater);
|
|
|
|
if (old_settings_table_size != max_dyn_table_size) {
|
2014-02-25 15:55:06 +01:00
|
|
|
json_object_set_new(obj, "header_table_size",
|
2015-10-21 17:57:00 +02:00
|
|
|
json_integer(max_dyn_table_size));
|
2014-02-25 15:55:06 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.dump_header_table) {
|
2015-10-21 17:57:00 +02:00
|
|
|
json_object_set_new(obj, "header_table",
|
|
|
|
dump_inflate_header_table(inflater));
|
2013-10-26 18:04:42 +02:00
|
|
|
}
|
2013-10-21 17:42:46 +02:00
|
|
|
json_dumpf(obj, stdout, JSON_INDENT(2) | JSON_PRESERVE_ORDER);
|
|
|
|
json_decref(obj);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int inflate_hd(json_t *obj, nghttp2_hd_inflater *inflater, int seq) {
|
2014-01-16 15:41:13 +01:00
|
|
|
ssize_t rv;
|
|
|
|
nghttp2_nv nv;
|
2014-01-25 10:24:15 +01:00
|
|
|
int inflate_flags;
|
2015-10-21 17:57:00 +02:00
|
|
|
size_t old_settings_table_size =
|
|
|
|
nghttp2_hd_inflate_get_max_dynamic_table_size(inflater);
|
2013-10-21 17:42:46 +02:00
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto wire = json_object_get(obj, "wire");
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (wire == nullptr) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr, "'wire' key is missing at %d\n", seq);
|
2013-10-21 17:42:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2015-05-15 15:29:57 +02:00
|
|
|
if (!json_is_string(wire)) {
|
|
|
|
fprintf(stderr, "'wire' value is not string at %d\n", seq);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto table_size = json_object_get(obj, "header_table_size");
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (table_size) {
|
|
|
|
if (!json_is_integer(table_size)) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"The value of 'header_table_size key' is not integer at %d\n",
|
|
|
|
seq);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
rv = nghttp2_hd_inflate_change_table_size(inflater,
|
|
|
|
json_integer_value(table_size));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"nghttp2_hd_change_table_size() failed with error %s at %d\n",
|
|
|
|
nghttp2_strerror(rv), seq);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
|
|
|
auto inputlen = strlen(json_string_value(wire));
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inputlen & 1) {
|
2013-10-21 17:42:46 +02:00
|
|
|
fprintf(stderr, "Badly formatted output value at %d\n", seq);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto buflen = inputlen / 2;
|
|
|
|
auto buf = std::vector<uint8_t>(buflen);
|
|
|
|
|
|
|
|
decode_hex(buf.data(), json_string_value(wire), inputlen);
|
|
|
|
|
|
|
|
auto headers = json_array();
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto p = buf.data();
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflate_flags = 0;
|
|
|
|
rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, p, buflen, 1);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2014-01-16 15:41:13 +01:00
|
|
|
fprintf(stderr, "inflate failed with error code %zd at %d\n", rv, seq);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
p += rv;
|
|
|
|
buflen -= rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
|
|
|
|
json_array_append_new(
|
|
|
|
headers, dump_header(nv.name, nv.namelen, nv.value, nv.valuelen));
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
|
2014-01-16 15:41:13 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
assert(buflen == 0);
|
|
|
|
nghttp2_hd_inflate_end_headers(inflater);
|
2014-02-25 15:55:06 +01:00
|
|
|
to_json(inflater, headers, wire, seq, old_settings_table_size);
|
2014-01-16 15:41:13 +01:00
|
|
|
json_decref(headers);
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int perform(void) {
|
2022-02-10 10:56:19 +01:00
|
|
|
nghttp2_hd_inflater *inflater = nullptr;
|
2013-10-21 17:42:46 +02:00
|
|
|
json_error_t error;
|
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto json = json_loadf(stdin, 0, &error);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (json == nullptr) {
|
2013-10-21 17:50:09 +02:00
|
|
|
fprintf(stderr, "JSON loading failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
|
|
|
auto cases = json_object_get(json, "cases");
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (cases == nullptr) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr, "Missing 'cases' key in root object\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!json_is_array(cases)) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr, "'cases' must be JSON array\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_hd_inflate_new(&inflater);
|
2014-02-13 15:22:52 +01:00
|
|
|
output_json_header();
|
2014-03-30 15:41:02 +02:00
|
|
|
auto len = json_array_size(cases);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2014-03-30 15:41:02 +02:00
|
|
|
auto obj = json_array_get(cases, i);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!json_is_object(obj)) {
|
|
|
|
fprintf(stderr, "Unexpected JSON type at %zu. It should be object.\n", i);
|
2013-10-21 17:42:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
if (inflate_hd(obj, inflater, i) != 0) {
|
2013-10-21 17:42:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (i + 1 < len) {
|
2013-10-21 17:42:46 +02:00
|
|
|
printf(",\n");
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 07:31:36 +01:00
|
|
|
output_json_footer();
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_hd_inflate_del(inflater);
|
2013-10-21 17:42:46 +02:00
|
|
|
json_decref(json);
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void print_help(void) {
|
2014-03-30 15:41:02 +02:00
|
|
|
std::cout << R"(HPACK HTTP/2 header decoder
|
|
|
|
Usage: inflatehd [OPTIONS] < INPUT
|
|
|
|
|
|
|
|
Reads JSON data from stdin and outputs inflated name/value pairs in
|
|
|
|
JSON.
|
|
|
|
|
|
|
|
The root JSON object must contain "context" key, which indicates which
|
|
|
|
compression context is used. If it is "request", request compression
|
|
|
|
context is used. Otherwise, response compression context is used.
|
|
|
|
The value of "cases" key contains the sequence of compressed header
|
|
|
|
block. They share the same compression context and are processed in
|
|
|
|
the order they appear. Each item in the sequence is a JSON object and
|
|
|
|
it must have at least "wire" key. Its value is a string containing
|
|
|
|
compressed header block in hex string.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
"context": "request",
|
|
|
|
"cases":
|
|
|
|
[
|
|
|
|
{ "wire": "0284f77778ff" },
|
|
|
|
{ "wire": "0185fafd3c3c7f81" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
The output of this program can be used as input for deflatehd.
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-d, --dump-header-table
|
2016-10-15 11:36:04 +02:00
|
|
|
Output dynamic header table.)"
|
|
|
|
<< std::endl;
|
2014-11-27 15:39:04 +01:00
|
|
|
;
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 11:28:00 +01:00
|
|
|
constexpr static struct option long_options[] = {
|
2014-11-27 15:39:04 +01:00
|
|
|
{"dump-header-table", no_argument, nullptr, 'd'}, {nullptr, 0, nullptr, 0}};
|
2013-10-21 17:42:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int main(int argc, char **argv) {
|
2013-10-26 18:04:42 +02:00
|
|
|
config.dump_header_table = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
while (1) {
|
2013-10-21 17:42:46 +02:00
|
|
|
int option_index = 0;
|
2014-02-13 15:22:52 +01:00
|
|
|
int c = getopt_long(argc, argv, "dh", long_options, &option_index);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (c == -1) {
|
2013-10-21 17:42:46 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (c) {
|
2013-10-21 17:42:46 +02:00
|
|
|
case 'h':
|
|
|
|
print_help();
|
|
|
|
exit(EXIT_SUCCESS);
|
2013-10-26 18:04:42 +02:00
|
|
|
case 'd':
|
2014-05-14 16:22:23 +02:00
|
|
|
// --dump-header-table
|
2013-10-26 18:04:42 +02:00
|
|
|
config.dump_header_table = 1;
|
|
|
|
break;
|
2013-10-21 17:42:46 +02:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-26 16:31:41 +02:00
|
|
|
perform();
|
2013-10-21 17:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-17 04:52:33 +02:00
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2015-10-17 16:34:05 +02:00
|
|
|
return nghttp2::run_app(nghttp2::main, argc, argv);
|
2015-10-17 04:52:33 +02:00
|
|
|
}
|