2013-11-10 12:46:18 +01:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2.0 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.
|
|
|
|
*/
|
2014-01-08 15:30:02 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <assert.h>
|
2013-10-26 16:31:41 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2013-10-21 17:42:46 +02:00
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
|
|
|
#include "nghttp2_hd.h"
|
|
|
|
#include "nghttp2_frame.h"
|
|
|
|
|
2013-10-26 18:04:42 +02:00
|
|
|
#include "comp_helper.h"
|
|
|
|
|
2013-10-26 16:31:41 +02:00
|
|
|
typedef struct {
|
|
|
|
size_t table_size;
|
2013-10-26 18:04:42 +02:00
|
|
|
int dump_header_table;
|
2013-10-26 16:31:41 +02:00
|
|
|
} inflate_config;
|
|
|
|
|
|
|
|
static inflate_config config;
|
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
static uint8_t to_ud(char c)
|
|
|
|
{
|
|
|
|
if(c >= 'A' && c <= 'Z') {
|
|
|
|
return c - 'A' + 10;
|
|
|
|
} else if(c >= 'a' && c <= 'z') {
|
|
|
|
return c - 'a' + 10;
|
|
|
|
} else {
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void decode_hex(uint8_t *dest, const char *src, size_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < len; i += 2) {
|
|
|
|
*dest++ = to_ud(src[i]) << 4 | to_ud(src[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
static void to_json(nghttp2_hd_context *inflater,
|
|
|
|
json_t *headers, json_t *wire, int seq)
|
2013-10-21 17:42:46 +02:00
|
|
|
{
|
|
|
|
json_t *obj;
|
2014-01-11 07:31:36 +01:00
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
obj = json_object();
|
|
|
|
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);
|
2014-01-11 07:31:36 +01:00
|
|
|
json_object_set_new(obj, "header_table_size",
|
|
|
|
json_integer(inflater->hd_table_bufsize_max));
|
2013-10-26 18:04:42 +02:00
|
|
|
if(config.dump_header_table) {
|
2014-01-11 07:31:36 +01:00
|
|
|
json_object_set_new(obj, "header_table", dump_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");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inflate_hd(json_t *obj, nghttp2_hd_context *inflater, int seq)
|
|
|
|
{
|
2014-01-16 15:41:13 +01:00
|
|
|
json_t *wire, *table_size, *headers;
|
2013-10-21 17:42:46 +02:00
|
|
|
size_t inputlen;
|
2014-01-16 15:41:13 +01:00
|
|
|
uint8_t *buf, *p;
|
|
|
|
size_t buflen;
|
|
|
|
ssize_t rv;
|
|
|
|
nghttp2_nv nv;
|
2014-01-25 10:24:15 +01:00
|
|
|
int inflate_flags;
|
2013-10-21 17:42:46 +02:00
|
|
|
|
2014-01-11 07:31:36 +01:00
|
|
|
wire = json_object_get(obj, "wire");
|
|
|
|
if(wire == NULL) {
|
|
|
|
fprintf(stderr, "'wire' key is missing at %d\n", seq);
|
2013-10-21 17:42:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-11 07:31:36 +01:00
|
|
|
table_size = json_object_get(obj, "header_table_size");
|
|
|
|
if(table_size) {
|
|
|
|
if(!json_is_integer(table_size)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"The value of 'header_table_size key' is not integer at %d\n",
|
|
|
|
seq);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rv = nghttp2_hd_change_table_size(inflater,
|
|
|
|
json_integer_value(table_size));
|
|
|
|
if(rv != 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"nghttp2_hd_change_table_size() failed with error %s at %d\n",
|
|
|
|
nghttp2_strerror(rv), seq);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inputlen = strlen(json_string_value(wire));
|
2013-10-21 17:42:46 +02:00
|
|
|
if(inputlen & 1) {
|
|
|
|
fprintf(stderr, "Badly formatted output value at %d\n", seq);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
buflen = inputlen / 2;
|
|
|
|
buf = malloc(buflen);
|
2014-01-11 07:31:36 +01:00
|
|
|
decode_hex(buf, json_string_value(wire), inputlen);
|
2013-10-21 17:42:46 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
headers = json_array();
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
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-01-16 15:41:13 +01:00
|
|
|
if(rv < 0) {
|
|
|
|
fprintf(stderr, "inflate failed with error code %zd at %d\n", rv, seq);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
p += rv;
|
|
|
|
buflen -= rv;
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
|
|
|
|
json_array_append_new(headers, dump_header(nv.name, nv.namelen,
|
|
|
|
nv.value, nv.valuelen));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
to_json(inflater, headers, wire, seq);
|
|
|
|
json_decref(headers);
|
2014-01-11 07:31:36 +01:00
|
|
|
free(buf);
|
2013-10-21 17:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-07 17:35:15 +01:00
|
|
|
static int perform(void)
|
2013-10-21 17:42:46 +02:00
|
|
|
{
|
|
|
|
nghttp2_hd_context inflater;
|
2013-10-21 17:50:09 +02:00
|
|
|
size_t i;
|
2014-01-11 07:31:36 +01:00
|
|
|
json_t *json, *cases;
|
2013-10-21 17:42:46 +02:00
|
|
|
json_error_t error;
|
|
|
|
size_t len;
|
2014-01-11 07:31:36 +01:00
|
|
|
nghttp2_hd_side side;
|
2013-10-21 17:42:46 +02:00
|
|
|
|
|
|
|
json = json_loadf(stdin, 0, &error);
|
|
|
|
if(json == NULL) {
|
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-01-11 07:31:36 +01:00
|
|
|
if(strcmp("request", json_string_value(json_object_get(json, "context")))
|
|
|
|
== 0) {
|
|
|
|
side = NGHTTP2_HD_SIDE_REQUEST;
|
|
|
|
} else {
|
|
|
|
side = NGHTTP2_HD_SIDE_RESPONSE;
|
|
|
|
}
|
|
|
|
cases = json_object_get(json, "cases");
|
|
|
|
if(cases == NULL) {
|
|
|
|
fprintf(stderr, "Missing 'cases' key in root object\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if(!json_is_array(cases)) {
|
|
|
|
fprintf(stderr, "'cases' must be JSON array\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
nghttp2_hd_inflate_init(&inflater, side);
|
2013-10-26 16:31:41 +02:00
|
|
|
nghttp2_hd_change_table_size(&inflater, config.table_size);
|
2014-01-11 07:31:36 +01:00
|
|
|
|
|
|
|
output_json_header(side);
|
|
|
|
len = json_array_size(cases);
|
2013-10-21 17:42:46 +02:00
|
|
|
for(i = 0; i < len; ++i) {
|
2014-01-11 07:31:36 +01:00
|
|
|
json_t *obj = json_array_get(cases, i);
|
2013-10-21 17:42:46 +02:00
|
|
|
if(!json_is_object(obj)) {
|
2013-10-21 17:50:09 +02:00
|
|
|
fprintf(stderr, "Unexpected JSON type at %zu. It should be object.\n",
|
|
|
|
i);
|
2013-10-21 17:42:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(inflate_hd(obj, &inflater, i) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(i + 1 < len) {
|
|
|
|
printf(",\n");
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 07:31:36 +01:00
|
|
|
output_json_footer();
|
2013-10-21 17:42:46 +02:00
|
|
|
nghttp2_hd_inflate_free(&inflater);
|
|
|
|
json_decref(json);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-21 17:50:09 +02:00
|
|
|
static void print_help(void)
|
2013-10-21 17:42:46 +02:00
|
|
|
{
|
2014-01-08 13:44:44 +01:00
|
|
|
printf("HPACK HTTP/2.0 header decoder\n"
|
2013-10-26 16:31:41 +02:00
|
|
|
"Usage: inflatehd [OPTIONS] < INPUT\n"
|
|
|
|
"\n"
|
2014-01-11 07:31:36 +01:00
|
|
|
"Reads JSON data from stdin and outputs inflated name/value pairs\n"
|
|
|
|
"in JSON.\n"
|
2013-10-21 17:42:46 +02:00
|
|
|
"\n"
|
2014-01-11 07:31:36 +01:00
|
|
|
"The root JSON object must contain \"context\" key, which indicates\n"
|
|
|
|
"which compression context is used. If it is \"request\", request\n"
|
|
|
|
"compression context is used. Otherwise, response compression\n"
|
|
|
|
"context is used. The value of \"cases\" key contains the sequence\n"
|
|
|
|
"of compressed header block. They share the same compression\n"
|
|
|
|
"context and are processed in the order they appear. Each item in\n"
|
|
|
|
"the sequence is a JSON object and it must have at least \"wire\"\n"
|
|
|
|
"key. Its value is a string containing compressed header block in\n"
|
|
|
|
"hex string.\n"
|
2013-10-21 17:42:46 +02:00
|
|
|
"\n"
|
|
|
|
"Example:\n"
|
2014-01-11 07:31:36 +01:00
|
|
|
"{\n"
|
|
|
|
" \"context\": \"request\",\n"
|
|
|
|
" \"cases\":\n"
|
|
|
|
" [\n"
|
|
|
|
" { \"wire\": \"0284f77778ff\" },\n"
|
|
|
|
" { \"wire\": \"0185fafd3c3c7f81\" }\n"
|
|
|
|
" ]\n"
|
|
|
|
"}\n"
|
2013-10-21 17:42:46 +02:00
|
|
|
"\n"
|
|
|
|
"The output of this program can be used as input for deflatehd.\n"
|
|
|
|
"\n"
|
|
|
|
"OPTIONS:\n"
|
2013-10-26 16:31:41 +02:00
|
|
|
" -s, --table-size=<N>\n"
|
2013-10-26 18:04:42 +02:00
|
|
|
" Set dynamic table size. In the HPACK\n"
|
2013-10-26 16:31:41 +02:00
|
|
|
" specification, this value is denoted by\n"
|
|
|
|
" SETTINGS_HEADER_TABLE_SIZE.\n"
|
2013-10-26 18:04:42 +02:00
|
|
|
" Default: 4096\n"
|
|
|
|
" -d, --dump-header-table\n"
|
|
|
|
" Output dynamic header table.\n");
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
2013-10-26 16:31:41 +02:00
|
|
|
{"table-size", required_argument, NULL, 's'},
|
2013-10-26 18:04:42 +02:00
|
|
|
{"dump-header-table", no_argument, NULL, 'd'},
|
2013-10-21 17:42:46 +02:00
|
|
|
{NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-10-26 16:31:41 +02:00
|
|
|
char *end;
|
|
|
|
config.table_size = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
|
2013-10-26 18:04:42 +02:00
|
|
|
config.dump_header_table = 0;
|
2013-10-21 17:42:46 +02:00
|
|
|
while(1) {
|
|
|
|
int option_index = 0;
|
2014-01-11 07:31:36 +01:00
|
|
|
int c = getopt_long(argc, argv, "dhs:", long_options, &option_index);
|
2013-10-21 17:42:46 +02:00
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
|
|
|
print_help();
|
|
|
|
exit(EXIT_SUCCESS);
|
2013-10-26 16:31:41 +02:00
|
|
|
case 's':
|
|
|
|
/* --table-size */
|
2014-01-18 07:32:50 +01:00
|
|
|
errno = 0;
|
2013-10-26 16:31:41 +02:00
|
|
|
config.table_size = strtoul(optarg, &end, 10);
|
|
|
|
if(errno == ERANGE || *end != '\0') {
|
|
|
|
fprintf(stderr, "-s: Bad option value\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2013-10-26 18:04:42 +02:00
|
|
|
case 'd':
|
|
|
|
/* --dump-header-table */
|
|
|
|
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;
|
|
|
|
}
|