2013-07-21 16:49:12 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-21 16:49:12 +02: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.
|
|
|
|
*/
|
2015-01-10 08:44:09 +01:00
|
|
|
#include "nghttp.h"
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
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
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <fcntl.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_FCNTL_H
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <netinet/in.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_NETINET_IN_H
|
2013-07-21 16:49:12 +02:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2015-02-01 09:59:49 +01:00
|
|
|
#include <iomanip>
|
2013-07-21 16:49:12 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2014-11-19 13:18:31 +01:00
|
|
|
#ifdef HAVE_JANSSON
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <jansson.h>
|
2014-11-19 13:18:31 +01:00
|
|
|
#endif // HAVE_JANSSON
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2013-07-22 15:12:54 +02:00
|
|
|
#include "app_helper.h"
|
2013-07-21 16:49:12 +02:00
|
|
|
#include "HtmlParser.h"
|
|
|
|
#include "util.h"
|
2013-08-03 11:51:01 +02:00
|
|
|
#include "base64.h"
|
2017-04-01 07:47:36 +02:00
|
|
|
#include "tls.h"
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2018-04-14 11:04:32 +02:00
|
|
|
#include "ssl_compat.h"
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
#ifndef O_BINARY
|
2018-06-09 09:21:30 +02:00
|
|
|
# define O_BINARY (0)
|
2013-07-21 16:49:12 +02:00
|
|
|
#endif // O_BINARY
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2015-04-17 14:31:11 +02:00
|
|
|
// The anchor stream nodes when --no-dep is not used. The stream ID =
|
|
|
|
// 1 is excluded since it is used as first stream in upgrade case. We
|
|
|
|
// follows the same dependency anchor nodes as Firefox does.
|
2015-04-16 16:42:36 +02:00
|
|
|
struct Anchor {
|
|
|
|
int32_t stream_id;
|
|
|
|
// stream ID this anchor depends on
|
|
|
|
int32_t dep_stream_id;
|
|
|
|
// .. with this weight.
|
|
|
|
int32_t weight;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is index into anchors. Firefox uses ANCHOR_FOLLOWERS for html
|
|
|
|
// file.
|
2014-12-12 17:19:27 +01:00
|
|
|
enum {
|
2015-04-16 16:42:36 +02:00
|
|
|
ANCHOR_LEADERS,
|
|
|
|
ANCHOR_UNBLOCKED,
|
|
|
|
ANCHOR_BACKGROUND,
|
|
|
|
ANCHOR_SPECULATIVE,
|
|
|
|
ANCHOR_FOLLOWERS,
|
2014-12-12 17:19:27 +01:00
|
|
|
};
|
|
|
|
|
2015-04-16 16:42:36 +02:00
|
|
|
namespace {
|
2015-05-29 15:31:02 +02:00
|
|
|
constexpr auto anchors = std::array<Anchor, 5>{{
|
2017-11-23 06:19:12 +01:00
|
|
|
{3, 0, 201},
|
|
|
|
{5, 0, 101},
|
|
|
|
{7, 0, 1},
|
|
|
|
{9, 7, 1},
|
|
|
|
{11, 3, 1},
|
2016-10-15 11:36:04 +02:00
|
|
|
}};
|
2015-04-16 16:42:36 +02:00
|
|
|
} // namespace
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
Config::Config()
|
2015-10-24 11:11:47 +02:00
|
|
|
: header_table_size(-1),
|
2016-01-27 13:14:07 +01:00
|
|
|
min_header_table_size(std::numeric_limits<uint32_t>::max()),
|
2016-09-17 15:19:50 +02:00
|
|
|
encoder_header_table_size(-1),
|
2016-01-27 13:14:07 +01:00
|
|
|
padding(0),
|
|
|
|
max_concurrent_streams(100),
|
|
|
|
peer_max_concurrent_streams(100),
|
|
|
|
multiply(1),
|
|
|
|
timeout(0.),
|
|
|
|
window_bits(-1),
|
|
|
|
connection_window_bits(-1),
|
|
|
|
verbose(0),
|
2017-01-17 16:29:51 +01:00
|
|
|
port_override(0),
|
2016-01-27 13:14:07 +01:00
|
|
|
null_out(false),
|
|
|
|
remote_name(false),
|
|
|
|
get_assets(false),
|
|
|
|
stat(false),
|
|
|
|
upgrade(false),
|
|
|
|
continuation(false),
|
|
|
|
no_content_length(false),
|
|
|
|
no_dep(false),
|
|
|
|
hexdump(false),
|
2016-03-21 19:34:09 +01:00
|
|
|
no_push(false),
|
2017-04-28 02:53:37 +02:00
|
|
|
expect_continue(false),
|
|
|
|
verify_peer(true) {
|
2015-01-10 08:44:09 +01:00
|
|
|
nghttp2_option_new(&http2_option);
|
|
|
|
nghttp2_option_set_peer_max_concurrent_streams(http2_option,
|
|
|
|
peer_max_concurrent_streams);
|
2016-04-03 15:58:25 +02:00
|
|
|
nghttp2_option_set_builtin_recv_extension_type(http2_option, NGHTTP2_ALTSVC);
|
2018-05-10 16:19:27 +02:00
|
|
|
nghttp2_option_set_builtin_recv_extension_type(http2_option, NGHTTP2_ORIGIN);
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Config::~Config() { nghttp2_option_del(http2_option); }
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-11-15 15:42:26 +01:00
|
|
|
namespace {
|
|
|
|
Config config;
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
namespace {
|
|
|
|
void print_protocol_nego_error() {
|
|
|
|
std::cerr << "[ERROR] HTTP/2 protocol was not selected."
|
|
|
|
<< " (nghttp2 expects " << NGHTTP2_PROTO_VERSION_ID << ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string strip_fragment(const char *raw_uri) {
|
2013-07-21 16:49:12 +02:00
|
|
|
const char *end;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (end = raw_uri; *end && *end != '#'; ++end)
|
|
|
|
;
|
|
|
|
size_t len = end - raw_uri;
|
2013-07-21 16:49:12 +02:00
|
|
|
return std::string(raw_uri, len);
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
Request::Request(const std::string &uri, const http_parser_url &u,
|
|
|
|
const nghttp2_data_provider *data_prd, int64_t data_length,
|
2015-04-17 14:31:11 +02:00
|
|
|
const nghttp2_priority_spec &pri_spec, int level)
|
2016-01-27 13:14:07 +01:00
|
|
|
: uri(uri),
|
|
|
|
u(u),
|
|
|
|
pri_spec(pri_spec),
|
|
|
|
data_length(data_length),
|
|
|
|
data_offset(0),
|
|
|
|
response_len(0),
|
|
|
|
inflater(nullptr),
|
|
|
|
data_prd(data_prd),
|
2016-02-04 15:12:41 +01:00
|
|
|
header_buffer_size(0),
|
2016-01-27 13:14:07 +01:00
|
|
|
stream_id(-1),
|
|
|
|
status(0),
|
|
|
|
level(level),
|
2015-01-10 08:44:09 +01:00
|
|
|
expect_final_response(false) {
|
|
|
|
http2::init_hdidx(res_hdidx);
|
|
|
|
http2::init_hdidx(req_hdidx);
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2017-01-17 16:34:39 +01:00
|
|
|
Request::~Request() { nghttp2_gzip_inflate_del(inflater); }
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
void Request::init_inflater() {
|
|
|
|
int rv;
|
2017-05-18 16:10:44 +02:00
|
|
|
// This is required with --disable-assert.
|
|
|
|
(void)rv;
|
2015-01-10 08:44:09 +01:00
|
|
|
rv = nghttp2_gzip_inflate_new(&inflater);
|
|
|
|
assert(rv == 0);
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:29:51 +01:00
|
|
|
StringRef Request::get_real_scheme() const {
|
|
|
|
return config.scheme_override.empty()
|
|
|
|
? util::get_uri_field(uri.c_str(), u, UF_SCHEMA)
|
|
|
|
: StringRef{config.scheme_override};
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Request::get_real_host() const {
|
|
|
|
return config.host_override.empty()
|
|
|
|
? util::get_uri_field(uri.c_str(), u, UF_HOST)
|
|
|
|
: StringRef{config.host_override};
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Request::get_real_port() const {
|
|
|
|
auto scheme = get_real_scheme();
|
|
|
|
return config.host_override.empty()
|
|
|
|
? util::has_uri_field(u, UF_PORT) ? u.port
|
|
|
|
: scheme == "https" ? 443 : 80
|
|
|
|
: config.port_override == 0 ? scheme == "https" ? 443 : 80
|
|
|
|
: config.port_override;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::init_html_parser() {
|
|
|
|
// We crawl HTML using overridden scheme, host, and port.
|
|
|
|
auto scheme = get_real_scheme();
|
|
|
|
auto host = get_real_host();
|
|
|
|
auto port = get_real_port();
|
|
|
|
auto ipv6_lit =
|
|
|
|
std::find(std::begin(host), std::end(host), ':') != std::end(host);
|
|
|
|
|
|
|
|
auto base_uri = scheme.str();
|
|
|
|
base_uri += "://";
|
|
|
|
if (ipv6_lit) {
|
|
|
|
base_uri += '[';
|
|
|
|
}
|
|
|
|
base_uri += host;
|
|
|
|
if (ipv6_lit) {
|
|
|
|
base_uri += ']';
|
|
|
|
}
|
|
|
|
if (!((scheme == "https" && port == 443) ||
|
|
|
|
(scheme == "http" && port == 80))) {
|
|
|
|
base_uri += ':';
|
|
|
|
base_uri += util::utos(port);
|
|
|
|
}
|
|
|
|
base_uri += util::get_uri_field(uri.c_str(), u, UF_PATH);
|
|
|
|
if (util::has_uri_field(u, UF_QUERY)) {
|
|
|
|
base_uri += '?';
|
|
|
|
base_uri += util::get_uri_field(uri.c_str(), u, UF_QUERY);
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
html_parser = std::make_unique<HtmlParser>(base_uri);
|
2017-01-17 16:29:51 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
|
|
|
|
int Request::update_html_parser(const uint8_t *data, size_t len, int fin) {
|
|
|
|
if (!html_parser) {
|
|
|
|
return 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
return html_parser->parse_chunk(reinterpret_cast<const char *>(data), len,
|
|
|
|
fin);
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
std::string Request::make_reqpath() const {
|
|
|
|
std::string path = util::has_uri_field(u, UF_PATH)
|
2016-03-08 13:38:15 +01:00
|
|
|
? util::get_uri_field(uri.c_str(), u, UF_PATH).str()
|
2015-01-10 08:44:09 +01:00
|
|
|
: "/";
|
|
|
|
if (util::has_uri_field(u, UF_QUERY)) {
|
2015-11-27 16:03:16 +01:00
|
|
|
path += '?';
|
2015-01-10 08:44:09 +01:00
|
|
|
path.append(uri.c_str() + u.field_data[UF_QUERY].off,
|
|
|
|
u.field_data[UF_QUERY].len);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
return path;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-06-18 13:00:02 +02:00
|
|
|
namespace {
|
|
|
|
// Perform special handling |host| if it is IPv6 literal and includes
|
|
|
|
// zone ID per RFC 6874.
|
2016-03-08 13:38:15 +01:00
|
|
|
std::string decode_host(const StringRef &host) {
|
2015-06-18 13:00:02 +02:00
|
|
|
auto zone_start = std::find(std::begin(host), std::end(host), '%');
|
|
|
|
if (zone_start == std::end(host) ||
|
|
|
|
!util::ipv6_numeric_addr(
|
|
|
|
std::string(std::begin(host), zone_start).c_str())) {
|
2016-03-08 13:38:15 +01:00
|
|
|
return host.str();
|
2015-06-18 13:00:02 +02:00
|
|
|
}
|
|
|
|
// case: ::1%
|
|
|
|
if (zone_start + 1 == std::end(host)) {
|
2016-03-08 13:38:15 +01:00
|
|
|
return StringRef{host.c_str(), host.size() - 1}.str();
|
2015-06-18 13:00:02 +02:00
|
|
|
}
|
|
|
|
// case: ::1%12 or ::1%1
|
|
|
|
if (zone_start + 3 >= std::end(host)) {
|
2016-03-08 13:38:15 +01:00
|
|
|
return host.str();
|
2015-06-18 13:00:02 +02:00
|
|
|
}
|
|
|
|
// If we see "%25", followed by more characters, then decode %25 as
|
|
|
|
// '%'.
|
|
|
|
auto zone_id_src = (*(zone_start + 1) == '2' && *(zone_start + 2) == '5')
|
|
|
|
? zone_start + 3
|
|
|
|
: zone_start + 1;
|
2015-11-27 16:14:11 +01:00
|
|
|
auto zone_id = util::percent_decode(zone_id_src, std::end(host));
|
2016-03-08 13:38:15 +01:00
|
|
|
auto res = std::string(std::begin(host), zone_start + 1);
|
|
|
|
res += zone_id;
|
|
|
|
return res;
|
2015-06-18 13:00:02 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-04-17 14:31:11 +02:00
|
|
|
namespace {
|
|
|
|
nghttp2_priority_spec resolve_dep(int res_type) {
|
2015-01-10 08:44:09 +01:00
|
|
|
nghttp2_priority_spec pri_spec;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.no_dep) {
|
2015-04-17 14:31:11 +02:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec);
|
2014-04-14 16:53:54 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return pri_spec;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-04-17 14:31:11 +02:00
|
|
|
int32_t anchor_id;
|
|
|
|
int32_t weight;
|
|
|
|
switch (res_type) {
|
|
|
|
case REQ_CSS:
|
|
|
|
case REQ_JS:
|
|
|
|
anchor_id = anchors[ANCHOR_LEADERS].stream_id;
|
2016-08-11 14:59:34 +02:00
|
|
|
weight = 32;
|
2015-04-17 14:31:11 +02:00
|
|
|
break;
|
|
|
|
case REQ_UNBLOCK_JS:
|
|
|
|
anchor_id = anchors[ANCHOR_UNBLOCKED].stream_id;
|
2016-08-11 14:59:34 +02:00
|
|
|
weight = 32;
|
2015-04-17 14:31:11 +02:00
|
|
|
break;
|
|
|
|
case REQ_IMG:
|
|
|
|
anchor_id = anchors[ANCHOR_FOLLOWERS].stream_id;
|
|
|
|
weight = 12;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
anchor_id = anchors[ANCHOR_FOLLOWERS].stream_id;
|
2016-08-11 14:59:34 +02:00
|
|
|
weight = 32;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:31:11 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, anchor_id, weight, 0);
|
2015-01-10 08:44:09 +01:00
|
|
|
return pri_spec;
|
|
|
|
}
|
2015-04-17 14:31:11 +02:00
|
|
|
} // namespace
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
bool Request::is_ipv6_literal_addr() const {
|
|
|
|
if (util::has_uri_field(u, UF_HOST)) {
|
|
|
|
return memchr(uri.c_str() + u.field_data[UF_HOST].off, ':',
|
|
|
|
u.field_data[UF_HOST].len);
|
|
|
|
} else {
|
|
|
|
return false;
|
2015-01-04 15:22:39 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
|
2016-02-21 08:44:00 +01:00
|
|
|
Headers::value_type *Request::get_res_header(int32_t token) {
|
2015-01-10 08:44:09 +01:00
|
|
|
auto idx = res_hdidx[token];
|
|
|
|
if (idx == -1) {
|
|
|
|
return nullptr;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
return &res_nva[idx];
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2016-02-21 08:44:00 +01:00
|
|
|
Headers::value_type *Request::get_req_header(int32_t token) {
|
2015-01-10 08:44:09 +01:00
|
|
|
auto idx = req_hdidx[token];
|
|
|
|
if (idx == -1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &req_nva[idx];
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
void Request::record_request_start_time() {
|
2015-02-01 09:12:31 +01:00
|
|
|
timing.state = RequestState::ON_REQUEST;
|
2015-02-17 16:38:20 +01:00
|
|
|
timing.request_start_time = get_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
void Request::record_response_start_time() {
|
2015-02-01 09:12:31 +01:00
|
|
|
timing.state = RequestState::ON_RESPONSE;
|
2015-02-17 16:38:20 +01:00
|
|
|
timing.response_start_time = get_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
void Request::record_response_end_time() {
|
2015-02-01 09:12:31 +01:00
|
|
|
timing.state = RequestState::ON_COMPLETE;
|
2015-02-17 16:38:20 +01:00
|
|
|
timing.response_end_time = get_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2016-03-22 20:41:26 +01:00
|
|
|
namespace {
|
|
|
|
void continue_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto client = static_cast<HttpClient *>(ev_userdata(loop));
|
|
|
|
auto req = static_cast<Request *>(w->data);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = nghttp2_submit_data(client->session, NGHTTP2_FLAG_END_STREAM,
|
|
|
|
req->stream_id, req->data_prd);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
std::cerr << "[ERROR] nghttp2_submit_data() returned error: "
|
|
|
|
<< nghttp2_strerror(error) << std::endl;
|
|
|
|
nghttp2_submit_rst_stream(client->session, NGHTTP2_FLAG_NONE,
|
|
|
|
req->stream_id, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
client->signal_write();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-03-31 13:06:14 +02:00
|
|
|
ContinueTimer::ContinueTimer(struct ev_loop *loop, Request *req) : loop(loop) {
|
2016-03-22 20:41:26 +01:00
|
|
|
ev_timer_init(&timer, continue_timeout_cb, 1., 0.);
|
2016-03-29 22:11:27 +02:00
|
|
|
timer.data = req;
|
2016-03-22 20:41:26 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 13:06:14 +02:00
|
|
|
ContinueTimer::~ContinueTimer() { stop(); }
|
2016-03-22 20:41:26 +01:00
|
|
|
|
2016-03-31 13:06:14 +02:00
|
|
|
void ContinueTimer::start() { ev_timer_start(loop, &timer); }
|
2016-03-22 20:41:26 +01:00
|
|
|
|
2016-03-31 13:06:14 +02:00
|
|
|
void ContinueTimer::stop() { ev_timer_stop(loop, &timer); }
|
2016-03-22 20:41:26 +01:00
|
|
|
|
|
|
|
void ContinueTimer::dispatch_continue() {
|
2016-03-23 17:09:13 +01:00
|
|
|
// Only dispatch the timeout callback if it hasn't already been called.
|
|
|
|
if (ev_is_active(&timer)) {
|
|
|
|
ev_feed_event(loop, &timer, 0);
|
|
|
|
}
|
2016-03-22 20:41:26 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
namespace {
|
|
|
|
int htp_msg_begincb(http_parser *htp) {
|
|
|
|
if (config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " HTTP Upgrade response" << std::endl;
|
2013-10-29 13:34:22 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
int htp_msg_completecb(http_parser *htp) {
|
|
|
|
auto client = static_cast<HttpClient *>(htp->data);
|
2017-06-27 18:00:01 +02:00
|
|
|
client->upgrade_response_status_code = htp->status_code;
|
2015-01-10 08:44:09 +01:00
|
|
|
client->upgrade_response_complete = true;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-03 11:51:01 +02:00
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
2017-01-09 09:11:37 +01:00
|
|
|
constexpr http_parser_settings htp_hooks = {
|
2015-01-10 08:44:09 +01:00
|
|
|
htp_msg_begincb, // http_cb on_message_begin;
|
|
|
|
nullptr, // http_data_cb on_url;
|
2017-06-27 18:00:01 +02:00
|
|
|
nullptr, // http_data_cb on_status;
|
2015-01-10 08:44:09 +01:00
|
|
|
nullptr, // http_data_cb on_header_field;
|
|
|
|
nullptr, // http_data_cb on_header_value;
|
|
|
|
nullptr, // http_cb on_headers_complete;
|
|
|
|
nullptr, // http_data_cb on_body;
|
|
|
|
htp_msg_completecb // http_cb on_message_complete;
|
|
|
|
};
|
2013-07-21 16:49:12 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
int submit_request(HttpClient *client, const Headers &headers, Request *req) {
|
|
|
|
auto path = req->make_reqpath();
|
|
|
|
auto scheme = util::get_uri_field(req->uri.c_str(), req->u, UF_SCHEMA);
|
|
|
|
auto build_headers = Headers{{":method", req->data_prd ? "POST" : "GET"},
|
|
|
|
{":path", path},
|
2016-03-08 13:38:15 +01:00
|
|
|
{":scheme", scheme.str()},
|
2015-01-10 08:44:09 +01:00
|
|
|
{":authority", client->hostport},
|
|
|
|
{"accept", "*/*"},
|
|
|
|
{"accept-encoding", "gzip, deflate"},
|
|
|
|
{"user-agent", "nghttp2/" NGHTTP2_VERSION}};
|
2016-03-22 20:41:26 +01:00
|
|
|
bool expect_continue = false;
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.continuation) {
|
|
|
|
for (size_t i = 0; i < 6; ++i) {
|
|
|
|
build_headers.emplace_back("continuation-test-" + util::utos(i + 1),
|
2015-06-21 07:32:47 +02:00
|
|
|
std::string(4_k, '-'));
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 22:43:48 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto num_initial_headers = build_headers.size();
|
2016-03-21 22:43:48 +01:00
|
|
|
|
|
|
|
if (req->data_prd) {
|
|
|
|
if (!config.no_content_length) {
|
2016-03-31 13:06:14 +02:00
|
|
|
build_headers.emplace_back("content-length",
|
|
|
|
util::utos(req->data_length));
|
2016-03-21 22:43:48 +01:00
|
|
|
}
|
|
|
|
if (config.expect_continue) {
|
2016-03-22 20:41:26 +01:00
|
|
|
expect_continue = true;
|
2016-03-21 22:43:48 +01:00
|
|
|
build_headers.emplace_back("expect", "100-continue");
|
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2016-03-21 22:43:48 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
for (auto &kv : headers) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < num_initial_headers; ++i) {
|
|
|
|
if (kv.name == build_headers[i].name) {
|
|
|
|
build_headers[i].value = kv.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < num_initial_headers) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
build_headers.emplace_back(kv.name, kv.value, kv.no_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nva = std::vector<nghttp2_nv>();
|
|
|
|
nva.reserve(build_headers.size());
|
|
|
|
|
|
|
|
for (auto &kv : build_headers) {
|
|
|
|
nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index));
|
|
|
|
}
|
|
|
|
|
2015-11-07 04:47:26 +01:00
|
|
|
auto method = http2::get_header(build_headers, ":method");
|
|
|
|
assert(method);
|
|
|
|
|
|
|
|
req->method = method->value;
|
|
|
|
|
2015-03-08 09:52:36 +01:00
|
|
|
std::string trailer_names;
|
|
|
|
if (!config.trailer.empty()) {
|
|
|
|
trailer_names = config.trailer[0].name;
|
|
|
|
for (size_t i = 1; i < config.trailer.size(); ++i) {
|
|
|
|
trailer_names += ", ";
|
|
|
|
trailer_names += config.trailer[i].name;
|
|
|
|
}
|
|
|
|
nva.push_back(http2::make_nv_ls("trailer", trailer_names));
|
|
|
|
}
|
|
|
|
|
2016-03-21 22:43:48 +01:00
|
|
|
int32_t stream_id;
|
|
|
|
|
2016-03-22 20:41:26 +01:00
|
|
|
if (expect_continue) {
|
2016-03-21 22:43:48 +01:00
|
|
|
stream_id = nghttp2_submit_headers(client->session, 0, -1, &req->pri_spec,
|
2016-03-31 13:06:14 +02:00
|
|
|
nva.data(), nva.size(), req);
|
2016-03-21 22:43:48 +01:00
|
|
|
} else {
|
|
|
|
stream_id =
|
|
|
|
nghttp2_submit_request(client->session, &req->pri_spec, nva.data(),
|
|
|
|
nva.size(), req->data_prd, req);
|
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (stream_id < 0) {
|
2016-03-21 22:43:48 +01:00
|
|
|
std::cerr << "[ERROR] nghttp2_submit_"
|
2016-03-22 20:41:26 +01:00
|
|
|
<< (expect_continue ? "headers" : "request")
|
2016-03-31 13:06:14 +02:00
|
|
|
<< "() returned error: " << nghttp2_strerror(stream_id)
|
|
|
|
<< std::endl;
|
2015-01-10 08:44:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->stream_id = stream_id;
|
2015-03-28 12:04:23 +01:00
|
|
|
client->request_done(req);
|
2015-01-10 08:44:09 +01:00
|
|
|
|
|
|
|
req->req_nva = std::move(build_headers);
|
|
|
|
|
2016-03-22 20:41:26 +01:00
|
|
|
if (expect_continue) {
|
2018-10-15 16:02:44 +02:00
|
|
|
auto timer = std::make_unique<ContinueTimer>(client->loop, req);
|
2016-03-29 22:11:27 +02:00
|
|
|
req->continue_timer = std::move(timer);
|
2016-03-22 20:41:26 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
void readcb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
auto client = static_cast<HttpClient *>(w->data);
|
|
|
|
if (client->do_read() != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
void writecb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
auto client = static_cast<HttpClient *>(w->data);
|
|
|
|
auto rv = client->do_write();
|
|
|
|
if (rv == HttpClient::ERR_CONNECT_FAIL) {
|
2015-03-28 12:04:23 +01:00
|
|
|
client->connect_fail();
|
2015-01-10 08:44:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
}
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
} // namespace
|
|
|
|
|
2013-10-27 15:02:39 +01:00
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto client = static_cast<HttpClient *>(w->data);
|
|
|
|
std::cerr << "[ERROR] Timeout" << std::endl;
|
|
|
|
client->disconnect();
|
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
void settings_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto client = static_cast<HttpClient *>(w->data);
|
|
|
|
ev_timer_stop(loop, w);
|
2014-01-22 16:29:09 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
nghttp2_session_terminate_session(client->session, NGHTTP2_SETTINGS_TIMEOUT);
|
2014-03-15 07:32:38 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
client->signal_write();
|
|
|
|
}
|
|
|
|
} // namespace
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
HttpClient::HttpClient(const nghttp2_session_callbacks *callbacks,
|
|
|
|
struct ev_loop *loop, SSL_CTX *ssl_ctx)
|
2016-04-21 16:30:35 +02:00
|
|
|
: wb(&mcpool),
|
|
|
|
session(nullptr),
|
2016-01-27 13:14:07 +01:00
|
|
|
callbacks(callbacks),
|
|
|
|
loop(loop),
|
|
|
|
ssl_ctx(ssl_ctx),
|
|
|
|
ssl(nullptr),
|
|
|
|
addrs(nullptr),
|
|
|
|
next_addr(nullptr),
|
|
|
|
cur_addr(nullptr),
|
|
|
|
complete(0),
|
|
|
|
success(0),
|
|
|
|
settings_payloadlen(0),
|
|
|
|
state(ClientState::IDLE),
|
|
|
|
upgrade_response_status_code(0),
|
|
|
|
fd(-1),
|
2015-01-10 08:44:09 +01:00
|
|
|
upgrade_response_complete(false) {
|
|
|
|
ev_io_init(&wev, writecb, 0, EV_WRITE);
|
|
|
|
ev_io_init(&rev, readcb, 0, EV_READ);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
wev.data = this;
|
|
|
|
rev.data = this;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_init(&wt, timeoutcb, 0., config.timeout);
|
|
|
|
ev_timer_init(&rt, timeoutcb, 0., config.timeout);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
wt.data = this;
|
|
|
|
rt.data = this;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_init(&settings_timer, settings_timeout_cb, 0., 10.);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
settings_timer.data = this;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
HttpClient::~HttpClient() {
|
|
|
|
disconnect();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (addrs) {
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
addrs = nullptr;
|
|
|
|
next_addr = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
bool HttpClient::need_upgrade() const {
|
|
|
|
return config.upgrade && scheme == "http";
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::resolve_host(const std::string &host, uint16_t port) {
|
|
|
|
int rv;
|
|
|
|
this->host = host;
|
2015-07-19 10:55:37 +02:00
|
|
|
addrinfo hints{};
|
2015-01-10 08:44:09 +01:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
|
|
|
rv = getaddrinfo(host.c_str(), util::utos(port).c_str(), &hints, &addrs);
|
|
|
|
if (rv != 0) {
|
|
|
|
std::cerr << "[ERROR] getaddrinfo() failed: " << gai_strerror(rv)
|
|
|
|
<< std::endl;
|
|
|
|
return -1;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
if (addrs == nullptr) {
|
|
|
|
std::cerr << "[ERROR] No address returned" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
next_addr = addrs;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2017-04-01 10:49:57 +02:00
|
|
|
namespace {
|
|
|
|
// Just returns 1 to continue handshake.
|
|
|
|
int verify_cb(int preverify_ok, X509_STORE_CTX *ctx) { return 1; }
|
|
|
|
} // namespace
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::initiate_connection() {
|
|
|
|
int rv;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
cur_addr = nullptr;
|
|
|
|
while (next_addr) {
|
|
|
|
cur_addr = next_addr;
|
|
|
|
next_addr = next_addr->ai_next;
|
|
|
|
fd = util::create_nonblock_socket(cur_addr->ai_family);
|
|
|
|
if (fd == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (ssl_ctx) {
|
|
|
|
// We are establishing TLS connection.
|
|
|
|
ssl = SSL_new(ssl_ctx);
|
|
|
|
if (!ssl) {
|
|
|
|
std::cerr << "[ERROR] SSL_new() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
SSL_set_fd(ssl, fd);
|
|
|
|
SSL_set_connect_state(ssl);
|
|
|
|
|
2015-04-09 17:15:01 +02:00
|
|
|
// If the user overrode the :authority or host header, use that
|
|
|
|
// value for the SNI extension
|
2017-01-17 16:29:51 +01:00
|
|
|
const auto &host_string =
|
|
|
|
config.host_override.empty() ? host : config.host_override;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2018-04-14 11:04:32 +02:00
|
|
|
#if LIBRESSL_2_7_API || \
|
|
|
|
(!LIBRESSL_IN_USE && OPENSSL_VERSION_NUMBER >= 0x10002000L) || \
|
2017-04-01 10:49:57 +02:00
|
|
|
defined(OPENSSL_IS_BORINGSSL)
|
|
|
|
auto param = SSL_get0_param(ssl);
|
|
|
|
X509_VERIFY_PARAM_set_hostflags(param, 0);
|
|
|
|
X509_VERIFY_PARAM_set1_host(param, host_string.c_str(),
|
|
|
|
host_string.size());
|
2018-04-14 11:04:32 +02:00
|
|
|
#endif // LIBRESSL_2_7_API || (!LIBRESSL_IN_USE &&
|
|
|
|
// OPENSSL_VERSION_NUMBER >= 0x10002000L) ||
|
|
|
|
// defined(OPENSSL_IS_BORINGSSL)
|
2017-04-01 10:49:57 +02:00
|
|
|
SSL_set_verify(ssl, SSL_VERIFY_PEER, verify_cb);
|
|
|
|
|
2017-01-17 16:29:51 +01:00
|
|
|
if (!util::numeric_host(host_string.c_str())) {
|
|
|
|
SSL_set_tlsext_host_name(ssl, host_string.c_str());
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
rv = connect(fd, cur_addr->ai_addr, cur_addr->ai_addrlen);
|
|
|
|
|
|
|
|
if (rv != 0 && errno != EINPROGRESS) {
|
|
|
|
if (ssl) {
|
|
|
|
SSL_free(ssl);
|
|
|
|
ssl = nullptr;
|
|
|
|
}
|
2013-12-22 11:23:01 +01:00
|
|
|
close(fd);
|
2014-12-27 18:59:06 +01:00
|
|
|
fd = -1;
|
2015-01-10 08:44:09 +01:00
|
|
|
continue;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
break;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
writefn = &HttpClient::connected;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (need_upgrade()) {
|
|
|
|
on_readfn = &HttpClient::on_upgrade_read;
|
|
|
|
on_writefn = &HttpClient::on_upgrade_connect;
|
|
|
|
} else {
|
|
|
|
on_readfn = &HttpClient::on_read;
|
|
|
|
on_writefn = &HttpClient::on_write;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_set(&rev, fd, EV_READ);
|
|
|
|
ev_io_set(&wev, fd, EV_WRITE);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_start(loop, &wev);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_again(loop, &wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
void HttpClient::disconnect() {
|
2015-02-01 09:12:31 +01:00
|
|
|
state = ClientState::IDLE;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-03-29 22:11:27 +02:00
|
|
|
for (auto req = std::begin(reqvec); req != std::end(reqvec); ++req) {
|
2016-03-30 01:02:10 +02:00
|
|
|
if ((*req)->continue_timer) {
|
|
|
|
(*req)->continue_timer->stop();
|
|
|
|
}
|
2016-03-29 22:11:27 +02:00
|
|
|
}
|
2016-03-22 20:41:26 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_stop(loop, &settings_timer);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_stop(loop, &rt);
|
|
|
|
ev_timer_stop(loop, &wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_stop(loop, &rev);
|
|
|
|
ev_io_stop(loop, &wev);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
nghttp2_session_del(session);
|
|
|
|
session = nullptr;
|
|
|
|
|
|
|
|
if (ssl) {
|
2017-01-02 03:48:38 +01:00
|
|
|
SSL_set_shutdown(ssl, SSL_get_shutdown(ssl) | SSL_RECEIVED_SHUTDOWN);
|
2015-01-10 08:44:09 +01:00
|
|
|
ERR_clear_error();
|
|
|
|
SSL_shutdown(ssl);
|
|
|
|
SSL_free(ssl);
|
|
|
|
ssl = nullptr;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (fd != -1) {
|
|
|
|
shutdown(fd, SHUT_WR);
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::read_clear() {
|
|
|
|
ev_timer_again(loop, &rt);
|
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
std::array<uint8_t, 8_k> buf;
|
2015-01-10 08:44:09 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ssize_t nread;
|
2015-02-05 16:06:01 +01:00
|
|
|
while ((nread = read(fd, buf.data(), buf.size())) == -1 && errno == EINTR)
|
2015-01-10 08:44:09 +01:00
|
|
|
;
|
|
|
|
if (nread == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
return 0;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
return -1;
|
2013-12-22 11:23:01 +01:00
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (nread == 0) {
|
|
|
|
return -1;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
if (on_readfn(*this, buf.data(), nread) != 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
return -1;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::write_clear() {
|
|
|
|
ev_timer_again(loop, &rt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-04-21 16:30:35 +02:00
|
|
|
std::array<struct iovec, 2> iov;
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
for (;;) {
|
|
|
|
if (on_writefn(*this) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-21 16:30:35 +02:00
|
|
|
|
|
|
|
auto iovcnt = wb.riovec(iov.data(), iov.size());
|
|
|
|
|
|
|
|
if (iovcnt == 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
break;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2016-04-21 16:30:35 +02:00
|
|
|
|
|
|
|
ssize_t nwrite;
|
|
|
|
while ((nwrite = writev(fd, iov.data(), iovcnt)) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (nwrite == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
ev_io_start(loop, &wev);
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.drain(nwrite);
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_stop(loop, &wev);
|
|
|
|
ev_timer_stop(loop, &wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::noop() { return 0; }
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
void HttpClient::connect_fail() {
|
2015-02-01 09:12:31 +01:00
|
|
|
if (state == ClientState::IDLE) {
|
2015-01-10 08:44:09 +01:00
|
|
|
std::cerr << "[ERROR] Could not connect to the address "
|
2015-02-26 15:59:53 +01:00
|
|
|
<< util::numeric_name(cur_addr->ai_addr, cur_addr->ai_addrlen)
|
|
|
|
<< std::endl;
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
|
|
|
auto cur_state = state;
|
|
|
|
disconnect();
|
2015-02-01 09:12:31 +01:00
|
|
|
if (cur_state == ClientState::IDLE) {
|
2015-01-10 08:44:09 +01:00
|
|
|
if (initiate_connection() == 0) {
|
2015-02-26 15:59:53 +01:00
|
|
|
std::cerr << "Trying next address "
|
|
|
|
<< util::numeric_name(cur_addr->ai_addr, cur_addr->ai_addrlen)
|
2015-01-10 08:44:09 +01:00
|
|
|
<< std::endl;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::connected() {
|
|
|
|
if (!util::check_socket_connected(fd)) {
|
|
|
|
return ERR_CONNECT_FAIL;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " Connected" << std::endl;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-01 09:12:31 +01:00
|
|
|
state = ClientState::CONNECTED;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_start(loop, &rev);
|
|
|
|
ev_io_stop(loop, &wev);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_again(loop, &rt);
|
|
|
|
ev_timer_stop(loop, &wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (ssl) {
|
|
|
|
readfn = &HttpClient::tls_handshake;
|
|
|
|
writefn = &HttpClient::tls_handshake;
|
|
|
|
|
|
|
|
return do_write();
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
readfn = &HttpClient::read_clear;
|
|
|
|
writefn = &HttpClient::write_clear;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (need_upgrade()) {
|
2018-10-15 16:02:44 +02:00
|
|
|
htp = std::make_unique<http_parser>();
|
2015-01-10 08:44:09 +01:00
|
|
|
http_parser_init(htp.get(), HTTP_RESPONSE);
|
|
|
|
htp->data = this;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return do_write();
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
if (connection_made() != 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
namespace {
|
|
|
|
size_t populate_settings(nghttp2_settings_entry *iv) {
|
|
|
|
size_t niv = 2;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
|
2015-07-07 17:22:21 +02:00
|
|
|
iv[0].value = config.max_concurrent_streams;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
|
|
if (config.window_bits != -1) {
|
|
|
|
iv[1].value = (1 << config.window_bits) - 1;
|
|
|
|
} else {
|
|
|
|
iv[1].value = NGHTTP2_INITIAL_WINDOW_SIZE;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.header_table_size >= 0) {
|
2015-10-24 11:11:47 +02:00
|
|
|
if (config.min_header_table_size < config.header_table_size) {
|
|
|
|
iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
|
|
|
|
iv[niv].value = config.min_header_table_size;
|
|
|
|
++niv;
|
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
|
|
|
|
iv[niv].value = config.header_table_size;
|
|
|
|
++niv;
|
|
|
|
}
|
2015-04-17 16:34:28 +02:00
|
|
|
|
|
|
|
if (config.no_push) {
|
|
|
|
iv[niv].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
|
|
|
|
iv[niv].value = 0;
|
|
|
|
++niv;
|
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return niv;
|
|
|
|
}
|
|
|
|
} // namespace
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::on_upgrade_connect() {
|
|
|
|
ssize_t rv;
|
2015-02-17 16:38:20 +01:00
|
|
|
record_connect_end_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
assert(!reqvec.empty());
|
2015-04-17 16:34:28 +02:00
|
|
|
std::array<nghttp2_settings_entry, 16> iv;
|
2015-02-05 16:06:01 +01:00
|
|
|
size_t niv = populate_settings(iv.data());
|
|
|
|
assert(settings_payload.size() >= 8 * niv);
|
|
|
|
rv = nghttp2_pack_settings_payload(settings_payload.data(),
|
|
|
|
settings_payload.size(), iv.data(), niv);
|
2015-01-10 08:44:09 +01:00
|
|
|
if (rv < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
settings_payloadlen = rv;
|
2015-02-05 16:06:01 +01:00
|
|
|
auto token68 =
|
|
|
|
base64::encode(std::begin(settings_payload),
|
|
|
|
std::begin(settings_payload) + settings_payloadlen);
|
2015-01-10 08:44:09 +01:00
|
|
|
util::to_token68(token68);
|
2015-11-07 04:47:26 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
std::string req;
|
|
|
|
if (reqvec[0]->data_prd) {
|
|
|
|
// If the request contains upload data, use OPTIONS * to upgrade
|
|
|
|
req = "OPTIONS *";
|
|
|
|
} else {
|
2015-11-07 03:14:28 +01:00
|
|
|
auto meth = std::find_if(
|
|
|
|
std::begin(config.headers), std::end(config.headers),
|
2015-11-07 04:16:22 +01:00
|
|
|
[](const Header &kv) { return util::streq_l(":method", kv.name); });
|
2015-11-07 03:14:28 +01:00
|
|
|
|
|
|
|
if (meth == std::end(config.headers)) {
|
|
|
|
req = "GET ";
|
2015-11-07 04:47:26 +01:00
|
|
|
reqvec[0]->method = "GET";
|
2015-11-07 03:14:28 +01:00
|
|
|
} else {
|
|
|
|
req = (*meth).value;
|
2015-11-27 16:03:16 +01:00
|
|
|
req += ' ';
|
2015-11-07 04:47:26 +01:00
|
|
|
reqvec[0]->method = (*meth).value;
|
2015-11-07 03:14:28 +01:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
req += reqvec[0]->make_reqpath();
|
|
|
|
}
|
2015-02-19 17:16:49 +01:00
|
|
|
|
2015-11-07 04:47:26 +01:00
|
|
|
auto headers = Headers{{"host", hostport},
|
|
|
|
{"connection", "Upgrade, HTTP2-Settings"},
|
|
|
|
{"upgrade", NGHTTP2_CLEARTEXT_PROTO_VERSION_ID},
|
|
|
|
{"http2-settings", token68},
|
|
|
|
{"accept", "*/*"},
|
|
|
|
{"user-agent", "nghttp2/" NGHTTP2_VERSION}};
|
2015-02-19 17:16:49 +01:00
|
|
|
auto initial_headerslen = headers.size();
|
|
|
|
|
|
|
|
for (auto &kv : config.headers) {
|
|
|
|
size_t i;
|
2015-11-07 04:47:26 +01:00
|
|
|
if (kv.name.empty() || kv.name[0] == ':') {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-19 17:16:49 +01:00
|
|
|
for (i = 0; i < initial_headerslen; ++i) {
|
2015-11-07 04:47:26 +01:00
|
|
|
if (kv.name == headers[i].name) {
|
2015-02-19 17:16:49 +01:00
|
|
|
headers[i].value = kv.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < initial_headerslen) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-07 04:47:26 +01:00
|
|
|
headers.emplace_back(kv.name, kv.value, kv.no_index);
|
2015-02-19 17:16:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
req += " HTTP/1.1\r\n";
|
|
|
|
|
|
|
|
for (auto &kv : headers) {
|
|
|
|
req += kv.name;
|
|
|
|
req += ": ";
|
|
|
|
req += kv.value;
|
|
|
|
req += "\r\n";
|
|
|
|
}
|
|
|
|
req += "\r\n";
|
2015-01-10 08:44:09 +01:00
|
|
|
|
2016-04-21 16:30:35 +02:00
|
|
|
wb.append(req);
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " HTTP Upgrade request\n" << req << std::endl;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-02-01 08:58:58 +01:00
|
|
|
if (!reqvec[0]->data_prd) {
|
2015-11-07 04:47:26 +01:00
|
|
|
// record request time if this is a part of real request.
|
2015-02-17 16:38:20 +01:00
|
|
|
reqvec[0]->record_request_start_time();
|
2015-11-07 04:47:26 +01:00
|
|
|
reqvec[0]->req_nva = std::move(headers);
|
2015-02-01 08:58:58 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
on_writefn = &HttpClient::noop;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
signal_write();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpClient::on_upgrade_read(const uint8_t *data, size_t len) {
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
auto nread = http_parser_execute(htp.get(), &htp_hooks,
|
|
|
|
reinterpret_cast<const char *>(data), len);
|
|
|
|
|
|
|
|
if (config.verbose) {
|
|
|
|
std::cout.write(reinterpret_cast<const char *>(data), nread);
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto htperr = HTTP_PARSER_ERRNO(htp.get());
|
|
|
|
|
|
|
|
if (htperr != HPE_OK) {
|
|
|
|
std::cerr << "[ERROR] Failed to parse HTTP Upgrade response header: "
|
|
|
|
<< "(" << http_errno_name(htperr) << ") "
|
|
|
|
<< http_errno_description(htperr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!upgrade_response_complete) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.verbose) {
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upgrade_response_status_code != 101) {
|
|
|
|
std::cerr << "[ERROR] HTTP Upgrade failed" << std::endl;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " HTTP Upgrade success" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_readfn = &HttpClient::on_read;
|
|
|
|
on_writefn = &HttpClient::on_write;
|
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
rv = connection_made();
|
2015-01-10 08:44:09 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read remaining data in the buffer because it is not notified
|
|
|
|
// callback anymore.
|
|
|
|
rv = on_readfn(*this, data + nread, len - nread);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpClient::do_read() { return readfn(*this); }
|
|
|
|
int HttpClient::do_write() { return writefn(*this); }
|
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
int HttpClient::connection_made() {
|
2015-01-10 08:44:09 +01:00
|
|
|
int rv;
|
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
if (!need_upgrade()) {
|
|
|
|
record_connect_end_time();
|
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (ssl) {
|
|
|
|
// Check NPN or ALPN result
|
|
|
|
const unsigned char *next_proto = nullptr;
|
|
|
|
unsigned int next_proto_len;
|
2018-03-25 18:27:23 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2015-01-10 08:44:09 +01:00
|
|
|
SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
|
2018-04-03 14:39:44 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2015-01-10 08:44:09 +01:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if (next_proto) {
|
2016-03-24 16:02:07 +01:00
|
|
|
auto proto = StringRef{next_proto, next_proto_len};
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.verbose) {
|
2016-03-24 16:02:07 +01:00
|
|
|
std::cout << "The negotiated protocol: " << proto << std::endl;
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2016-03-24 16:02:07 +01:00
|
|
|
if (!util::check_h2_is_selected(proto)) {
|
2015-01-10 08:44:09 +01:00
|
|
|
next_proto = nullptr;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2015-01-10 08:44:09 +01:00
|
|
|
SSL_get0_alpn_selected(ssl, &next_proto, &next_proto_len);
|
2014-12-27 18:59:06 +01:00
|
|
|
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2015-01-10 08:44:09 +01:00
|
|
|
break;
|
2014-12-27 18:59:06 +01:00
|
|
|
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
if (!next_proto) {
|
|
|
|
print_protocol_nego_error();
|
|
|
|
return -1;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
rv = nghttp2_session_client_new2(&session, callbacks, this,
|
|
|
|
config.http2_option);
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (need_upgrade()) {
|
|
|
|
// Adjust stream user-data depending on the existence of upload
|
|
|
|
// data
|
|
|
|
Request *stream_user_data = nullptr;
|
|
|
|
if (!reqvec[0]->data_prd) {
|
|
|
|
stream_user_data = reqvec[0].get();
|
|
|
|
}
|
2015-11-07 04:16:22 +01:00
|
|
|
// If HEAD is used, that is only when user specified it with -H
|
|
|
|
// option.
|
2015-11-07 04:47:26 +01:00
|
|
|
auto head_request = stream_user_data && stream_user_data->method == "HEAD";
|
2015-11-07 04:16:22 +01:00
|
|
|
rv = nghttp2_session_upgrade2(session, settings_payload.data(),
|
|
|
|
settings_payloadlen, head_request,
|
|
|
|
stream_user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: "
|
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
2013-09-14 12:41:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
if (stream_user_data) {
|
|
|
|
stream_user_data->stream_id = 1;
|
2015-03-28 12:04:23 +01:00
|
|
|
request_done(stream_user_data);
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If upgrade succeeds, the SETTINGS value sent with
|
|
|
|
// HTTP2-Settings header field has already been submitted to
|
|
|
|
// session object.
|
|
|
|
if (!need_upgrade()) {
|
2015-02-05 16:06:01 +01:00
|
|
|
std::array<nghttp2_settings_entry, 16> iv;
|
|
|
|
auto niv = populate_settings(iv.data());
|
|
|
|
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv.data(), niv);
|
2015-01-10 08:44:09 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2015-04-17 14:31:11 +02:00
|
|
|
if (!config.no_dep) {
|
2015-01-10 08:44:09 +01:00
|
|
|
// Create anchor stream nodes
|
|
|
|
nghttp2_priority_spec pri_spec;
|
2014-12-12 17:19:27 +01:00
|
|
|
|
2015-04-16 16:42:36 +02:00
|
|
|
for (auto &anchor : anchors) {
|
|
|
|
nghttp2_priority_spec_init(&pri_spec, anchor.dep_stream_id, anchor.weight,
|
|
|
|
0);
|
|
|
|
rv = nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, anchor.stream_id,
|
2015-01-10 08:44:09 +01:00
|
|
|
&pri_spec);
|
2014-12-17 15:36:29 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-18 12:43:59 +01:00
|
|
|
|
2015-04-16 16:42:36 +02:00
|
|
|
rv = nghttp2_session_set_next_stream_id(
|
|
|
|
session, anchors[ANCHOR_FOLLOWERS].stream_id + 2);
|
2015-01-10 08:44:09 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-26 22:10:27 +02:00
|
|
|
if (need_upgrade() && !reqvec[0]->data_prd) {
|
2014-12-18 12:43:59 +01:00
|
|
|
// Amend the priority because we cannot send priority in
|
|
|
|
// HTTP/1.1 Upgrade.
|
2015-04-16 16:42:36 +02:00
|
|
|
auto &anchor = anchors[ANCHOR_FOLLOWERS];
|
2016-07-26 22:10:27 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, anchor.stream_id,
|
|
|
|
reqvec[0]->pri_spec.weight, 0);
|
2014-12-18 12:43:59 +01:00
|
|
|
|
|
|
|
rv = nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 1, &pri_spec);
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 22:10:27 +02:00
|
|
|
} else if (need_upgrade() && !reqvec[0]->data_prd &&
|
|
|
|
reqvec[0]->pri_spec.weight != NGHTTP2_DEFAULT_WEIGHT) {
|
|
|
|
// Amend the priority because we cannot send priority in HTTP/1.1
|
|
|
|
// Upgrade.
|
2015-01-10 08:44:09 +01:00
|
|
|
nghttp2_priority_spec pri_spec;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-07-26 22:10:27 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, 0, reqvec[0]->pri_spec.weight, 0);
|
2013-10-27 15:02:39 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
rv = nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 1, &pri_spec);
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_timer_again(loop, &settings_timer);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.connection_window_bits != -1) {
|
2016-05-29 16:34:38 +02:00
|
|
|
int32_t window_size = (1 << config.connection_window_bits) - 1;
|
|
|
|
rv = nghttp2_session_set_local_window_size(session, NGHTTP2_FLAG_NONE, 0,
|
|
|
|
window_size);
|
2015-01-10 08:44:09 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
// Adjust first request depending on the existence of the upload
|
|
|
|
// data
|
|
|
|
for (auto i = std::begin(reqvec) + (need_upgrade() && !reqvec[0]->data_prd);
|
|
|
|
i != std::end(reqvec); ++i) {
|
|
|
|
if (submit_request(this, config.headers, (*i).get()) != 0) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
signal_write();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpClient::on_read(const uint8_t *data, size_t len) {
|
2015-03-23 18:30:51 +01:00
|
|
|
if (config.hexdump) {
|
|
|
|
util::hexdump(stdout, data, len);
|
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto rv = nghttp2_session_mem_recv(session, data, len);
|
|
|
|
if (rv < 0) {
|
|
|
|
std::cerr << "[ERROR] nghttp2_session_mem_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(static_cast<size_t>(rv) == len);
|
|
|
|
|
|
|
|
if (nghttp2_session_want_read(session) == 0 &&
|
|
|
|
nghttp2_session_want_write(session) == 0 && wb.rleft() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_write();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpClient::on_write() {
|
2016-04-21 16:30:35 +02:00
|
|
|
for (;;) {
|
|
|
|
if (wb.rleft() >= 16384) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *data;
|
|
|
|
auto len = nghttp2_session_mem_send(session, &data);
|
|
|
|
if (len < 0) {
|
|
|
|
std::cerr << "[ERROR] nghttp2_session_send() returned error: "
|
|
|
|
<< nghttp2_strerror(len) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.append(data, len);
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nghttp2_session_want_read(session) == 0 &&
|
|
|
|
nghttp2_session_want_write(session) == 0 && wb.rleft() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpClient::tls_handshake() {
|
|
|
|
ev_timer_again(loop, &rt);
|
2014-02-19 15:12:32 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ERR_clear_error();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto rv = SSL_do_handshake(ssl);
|
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
if (rv <= 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
auto err = SSL_get_error(ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
ev_io_stop(loop, &wev);
|
|
|
|
ev_timer_stop(loop, &wt);
|
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
ev_io_start(loop, &wev);
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
return 0;
|
|
|
|
default:
|
2014-12-27 18:59:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_stop(loop, &wev);
|
|
|
|
ev_timer_stop(loop, &wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
readfn = &HttpClient::read_tls;
|
|
|
|
writefn = &HttpClient::write_tls;
|
|
|
|
|
2017-04-28 02:53:37 +02:00
|
|
|
if (config.verify_peer) {
|
|
|
|
auto verify_res = SSL_get_verify_result(ssl);
|
|
|
|
if (verify_res != X509_V_OK) {
|
|
|
|
std::cerr << "[WARNING] Certificate verification failed: "
|
|
|
|
<< X509_verify_cert_error_string(verify_res) << std::endl;
|
|
|
|
}
|
2017-04-01 10:49:57 +02:00
|
|
|
}
|
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
if (connection_made() != 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
return -1;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpClient::read_tls() {
|
|
|
|
ev_timer_again(loop, &rt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ERR_clear_error();
|
2015-01-09 01:10:59 +01:00
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
std::array<uint8_t, 8_k> buf;
|
2015-01-10 08:44:09 +01:00
|
|
|
for (;;) {
|
2015-02-05 16:06:01 +01:00
|
|
|
auto rv = SSL_read(ssl, buf.data(), buf.size());
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
if (rv <= 0) {
|
2014-12-27 18:59:06 +01:00
|
|
|
auto err = SSL_get_error(ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2015-01-10 08:44:09 +01:00
|
|
|
// renegotiation started
|
|
|
|
return -1;
|
2014-12-27 18:59:06 +01:00
|
|
|
default:
|
|
|
|
return -1;
|
2014-06-01 14:01:01 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
if (on_readfn(*this, buf.data(), rv) != 0) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
int HttpClient::write_tls() {
|
|
|
|
ev_timer_again(loop, &rt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ERR_clear_error();
|
2015-01-09 01:10:59 +01:00
|
|
|
|
2016-04-21 16:30:35 +02:00
|
|
|
struct iovec iov;
|
2015-01-10 08:44:09 +01:00
|
|
|
|
2016-04-21 16:30:35 +02:00
|
|
|
for (;;) {
|
2015-01-10 08:44:09 +01:00
|
|
|
if (on_writefn(*this) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-21 16:30:35 +02:00
|
|
|
|
|
|
|
auto iovcnt = wb.riovec(&iov, 1);
|
|
|
|
|
|
|
|
if (iovcnt == 0) {
|
2015-01-10 08:44:09 +01:00
|
|
|
break;
|
2014-06-01 14:01:01 +02:00
|
|
|
}
|
2016-04-21 16:30:35 +02:00
|
|
|
|
|
|
|
auto rv = SSL_write(ssl, iov.iov_base, iov.iov_len);
|
|
|
|
|
|
|
|
if (rv <= 0) {
|
|
|
|
auto err = SSL_get_error(ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
// renegotiation started
|
|
|
|
return -1;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
ev_io_start(loop, &wev);
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.drain(rv);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
ev_io_stop(loop, &wev);
|
|
|
|
ev_timer_stop(loop, &wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-09 01:10:59 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
void HttpClient::signal_write() { ev_io_start(loop, &wev); }
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
bool HttpClient::all_requests_processed() const {
|
|
|
|
return complete == reqvec.size();
|
|
|
|
}
|
2014-02-19 15:12:32 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
void HttpClient::update_hostport() {
|
|
|
|
if (reqvec.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-08 13:38:15 +01:00
|
|
|
scheme = util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_SCHEMA)
|
|
|
|
.str();
|
2015-01-10 08:44:09 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
if (reqvec[0]->is_ipv6_literal_addr()) {
|
2015-06-18 13:00:02 +02:00
|
|
|
// we may have zone ID, which must start with "%25", or "%". RFC
|
|
|
|
// 6874 defines "%25" only, and just "%" is allowed for just
|
|
|
|
// convenience to end-user input.
|
|
|
|
auto host =
|
|
|
|
util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_HOST);
|
|
|
|
auto end = std::find(std::begin(host), std::end(host), '%');
|
2015-01-10 08:44:09 +01:00
|
|
|
ss << "[";
|
2015-06-18 13:00:02 +02:00
|
|
|
ss.write(host.c_str(), end - std::begin(host));
|
2015-01-10 08:44:09 +01:00
|
|
|
ss << "]";
|
|
|
|
} else {
|
|
|
|
util::write_uri_field(ss, reqvec[0]->uri.c_str(), reqvec[0]->u, UF_HOST);
|
|
|
|
}
|
|
|
|
if (util::has_uri_field(reqvec[0]->u, UF_PORT) &&
|
|
|
|
reqvec[0]->u.port !=
|
|
|
|
util::get_default_port(reqvec[0]->uri.c_str(), reqvec[0]->u)) {
|
|
|
|
ss << ":" << reqvec[0]->u.port;
|
|
|
|
}
|
|
|
|
hostport = ss.str();
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
bool HttpClient::add_request(const std::string &uri,
|
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
int64_t data_length,
|
2015-04-17 14:31:11 +02:00
|
|
|
const nghttp2_priority_spec &pri_spec, int level) {
|
2015-07-19 10:55:37 +02:00
|
|
|
http_parser_url u{};
|
2015-01-10 08:44:09 +01:00
|
|
|
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (path_cache.count(uri)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
if (config.multiply == 1) {
|
|
|
|
path_cache.insert(uri);
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
reqvec.push_back(std::make_unique<Request>(uri, u, data_prd, data_length,
|
|
|
|
pri_spec, level));
|
2015-01-10 08:44:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
void HttpClient::record_start_time() {
|
|
|
|
timing.system_start_time = std::chrono::system_clock::now();
|
|
|
|
timing.start_time = get_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
void HttpClient::record_domain_lookup_end_time() {
|
|
|
|
timing.domain_lookup_end_time = get_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
void HttpClient::record_connect_end_time() {
|
|
|
|
timing.connect_end_time = get_time();
|
2015-01-10 08:44:09 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
void HttpClient::request_done(Request *req) {
|
2015-01-10 08:44:09 +01:00
|
|
|
if (req->stream_id % 2 == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
#ifdef HAVE_JANSSON
|
|
|
|
void HttpClient::output_har(FILE *outfile) {
|
|
|
|
static auto PAGE_ID = "page_0";
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto root = json_object();
|
|
|
|
auto log = json_object();
|
|
|
|
json_object_set_new(root, "log", log);
|
|
|
|
json_object_set_new(log, "version", json_string("1.2"));
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto creator = json_object();
|
|
|
|
json_object_set_new(log, "creator", creator);
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(creator, "name", json_string("nghttp"));
|
|
|
|
json_object_set_new(creator, "version", json_string(NGHTTP2_VERSION));
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto pages = json_array();
|
|
|
|
json_object_set_new(log, "pages", pages);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto page = json_object();
|
|
|
|
json_array_append_new(pages, page);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(
|
|
|
|
page, "startedDateTime",
|
2015-02-17 16:38:20 +01:00
|
|
|
json_string(util::format_iso8601(timing.system_start_time).c_str()));
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(page, "id", json_string(PAGE_ID));
|
|
|
|
json_object_set_new(page, "title", json_string(""));
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(page, "pageTimings", json_object());
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto entries = json_array();
|
|
|
|
json_object_set_new(log, "entries", entries);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2016-10-15 11:36:04 +02:00
|
|
|
auto dns_delta = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
timing.domain_lookup_end_time - timing.start_time)
|
|
|
|
.count() /
|
|
|
|
1000.0;
|
2015-01-10 08:44:09 +01:00
|
|
|
auto connect_delta =
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(
|
2016-10-15 11:36:04 +02:00
|
|
|
timing.connect_end_time - timing.domain_lookup_end_time)
|
|
|
|
.count() /
|
2015-01-10 08:44:09 +01:00
|
|
|
1000.0;
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
for (size_t i = 0; i < reqvec.size(); ++i) {
|
|
|
|
auto &req = reqvec[i];
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-02-01 09:12:31 +01:00
|
|
|
if (req->timing.state != RequestState::ON_COMPLETE) {
|
2015-01-10 08:44:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto entry = json_object();
|
|
|
|
json_array_append_new(entries, entry);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-02-01 09:12:31 +01:00
|
|
|
auto &req_timing = req->timing;
|
2015-01-10 08:44:09 +01:00
|
|
|
auto request_time =
|
2015-02-17 16:38:20 +01:00
|
|
|
(i == 0) ? timing.system_start_time
|
|
|
|
: timing.system_start_time +
|
2015-01-10 08:44:09 +01:00
|
|
|
std::chrono::duration_cast<
|
|
|
|
std::chrono::system_clock::duration>(
|
2015-02-17 16:38:20 +01:00
|
|
|
req_timing.request_start_time - timing.start_time);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2016-10-15 11:36:04 +02:00
|
|
|
auto wait_delta =
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
req_timing.response_start_time - req_timing.request_start_time)
|
|
|
|
.count() /
|
|
|
|
1000.0;
|
|
|
|
auto receive_delta =
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
req_timing.response_end_time - req_timing.response_start_time)
|
|
|
|
.count() /
|
|
|
|
1000.0;
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto time_sum =
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(
|
2015-02-17 16:38:20 +01:00
|
|
|
(i == 0) ? (req_timing.response_end_time - timing.start_time)
|
|
|
|
: (req_timing.response_end_time -
|
2016-10-15 11:36:04 +02:00
|
|
|
req_timing.request_start_time))
|
|
|
|
.count() /
|
2015-01-10 08:44:09 +01:00
|
|
|
1000.0;
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(
|
|
|
|
entry, "startedDateTime",
|
|
|
|
json_string(util::format_iso8601(request_time).c_str()));
|
|
|
|
json_object_set_new(entry, "time", json_real(time_sum));
|
2015-07-14 14:37:34 +02:00
|
|
|
|
2015-06-29 20:31:00 +02:00
|
|
|
auto pushed = req->stream_id % 2 == 0;
|
|
|
|
|
2015-07-14 14:37:34 +02:00
|
|
|
json_object_set_new(entry, "comment",
|
|
|
|
json_string(pushed ? "Pushed Object" : ""));
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto request = json_object();
|
|
|
|
json_object_set_new(entry, "request", request);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto req_headers = json_array();
|
|
|
|
json_object_set_new(request, "headers", req_headers);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
for (auto &nv : req->req_nva) {
|
|
|
|
auto hd = json_object();
|
|
|
|
json_array_append_new(req_headers, hd);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(hd, "name", json_string(nv.name.c_str()));
|
|
|
|
json_object_set_new(hd, "value", json_string(nv.value.c_str()));
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-11-07 04:47:26 +01:00
|
|
|
json_object_set_new(request, "method", json_string(req->method.c_str()));
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(request, "url", json_string(req->uri.c_str()));
|
|
|
|
json_object_set_new(request, "httpVersion", json_string("HTTP/2.0"));
|
|
|
|
json_object_set_new(request, "cookies", json_array());
|
|
|
|
json_object_set_new(request, "queryString", json_array());
|
|
|
|
json_object_set_new(request, "headersSize", json_integer(-1));
|
|
|
|
json_object_set_new(request, "bodySize", json_integer(-1));
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto response = json_object();
|
|
|
|
json_object_set_new(entry, "response", response);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto res_headers = json_array();
|
|
|
|
json_object_set_new(response, "headers", res_headers);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
for (auto &nv : req->res_nva) {
|
|
|
|
auto hd = json_object();
|
|
|
|
json_array_append_new(res_headers, hd);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(hd, "name", json_string(nv.name.c_str()));
|
|
|
|
json_object_set_new(hd, "value", json_string(nv.value.c_str()));
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(response, "status", json_integer(req->status));
|
|
|
|
json_object_set_new(response, "statusText", json_string(""));
|
|
|
|
json_object_set_new(response, "httpVersion", json_string("HTTP/2.0"));
|
|
|
|
json_object_set_new(response, "cookies", json_array());
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto content = json_object();
|
|
|
|
json_object_set_new(response, "content", content);
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(content, "size", json_integer(req->response_len));
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto content_type_ptr = http2::get_header(req->res_nva, "content-type");
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
const char *content_type = "";
|
|
|
|
if (content_type_ptr) {
|
|
|
|
content_type = content_type_ptr->value.c_str();
|
2014-11-15 14:06:16 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(content, "mimeType", json_string(content_type));
|
2013-08-03 11:51:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(response, "redirectURL", json_string(""));
|
|
|
|
json_object_set_new(response, "headersSize", json_integer(-1));
|
|
|
|
json_object_set_new(response, "bodySize", json_integer(-1));
|
|
|
|
json_object_set_new(entry, "cache", json_object());
|
2013-08-03 11:51:01 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto timings = json_object();
|
|
|
|
json_object_set_new(entry, "timings", timings);
|
2014-04-03 04:33:15 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto dns_timing = (i == 0) ? dns_delta : 0;
|
|
|
|
auto connect_timing = (i == 0) ? connect_delta : 0;
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(timings, "dns", json_real(dns_timing));
|
|
|
|
json_object_set_new(timings, "connect", json_real(connect_timing));
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(timings, "blocked", json_real(0.0));
|
|
|
|
json_object_set_new(timings, "send", json_real(0.0));
|
|
|
|
json_object_set_new(timings, "wait", json_real(wait_delta));
|
|
|
|
json_object_set_new(timings, "receive", json_real(receive_delta));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_object_set_new(entry, "pageref", json_string(PAGE_ID));
|
2015-07-14 14:37:34 +02:00
|
|
|
json_object_set_new(entry, "connection",
|
|
|
|
json_string(util::utos(req->stream_id).c_str()));
|
2013-10-24 16:04:28 +02:00
|
|
|
}
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
json_dumpf(root, outfile, JSON_PRESERVE_ORDER | JSON_INDENT(2));
|
|
|
|
json_decref(root);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-01-10 08:44:09 +01:00
|
|
|
#endif // HAVE_JANSSON
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void update_html_parser(HttpClient *client, Request *req, const uint8_t *data,
|
|
|
|
size_t len, int fin) {
|
|
|
|
if (!req->html_parser) {
|
2013-07-21 16:49:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
req->update_html_parser(data, len, fin);
|
|
|
|
|
2017-01-17 16:29:51 +01:00
|
|
|
auto scheme = req->get_real_scheme();
|
|
|
|
auto host = req->get_real_host();
|
|
|
|
auto port = req->get_real_port();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &p : req->html_parser->get_links()) {
|
2013-09-29 11:58:46 +02:00
|
|
|
auto uri = strip_fragment(p.first.c_str());
|
2015-04-17 14:31:11 +02:00
|
|
|
auto res_type = p.second;
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-07-19 10:55:37 +02:00
|
|
|
http_parser_url u{};
|
2017-01-17 16:29:51 +01:00
|
|
|
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-14 11:33:39 +01:00
|
|
|
|
2017-01-17 16:29:51 +01:00
|
|
|
if (!util::fieldeq(uri.c_str(), u, UF_SCHEMA, scheme) ||
|
|
|
|
!util::fieldeq(uri.c_str(), u, UF_HOST, host)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2017-01-17 16:29:51 +01:00
|
|
|
auto link_port =
|
|
|
|
util::has_uri_field(u, UF_PORT) ? u.port : scheme == "https" ? 443 : 80;
|
|
|
|
|
|
|
|
if (port != link_port) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No POST data for assets
|
|
|
|
auto pri_spec = resolve_dep(res_type);
|
|
|
|
|
|
|
|
if (client->add_request(uri, nullptr, 0, pri_spec, req->level + 1)) {
|
|
|
|
submit_request(client, config.headers, client->reqvec.back().get());
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
req->html_parser->clear_links();
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2015-01-10 08:44:09 +01:00
|
|
|
HttpClient *get_client(void *user_data) {
|
2014-11-27 15:39:04 +01:00
|
|
|
return static_cast<HttpClient *>(user_data);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id, const uint8_t *data,
|
|
|
|
size_t len, void *user_data) {
|
2015-01-10 08:44:09 +01:00
|
|
|
auto client = get_client(user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, stream_id));
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!req) {
|
2014-05-07 17:29:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.verbose >= 2) {
|
2014-08-10 10:15:35 +02:00
|
|
|
verbose_on_data_chunk_recv_callback(session, flags, stream_id, data, len,
|
|
|
|
user_data);
|
|
|
|
}
|
|
|
|
|
2015-02-15 04:30:06 +01:00
|
|
|
req->response_len += len;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (req->inflater) {
|
|
|
|
while (len > 0) {
|
2015-06-21 07:32:47 +02:00
|
|
|
const size_t MAX_OUTLEN = 4_k;
|
2015-02-05 16:06:01 +01:00
|
|
|
std::array<uint8_t, MAX_OUTLEN> out;
|
2014-05-07 17:29:46 +02:00
|
|
|
size_t outlen = MAX_OUTLEN;
|
|
|
|
size_t tlen = len;
|
2015-02-05 16:06:01 +01:00
|
|
|
int rv =
|
|
|
|
nghttp2_gzip_inflate(req->inflater, out.data(), &outlen, data, &tlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-07 17:29:46 +02:00
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.null_out) {
|
2015-02-05 16:06:01 +01:00
|
|
|
std::cout.write(reinterpret_cast<const char *>(out.data()), outlen);
|
2014-05-07 17:29:46 +02:00
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
update_html_parser(client, req, out.data(), outlen, 0);
|
2014-05-07 17:29:46 +02:00
|
|
|
data += tlen;
|
|
|
|
len -= tlen;
|
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2014-06-07 09:04:43 +02:00
|
|
|
return 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.null_out) {
|
|
|
|
std::cout.write(reinterpret_cast<const char *>(data), len);
|
2014-05-02 16:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 09:04:43 +02:00
|
|
|
update_html_parser(client, req, data, len, 0);
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-02-11 07:28:44 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t select_padding_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, size_t max_payload,
|
|
|
|
void *user_data) {
|
2014-02-15 08:40:32 +01:00
|
|
|
return std::min(max_payload, frame->hd.length + config.padding);
|
2014-02-11 07:28:44 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void check_response_header(nghttp2_session *session, Request *req) {
|
2013-07-21 16:49:12 +02:00
|
|
|
bool gzip = false;
|
2014-08-08 16:03:12 +02:00
|
|
|
|
|
|
|
req->expect_final_response = false;
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto status_hd = req->get_res_header(http2::HD__STATUS);
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
if (!status_hd) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, req->stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
auto status = http2::parse_http_status_code(StringRef{status_hd->value});
|
2015-01-04 15:22:39 +01:00
|
|
|
if (status == -1) {
|
2014-08-08 16:03:12 +02:00
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, req->stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
req->status = status;
|
|
|
|
|
|
|
|
for (auto &nv : req->res_nva) {
|
|
|
|
if ("content-encoding" == nv.name) {
|
2015-02-22 07:32:48 +01:00
|
|
|
gzip = util::strieq_l("gzip", nv.value) ||
|
|
|
|
util::strieq_l("deflate", nv.value);
|
2015-01-04 15:22:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (req->status / 100 == 1) {
|
2016-03-29 22:11:27 +02:00
|
|
|
if (req->continue_timer && (req->status == 100)) {
|
2016-03-22 20:41:26 +01:00
|
|
|
// If the request is waiting for a 100 Continue, complete the handshake.
|
2016-03-29 22:11:27 +02:00
|
|
|
req->continue_timer->dispatch_continue();
|
2016-03-21 22:43:48 +01:00
|
|
|
}
|
|
|
|
|
2014-08-08 16:03:12 +02:00
|
|
|
req->expect_final_response = true;
|
|
|
|
req->status = 0;
|
|
|
|
req->res_nva.clear();
|
2015-01-05 16:51:18 +01:00
|
|
|
http2::init_hdidx(req->res_hdidx);
|
2014-08-08 16:03:12 +02:00
|
|
|
return;
|
2016-03-29 22:11:27 +02:00
|
|
|
} else if (req->continue_timer) {
|
2016-03-29 21:41:28 +02:00
|
|
|
// A final response stops any pending Expect/Continue handshake.
|
2016-03-29 22:11:27 +02:00
|
|
|
req->continue_timer->stop();
|
2014-08-08 16:03:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (gzip) {
|
|
|
|
if (!req->inflater) {
|
2013-07-21 16:49:12 +02:00
|
|
|
req->init_inflater();
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.get_assets && req->level == 0) {
|
|
|
|
if (!req->html_parser) {
|
2013-07-21 16:49:12 +02:00
|
|
|
req->init_html_parser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
namespace {
|
|
|
|
int on_begin_headers_callback(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
2015-01-10 08:44:09 +01:00
|
|
|
auto client = get_client(user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2015-02-17 16:38:20 +01:00
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
|
|
|
if (!req) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (frame->headers.cat) {
|
|
|
|
case NGHTTP2_HCAT_RESPONSE:
|
|
|
|
case NGHTTP2_HCAT_PUSH_RESPONSE:
|
|
|
|
req->record_response_start_time();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-02-24 17:16:47 +01:00
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
|
|
|
auto stream_id = frame->push_promise.promised_stream_id;
|
2015-07-19 10:55:37 +02:00
|
|
|
http_parser_url u{};
|
2014-02-24 17:16:47 +01:00
|
|
|
// TODO Set pri and level
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec pri_spec;
|
|
|
|
|
|
|
|
nghttp2_priority_spec_default_init(&pri_spec);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
auto req = std::make_unique<Request>("", u, nullptr, 0, pri_spec);
|
2014-05-07 16:24:07 +02:00
|
|
|
req->stream_id = stream_id;
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
nghttp2_session_set_stream_user_data(session, stream_id, req.get());
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2015-03-28 12:04:23 +01:00
|
|
|
client->request_done(req.get());
|
2015-02-17 16:38:20 +01:00
|
|
|
req->record_request_start_time();
|
2014-02-24 17:16:47 +01:00
|
|
|
client->reqvec.push_back(std::move(req));
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
} // namespace
|
2014-02-24 17:16:47 +01:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
2014-01-16 15:41:13 +01:00
|
|
|
const uint8_t *name, size_t namelen,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *value, size_t valuelen, uint8_t flags,
|
|
|
|
void *user_data) {
|
|
|
|
if (config.verbose) {
|
2014-01-16 15:41:13 +01:00
|
|
|
verbose_on_header_callback(session, frame, name, namelen, value, valuelen,
|
2014-04-01 19:10:35 +02:00
|
|
|
flags, user_data);
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2014-08-07 15:01:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2014-02-24 17:16:47 +01:00
|
|
|
case NGHTTP2_HEADERS: {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!req) {
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2015-02-19 16:58:20 +01:00
|
|
|
/* ignore trailer header */
|
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_HEADERS &&
|
|
|
|
!req->expect_final_response) {
|
2014-08-08 16:03:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:12:41 +01:00
|
|
|
if (req->header_buffer_size + namelen + valuelen > 64_k) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, frame->hd.stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->header_buffer_size += namelen + valuelen;
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto token = http2::lookup_token(name, namelen);
|
|
|
|
|
|
|
|
http2::index_header(req->res_hdidx, token, req->res_nva.size());
|
2014-07-12 11:55:08 +02:00
|
|
|
http2::add_header(req->res_nva, name, namelen, value, valuelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
flags & NGHTTP2_NV_FLAG_NO_INDEX, token);
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(nghttp2_session_get_stream_user_data(
|
|
|
|
session, frame->push_promise.promised_stream_id));
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!req) {
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2016-02-04 15:12:41 +01:00
|
|
|
if (req->header_buffer_size + namelen + valuelen > 64_k) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->header_buffer_size += namelen + valuelen;
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto token = http2::lookup_token(name, namelen);
|
|
|
|
|
|
|
|
http2::index_header(req->req_hdidx, token, req->req_nva.size());
|
2014-11-15 14:06:16 +01:00
|
|
|
http2::add_header(req->req_nva, name, namelen, value, valuelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
flags & NGHTTP2_NV_FLAG_NO_INDEX, token);
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_recv_callback2(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
2014-05-02 16:34:57 +02:00
|
|
|
int rv = 0;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.verbose) {
|
2014-08-08 16:03:12 +02:00
|
|
|
verbose_on_frame_recv_callback(session, frame, user_data);
|
|
|
|
}
|
|
|
|
|
2015-01-10 08:44:09 +01:00
|
|
|
auto client = get_client(user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2015-02-17 16:38:20 +01:00
|
|
|
case NGHTTP2_DATA: {
|
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
|
|
|
if (!req) {
|
|
|
|
return 0;
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
req->record_response_end_time();
|
2015-03-13 15:07:58 +01:00
|
|
|
++client->success;
|
2015-02-17 16:38:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-02-24 17:16:47 +01:00
|
|
|
case NGHTTP2_HEADERS: {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2013-08-03 11:51:01 +02:00
|
|
|
// If this is the HTTP Upgrade with OPTIONS method to avoid POST,
|
|
|
|
// req is nullptr.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!req) {
|
2015-02-17 16:38:20 +01:00
|
|
|
return 0;
|
|
|
|
;
|
2014-08-08 16:03:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
switch (frame->headers.cat) {
|
|
|
|
case NGHTTP2_HCAT_RESPONSE:
|
|
|
|
case NGHTTP2_HCAT_PUSH_RESPONSE:
|
2014-01-29 13:23:13 +01:00
|
|
|
check_response_header(session, req);
|
2014-08-08 16:03:12 +02:00
|
|
|
break;
|
2015-02-17 16:38:20 +01:00
|
|
|
case NGHTTP2_HCAT_HEADERS:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (req->expect_final_response) {
|
2014-08-08 16:03:12 +02:00
|
|
|
check_response_header(session, req);
|
2015-02-17 16:38:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
|
2014-08-08 16:03:12 +02:00
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
|
2015-02-17 16:38:20 +01:00
|
|
|
return 0;
|
2014-08-08 16:03:12 +02:00
|
|
|
}
|
2015-02-17 16:38:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
req->record_response_end_time();
|
2015-03-13 15:07:58 +01:00
|
|
|
++client->success;
|
2014-08-08 16:03:12 +02:00
|
|
|
}
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGHTTP2_SETTINGS:
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_stop(client->loop, &client->settings_timer);
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(nghttp2_session_get_stream_user_data(
|
|
|
|
session, frame->push_promise.promised_stream_id));
|
|
|
|
if (!req) {
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-02-04 15:12:41 +01:00
|
|
|
|
|
|
|
// Reset for response header field reception
|
|
|
|
req->header_buffer_size = 0;
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto scheme = req->get_req_header(http2::HD__SCHEME);
|
|
|
|
auto authority = req->get_req_header(http2::HD__AUTHORITY);
|
|
|
|
auto path = req->get_req_header(http2::HD__PATH);
|
|
|
|
|
|
|
|
if (!authority) {
|
|
|
|
authority = req->get_req_header(http2::HD_HOST);
|
2014-02-24 17:16:47 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
|
2015-02-20 11:35:54 +01:00
|
|
|
// libnghttp2 guarantees :scheme, :method, :path and (:authority |
|
|
|
|
// host) exist and non-empty.
|
|
|
|
if (path->value[0] != '/') {
|
2014-02-24 17:16:47 +01:00
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
std::string uri = scheme->value;
|
2014-02-24 17:16:47 +01:00
|
|
|
uri += "://";
|
2015-01-04 15:22:39 +01:00
|
|
|
uri += authority->value;
|
|
|
|
uri += path->value;
|
2015-07-19 10:55:37 +02:00
|
|
|
http_parser_url u{};
|
2014-11-27 15:39:04 +01:00
|
|
|
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
2014-02-24 17:16:47 +01:00
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req->uri = uri;
|
|
|
|
req->u = u;
|
2015-02-15 04:36:18 +01:00
|
|
|
|
|
|
|
if (client->path_cache.count(uri)) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_CANCEL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.multiply == 1) {
|
|
|
|
client->path_cache.insert(uri);
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
return rv;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-02-01 08:58:58 +01:00
|
|
|
namespace {
|
|
|
|
int before_frame_send_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2015-02-11 15:20:13 +01:00
|
|
|
assert(req);
|
2015-02-17 16:38:20 +01:00
|
|
|
req->record_request_start_time();
|
2015-02-01 08:58:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-03-29 00:24:20 +02:00
|
|
|
namespace {
|
2016-03-31 13:06:14 +02:00
|
|
|
int on_frame_send_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
2016-03-29 00:24:20 +02:00
|
|
|
void *user_data) {
|
|
|
|
if (config.verbose) {
|
|
|
|
verbose_on_frame_send_callback(session, frame, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
|
|
|
if (!req) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this request is using Expect/Continue, start its ContinueTimer.
|
2016-03-29 22:11:27 +02:00
|
|
|
if (req->continue_timer) {
|
|
|
|
req->continue_timer->start();
|
2016-03-29 00:24:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-15 17:41:55 +02:00
|
|
|
namespace {
|
|
|
|
int on_frame_not_send_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, int lib_error_code,
|
|
|
|
void *user_data) {
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-16 15:18:09 +02:00
|
|
|
|
2015-09-15 17:41:55 +02:00
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2015-09-16 15:18:09 +02:00
|
|
|
if (!req) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-15 17:41:55 +02:00
|
|
|
std::cerr << "[ERROR] request " << req->uri
|
|
|
|
<< " failed: " << nghttp2_strerror(lib_error_code) << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint32_t error_code, void *user_data) {
|
2015-01-10 08:44:09 +01:00
|
|
|
auto client = get_client(user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, stream_id));
|
2014-05-07 17:29:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!req) {
|
2014-05-07 17:29:46 +02:00
|
|
|
return 0;
|
2014-02-24 17:16:47 +01:00
|
|
|
}
|
2014-05-07 17:29:46 +02:00
|
|
|
|
2016-03-29 21:41:28 +02:00
|
|
|
// If this request is using Expect/Continue, stop its ContinueTimer.
|
2016-03-29 22:11:27 +02:00
|
|
|
if (req->continue_timer) {
|
|
|
|
req->continue_timer->stop();
|
2016-03-29 21:41:28 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 17:29:46 +02:00
|
|
|
update_html_parser(client, req, nullptr, 0, 1);
|
|
|
|
++client->complete;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (client->all_requests_processed()) {
|
2014-02-24 17:16:47 +01:00
|
|
|
nghttp2_session_terminate_session(session, NGHTTP2_NO_ERROR);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-05-07 17:29:46 +02:00
|
|
|
|
2013-08-29 15:58:05 +02:00
|
|
|
return 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2015-02-01 09:59:49 +01:00
|
|
|
struct RequestResult {
|
|
|
|
std::chrono::microseconds time;
|
|
|
|
};
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_stats(const HttpClient &client) {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cout << "***** Statistics *****" << std::endl;
|
2015-02-01 09:59:49 +01:00
|
|
|
|
|
|
|
std::vector<Request *> reqs;
|
|
|
|
reqs.reserve(client.reqvec.size());
|
|
|
|
for (const auto &req : client.reqvec) {
|
|
|
|
if (req->timing.state == RequestState::ON_COMPLETE) {
|
|
|
|
reqs.push_back(req.get());
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-02-01 09:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(std::begin(reqs), std::end(reqs),
|
|
|
|
[](const Request *lhs, const Request *rhs) {
|
2015-11-12 16:53:29 +01:00
|
|
|
const auto <iming = lhs->timing;
|
|
|
|
const auto &rtiming = rhs->timing;
|
|
|
|
return ltiming.response_end_time < rtiming.response_end_time ||
|
|
|
|
(ltiming.response_end_time == rtiming.response_end_time &&
|
|
|
|
ltiming.request_start_time < rtiming.request_start_time);
|
|
|
|
});
|
2015-02-01 09:59:49 +01:00
|
|
|
|
|
|
|
std::cout << R"(
|
|
|
|
Request timing:
|
2015-02-17 16:38:20 +01:00
|
|
|
responseEnd: the time when last byte of response was received
|
|
|
|
relative to connectEnd
|
|
|
|
requestStart: the time just before first byte of request was sent
|
|
|
|
relative to connectEnd. If '*' is shown, this was
|
|
|
|
pushed by server.
|
|
|
|
process: responseEnd - requestStart
|
|
|
|
code: HTTP status code
|
|
|
|
size: number of bytes received as response body without
|
|
|
|
inflation.
|
|
|
|
URI: request URI
|
|
|
|
|
|
|
|
see http://www.w3.org/TR/resource-timing/#processing-model
|
2015-02-01 09:59:49 +01:00
|
|
|
|
|
|
|
sorted by 'complete'
|
|
|
|
|
2016-10-15 11:36:04 +02:00
|
|
|
id responseEnd requestStart process code size request path)"
|
|
|
|
<< std::endl;
|
2015-02-01 09:59:49 +01:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
const auto &base = client.timing.connect_end_time;
|
2015-02-01 09:59:49 +01:00
|
|
|
for (const auto &req : reqs) {
|
2015-02-17 16:38:20 +01:00
|
|
|
auto response_end = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
req->timing.response_end_time - base);
|
|
|
|
auto request_start = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
req->timing.request_start_time - base);
|
2015-02-01 09:59:49 +01:00
|
|
|
auto total = std::chrono::duration_cast<std::chrono::microseconds>(
|
2015-02-17 16:38:20 +01:00
|
|
|
req->timing.response_end_time - req->timing.request_start_time);
|
2015-02-15 04:46:00 +01:00
|
|
|
auto pushed = req->stream_id % 2 == 0;
|
2015-02-01 09:59:49 +01:00
|
|
|
|
2015-04-17 16:33:06 +02:00
|
|
|
std::cout << std::setw(3) << req->stream_id << " " << std::setw(11)
|
|
|
|
<< ("+" + util::format_duration(response_end)) << " "
|
|
|
|
<< (pushed ? "*" : " ") << std::setw(11)
|
2015-02-17 16:38:20 +01:00
|
|
|
<< ("+" + util::format_duration(request_start)) << " "
|
2015-02-01 09:59:49 +01:00
|
|
|
<< std::setw(8) << util::format_duration(total) << " "
|
2015-02-15 04:30:06 +01:00
|
|
|
<< std::setw(4) << req->status << " " << std::setw(4)
|
2016-01-11 10:12:31 +01:00
|
|
|
<< util::utos_unit(req->response_len) << " "
|
2015-02-15 04:30:06 +01:00
|
|
|
<< req->make_reqpath() << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2018-04-03 14:33:09 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int client_select_next_proto_cb(SSL *ssl, unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg) {
|
|
|
|
if (config.verbose) {
|
2013-07-21 16:49:12 +02:00
|
|
|
print_timer();
|
2014-01-01 15:54:28 +01:00
|
|
|
std::cout << "[NPN] server offers:" << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2016-10-15 11:36:04 +02:00
|
|
|
for (unsigned int i = 0; i < inlen; i += in[i] + 1) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.verbose) {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cout << " * ";
|
2014-11-27 15:39:04 +01:00
|
|
|
std::cout.write(reinterpret_cast<const char *>(&in[i + 1]), in[i]);
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 14:51:34 +01:00
|
|
|
if (!util::select_h2(const_cast<const unsigned char **>(out), outlen, in,
|
|
|
|
inlen)) {
|
2014-01-01 15:54:28 +01:00
|
|
|
print_protocol_nego_error();
|
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
} // namespace
|
2018-04-03 14:33:09 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2016-10-15 11:36:04 +02:00
|
|
|
int communicate(
|
|
|
|
const std::string &scheme, const std::string &host, uint16_t port,
|
|
|
|
std::vector<
|
|
|
|
std::tuple<std::string, nghttp2_data_provider *, int64_t, int32_t>>
|
|
|
|
requests,
|
|
|
|
const nghttp2_session_callbacks *callbacks) {
|
2013-07-21 16:49:12 +02:00
|
|
|
int result = 0;
|
2014-12-27 18:59:06 +01:00
|
|
|
auto loop = EV_DEFAULT;
|
2013-07-21 16:49:12 +02:00
|
|
|
SSL_CTX *ssl_ctx = nullptr;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (scheme == "https") {
|
2013-07-21 16:49:12 +02:00
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl_ctx) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Failed to create SSL_CTX: "
|
2013-07-21 16:49:12 +02:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
2015-06-22 16:26:45 +02:00
|
|
|
|
|
|
|
auto ssl_opts = (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
|
|
|
|
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION |
|
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION;
|
|
|
|
|
|
|
|
SSL_CTX_set_options(ssl_ctx, ssl_opts);
|
2013-07-21 16:49:12 +02:00
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
2017-02-14 16:40:43 +01:00
|
|
|
|
2017-04-01 10:49:57 +02:00
|
|
|
if (SSL_CTX_set_default_verify_paths(ssl_ctx) != 1) {
|
|
|
|
std::cerr << "[WARNING] Could not load system trusted CA certificates: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:47:36 +02:00
|
|
|
if (nghttp2::tls::ssl_ctx_set_proto_versions(
|
|
|
|
ssl_ctx, nghttp2::tls::NGHTTP2_TLS_MIN_VERSION,
|
|
|
|
nghttp2::tls::NGHTTP2_TLS_MAX_VERSION) != 0) {
|
2017-02-14 16:40:43 +01:00
|
|
|
std::cerr << "[ERROR] Could not set TLS versions" << std::endl;
|
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:47:36 +02:00
|
|
|
if (SSL_CTX_set_cipher_list(ssl_ctx, tls::DEFAULT_CIPHER_LIST) == 0) {
|
2014-12-12 17:37:57 +01:00
|
|
|
std::cerr << "[ERROR] " << ERR_error_string(ERR_get_error(), nullptr)
|
|
|
|
<< std::endl;
|
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.keyfile.empty()) {
|
|
|
|
if (SSL_CTX_use_PrivateKey_file(ssl_ctx, config.keyfile.c_str(),
|
|
|
|
SSL_FILETYPE_PEM) != 1) {
|
|
|
|
std::cerr << "[ERROR] " << ERR_error_string(ERR_get_error(), nullptr)
|
|
|
|
<< std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.certfile.empty()) {
|
|
|
|
if (SSL_CTX_use_certificate_chain_file(ssl_ctx,
|
|
|
|
config.certfile.c_str()) != 1) {
|
|
|
|
std::cerr << "[ERROR] " << ERR_error_string(ERR_get_error(), nullptr)
|
|
|
|
<< std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 18:27:23 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2014-11-27 15:39:04 +01:00
|
|
|
SSL_CTX_set_next_proto_select_cb(ssl_ctx, client_select_next_proto_cb,
|
|
|
|
nullptr);
|
2018-04-03 14:39:44 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2014-01-01 15:54:28 +01:00
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2014-11-14 15:14:39 +01:00
|
|
|
auto proto_list = util::get_default_alpn();
|
|
|
|
|
|
|
|
SSL_CTX_set_alpn_protos(ssl_ctx, proto_list.data(), proto_list.size());
|
2014-01-01 15:54:28 +01:00
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
{
|
2014-12-27 18:59:06 +01:00
|
|
|
HttpClient client{callbacks, loop, ssl_ctx};
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-12 17:19:27 +01:00
|
|
|
int32_t dep_stream_id = 0;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-04-17 14:31:11 +02:00
|
|
|
if (!config.no_dep) {
|
2015-04-16 16:42:36 +02:00
|
|
|
dep_stream_id = anchors[ANCHOR_FOLLOWERS].stream_id;
|
2014-12-12 17:19:27 +01:00
|
|
|
}
|
2014-12-18 12:43:59 +01:00
|
|
|
|
2016-07-26 22:10:27 +02:00
|
|
|
for (auto &req : requests) {
|
|
|
|
nghttp2_priority_spec pri_spec;
|
|
|
|
|
|
|
|
nghttp2_priority_spec_init(&pri_spec, dep_stream_id, std::get<3>(req), 0);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (int i = 0; i < config.multiply; ++i) {
|
|
|
|
client.add_request(std::get<0>(req), std::get<1>(req), std::get<2>(req),
|
2015-04-17 14:31:11 +02:00
|
|
|
pri_spec);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
client.update_hostport();
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
client.record_start_time();
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (client.resolve_host(host, port) != 0) {
|
2014-01-22 16:29:09 +01:00
|
|
|
goto fin;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2015-02-17 16:38:20 +01:00
|
|
|
client.record_domain_lookup_end_time();
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (client.initiate_connection() != 0) {
|
2015-06-18 11:03:25 +02:00
|
|
|
std::cerr << "[ERROR] Could not connect to " << host << ", port " << port
|
|
|
|
<< std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
goto fin;
|
|
|
|
}
|
2016-03-22 20:41:26 +01:00
|
|
|
|
|
|
|
ev_set_userdata(loop, &client);
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_run(loop, 0);
|
2016-03-22 20:41:26 +01:00
|
|
|
ev_set_userdata(loop, nullptr);
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-11-15 14:06:16 +01:00
|
|
|
#ifdef HAVE_JANSSON
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.harfile.empty()) {
|
2014-11-15 14:06:16 +01:00
|
|
|
FILE *outfile;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.harfile == "-") {
|
2014-11-15 14:06:16 +01:00
|
|
|
outfile = stdout;
|
|
|
|
} else {
|
|
|
|
outfile = fopen(config.harfile.c_str(), "wb");
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (outfile) {
|
2014-11-15 14:06:16 +01:00
|
|
|
client.output_har(outfile);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (outfile != stdout) {
|
2014-11-15 14:06:16 +01:00
|
|
|
fclose(outfile);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << "Cannot open file " << config.harfile << ". "
|
2014-11-27 15:39:04 +01:00
|
|
|
<< "har file could not be created." << std::endl;
|
2014-11-15 14:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAVE_JANSSON
|
|
|
|
|
2015-03-13 15:07:58 +01:00
|
|
|
if (client.success != client.reqvec.size()) {
|
2015-03-14 10:32:53 +01:00
|
|
|
std::cerr << "Some requests were not processed. total="
|
|
|
|
<< client.reqvec.size() << ", processed=" << client.success
|
|
|
|
<< std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.stat) {
|
2013-07-21 16:49:12 +02:00
|
|
|
print_stats(client);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
fin:
|
|
|
|
if (ssl_ctx) {
|
2013-07-21 16:49:12 +02:00
|
|
|
SSL_CTX_free(ssl_ctx);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length, uint32_t *data_flags,
|
|
|
|
nghttp2_data_source *source, void *user_data) {
|
2015-03-07 09:39:31 +01:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto req = static_cast<Request *>(
|
|
|
|
nghttp2_session_get_stream_user_data(session, stream_id));
|
2013-09-07 09:38:21 +02:00
|
|
|
assert(req);
|
2013-07-21 16:49:12 +02:00
|
|
|
int fd = source->fd;
|
2014-05-02 16:34:57 +02:00
|
|
|
ssize_t nread;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
while ((nread = pread(fd, buf, length, req->data_offset)) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == -1) {
|
2014-06-07 09:04:43 +02:00
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2016-05-13 17:18:22 +02:00
|
|
|
req->data_offset += nread;
|
|
|
|
|
|
|
|
if (req->data_offset == req->data_length) {
|
2014-05-02 16:34:57 +02:00
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
2015-03-07 09:39:31 +01:00
|
|
|
if (!config.trailer.empty()) {
|
|
|
|
std::vector<nghttp2_nv> nva;
|
|
|
|
nva.reserve(config.trailer.size());
|
|
|
|
for (auto &kv : config.trailer) {
|
|
|
|
nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index));
|
|
|
|
}
|
|
|
|
rv = nghttp2_submit_trailer(session, stream_id, nva.data(), nva.size());
|
|
|
|
if (rv != 0) {
|
2015-03-08 08:28:23 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
|
2015-03-07 09:39:31 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-13 17:18:22 +02:00
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2016-05-13 17:23:44 +02:00
|
|
|
if (req->data_offset > req->data_length || nread == 0) {
|
2016-05-13 17:18:22 +02:00
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2014-05-02 16:34:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int run(char **uris, int n) {
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks *callbacks;
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_new(&callbacks);
|
2015-02-06 15:27:15 +01:00
|
|
|
auto cbsdel = defer(nghttp2_session_callbacks_del, callbacks);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback(
|
|
|
|
callbacks, on_stream_close_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
|
|
|
|
on_frame_recv_callback2);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.verbose) {
|
|
|
|
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
|
|
|
|
callbacks, verbose_on_invalid_frame_recv_callback);
|
2016-03-13 10:21:07 +01:00
|
|
|
|
2017-11-19 08:47:39 +01:00
|
|
|
nghttp2_session_callbacks_set_error_callback2(callbacks,
|
|
|
|
verbose_error_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
|
|
|
|
callbacks, on_data_chunk_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_begin_headers_callback(
|
|
|
|
callbacks, on_begin_headers_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_header_callback(callbacks,
|
|
|
|
on_header_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2015-02-01 08:58:58 +01:00
|
|
|
nghttp2_session_callbacks_set_before_frame_send_callback(
|
|
|
|
callbacks, before_frame_send_callback);
|
|
|
|
|
2016-03-31 13:06:14 +02:00
|
|
|
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
|
|
|
|
on_frame_send_callback);
|
2016-03-29 00:24:20 +02:00
|
|
|
|
2015-09-15 17:41:55 +02:00
|
|
|
nghttp2_session_callbacks_set_on_frame_not_send_callback(
|
|
|
|
callbacks, on_frame_not_send_callback);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.padding) {
|
|
|
|
nghttp2_session_callbacks_set_select_padding_callback(
|
|
|
|
callbacks, select_padding_callback);
|
2014-02-11 08:48:27 +01:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2013-08-12 15:33:51 +02:00
|
|
|
std::string prev_scheme;
|
2013-07-21 16:49:12 +02:00
|
|
|
std::string prev_host;
|
|
|
|
uint16_t prev_port = 0;
|
|
|
|
int failures = 0;
|
|
|
|
int data_fd = -1;
|
|
|
|
nghttp2_data_provider data_prd;
|
|
|
|
struct stat data_stat;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.datafile.empty()) {
|
2014-12-21 06:06:44 +01:00
|
|
|
if (config.datafile == "-") {
|
|
|
|
if (fstat(0, &data_stat) == 0 &&
|
|
|
|
(data_stat.st_mode & S_IFMT) == S_IFREG) {
|
|
|
|
// use STDIN if it is a regular file
|
|
|
|
data_fd = 0;
|
|
|
|
} else {
|
|
|
|
// copy the contents of STDIN to a temporary file
|
|
|
|
char tempfn[] = "/tmp/nghttp.temp.XXXXXX";
|
|
|
|
data_fd = mkstemp(tempfn);
|
|
|
|
if (data_fd == -1) {
|
|
|
|
std::cerr << "[ERROR] Could not create a temporary file in /tmp"
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-21 23:24:36 +01:00
|
|
|
if (unlink(tempfn) != 0) {
|
|
|
|
std::cerr << "[WARNING] failed to unlink temporary file:" << tempfn
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2014-12-21 06:06:44 +01:00
|
|
|
while (1) {
|
2015-06-21 07:32:47 +02:00
|
|
|
std::array<char, 1_k> buf;
|
2014-12-21 06:06:44 +01:00
|
|
|
ssize_t rret, wret;
|
2015-02-05 16:06:01 +01:00
|
|
|
while ((rret = read(0, buf.data(), buf.size())) == -1 &&
|
|
|
|
errno == EINTR)
|
2014-12-21 06:06:44 +01:00
|
|
|
;
|
|
|
|
if (rret == 0)
|
|
|
|
break;
|
|
|
|
if (rret == -1) {
|
|
|
|
std::cerr << "[ERROR] I/O error while reading from STDIN"
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-02-05 16:06:01 +01:00
|
|
|
while ((wret = write(data_fd, buf.data(), rret)) == -1 &&
|
|
|
|
errno == EINTR)
|
2014-12-21 06:06:44 +01:00
|
|
|
;
|
|
|
|
if (wret != rret) {
|
|
|
|
std::cerr << "[ERROR] I/O error while writing to temporary file"
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 23:26:02 +01:00
|
|
|
if (fstat(data_fd, &data_stat) == -1) {
|
|
|
|
close(data_fd);
|
|
|
|
std::cerr << "[ERROR] Could not stat temporary file" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-21 06:06:44 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data_fd = open(config.datafile.c_str(), O_RDONLY | O_BINARY);
|
|
|
|
if (data_fd == -1) {
|
|
|
|
std::cerr << "[ERROR] Could not open file " << config.datafile
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (fstat(data_fd, &data_stat) == -1) {
|
|
|
|
close(data_fd);
|
|
|
|
std::cerr << "[ERROR] Could not stat file " << config.datafile
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
data_prd.source.fd = data_fd;
|
|
|
|
data_prd.read_callback = file_read_callback;
|
|
|
|
}
|
2016-10-15 11:36:04 +02:00
|
|
|
std::vector<
|
|
|
|
std::tuple<std::string, nghttp2_data_provider *, int64_t, int32_t>>
|
|
|
|
requests;
|
2016-07-29 12:36:05 +02:00
|
|
|
|
|
|
|
size_t next_weight_idx = 0;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2015-07-19 10:55:37 +02:00
|
|
|
http_parser_url u{};
|
2013-09-03 16:09:56 +02:00
|
|
|
auto uri = strip_fragment(uris[i]);
|
2015-06-18 11:00:24 +02:00
|
|
|
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
2016-07-29 12:36:05 +02:00
|
|
|
++next_weight_idx;
|
2015-06-18 11:00:24 +02:00
|
|
|
std::cerr << "[ERROR] Could not parse URI " << uri << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!util::has_uri_field(u, UF_SCHEMA)) {
|
2016-07-29 12:36:05 +02:00
|
|
|
++next_weight_idx;
|
2015-06-18 11:00:24 +02:00
|
|
|
std::cerr << "[ERROR] URI " << uri << " does not have scheme part"
|
|
|
|
<< std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto port = util::has_uri_field(u, UF_PORT)
|
|
|
|
? u.port
|
|
|
|
: util::get_default_port(uri.c_str(), u);
|
2015-06-18 13:00:02 +02:00
|
|
|
auto host = decode_host(util::get_uri_field(uri.c_str(), u, UF_HOST));
|
2015-06-18 11:00:24 +02:00
|
|
|
if (!util::fieldeq(uri.c_str(), u, UF_SCHEMA, prev_scheme.c_str()) ||
|
2015-06-18 13:00:02 +02:00
|
|
|
host != prev_host || port != prev_port) {
|
2015-06-18 11:00:24 +02:00
|
|
|
if (!requests.empty()) {
|
|
|
|
if (communicate(prev_scheme, prev_host, prev_port, std::move(requests),
|
|
|
|
callbacks) != 0) {
|
|
|
|
++failures;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-06-18 11:00:24 +02:00
|
|
|
requests.clear();
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2016-03-08 13:38:15 +01:00
|
|
|
prev_scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA).str();
|
2015-06-18 13:00:02 +02:00
|
|
|
prev_host = std::move(host);
|
2015-06-18 11:00:24 +02:00
|
|
|
prev_port = port;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2015-06-18 11:00:24 +02:00
|
|
|
requests.emplace_back(uri, data_fd == -1 ? nullptr : &data_prd,
|
2016-07-29 12:36:05 +02:00
|
|
|
data_stat.st_size, config.weight[next_weight_idx++]);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!requests.empty()) {
|
2013-08-12 15:33:51 +02:00
|
|
|
if (communicate(prev_scheme, prev_host, prev_port, std::move(requests),
|
2014-08-22 13:59:50 +02:00
|
|
|
callbacks) != 0) {
|
2013-07-21 16:49:12 +02:00
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return failures;
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-02-16 11:39:41 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_version(std::ostream &out) {
|
2014-02-16 11:39:41 +01:00
|
|
|
out << "nghttp nghttp2/" NGHTTP2_VERSION << std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_usage(std::ostream &out) {
|
2014-03-13 17:53:28 +01:00
|
|
|
out << R"(Usage: nghttp [OPTIONS]... <URI>...
|
2016-10-15 11:36:04 +02:00
|
|
|
HTTP/2 client)"
|
|
|
|
<< std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_help(std::ostream &out) {
|
2013-07-21 16:49:12 +02:00
|
|
|
print_usage(out);
|
2014-03-13 17:53:28 +01:00
|
|
|
out << R"(
|
2015-01-15 16:00:38 +01:00
|
|
|
<URI> Specify URI to access.
|
2014-03-13 17:53:28 +01:00
|
|
|
Options:
|
2015-01-15 16:00:38 +01:00
|
|
|
-v, --verbose
|
|
|
|
Print debug information such as reception and
|
|
|
|
transmission of frames and name/value pairs. Specifying
|
|
|
|
this option multiple times increases verbosity.
|
|
|
|
-n, --null-out
|
|
|
|
Discard downloaded data.
|
|
|
|
-O, --remote-name
|
|
|
|
Save download data in the current directory. The
|
2015-11-09 13:43:25 +01:00
|
|
|
filename is derived from URI. If URI ends with '/',
|
2015-01-15 16:00:38 +01:00
|
|
|
'index.html' is used as a filename. Not implemented
|
|
|
|
yet.
|
2015-04-28 14:51:28 +02:00
|
|
|
-t, --timeout=<DURATION>
|
|
|
|
Timeout each request after <DURATION>. Set 0 to disable
|
|
|
|
timeout.
|
2014-03-13 17:53:28 +01:00
|
|
|
-w, --window-bits=<N>
|
2015-01-15 16:00:38 +01:00
|
|
|
Sets the stream level initial window size to 2**<N>-1.
|
2014-03-13 17:53:28 +01:00
|
|
|
-W, --connection-window-bits=<N>
|
2015-01-15 16:00:38 +01:00
|
|
|
Sets the connection level initial window size to
|
|
|
|
2**<N>-1.
|
|
|
|
-a, --get-assets
|
|
|
|
Download assets such as stylesheets, images and script
|
|
|
|
files linked from the downloaded resource. Only links
|
|
|
|
whose origins are the same with the linking resource
|
|
|
|
will be downloaded. nghttp prioritizes resources using
|
|
|
|
HTTP/2 dependency based priority. The priority order,
|
|
|
|
from highest to lowest, is html itself, css, javascript
|
|
|
|
and images.
|
|
|
|
-s, --stat Print statistics.
|
2014-10-21 15:46:47 +02:00
|
|
|
-H, --header=<HEADER>
|
2015-01-15 16:00:38 +01:00
|
|
|
Add a header to the requests. Example: -H':method: PUT'
|
2015-03-07 09:39:31 +01:00
|
|
|
--trailer=<HEADER>
|
|
|
|
Add a trailer header to the requests. <HEADER> must not
|
|
|
|
include pseudo header field (header field name starting
|
|
|
|
with ':'). To send trailer, one must use -d option to
|
|
|
|
send request body. Example: --trailer 'foo: bar'.
|
2015-01-15 16:00:38 +01:00
|
|
|
--cert=<CERT>
|
|
|
|
Use the specified client certificate file. The file
|
|
|
|
must be in PEM format.
|
|
|
|
--key=<KEY> Use the client private key file. The file must be in
|
|
|
|
PEM format.
|
2015-07-20 16:50:05 +02:00
|
|
|
-d, --data=<PATH>
|
2015-01-15 16:00:38 +01:00
|
|
|
Post FILE to server. If '-' is given, data will be read
|
|
|
|
from stdin.
|
|
|
|
-m, --multiply=<N>
|
|
|
|
Request each URI <N> times. By default, same URI is not
|
|
|
|
requested twice. This option disables it too.
|
|
|
|
-u, --upgrade
|
|
|
|
Perform HTTP Upgrade for HTTP/2. This option is ignored
|
|
|
|
if the request URI has https scheme. If -d is used, the
|
|
|
|
HTTP upgrade request is performed with OPTIONS method.
|
2014-03-25 18:04:24 +01:00
|
|
|
-p, --weight=<WEIGHT>
|
2016-07-26 22:10:27 +02:00
|
|
|
Sets weight of given URI. This option can be used
|
|
|
|
multiple times, and N-th -p option sets weight of N-th
|
|
|
|
URI in the command line. If the number of -p option is
|
|
|
|
less than the number of URI, the last -p option value is
|
|
|
|
repeated. If there is no -p option, default weight, 16,
|
|
|
|
is assumed. The valid value range is
|
2016-10-15 11:36:04 +02:00
|
|
|
[)"
|
|
|
|
<< NGHTTP2_MIN_WEIGHT << ", " << NGHTTP2_MAX_WEIGHT << R"(], inclusive.
|
2014-03-13 17:53:28 +01:00
|
|
|
-M, --peer-max-concurrent-streams=<N>
|
2015-01-15 16:00:38 +01:00
|
|
|
Use <N> as SETTINGS_MAX_CONCURRENT_STREAMS value of
|
|
|
|
remote endpoint as if it is received in SETTINGS frame.
|
2015-12-04 15:08:37 +01:00
|
|
|
Default: 100
|
2015-01-15 16:00:38 +01:00
|
|
|
-c, --header-table-size=<SIZE>
|
2015-10-24 11:11:47 +02:00
|
|
|
Specify decoder header table size. If this option is
|
|
|
|
used multiple times, and the minimum value among the
|
|
|
|
given values except for last one is strictly less than
|
|
|
|
the last value, that minimum value is set in SETTINGS
|
|
|
|
frame payload before the last value, to simulate
|
|
|
|
multiple header table size change.
|
2016-09-17 15:19:50 +02:00
|
|
|
--encoder-header-table-size=<SIZE>
|
|
|
|
Specify encoder header table size. The decoder (server)
|
|
|
|
specifies the maximum dynamic table size it accepts.
|
|
|
|
Then the negotiated dynamic table size is the minimum of
|
|
|
|
this option value and the value which server specified.
|
2015-01-15 16:00:38 +01:00
|
|
|
-b, --padding=<N>
|
|
|
|
Add at most <N> bytes to a frame payload as padding.
|
|
|
|
Specify 0 to disable padding.
|
2015-07-20 16:50:05 +02:00
|
|
|
-r, --har=<PATH>
|
|
|
|
Output HTTP transactions <PATH> in HAR format. If '-'
|
2015-01-15 16:00:38 +01:00
|
|
|
is given, data is written to stdout.
|
|
|
|
--color Force colored log output.
|
|
|
|
--continuation
|
|
|
|
Send large header to test CONTINUATION.
|
2014-07-03 15:48:43 +02:00
|
|
|
--no-content-length
|
2015-01-15 16:00:38 +01:00
|
|
|
Don't send content-length header field.
|
|
|
|
--no-dep Don't send dependency based priority hint to server.
|
2015-03-26 16:36:19 +01:00
|
|
|
--hexdump Display the incoming traffic in hexadecimal (Canonical
|
|
|
|
hex+ASCII display). If SSL/TLS is used, decrypted data
|
|
|
|
are used.
|
2015-04-17 16:34:28 +02:00
|
|
|
--no-push Disable server push.
|
2015-07-07 17:22:21 +02:00
|
|
|
--max-concurrent-streams=<N>
|
|
|
|
The number of concurrent pushed streams this client
|
|
|
|
accepts.
|
2016-03-21 19:34:09 +01:00
|
|
|
--expect-continue
|
|
|
|
Perform an Expect/Continue handshake: wait to send DATA
|
|
|
|
(up to a short timeout) until the server sends a 100
|
|
|
|
Continue interim response. This option is ignored unless
|
|
|
|
combined with the -d option.
|
2017-04-28 02:53:37 +02:00
|
|
|
-y, --no-verify-peer
|
|
|
|
Suppress warning on server certificate verification
|
|
|
|
failure.
|
2015-01-15 16:00:38 +01:00
|
|
|
--version Display version information and exit.
|
2015-01-15 16:06:47 +01:00
|
|
|
-h, --help Display this help and exit.
|
|
|
|
|
2015-03-30 17:21:52 +02:00
|
|
|
--
|
|
|
|
|
2015-01-15 16:06:47 +01:00
|
|
|
The <SIZE> argument is an integer and an optional unit (e.g., 10K is
|
2015-04-28 14:51:28 +02:00
|
|
|
10 * 1024). Units are K, M and G (powers of 1024).
|
|
|
|
|
|
|
|
The <DURATION> argument is an integer and an optional unit (e.g., 1s
|
|
|
|
is 1 second and 500ms is 500 milliseconds). Units are h, m, s or ms
|
|
|
|
(hours, minutes, seconds and milliseconds, respectively). If a unit
|
2016-10-15 11:36:04 +02:00
|
|
|
is omitted, a second is used as unit.)"
|
|
|
|
<< std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int main(int argc, char **argv) {
|
2017-04-01 07:47:36 +02:00
|
|
|
tls::libssl_init();
|
2015-05-22 17:23:38 +02:00
|
|
|
|
2013-11-01 15:06:53 +01:00
|
|
|
bool color = false;
|
2014-11-27 15:39:04 +01:00
|
|
|
while (1) {
|
2014-04-30 15:44:51 +02:00
|
|
|
static int flag = 0;
|
2017-01-09 11:28:00 +01:00
|
|
|
constexpr static option long_options[] = {
|
2014-11-27 15:39:04 +01:00
|
|
|
{"verbose", no_argument, nullptr, 'v'},
|
|
|
|
{"null-out", no_argument, nullptr, 'n'},
|
|
|
|
{"remote-name", no_argument, nullptr, 'O'},
|
|
|
|
{"timeout", required_argument, nullptr, 't'},
|
|
|
|
{"window-bits", required_argument, nullptr, 'w'},
|
|
|
|
{"connection-window-bits", required_argument, nullptr, 'W'},
|
|
|
|
{"get-assets", no_argument, nullptr, 'a'},
|
|
|
|
{"stat", no_argument, nullptr, 's'},
|
|
|
|
{"help", no_argument, nullptr, 'h'},
|
|
|
|
{"header", required_argument, nullptr, 'H'},
|
|
|
|
{"data", required_argument, nullptr, 'd'},
|
|
|
|
{"multiply", required_argument, nullptr, 'm'},
|
|
|
|
{"upgrade", no_argument, nullptr, 'u'},
|
|
|
|
{"weight", required_argument, nullptr, 'p'},
|
|
|
|
{"peer-max-concurrent-streams", required_argument, nullptr, 'M'},
|
|
|
|
{"header-table-size", required_argument, nullptr, 'c'},
|
|
|
|
{"padding", required_argument, nullptr, 'b'},
|
|
|
|
{"har", required_argument, nullptr, 'r'},
|
2017-04-28 02:53:37 +02:00
|
|
|
{"no-verify-peer", no_argument, nullptr, 'y'},
|
2014-11-27 15:39:04 +01:00
|
|
|
{"cert", required_argument, &flag, 1},
|
|
|
|
{"key", required_argument, &flag, 2},
|
|
|
|
{"color", no_argument, &flag, 3},
|
|
|
|
{"continuation", no_argument, &flag, 4},
|
|
|
|
{"version", no_argument, &flag, 5},
|
|
|
|
{"no-content-length", no_argument, &flag, 6},
|
|
|
|
{"no-dep", no_argument, &flag, 7},
|
2015-03-07 09:39:31 +01:00
|
|
|
{"trailer", required_argument, &flag, 9},
|
2015-03-23 18:30:51 +01:00
|
|
|
{"hexdump", no_argument, &flag, 10},
|
2015-04-17 16:34:28 +02:00
|
|
|
{"no-push", no_argument, &flag, 11},
|
2015-07-07 17:22:21 +02:00
|
|
|
{"max-concurrent-streams", required_argument, &flag, 12},
|
2016-03-21 19:34:09 +01:00
|
|
|
{"expect-continue", no_argument, &flag, 13},
|
2016-09-17 15:19:50 +02:00
|
|
|
{"encoder-header-table-size", required_argument, &flag, 14},
|
2014-11-27 15:39:04 +01:00
|
|
|
{nullptr, 0, nullptr, 0}};
|
2013-07-21 16:49:12 +02:00
|
|
|
int option_index = 0;
|
2017-04-30 08:45:53 +02:00
|
|
|
int c =
|
|
|
|
getopt_long(argc, argv, "M:Oab:c:d:m:np:r:hH:vst:uw:yW:", long_options,
|
|
|
|
&option_index);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (c == -1) {
|
2013-07-21 16:49:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (c) {
|
2013-09-16 09:44:13 +02:00
|
|
|
case 'M':
|
|
|
|
// peer-max-concurrent-streams option
|
|
|
|
config.peer_max_concurrent_streams = strtoul(optarg, nullptr, 10);
|
|
|
|
break;
|
2013-07-21 16:49:12 +02:00
|
|
|
case 'O':
|
|
|
|
config.remote_name = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
print_help(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-02-07 15:22:17 +01:00
|
|
|
case 'b':
|
2014-02-15 08:40:32 +01:00
|
|
|
config.padding = strtol(optarg, nullptr, 10);
|
2014-02-07 15:22:17 +01:00
|
|
|
break;
|
2013-07-21 16:49:12 +02:00
|
|
|
case 'n':
|
|
|
|
config.null_out = true;
|
|
|
|
break;
|
2013-08-04 11:55:26 +02:00
|
|
|
case 'p': {
|
2014-04-05 12:15:45 +02:00
|
|
|
errno = 0;
|
2013-08-04 11:55:26 +02:00
|
|
|
auto n = strtoul(optarg, nullptr, 10);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (errno == 0 && NGHTTP2_MIN_WEIGHT <= n && n <= NGHTTP2_MAX_WEIGHT) {
|
2016-07-26 22:10:27 +02:00
|
|
|
config.weight.push_back(n);
|
2013-08-04 11:55:26 +02:00
|
|
|
} else {
|
2014-04-05 12:15:45 +02:00
|
|
|
std::cerr << "-p: specify the integer in the range ["
|
2014-11-27 15:39:04 +01:00
|
|
|
<< NGHTTP2_MIN_WEIGHT << ", " << NGHTTP2_MAX_WEIGHT
|
|
|
|
<< "], inclusive" << std::endl;
|
2013-08-04 11:55:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
case 'r':
|
|
|
|
#ifdef HAVE_JANSSON
|
|
|
|
config.harfile = optarg;
|
2014-11-27 15:39:04 +01:00
|
|
|
#else // !HAVE_JANSSON
|
2014-11-15 14:06:16 +01:00
|
|
|
std::cerr << "[WARNING]: -r, --har option is ignored because\n"
|
2014-11-27 15:39:04 +01:00
|
|
|
<< "the binary was not compiled with libjansson." << std::endl;
|
2014-11-15 14:06:16 +01:00
|
|
|
#endif // !HAVE_JANSSON
|
|
|
|
break;
|
2013-07-21 16:49:12 +02:00
|
|
|
case 'v':
|
2014-11-04 16:42:12 +01:00
|
|
|
++config.verbose;
|
2013-07-21 16:49:12 +02:00
|
|
|
break;
|
|
|
|
case 't':
|
2015-04-28 14:51:28 +02:00
|
|
|
config.timeout = util::parse_duration_with_unit(optarg);
|
|
|
|
if (config.timeout == std::numeric_limits<double>::infinity()) {
|
|
|
|
std::cerr << "-t: bad timeout value: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
break;
|
2013-08-03 11:51:01 +02:00
|
|
|
case 'u':
|
|
|
|
config.upgrade = true;
|
|
|
|
break;
|
2013-10-10 15:04:37 +02:00
|
|
|
case 'w':
|
|
|
|
case 'W': {
|
2013-07-21 16:49:12 +02:00
|
|
|
errno = 0;
|
2013-10-10 15:04:37 +02:00
|
|
|
char *endptr = nullptr;
|
|
|
|
unsigned long int n = strtoul(optarg, &endptr, 10);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (errno == 0 && *endptr == '\0' && n < 31) {
|
|
|
|
if (c == 'w') {
|
2013-10-10 15:04:37 +02:00
|
|
|
config.window_bits = n;
|
|
|
|
} else {
|
|
|
|
config.connection_window_bits = n;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
} else {
|
2013-10-10 15:04:37 +02:00
|
|
|
std::cerr << "-" << static_cast<char>(c)
|
|
|
|
<< ": specify the integer in the range [0, 30], inclusive"
|
2013-07-21 16:49:12 +02:00
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'H': {
|
|
|
|
char *header = optarg;
|
2013-10-24 16:04:28 +02:00
|
|
|
// Skip first possible ':' in the header name
|
2014-11-27 15:39:04 +01:00
|
|
|
char *value = strchr(optarg + 1, ':');
|
|
|
|
if (!value || (header[0] == ':' && header + 1 == value)) {
|
|
|
|
std::cerr << "-H: invalid header: " << optarg << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*value = 0;
|
|
|
|
value++;
|
2014-11-27 15:39:04 +01:00
|
|
|
while (isspace(*value)) {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
if (*value == 0) {
|
2013-07-21 16:49:12 +02:00
|
|
|
// This could also be a valid case for suppressing a header
|
|
|
|
// similar to curl
|
|
|
|
std::cerr << "-H: invalid header - value missing: " << optarg
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-04-14 18:21:27 +02:00
|
|
|
config.headers.emplace_back(header, value, false);
|
2014-10-21 16:24:50 +02:00
|
|
|
util::inp_strlower(config.headers.back().name);
|
2013-07-21 16:49:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a':
|
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
config.get_assets = true;
|
2014-11-27 15:39:04 +01:00
|
|
|
#else // !HAVE_LIBXML2
|
2014-11-15 15:34:37 +01:00
|
|
|
std::cerr << "[WARNING]: -a, --get-assets option is ignored because\n"
|
2014-11-27 15:39:04 +01:00
|
|
|
<< "the binary was not compiled with libxml2." << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
#endif // !HAVE_LIBXML2
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
config.stat = true;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2014-12-21 06:06:44 +01:00
|
|
|
config.datafile = optarg;
|
2013-07-21 16:49:12 +02:00
|
|
|
break;
|
|
|
|
case 'm':
|
2013-09-03 16:09:56 +02:00
|
|
|
config.multiply = strtoul(optarg, nullptr, 10);
|
2013-07-21 16:49:12 +02:00
|
|
|
break;
|
2016-09-17 15:22:59 +02:00
|
|
|
case 'c': {
|
|
|
|
auto n = util::parse_uint_with_unit(optarg);
|
|
|
|
if (n == -1) {
|
2013-10-29 13:34:22 +01:00
|
|
|
std::cerr << "-c: Bad option value: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-09-17 15:22:59 +02:00
|
|
|
if (n > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
std::cerr << "-c: Value too large. It should be less than or equal to "
|
|
|
|
<< std::numeric_limits<uint32_t>::max() << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
config.header_table_size = n;
|
|
|
|
config.min_header_table_size = std::min(config.min_header_table_size, n);
|
2013-10-29 13:34:22 +01:00
|
|
|
break;
|
2016-09-17 15:22:59 +02:00
|
|
|
}
|
2017-04-28 02:53:37 +02:00
|
|
|
case 'y':
|
|
|
|
config.verify_peer = false;
|
|
|
|
break;
|
2013-07-21 16:49:12 +02:00
|
|
|
case '?':
|
2014-01-08 17:27:56 +01:00
|
|
|
util::show_candidates(argv[optind - 1], long_options);
|
2013-07-21 16:49:12 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 0:
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (flag) {
|
2013-07-21 16:49:12 +02:00
|
|
|
case 1:
|
|
|
|
// cert option
|
|
|
|
config.certfile = optarg;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// key option
|
|
|
|
config.keyfile = optarg;
|
|
|
|
break;
|
2013-11-01 15:06:53 +01:00
|
|
|
case 3:
|
|
|
|
// color option
|
|
|
|
color = true;
|
|
|
|
break;
|
2014-02-07 16:22:19 +01:00
|
|
|
case 4:
|
|
|
|
// continuation option
|
|
|
|
config.continuation = true;
|
|
|
|
break;
|
2014-02-16 11:39:41 +01:00
|
|
|
case 5:
|
|
|
|
// version option
|
|
|
|
print_version(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-07-03 15:48:43 +02:00
|
|
|
case 6:
|
|
|
|
// no-content-length option
|
|
|
|
config.no_content_length = true;
|
|
|
|
break;
|
2014-11-15 15:42:26 +01:00
|
|
|
case 7:
|
|
|
|
// no-dep option
|
|
|
|
config.no_dep = true;
|
|
|
|
break;
|
2015-03-07 09:39:31 +01:00
|
|
|
case 9: {
|
|
|
|
// trailer option
|
|
|
|
auto header = optarg;
|
|
|
|
auto value = strchr(optarg, ':');
|
|
|
|
if (!value) {
|
|
|
|
std::cerr << "--trailer: invalid header: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*value = 0;
|
|
|
|
value++;
|
|
|
|
while (isspace(*value)) {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
if (*value == 0) {
|
|
|
|
// This could also be a valid case for suppressing a header
|
|
|
|
// similar to curl
|
|
|
|
std::cerr << "--trailer: invalid header - value missing: " << optarg
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
config.trailer.emplace_back(header, value, false);
|
|
|
|
util::inp_strlower(config.trailer.back().name);
|
|
|
|
break;
|
|
|
|
}
|
2015-03-23 18:30:51 +01:00
|
|
|
case 10:
|
|
|
|
// hexdump option
|
|
|
|
config.hexdump = true;
|
|
|
|
break;
|
2015-04-17 16:34:28 +02:00
|
|
|
case 11:
|
|
|
|
// no-push option
|
|
|
|
config.no_push = true;
|
|
|
|
break;
|
2015-07-07 17:22:21 +02:00
|
|
|
case 12:
|
|
|
|
// max-concurrent-streams option
|
|
|
|
config.max_concurrent_streams = strtoul(optarg, nullptr, 10);
|
|
|
|
break;
|
2016-03-21 19:34:09 +01:00
|
|
|
case 13:
|
|
|
|
// expect-continue option
|
|
|
|
config.expect_continue = true;
|
|
|
|
break;
|
2016-09-17 15:19:50 +02:00
|
|
|
case 14: {
|
|
|
|
// encoder-header-table-size option
|
|
|
|
auto n = util::parse_uint_with_unit(optarg);
|
|
|
|
if (n == -1) {
|
|
|
|
std::cerr << "--encoder-header-table-size: Bad option value: "
|
|
|
|
<< optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (n > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
std::cerr << "--encoder-header-table-size: Value too large. It "
|
|
|
|
"should be less than or equal to "
|
|
|
|
<< std::numeric_limits<uint32_t>::max() << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
config.encoder_header_table_size = n;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 22:10:27 +02:00
|
|
|
int32_t weight_to_fill;
|
|
|
|
if (config.weight.empty()) {
|
|
|
|
weight_to_fill = NGHTTP2_DEFAULT_WEIGHT;
|
|
|
|
} else {
|
|
|
|
weight_to_fill = config.weight.back();
|
|
|
|
}
|
|
|
|
config.weight.insert(std::end(config.weight), argc - optind, weight_to_fill);
|
|
|
|
|
2017-01-17 16:29:51 +01:00
|
|
|
// Find scheme overridden by extra header fields.
|
|
|
|
auto scheme_it =
|
|
|
|
std::find_if(std::begin(config.headers), std::end(config.headers),
|
|
|
|
[](const Header &nv) { return nv.name == ":scheme"; });
|
|
|
|
if (scheme_it != std::end(config.headers)) {
|
|
|
|
config.scheme_override = (*scheme_it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find host and port overridden by extra header fields.
|
|
|
|
auto authority_it =
|
|
|
|
std::find_if(std::begin(config.headers), std::end(config.headers),
|
|
|
|
[](const Header &nv) { return nv.name == ":authority"; });
|
|
|
|
if (authority_it == std::end(config.headers)) {
|
|
|
|
authority_it =
|
|
|
|
std::find_if(std::begin(config.headers), std::end(config.headers),
|
|
|
|
[](const Header &nv) { return nv.name == "host"; });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authority_it != std::end(config.headers)) {
|
|
|
|
// authority_it may looks like "host:port".
|
|
|
|
auto uri = "https://" + (*authority_it).value;
|
|
|
|
http_parser_url u{};
|
|
|
|
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
|
|
|
std::cerr << "[ERROR] Could not parse authority in "
|
|
|
|
<< (*authority_it).name << ": " << (*authority_it).value
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.host_override = util::get_uri_field(uri.c_str(), u, UF_HOST).str();
|
|
|
|
if (util::has_uri_field(u, UF_PORT)) {
|
|
|
|
config.port_override = u.port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 15:06:53 +01:00
|
|
|
set_color_output(color || isatty(fileno(stdout)));
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_option_set_peer_max_concurrent_streams(
|
|
|
|
config.http2_option, config.peer_max_concurrent_streams);
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2016-09-17 15:19:50 +02:00
|
|
|
if (config.encoder_header_table_size != -1) {
|
|
|
|
nghttp2_option_set_max_deflate_dynamic_table_size(
|
|
|
|
config.http2_option, config.encoder_header_table_size);
|
|
|
|
}
|
|
|
|
|
2015-07-19 10:55:37 +02:00
|
|
|
struct sigaction act {};
|
2013-07-21 16:49:12 +02:00
|
|
|
act.sa_handler = SIG_IGN;
|
2013-09-03 16:09:56 +02:00
|
|
|
sigaction(SIGPIPE, &act, nullptr);
|
2013-07-21 16:49:12 +02:00
|
|
|
reset_timer();
|
2014-11-27 15:39:04 +01:00
|
|
|
return run(argv + optind, argc - optind);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
2015-10-17 04:52:33 +02:00
|
|
|
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
|
|
|
}
|