From 328ad0a41c6bdf596224ff2e9ab9c0fabde8634d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 09:32:08 +0200 Subject: Adding upstream version 0.8.0. Signed-off-by: Daniel Baumann --- examples/qpack.cc | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 examples/qpack.cc (limited to 'examples/qpack.cc') diff --git a/examples/qpack.cc b/examples/qpack.cc new file mode 100644 index 0000000..1353ad9 --- /dev/null +++ b/examples/qpack.cc @@ -0,0 +1,143 @@ +/* + * nghttp3 + * + * Copyright (c) 2019 nghttp3 contributors + * + * 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 "qpack.h" + +#include +#include +#include + +#include + +#include "qpack_encode.h" +#include "qpack_decode.h" + +namespace nghttp3 { + +Config config{}; + +namespace { +void print_usage() { + std::cerr << "Usage: qpack [OPTIONS] " + << std::endl; +} +} // namespace + +namespace { +void print_help() { + print_usage(); + + std::cerr << R"( + "encode" or "decode" + Path to an input file + Path to an output file +Options: + -h, --help Display this help and exit. + -m, --max-blocked= + The maximum number of streams which are permitted to be blocked. + -s, --max-dtable-size= + The maximum size of dynamic table. + -a, --immediate-ack + Turn on immediate acknowlegement. +)"; +} +} // namespace + +int main(int argc, char **argv) { + for (;;) { + static int flag = 0; + (void)flag; + constexpr static option long_opts[] = { + {"help", no_argument, nullptr, 'h'}, + {"max-blocked", required_argument, nullptr, 'm'}, + {"max-dtable-size", required_argument, nullptr, 's'}, + {"immediate-ack", no_argument, nullptr, 'a'}, + {nullptr, 0, nullptr, 0}, + }; + + auto optidx = 0; + auto c = getopt_long(argc, argv, "hm:s:a", long_opts, &optidx); + if (c == -1) { + break; + } + switch (c) { + case 'h': + // --help + print_help(); + exit(EXIT_SUCCESS); + case 'm': { + // --max-blocked + config.max_blocked = strtoul(optarg, nullptr, 10); + break; + } + case 's': { + // --max-dtable-size + config.max_dtable_size = strtoul(optarg, nullptr, 10); + break; + } + case 'a': + // --immediate-ack + config.immediate_ack = true; + break; + case '?': + print_usage(); + exit(EXIT_FAILURE); + case 0: + break; + default: + break; + }; + } + + if (argc - optind < 3) { + std::cerr << "Too few arguments" << std::endl; + print_usage(); + exit(EXIT_FAILURE); + } + + auto command = std::string_view(argv[optind++]); + auto infile = std::string_view(argv[optind++]); + auto outfile = std::string_view(argv[optind++]); + + int rv; + if (command == "encode") { + rv = encode(outfile, infile); + } else if (command == "decode") { + rv = decode(outfile, infile); + } else { + std::cerr << "Unrecognized command: " << command << std::endl; + print_usage(); + exit(EXIT_FAILURE); + } + + if (rv != 0) { + exit(EXIT_FAILURE); + } + + return 0; +} + +} // namespace nghttp3 + +int main(int argc, char **argv) { return nghttp3::main(argc, argv); } -- cgit v1.2.3