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
|
2014-11-27 15:39:04 +01: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
|
2013-10-21 17:42:46 +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>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
2013-10-21 17:42:46 +02:00
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
extern "C" {
|
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
#include "nghttp2_hd.h"
|
2013-10-26 15:46:44 +02:00
|
|
|
#include "nghttp2_frame.h"
|
2013-10-21 17:42:46 +02:00
|
|
|
|
2013-10-26 18:04:42 +02:00
|
|
|
#include "comp_helper.h"
|
2014-03-30 15:41:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
#include "template.h"
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2013-10-26 16:31:41 +02:00
|
|
|
typedef struct {
|
|
|
|
size_t table_size;
|
2013-10-28 16:42:08 +01:00
|
|
|
size_t deflate_table_size;
|
2013-10-26 16:31:41 +02:00
|
|
|
int http1text;
|
2013-10-26 18:04:42 +02:00
|
|
|
int dump_header_table;
|
2013-10-26 16:31:41 +02:00
|
|
|
} deflate_config;
|
|
|
|
|
|
|
|
static deflate_config config;
|
|
|
|
|
2013-11-17 16:15:08 +01:00
|
|
|
static size_t input_sum;
|
|
|
|
static size_t output_sum;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static char to_hex_digit(uint8_t n) {
|
|
|
|
if (n > 9) {
|
2014-01-11 07:31:36 +01:00
|
|
|
return n - 10 + 'a';
|
|
|
|
}
|
|
|
|
return n + '0';
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void to_hex(char *dest, const uint8_t *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) {
|
2014-01-11 07:31:36 +01:00
|
|
|
*dest++ = to_hex_digit(src[i] >> 4);
|
|
|
|
*dest++ = to_hex_digit(src[i] & 0xf);
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void output_to_json(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
|
|
|
|
size_t inputlen, const std::vector<nghttp2_nv> &nva,
|
|
|
|
int seq) {
|
2014-03-30 15:41:02 +02:00
|
|
|
auto len = nghttp2_bufs_len(bufs);
|
|
|
|
auto hex = std::vector<char>(len * 2);
|
|
|
|
auto obj = json_object();
|
2014-04-05 13:04:09 +02:00
|
|
|
auto comp_ratio = inputlen == 0 ? 0.0 : (double)len / inputlen * 100;
|
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_new(obj, "input_length", json_integer(inputlen));
|
|
|
|
json_object_set_new(obj, "output_length", json_integer(len));
|
|
|
|
json_object_set_new(obj, "percentage_of_original_size",
|
2014-04-05 13:04:09 +02:00
|
|
|
json_real(comp_ratio));
|
2014-03-13 14:11:02 +01:00
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto hexp = hex.data();
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto ci = bufs->head; ci; ci = ci->next) {
|
2014-03-30 15:41:02 +02:00
|
|
|
auto buf = &ci->buf;
|
2014-03-13 14:11:02 +01:00
|
|
|
to_hex(hexp, buf->pos, nghttp2_buf_len(buf));
|
|
|
|
hexp += nghttp2_buf_len(buf);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (len == 0) {
|
2014-01-24 13:54:00 +01:00
|
|
|
json_object_set_new(obj, "wire", json_string(""));
|
|
|
|
} else {
|
2014-03-30 15:41:02 +02:00
|
|
|
json_object_set_new(obj, "wire", json_pack("s#", hex.data(), hex.size()));
|
2014-01-24 13:54:00 +01:00
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
json_object_set_new(obj, "headers", dump_headers(nva.data(), nva.size()));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (seq == 0) {
|
2014-05-14 16:22:23 +02:00
|
|
|
// We only change the header table size only once at the beginning
|
2014-02-25 15:55:06 +01:00
|
|
|
json_object_set_new(obj, "header_table_size",
|
|
|
|
json_integer(config.table_size));
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.dump_header_table) {
|
|
|
|
json_object_set_new(obj, "header_table", dump_header_table(&deflater->ctx));
|
2013-10-26 18:04:42 +02:00
|
|
|
}
|
|
|
|
json_dumpf(obj, stdout, JSON_PRESERVE_ORDER | JSON_INDENT(2));
|
2013-10-21 17:42:46 +02:00
|
|
|
printf("\n");
|
|
|
|
json_decref(obj);
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static void deflate_hd(nghttp2_hd_deflater *deflater,
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::vector<nghttp2_nv> &nva, size_t inputlen,
|
|
|
|
int seq) {
|
2013-10-26 15:46:44 +02:00
|
|
|
ssize_t rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs bufs;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
nghttp2_bufs_init2(&bufs, 4_k, 16, 0, nghttp2_mem_default());
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, (nghttp2_nv *)nva.data(),
|
|
|
|
nva.size());
|
|
|
|
if (rv < 0) {
|
2013-10-26 15:46:44 +02:00
|
|
|
fprintf(stderr, "deflate failed with error code %zd at %d\n", rv, seq);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-11-17 16:15:08 +01:00
|
|
|
input_sum += inputlen;
|
2014-03-30 15:41:02 +02:00
|
|
|
output_sum += nghttp2_bufs_len(&bufs);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
output_to_json(deflater, &bufs, inputlen, nva, seq);
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs_free(&bufs);
|
2013-10-26 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int deflate_hd_json(json_t *obj, nghttp2_hd_deflater *deflater,
|
|
|
|
int seq) {
|
2013-10-21 17:42:46 +02:00
|
|
|
size_t inputlen = 0;
|
|
|
|
|
2014-03-30 15:41:02 +02:00
|
|
|
auto js = json_object_get(obj, "headers");
|
2014-11-27 15:39:04 +01:00
|
|
|
if (js == nullptr) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr, "'headers' key is missing at %d\n", seq);
|
2013-10-21 17:42:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!json_is_array(js)) {
|
|
|
|
fprintf(stderr, "The value of 'headers' key must be an array at %d\n", seq);
|
2013-10-21 17:42:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
|
|
|
auto len = json_array_size(js);
|
|
|
|
auto nva = std::vector<nghttp2_nv>(len);
|
|
|
|
|
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 nv_pair = json_array_get(js, i);
|
2014-01-11 07:31:36 +01:00
|
|
|
const char *name;
|
|
|
|
json_t *value;
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!json_is_object(nv_pair) || json_object_size(nv_pair) != 1) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr, "bad formatted name/value pair object at %d\n", seq);
|
2013-10-21 17:42:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-01-11 07:31:36 +01:00
|
|
|
json_object_foreach(nv_pair, name, value) {
|
2014-11-27 15:39:04 +01:00
|
|
|
nva[i].name = (uint8_t *)name;
|
2014-01-11 07:31:36 +01:00
|
|
|
nva[i].namelen = strlen(name);
|
2013-10-21 17:42:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!json_is_string(value)) {
|
2014-01-11 07:31:36 +01:00
|
|
|
fprintf(stderr, "value is not string at %d\n", seq);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nva[i].value = (uint8_t *)json_string_value(value);
|
2014-01-11 07:31:36 +01:00
|
|
|
nva[i].valuelen = strlen(json_string_value(value));
|
2014-04-02 13:37:33 +02:00
|
|
|
|
|
|
|
nva[i].flags = NGHTTP2_NV_FLAG_NONE;
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
inputlen += nva[i].namelen + nva[i].valuelen;
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
|
|
|
deflate_hd(deflater, nva, inputlen, seq);
|
|
|
|
|
2013-10-21 17:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-19 15:23:42 +01:00
|
|
|
static nghttp2_hd_deflater *init_deflater() {
|
|
|
|
nghttp2_hd_deflater *deflater;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_hd_deflate_new(&deflater, config.deflate_table_size);
|
2014-02-13 15:22:52 +01:00
|
|
|
nghttp2_hd_deflate_change_table_size(deflater, config.table_size);
|
2014-12-19 15:23:42 +01:00
|
|
|
return deflater;
|
2014-01-11 07:31:36 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void deinit_deflater(nghttp2_hd_deflater *deflater) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_hd_deflate_del(deflater);
|
2014-01-11 07:31:36 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int perform(void) {
|
2013-10-21 17:42:46 +02:00
|
|
|
json_error_t error;
|
2014-01-11 07:31:36 +01:00
|
|
|
|
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:42:46 +02:00
|
|
|
fprintf(stderr, "JSON loading failed\n");
|
2013-10-21 17:50:09 +02:00
|
|
|
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-19 15:23:42 +01:00
|
|
|
auto deflater = init_deflater();
|
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 (deflate_hd_json(obj, deflater, 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
|
|
|
deinit_deflater(deflater);
|
2013-10-21 17:42:46 +02:00
|
|
|
json_decref(json);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int perform_from_http1text(void) {
|
2013-10-26 15:46:44 +02:00
|
|
|
char line[1 << 14];
|
|
|
|
int seq = 0;
|
2014-12-19 15:23:42 +01:00
|
|
|
|
|
|
|
auto deflater = init_deflater();
|
2014-02-13 15:22:52 +01:00
|
|
|
output_json_header();
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2014-06-04 17:59:22 +02:00
|
|
|
std::vector<nghttp2_nv> nva;
|
2013-10-26 15:46:44 +02:00
|
|
|
int end = 0;
|
|
|
|
size_t inputlen = 0;
|
2014-06-04 17:59:22 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2013-10-26 15:46:44 +02:00
|
|
|
char *rv = fgets(line, sizeof(line), stdin);
|
|
|
|
char *val, *val_end;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == nullptr) {
|
2013-10-26 15:46:44 +02:00
|
|
|
end = 1;
|
|
|
|
break;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (line[0] == '\n') {
|
2013-10-26 15:46:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-06-04 17:59:22 +02:00
|
|
|
nva.emplace_back();
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &nv = nva.back();
|
2014-03-30 15:41:02 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
val = strchr(line + 1, ':');
|
|
|
|
if (val == nullptr) {
|
2013-10-26 15:46:44 +02:00
|
|
|
fprintf(stderr, "Bad HTTP/1 header field format at %d.\n", seq);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*val = '\0';
|
|
|
|
++val;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; *val && (*val == ' ' || *val == '\t'); ++val)
|
|
|
|
;
|
|
|
|
for (val_end = val; *val_end && (*val_end != '\r' && *val_end != '\n');
|
|
|
|
++val_end)
|
|
|
|
;
|
2013-10-26 15:46:44 +02:00
|
|
|
*val_end = '\0';
|
2014-05-14 16:22:23 +02:00
|
|
|
|
2014-06-04 17:59:22 +02:00
|
|
|
nv.namelen = strlen(line);
|
|
|
|
nv.valuelen = strlen(val);
|
2014-11-27 15:39:04 +01:00
|
|
|
nv.name = (uint8_t *)strdup(line);
|
|
|
|
nv.value = (uint8_t *)strdup(val);
|
2014-06-04 17:59:22 +02:00
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NONE;
|
2014-04-02 13:37:33 +02:00
|
|
|
|
2014-06-04 17:59:22 +02:00
|
|
|
inputlen += nv.namelen + nv.valuelen;
|
2013-10-26 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!end) {
|
|
|
|
if (seq > 0) {
|
2013-11-17 14:49:38 +01:00
|
|
|
printf(",\n");
|
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
deflate_hd(deflater, nva, inputlen, seq);
|
2013-11-17 14:49:38 +01:00
|
|
|
}
|
2013-10-26 15:46:44 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &nv : nva) {
|
2014-06-04 17:59:22 +02:00
|
|
|
free(nv.name);
|
|
|
|
free(nv.value);
|
2013-10-26 15:46:44 +02:00
|
|
|
}
|
2014-06-04 17:59:22 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (end)
|
|
|
|
break;
|
2013-10-26 15:46:44 +02:00
|
|
|
++seq;
|
|
|
|
}
|
2014-01-11 07:31:36 +01:00
|
|
|
output_json_footer();
|
2014-12-07 15:07:13 +01:00
|
|
|
deinit_deflater(deflater);
|
2013-10-26 15:46:44 +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 encoder
|
|
|
|
Usage: deflatehd [OPTIONS] < INPUT
|
|
|
|
|
|
|
|
Reads JSON data or HTTP/1-style header fields from stdin and outputs
|
|
|
|
deflated header block in JSON array.
|
|
|
|
|
|
|
|
For the JSON input, 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 input header set. 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 "headers" key. Its value
|
|
|
|
is an array of a JSON object containing exactly one name/value pair.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
"context": "request",
|
|
|
|
"cases":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"headers": [
|
|
|
|
{ ":method": "GET" },
|
|
|
|
{ ":path": "/" }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"headers": [
|
|
|
|
{ ":method": "POST" },
|
|
|
|
{ ":path": "/" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
With -t option, the program can accept more familiar HTTP/1 style
|
|
|
|
header field block. Each header set must be followed by one empty
|
|
|
|
line:
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
:method: GET
|
|
|
|
:scheme: https
|
|
|
|
:path: /
|
|
|
|
|
|
|
|
:method: POST
|
|
|
|
user-agent: nghttp2
|
|
|
|
|
|
|
|
The output of this program can be used as input for inflatehd.
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-t, --http1text Use HTTP/1 style header field text as input.
|
|
|
|
Each header set is delimited by single empty
|
|
|
|
line.
|
|
|
|
-s, --table-size=<N>
|
|
|
|
Set dynamic table size. In the HPACK
|
|
|
|
specification, this value is denoted by
|
|
|
|
SETTINGS_HEADER_TABLE_SIZE.
|
|
|
|
Default: 4096
|
|
|
|
-S, --deflate-table-size=<N>
|
|
|
|
Use first N bytes of dynamic header table
|
|
|
|
buffer.
|
|
|
|
Default: 4096
|
|
|
|
-d, --dump-header-table
|
2014-11-27 15:39:04 +01:00
|
|
|
Output dynamic header table.)" << std::endl;
|
2013-10-21 17:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
2014-11-27 15:39:04 +01:00
|
|
|
{"http1text", no_argument, nullptr, 't'},
|
|
|
|
{"table-size", required_argument, nullptr, 's'},
|
|
|
|
{"deflate-table-size", required_argument, nullptr, 'S'},
|
|
|
|
{"dump-header-table", no_argument, nullptr, 'd'},
|
|
|
|
{nullptr, 0, nullptr, 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-28 16:42:08 +01:00
|
|
|
config.deflate_table_size = NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE;
|
2013-10-26 16:31:41 +02:00
|
|
|
config.http1text = 0;
|
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-07-12 11:47:55 +02:00
|
|
|
int c = getopt_long(argc, argv, "S:dhs:t", 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 15:46:44 +02:00
|
|
|
case 't':
|
2014-05-14 16:22:23 +02:00
|
|
|
// --http1text
|
2013-10-26 16:31:41 +02:00
|
|
|
config.http1text = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
2014-05-14 16:22:23 +02:00
|
|
|
// --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);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (errno == ERANGE || *end != '\0') {
|
2013-10-26 16:31:41 +02:00
|
|
|
fprintf(stderr, "-s: Bad option value\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'S':
|
2014-05-14 16:22:23 +02:00
|
|
|
// --deflate-table-size
|
2014-01-18 07:32:50 +01:00
|
|
|
errno = 0;
|
2013-10-28 16:42:08 +01:00
|
|
|
config.deflate_table_size = strtoul(optarg, &end, 10);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (errno == ERANGE || *end != '\0') {
|
2013-10-26 16:31:41 +02:00
|
|
|
fprintf(stderr, "-S: Bad option value\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-10-26 15:46:44 +02:00
|
|
|
break;
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.http1text) {
|
2014-01-11 07:31:36 +01:00
|
|
|
perform_from_http1text();
|
2013-10-26 15:46:44 +02:00
|
|
|
} else {
|
2014-01-11 07:31:36 +01:00
|
|
|
perform();
|
2013-10-26 15:46:44 +02:00
|
|
|
}
|
2014-04-05 13:04:09 +02:00
|
|
|
|
|
|
|
auto comp_ratio = input_sum == 0 ? 0.0 : (double)output_sum / input_sum;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
fprintf(stderr, "Overall: input=%zu output=%zu ratio=%.02f\n", input_sum,
|
|
|
|
output_sum, comp_ratio);
|
2013-10-21 17:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-06-21 07:32:47 +02:00
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
int main(int argc, char **argv) { return nghttp2::main(argc, argv); }
|