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.
|
|
|
|
*/
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
#include <tuple>
|
2013-09-16 10:36:24 +02:00
|
|
|
#include <chrono>
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
2014-08-02 03:11:45 +02:00
|
|
|
#include <openssl/conf.h>
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
#include <event.h>
|
2013-10-27 15:02:39 +01:00
|
|
|
#include <event2/event.h>
|
2013-07-21 16:49:12 +02:00
|
|
|
#include <event2/bufferevent_ssl.h>
|
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2014-11-15 14:06:16 +01:00
|
|
|
#include <jansson.h>
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
#include "http-parser/http_parser.h"
|
|
|
|
|
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"
|
2014-09-21 14:50:12 +02:00
|
|
|
#include "libevent_util.h"
|
2013-08-03 11:51:01 +02:00
|
|
|
#include "base64.h"
|
2013-12-05 13:54:36 +01:00
|
|
|
#include "http2.h"
|
2014-05-06 16:10:50 +02:00
|
|
|
#include "nghttp2_gzip.h"
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY (0)
|
|
|
|
#endif // O_BINARY
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
struct Config {
|
2014-10-21 16:24:50 +02:00
|
|
|
Headers headers;
|
2013-12-06 15:17:38 +01:00
|
|
|
std::string certfile;
|
|
|
|
std::string keyfile;
|
|
|
|
std::string datafile;
|
2014-11-15 14:06:16 +01:00
|
|
|
std::string harfile;
|
2014-04-04 14:57:47 +02:00
|
|
|
nghttp2_option *http2_option;
|
2013-12-06 15:17:38 +01:00
|
|
|
size_t output_upper_thres;
|
2014-02-15 08:40:32 +01:00
|
|
|
size_t padding;
|
2013-12-06 15:17:38 +01:00
|
|
|
ssize_t peer_max_concurrent_streams;
|
|
|
|
ssize_t header_table_size;
|
2014-03-25 18:04:24 +01:00
|
|
|
int32_t weight;
|
2013-12-06 15:17:38 +01:00
|
|
|
int multiply;
|
|
|
|
// milliseconds
|
|
|
|
int timeout;
|
|
|
|
int window_bits;
|
|
|
|
int connection_window_bits;
|
2014-11-04 16:42:12 +01:00
|
|
|
int verbose;
|
2013-07-21 16:49:12 +02:00
|
|
|
bool null_out;
|
|
|
|
bool remote_name;
|
|
|
|
bool get_assets;
|
|
|
|
bool stat;
|
2013-08-03 11:51:01 +02:00
|
|
|
bool upgrade;
|
2014-02-07 16:22:19 +01:00
|
|
|
bool continuation;
|
2014-07-03 15:48:43 +02:00
|
|
|
bool no_content_length;
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2013-07-28 12:09:15 +02:00
|
|
|
Config()
|
2013-12-06 15:17:38 +01:00
|
|
|
: output_upper_thres(1024*1024),
|
2014-02-15 08:40:32 +01:00
|
|
|
padding(0),
|
2013-12-06 15:17:38 +01:00
|
|
|
peer_max_concurrent_streams(NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS),
|
|
|
|
header_table_size(-1),
|
2014-03-25 18:04:24 +01:00
|
|
|
weight(NGHTTP2_DEFAULT_WEIGHT),
|
2013-07-28 12:09:15 +02:00
|
|
|
multiply(1),
|
|
|
|
timeout(-1),
|
|
|
|
window_bits(-1),
|
2013-10-10 15:04:37 +02:00
|
|
|
connection_window_bits(-1),
|
2014-11-04 16:42:12 +01:00
|
|
|
verbose(0),
|
2013-12-06 15:17:38 +01:00
|
|
|
null_out(false),
|
|
|
|
remote_name(false),
|
|
|
|
get_assets(false),
|
|
|
|
stat(false),
|
2014-02-07 16:22:19 +01:00
|
|
|
upgrade(false),
|
2014-07-03 15:48:43 +02:00
|
|
|
continuation(false),
|
|
|
|
no_content_length(false)
|
2014-04-04 14:57:47 +02:00
|
|
|
{
|
|
|
|
nghttp2_option_new(&http2_option);
|
|
|
|
nghttp2_option_set_peer_max_concurrent_streams
|
|
|
|
(http2_option,
|
|
|
|
peer_max_concurrent_streams);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Config()
|
|
|
|
{
|
|
|
|
nghttp2_option_del(http2_option);
|
|
|
|
}
|
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 10:36:24 +02:00
|
|
|
enum StatStage {
|
|
|
|
STAT_INITIAL,
|
|
|
|
STAT_ON_REQUEST,
|
|
|
|
STAT_ON_RESPONSE,
|
|
|
|
STAT_ON_COMPLETE
|
2013-07-21 16:49:12 +02:00
|
|
|
};
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-09-16 10:36:24 +02:00
|
|
|
struct RequestStat {
|
|
|
|
std::chrono::steady_clock::time_point on_request_time;
|
|
|
|
std::chrono::steady_clock::time_point on_response_time;
|
|
|
|
std::chrono::steady_clock::time_point on_complete_time;
|
|
|
|
StatStage stage;
|
|
|
|
RequestStat():stage(STAT_INITIAL) {}
|
|
|
|
};
|
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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::string strip_fragment(const char *raw_uri)
|
|
|
|
{
|
|
|
|
const char *end;
|
|
|
|
for(end = raw_uri; *end && *end != '#'; ++end);
|
|
|
|
size_t len = end-raw_uri;
|
|
|
|
return std::string(raw_uri, len);
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
namespace {
|
|
|
|
struct Request;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct Dependency {
|
|
|
|
std::vector<std::vector<Request*>> deps;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
struct Request {
|
2014-01-16 15:41:13 +01:00
|
|
|
Headers res_nva;
|
2014-11-15 14:06:16 +01:00
|
|
|
Headers req_nva;
|
2013-07-21 16:49:12 +02:00
|
|
|
// URI without fragment
|
|
|
|
std::string uri;
|
|
|
|
http_parser_url u;
|
2014-03-25 18:04:24 +01:00
|
|
|
std::shared_ptr<Dependency> dep;
|
|
|
|
nghttp2_priority_spec pri_spec;
|
2013-12-06 15:17:38 +01:00
|
|
|
RequestStat stat;
|
|
|
|
int64_t data_length;
|
|
|
|
int64_t data_offset;
|
2014-11-15 14:06:16 +01:00
|
|
|
// Number of bytes received from server
|
|
|
|
int64_t response_len;
|
2013-07-21 16:49:12 +02:00
|
|
|
nghttp2_gzip *inflater;
|
|
|
|
HtmlParser *html_parser;
|
|
|
|
const nghttp2_data_provider *data_prd;
|
2014-03-25 18:04:24 +01:00
|
|
|
int32_t stream_id;
|
2014-08-08 16:03:12 +02:00
|
|
|
int status;
|
2013-07-21 16:49:12 +02:00
|
|
|
// Recursion level: 0: first entity, 1: entity linked from first entity
|
|
|
|
int level;
|
2014-03-25 18:04:24 +01:00
|
|
|
// RequestPriority value defined in HtmlParser.h
|
|
|
|
int pri;
|
2014-08-08 16:03:12 +02:00
|
|
|
bool expect_final_response;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
// For pushed request, |uri| is empty and |u| is zero-cleared.
|
2013-07-21 16:49:12 +02:00
|
|
|
Request(const std::string& uri, const http_parser_url &u,
|
|
|
|
const nghttp2_data_provider *data_prd, int64_t data_length,
|
2014-03-25 18:04:24 +01:00
|
|
|
const nghttp2_priority_spec& pri_spec,
|
2014-05-07 16:24:07 +02:00
|
|
|
std::shared_ptr<Dependency> dep, int pri = 0, int level = 0)
|
2013-12-06 15:17:38 +01:00
|
|
|
: uri(uri),
|
|
|
|
u(u),
|
2014-03-25 18:04:24 +01:00
|
|
|
dep(std::move(dep)),
|
|
|
|
pri_spec(pri_spec),
|
2013-12-06 15:17:38 +01:00
|
|
|
data_length(data_length),
|
|
|
|
data_offset(0),
|
2014-11-15 14:06:16 +01:00
|
|
|
response_len(0),
|
2013-12-06 15:17:38 +01:00
|
|
|
inflater(nullptr),
|
|
|
|
html_parser(nullptr),
|
|
|
|
data_prd(data_prd),
|
2014-03-25 18:04:24 +01:00
|
|
|
stream_id(-1),
|
2014-08-08 16:03:12 +02:00
|
|
|
status(0),
|
2014-03-25 18:04:24 +01:00
|
|
|
level(level),
|
2014-08-08 16:03:12 +02:00
|
|
|
pri(pri),
|
|
|
|
expect_final_response(false)
|
2013-07-21 16:49:12 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
~Request()
|
|
|
|
{
|
|
|
|
nghttp2_gzip_inflate_del(inflater);
|
|
|
|
delete html_parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_inflater()
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
rv = nghttp2_gzip_inflate_new(&inflater);
|
|
|
|
assert(rv == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_html_parser()
|
|
|
|
{
|
|
|
|
html_parser = new HtmlParser(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
int update_html_parser(const uint8_t *data, size_t len, int fin)
|
|
|
|
{
|
|
|
|
if(!html_parser) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int rv;
|
|
|
|
rv = html_parser->parse_chunk(reinterpret_cast<const char*>(data), len,
|
|
|
|
fin);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string make_reqpath() const
|
|
|
|
{
|
2014-02-27 13:47:04 +01:00
|
|
|
std::string path = util::has_uri_field(u, UF_PATH) ?
|
|
|
|
util::get_uri_field(uri.c_str(), u, UF_PATH) : "/";
|
|
|
|
if(util::has_uri_field(u, UF_QUERY)) {
|
2013-07-21 16:49:12 +02:00
|
|
|
path += "?";
|
|
|
|
path.append(uri.c_str()+u.field_data[UF_QUERY].off,
|
|
|
|
u.field_data[UF_QUERY].len);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
int32_t find_dep_stream_id(int start)
|
|
|
|
{
|
|
|
|
for(auto i = start; i >= 0; --i) {
|
|
|
|
for(auto req : dep->deps[i]) {
|
2014-04-27 07:48:43 +02:00
|
|
|
return req->stream_id;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_priority_spec resolve_dep(int32_t pri)
|
|
|
|
{
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec pri_spec;
|
2014-03-25 18:04:24 +01:00
|
|
|
int exclusive = 0;
|
|
|
|
int32_t stream_id = -1;
|
|
|
|
|
2014-04-25 14:23:47 +02:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec);
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
if(pri == 0) {
|
|
|
|
return pri_spec;
|
|
|
|
}
|
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec);
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
auto start = std::min(pri, (int)dep->deps.size() - 1);
|
|
|
|
|
|
|
|
for(auto i = start; i >= 0; --i) {
|
|
|
|
if(dep->deps[i][0]->pri < pri) {
|
|
|
|
stream_id = find_dep_stream_id(i);
|
|
|
|
|
|
|
|
if(i != (int)dep->deps.size() - 1) {
|
|
|
|
exclusive = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else if(dep->deps[i][0]->pri == pri) {
|
|
|
|
stream_id = find_dep_stream_id(i - 1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stream_id == -1) {
|
|
|
|
return pri_spec;
|
|
|
|
}
|
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, stream_id, NGHTTP2_DEFAULT_WEIGHT,
|
|
|
|
exclusive);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
return pri_spec;
|
|
|
|
}
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
bool is_ipv6_literal_addr() const
|
|
|
|
{
|
2014-02-27 13:47:04 +01:00
|
|
|
if(util::has_uri_field(u, UF_HOST)) {
|
2013-07-21 16:49:12 +02:00
|
|
|
return memchr(uri.c_str()+u.field_data[UF_HOST].off, ':',
|
|
|
|
u.field_data[UF_HOST].len);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:03:12 +02:00
|
|
|
bool response_pseudo_header_allowed() const
|
|
|
|
{
|
|
|
|
return res_nva.empty() || res_nva.back().name.c_str()[0] == ':';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool push_request_pseudo_header_allowed() const
|
|
|
|
{
|
2014-11-15 14:06:16 +01:00
|
|
|
return res_nva.empty() || req_nva.back().name.c_str()[0] == ':';
|
2014-08-08 16:03:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 10:36:24 +02:00
|
|
|
void record_request_time()
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
2013-09-16 10:36:24 +02:00
|
|
|
stat.stage = STAT_ON_REQUEST;
|
|
|
|
stat.on_request_time = get_time();
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 10:36:24 +02:00
|
|
|
void record_response_time()
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
2013-09-16 10:36:24 +02:00
|
|
|
stat.stage = STAT_ON_RESPONSE;
|
|
|
|
stat.on_response_time = get_time();
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void record_complete_time()
|
|
|
|
{
|
2013-09-16 10:36:24 +02:00
|
|
|
stat.stage = STAT_ON_COMPLETE;
|
|
|
|
stat.on_complete_time = get_time();
|
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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
struct SessionStat {
|
2014-11-15 14:06:16 +01:00
|
|
|
// The point in time when download was started.
|
|
|
|
std::chrono::system_clock::time_point started_system_time;
|
|
|
|
// The point of time when download was started.
|
|
|
|
std::chrono::steady_clock::time_point on_started_time;
|
|
|
|
// The point of time when DNS resolution was completed.
|
|
|
|
std::chrono::steady_clock::time_point on_dns_complete_time;
|
|
|
|
// The point of time when connection was established or SSL/TLS
|
|
|
|
// handshake was completed.
|
|
|
|
std::chrono::steady_clock::time_point on_connect_time;
|
|
|
|
// The point of time when HTTP/2 commnucation was started.
|
2013-09-16 10:36:24 +02:00
|
|
|
std::chrono::steady_clock::time_point on_handshake_time;
|
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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
Config config;
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
namespace {
|
|
|
|
size_t populate_settings(nghttp2_settings_entry *iv)
|
|
|
|
{
|
2014-06-07 09:04:43 +02:00
|
|
|
size_t niv = 2;
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
|
|
|
|
iv[0].value = 100;
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
|
|
if(config.window_bits != -1) {
|
2013-08-03 12:53:07 +02:00
|
|
|
iv[1].value = (1 << config.window_bits) - 1;
|
2013-08-03 11:51:01 +02:00
|
|
|
} else {
|
|
|
|
iv[1].value = NGHTTP2_INITIAL_WINDOW_SIZE;
|
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2013-10-29 13:34:22 +01:00
|
|
|
if(config.header_table_size >= 0) {
|
|
|
|
iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
|
|
|
|
iv[niv].value = config.header_table_size;
|
|
|
|
++niv;
|
|
|
|
}
|
2013-08-03 11:51:01 +02:00
|
|
|
return niv;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
|
|
|
void eventcb(bufferevent *bev, short events, void *ptr);
|
|
|
|
} // namespace
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
namespace {
|
|
|
|
extern http_parser_settings htp_hooks;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void upgrade_readcb(bufferevent *bev, void *ptr);
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
|
|
|
void readcb(bufferevent *bev, void *ptr);
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void writecb(bufferevent *bev, void *ptr);
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
struct HttpClient;
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
namespace {
|
2013-11-16 13:13:09 +01:00
|
|
|
int submit_request
|
2014-10-21 16:24:50 +02:00
|
|
|
(HttpClient *client, const Headers& headers, Request *req);
|
2013-07-21 16:49:12 +02:00
|
|
|
} // namespace
|
|
|
|
|
2013-10-27 15:02:39 +01:00
|
|
|
namespace {
|
|
|
|
void settings_timeout_cb(evutil_socket_t fd, short what, void *arg);
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
enum client_state {
|
|
|
|
STATE_IDLE,
|
|
|
|
STATE_CONNECTED
|
|
|
|
};
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
struct HttpClient {
|
|
|
|
std::vector<std::unique_ptr<Request>> reqvec;
|
|
|
|
// Insert path already added in reqvec to prevent multiple request
|
|
|
|
// for 1 resource.
|
|
|
|
std::set<std::string> path_cache;
|
2013-08-12 15:33:51 +02:00
|
|
|
std::string scheme;
|
2014-01-22 16:29:09 +01:00
|
|
|
std::string host;
|
2013-07-21 16:49:12 +02:00
|
|
|
std::string hostport;
|
2013-08-03 11:51:01 +02:00
|
|
|
// Used for parse the HTTP upgrade response from server
|
2013-09-03 16:09:56 +02:00
|
|
|
std::unique_ptr<http_parser> htp;
|
2013-12-06 15:17:38 +01:00
|
|
|
SessionStat stat;
|
|
|
|
nghttp2_session *session;
|
|
|
|
const nghttp2_session_callbacks *callbacks;
|
|
|
|
event_base *evbase;
|
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
SSL *ssl;
|
|
|
|
bufferevent *bev;
|
|
|
|
event *settings_timerev;
|
2014-01-22 16:29:09 +01:00
|
|
|
addrinfo *addrs;
|
|
|
|
addrinfo *next_addr;
|
2013-12-06 15:17:38 +01:00
|
|
|
// The number of completed requests, including failed ones.
|
|
|
|
size_t complete;
|
|
|
|
// The length of settings_payload
|
|
|
|
size_t settings_payloadlen;
|
|
|
|
client_state state;
|
|
|
|
// The HTTP status code of the response message of HTTP Upgrade.
|
|
|
|
unsigned int upgrade_response_status_code;
|
2013-08-03 11:51:01 +02:00
|
|
|
// true if the response message of HTTP Upgrade request is fully
|
|
|
|
// received. It is not relevant the upgrade succeeds, or not.
|
|
|
|
bool upgrade_response_complete;
|
|
|
|
// SETTINGS payload sent as token68 in HTTP Upgrade
|
2013-12-09 13:12:09 +01:00
|
|
|
uint8_t settings_payload[128];
|
2013-08-03 11:51:01 +02:00
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
HttpClient(const nghttp2_session_callbacks* callbacks,
|
|
|
|
event_base *evbase, SSL_CTX *ssl_ctx)
|
|
|
|
: session(nullptr),
|
|
|
|
callbacks(callbacks),
|
|
|
|
evbase(evbase),
|
|
|
|
ssl_ctx(ssl_ctx),
|
|
|
|
ssl(nullptr),
|
|
|
|
bev(nullptr),
|
2013-10-27 15:02:39 +01:00
|
|
|
settings_timerev(nullptr),
|
2014-01-22 16:29:09 +01:00
|
|
|
addrs(nullptr),
|
|
|
|
next_addr(nullptr),
|
2013-08-03 11:51:01 +02:00
|
|
|
complete(0),
|
2013-12-06 15:17:38 +01:00
|
|
|
settings_payloadlen(0),
|
|
|
|
state(STATE_IDLE),
|
2013-09-07 09:38:21 +02:00
|
|
|
upgrade_response_status_code(0),
|
2013-12-06 15:17:38 +01:00
|
|
|
upgrade_response_complete(false)
|
2013-07-21 16:49:12 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
~HttpClient()
|
|
|
|
{
|
|
|
|
disconnect();
|
2014-01-27 17:40:00 +01:00
|
|
|
if(addrs) {
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
addrs = nullptr;
|
|
|
|
next_addr = nullptr;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 15:33:51 +02:00
|
|
|
bool need_upgrade() const
|
|
|
|
{
|
|
|
|
return config.upgrade && scheme == "http";
|
|
|
|
}
|
|
|
|
|
2014-01-22 16:29:09 +01:00
|
|
|
int resolve_host(const std::string& host, uint16_t port)
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
|
|
|
int rv;
|
2014-01-27 17:40:00 +01:00
|
|
|
addrinfo hints;
|
2014-01-22 16:29:09 +01:00
|
|
|
this->host = host;
|
2014-01-27 17:40:00 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
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);
|
2014-01-22 16:29:09 +01:00
|
|
|
if(rv != 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] getaddrinfo() failed: "
|
2014-01-22 16:29:09 +01:00
|
|
|
<< gai_strerror(rv) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(addrs == nullptr) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] No address returned" << std::endl;
|
2014-01-22 16:29:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
next_addr = addrs;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int initiate_connection()
|
|
|
|
{
|
|
|
|
int rv = 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
if(ssl_ctx) {
|
|
|
|
// We are establishing TLS connection.
|
|
|
|
ssl = SSL_new(ssl_ctx);
|
|
|
|
if(!ssl) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] SSL_new() failed: "
|
2013-07-21 16:49:12 +02:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user overrode the host header, use that value for the
|
|
|
|
// SNI extension
|
|
|
|
const char *host_string = nullptr;
|
2013-11-16 13:13:09 +01:00
|
|
|
auto i = std::find_if(std::begin(config.headers),
|
|
|
|
std::end(config.headers),
|
2014-10-21 16:24:50 +02:00
|
|
|
[](const Header& nv)
|
2013-11-16 13:13:09 +01:00
|
|
|
{
|
2014-10-21 16:24:50 +02:00
|
|
|
return "host" == nv.name;
|
2013-11-16 13:13:09 +01:00
|
|
|
});
|
|
|
|
if ( i != std::end(config.headers) ) {
|
2014-10-21 16:24:50 +02:00
|
|
|
host_string = (*i).value.c_str();
|
2013-07-21 16:49:12 +02:00
|
|
|
} else {
|
|
|
|
host_string = host.c_str();
|
|
|
|
}
|
2014-03-15 07:32:38 +01:00
|
|
|
|
|
|
|
if (!util::numeric_host(host_string)) {
|
|
|
|
SSL_set_tlsext_host_name(ssl, host_string);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-03-15 07:32:38 +01:00
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
bev = bufferevent_openssl_socket_new(evbase, -1, ssl,
|
|
|
|
BUFFEREVENT_SSL_CONNECTING,
|
|
|
|
BEV_OPT_DEFER_CALLBACKS);
|
|
|
|
} else {
|
|
|
|
bev = bufferevent_socket_new(evbase, -1, BEV_OPT_DEFER_CALLBACKS);
|
|
|
|
}
|
2014-01-27 17:40:00 +01:00
|
|
|
rv = -1;
|
2014-01-22 16:29:09 +01:00
|
|
|
while(next_addr) {
|
|
|
|
rv = bufferevent_socket_connect
|
|
|
|
(bev, next_addr->ai_addr, next_addr->ai_addrlen);
|
2014-01-27 17:40:00 +01:00
|
|
|
next_addr = next_addr->ai_next;
|
2014-01-22 16:29:09 +01:00
|
|
|
if(rv == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bufferevent_enable(bev, EV_READ);
|
2013-08-12 15:33:51 +02:00
|
|
|
if(need_upgrade()) {
|
2013-09-03 16:09:56 +02:00
|
|
|
htp = util::make_unique<http_parser>();
|
|
|
|
http_parser_init(htp.get(), HTTP_RESPONSE);
|
2013-08-03 11:51:01 +02:00
|
|
|
htp->data = this;
|
|
|
|
bufferevent_setcb(bev, upgrade_readcb, nullptr, eventcb, this);
|
|
|
|
} else {
|
|
|
|
bufferevent_setcb(bev, readcb, writecb, eventcb, this);
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
if(config.timeout != -1) {
|
|
|
|
timeval tv = { config.timeout, 0 };
|
|
|
|
bufferevent_set_timeouts(bev, &tv, &tv);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disconnect()
|
|
|
|
{
|
2013-12-22 11:23:01 +01:00
|
|
|
int fd = -1;
|
2013-07-21 16:49:12 +02:00
|
|
|
state = STATE_IDLE;
|
|
|
|
nghttp2_session_del(session);
|
|
|
|
session = nullptr;
|
|
|
|
if(ssl) {
|
2013-12-22 11:23:01 +01:00
|
|
|
fd = SSL_get_fd(ssl);
|
2014-01-08 15:32:47 +01:00
|
|
|
SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN);
|
2013-07-21 16:49:12 +02:00
|
|
|
SSL_shutdown(ssl);
|
|
|
|
}
|
|
|
|
if(bev) {
|
|
|
|
bufferevent_disable(bev, EV_READ | EV_WRITE);
|
|
|
|
bufferevent_free(bev);
|
|
|
|
bev = nullptr;
|
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
if(settings_timerev) {
|
|
|
|
event_free(settings_timerev);
|
|
|
|
settings_timerev = nullptr;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
if(ssl) {
|
|
|
|
SSL_free(ssl);
|
|
|
|
ssl = nullptr;
|
|
|
|
}
|
2013-12-22 11:23:01 +01:00
|
|
|
if(fd != -1) {
|
|
|
|
shutdown(fd, SHUT_WR);
|
|
|
|
close(fd);
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
int on_upgrade_connect()
|
|
|
|
{
|
2013-09-07 09:38:21 +02:00
|
|
|
ssize_t rv;
|
2013-08-03 11:51:01 +02:00
|
|
|
record_handshake_time();
|
|
|
|
assert(!reqvec.empty());
|
2014-05-02 16:34:57 +02:00
|
|
|
nghttp2_settings_entry iv[32];
|
2013-08-03 11:51:01 +02:00
|
|
|
size_t niv = populate_settings(iv);
|
|
|
|
assert(sizeof(settings_payload) >= 8*niv);
|
2013-09-09 14:25:46 +02:00
|
|
|
rv = nghttp2_pack_settings_payload(settings_payload,
|
|
|
|
sizeof(settings_payload), iv, niv);
|
2013-09-07 09:38:21 +02:00
|
|
|
if(rv < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
settings_payloadlen = rv;
|
2013-08-03 11:51:01 +02:00
|
|
|
auto token68 = base64::encode(&settings_payload[0],
|
|
|
|
&settings_payload[settings_payloadlen]);
|
|
|
|
util::to_token68(token68);
|
|
|
|
std::string req;
|
|
|
|
if(reqvec[0]->data_prd) {
|
|
|
|
// If the request contains upload data, use OPTIONS * to upgrade
|
|
|
|
req = "OPTIONS *";
|
|
|
|
} else {
|
|
|
|
req = "GET ";
|
|
|
|
req += reqvec[0]->make_reqpath();
|
|
|
|
}
|
|
|
|
req += " HTTP/1.1\r\n"
|
|
|
|
"Host: ";
|
|
|
|
req += hostport;
|
2013-09-03 16:09:56 +02:00
|
|
|
req += "\r\n"
|
|
|
|
"Connection: Upgrade, HTTP2-Settings\r\n"
|
2014-03-30 14:30:47 +02:00
|
|
|
"Upgrade: " NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "\r\n"
|
2013-08-03 11:51:01 +02:00
|
|
|
"HTTP2-Settings: ";
|
|
|
|
req += token68;
|
2013-09-03 16:09:56 +02:00
|
|
|
req += "\r\n"
|
|
|
|
"Accept: */*\r\n"
|
2013-08-03 11:51:01 +02:00
|
|
|
"User-Agent: nghttp2/" NGHTTP2_VERSION "\r\n"
|
|
|
|
"\r\n";
|
|
|
|
bufferevent_write(bev, req.c_str(), req.size());
|
|
|
|
if(config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " HTTP Upgrade request\n"
|
|
|
|
<< req << std::endl;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int on_upgrade_read()
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
auto input = bufferevent_get_input(bev);
|
|
|
|
|
2014-06-01 14:01:01 +02:00
|
|
|
for(;;) {
|
|
|
|
auto inputlen = evbuffer_get_contiguous_space(input);
|
|
|
|
|
|
|
|
if(inputlen == 0) {
|
|
|
|
assert(evbuffer_get_length(input) == 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mem = evbuffer_pullup(input, inputlen);
|
|
|
|
|
|
|
|
auto nread = http_parser_execute(htp.get(), &htp_hooks,
|
|
|
|
reinterpret_cast<const char*>(mem),
|
|
|
|
inputlen);
|
|
|
|
|
|
|
|
if(config.verbose) {
|
|
|
|
std::cout.write(reinterpret_cast<const char*>(mem), nread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evbuffer_drain(input, nread) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto htperr = HTTP_PARSER_ERRNO(htp.get());
|
|
|
|
|
|
|
|
if(htperr != HPE_OK) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Failed to parse HTTP Upgrade response header: "
|
2014-06-01 14:01:01 +02:00
|
|
|
<< "(" << http_errno_name(htperr) << ") "
|
|
|
|
<< http_errno_description(htperr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
if(upgrade_response_complete) {
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
if(config.verbose) {
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
if(upgrade_response_status_code == 101) {
|
|
|
|
if(config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " HTTP Upgrade success" << std::endl;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
bufferevent_setcb(bev, readcb, writecb, eventcb, this);
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
rv = on_connect();
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-04 11:43:10 +02:00
|
|
|
// Read remaining data in the buffer because it is not
|
|
|
|
// notified callback anymore.
|
|
|
|
rv = on_read();
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2013-08-04 11:43:10 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
|
|
|
return 0;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] HTTP Upgrade failed" << std::endl;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
|
|
|
return -1;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
int on_connect()
|
|
|
|
{
|
|
|
|
int rv;
|
2013-08-12 15:33:51 +02:00
|
|
|
if(!need_upgrade()) {
|
2013-08-03 11:51:01 +02:00
|
|
|
record_handshake_time();
|
|
|
|
}
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2013-11-07 16:12:39 +01:00
|
|
|
rv = nghttp2_session_client_new2(&session, callbacks, this,
|
2014-04-04 14:57:47 +02:00
|
|
|
config.http2_option);
|
|
|
|
|
2013-09-14 12:41:49 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-12 15:33:51 +02:00
|
|
|
if(need_upgrade()) {
|
2013-08-03 11:51:01 +02:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
rv = nghttp2_session_upgrade(session, settings_payload,
|
|
|
|
settings_payloadlen, stream_user_data);
|
|
|
|
if(rv != 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: "
|
2013-08-03 11:51:01 +02:00
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(stream_user_data) {
|
2014-05-07 16:24:07 +02:00
|
|
|
stream_user_data->stream_id = 1;
|
|
|
|
on_request(stream_user_data);
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
// Send connection header here
|
2014-03-30 14:02:25 +02:00
|
|
|
bufferevent_write(bev, NGHTTP2_CLIENT_CONNECTION_PREFACE,
|
|
|
|
NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN);
|
2013-08-03 11:51:01 +02:00
|
|
|
// If upgrade succeeds, the SETTINGS value sent with
|
|
|
|
// HTTP2-Settings header field has already been submitted to
|
|
|
|
// session object.
|
2013-08-12 15:33:51 +02:00
|
|
|
if(!need_upgrade()) {
|
2013-08-03 11:51:01 +02:00
|
|
|
nghttp2_settings_entry iv[16];
|
|
|
|
auto niv = populate_settings(iv);
|
2013-10-25 15:50:24 +02:00
|
|
|
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
|
2013-08-03 11:51:01 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
assert(settings_timerev == nullptr);
|
|
|
|
settings_timerev = evtimer_new(evbase, settings_timeout_cb, this);
|
|
|
|
// SETTINGS ACK timeout is 10 seconds for now
|
|
|
|
timeval settings_timeout = { 10, 0 };
|
|
|
|
evtimer_add(settings_timerev, &settings_timeout);
|
|
|
|
|
2013-10-10 15:04:37 +02:00
|
|
|
if(config.connection_window_bits != -1) {
|
2013-10-12 10:02:37 +02:00
|
|
|
int32_t wininc = (1 << config.connection_window_bits) - 1
|
|
|
|
- NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
|
2013-10-10 15:04:37 +02:00
|
|
|
rv = nghttp2_submit_window_update
|
2013-10-12 10:02:37 +02:00
|
|
|
(session, NGHTTP2_FLAG_NONE, 0, wininc);
|
2013-10-10 15:04:37 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 11:51:01 +02:00
|
|
|
// Adjust first request depending on the existence of the upload
|
|
|
|
// data
|
2013-08-12 15:33:51 +02:00
|
|
|
for(auto i = std::begin(reqvec)+(need_upgrade() && !reqvec[0]->data_prd);
|
2013-08-03 11:51:01 +02:00
|
|
|
i != std::end(reqvec); ++i) {
|
2013-10-24 16:04:28 +02:00
|
|
|
if(submit_request(this, config.headers, (*i).get()) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
return on_write();
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int on_read()
|
|
|
|
{
|
2014-02-19 15:12:32 +01:00
|
|
|
int rv;
|
|
|
|
auto input = bufferevent_get_input(bev);
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-06-01 14:01:01 +02:00
|
|
|
for(;;) {
|
|
|
|
auto inputlen = evbuffer_get_contiguous_space(input);
|
2014-02-19 15:12:32 +01:00
|
|
|
|
2014-06-01 14:01:01 +02:00
|
|
|
if(inputlen == 0) {
|
|
|
|
assert(evbuffer_get_length(input) == 0);
|
|
|
|
|
|
|
|
return on_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mem = evbuffer_pullup(input, inputlen);
|
|
|
|
|
|
|
|
rv = nghttp2_session_mem_recv(session, mem, inputlen);
|
|
|
|
|
|
|
|
if(rv < 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] nghttp2_session_mem_recv() returned error: "
|
2014-06-01 14:01:01 +02:00
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evbuffer_drain(input, rv) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 15:12:32 +01:00
|
|
|
int on_write()
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
|
|
|
int rv;
|
2014-03-04 16:23:33 +01:00
|
|
|
uint8_t buf[4096];
|
2013-09-03 16:09:56 +02:00
|
|
|
auto output = bufferevent_get_output(bev);
|
2014-03-04 16:23:33 +01:00
|
|
|
util::EvbufferBuffer evbbuf(output, buf, sizeof(buf));
|
2014-02-19 15:12:32 +01:00
|
|
|
for(;;) {
|
2014-03-04 16:23:33 +01:00
|
|
|
if(evbuffer_get_length(output) + evbbuf.get_buflen() >
|
|
|
|
config.output_upper_thres) {
|
2014-02-19 15:12:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-02-19 15:12:32 +01:00
|
|
|
const uint8_t *data;
|
|
|
|
auto datalen = nghttp2_session_mem_send(session, &data);
|
|
|
|
|
|
|
|
if(datalen < 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] nghttp2_session_mem_send() returned error: "
|
2014-02-19 15:12:32 +01:00
|
|
|
<< nghttp2_strerror(datalen) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(datalen == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2014-03-04 16:23:33 +01:00
|
|
|
rv = evbbuf.add(data, datalen);
|
|
|
|
if(rv != 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] evbuffer_add() failed" << std::endl;
|
2014-02-19 15:12:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 16:23:33 +01:00
|
|
|
rv = evbbuf.flush();
|
|
|
|
if(rv != 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] evbuffer_add() failed" << std::endl;
|
2014-03-04 16:23:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-19 15:12:32 +01:00
|
|
|
if(nghttp2_session_want_read(session) == 0 &&
|
|
|
|
nghttp2_session_want_write(session) == 0 &&
|
|
|
|
evbuffer_get_length(output) == 0) {
|
|
|
|
return -1;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-02-19 15:12:32 +01:00
|
|
|
return 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool all_requests_processed() const
|
|
|
|
{
|
|
|
|
return complete == reqvec.size();
|
|
|
|
}
|
|
|
|
void update_hostport()
|
|
|
|
{
|
|
|
|
if(reqvec.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 13:47:04 +01:00
|
|
|
scheme = util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u,
|
|
|
|
UF_SCHEMA);
|
2013-07-21 16:49:12 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
if(reqvec[0]->is_ipv6_literal_addr()) {
|
|
|
|
ss << "[";
|
2014-02-27 13:47:04 +01:00
|
|
|
util::write_uri_field(ss, reqvec[0]->uri.c_str(), reqvec[0]->u, UF_HOST);
|
2013-07-21 16:49:12 +02:00
|
|
|
ss << "]";
|
|
|
|
} else {
|
2014-02-27 13:47:04 +01:00
|
|
|
util::write_uri_field(ss, reqvec[0]->uri.c_str(), reqvec[0]->u, UF_HOST);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-02-27 13:47:04 +01:00
|
|
|
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)) {
|
2013-07-21 16:49:12 +02:00
|
|
|
ss << ":" << reqvec[0]->u.port;
|
|
|
|
}
|
|
|
|
hostport = ss.str();
|
|
|
|
}
|
|
|
|
bool add_request(const std::string& uri,
|
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
int64_t data_length,
|
2014-03-25 18:04:24 +01:00
|
|
|
const nghttp2_priority_spec& pri_spec,
|
|
|
|
std::shared_ptr<Dependency> dep,
|
2014-05-07 16:24:07 +02:00
|
|
|
int pri = 0, int level = 0)
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
|
|
|
http_parser_url u;
|
2014-02-27 14:11:31 +01:00
|
|
|
memset(&u, 0, sizeof(u));
|
2013-09-07 09:38:21 +02:00
|
|
|
if(http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
if(path_cache.count(uri)) {
|
|
|
|
return false;
|
2014-06-12 15:48:40 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-06-12 15:48:40 +02:00
|
|
|
if(config.multiply == 1) {
|
|
|
|
path_cache.insert(uri);
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-06-12 15:48:40 +02:00
|
|
|
|
|
|
|
reqvec.push_back(util::make_unique<Request>(uri, u, data_prd,
|
|
|
|
data_length,
|
|
|
|
pri_spec, std::move(dep),
|
|
|
|
pri, level));
|
|
|
|
return true;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
void record_handshake_time()
|
|
|
|
{
|
2013-09-16 10:36:24 +02:00
|
|
|
stat.on_handshake_time = get_time();
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2014-11-15 14:06:16 +01:00
|
|
|
void record_started_time()
|
|
|
|
{
|
|
|
|
stat.started_system_time = std::chrono::system_clock::now();
|
|
|
|
stat.on_started_time = get_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
void record_dns_complete_time()
|
|
|
|
{
|
|
|
|
stat.on_dns_complete_time = get_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
void record_connect_time()
|
|
|
|
{
|
|
|
|
stat.on_connect_time = get_time();
|
|
|
|
}
|
|
|
|
|
2014-05-07 16:24:07 +02:00
|
|
|
void on_request(Request *req)
|
|
|
|
{
|
|
|
|
req->record_request_time();
|
|
|
|
|
|
|
|
if(req->pri == 0 && req->dep) {
|
|
|
|
assert(req->dep->deps.empty());
|
|
|
|
|
|
|
|
req->dep->deps.push_back(std::vector<Request*>{req});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(req->stream_id % 2 == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto itr = std::begin(req->dep->deps);
|
|
|
|
for(; itr != std::end(req->dep->deps); ++itr) {
|
|
|
|
if((*itr)[0]->pri == req->pri) {
|
|
|
|
(*itr).push_back(req);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((*itr)[0]->pri > req->pri) {
|
|
|
|
auto v = std::vector<Request*>{req};
|
|
|
|
req->dep->deps.insert(itr, std::move(v));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(itr == std::end(req->dep->deps)) {
|
|
|
|
req->dep->deps.push_back(std::vector<Request*>{req});
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_JANSSON
|
|
|
|
void output_har(FILE *outfile)
|
|
|
|
{
|
|
|
|
static auto PAGE_ID = "page_0";
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
|
|
|
auto creator = json_object();
|
|
|
|
json_object_set_new(log, "creator", creator);
|
|
|
|
|
|
|
|
json_object_set_new(creator, "name", json_string("nghttp"));
|
|
|
|
json_object_set_new(creator, "version", json_string(NGHTTP2_VERSION));
|
|
|
|
|
|
|
|
auto pages = json_array();
|
|
|
|
json_object_set_new(log, "pages", pages);
|
|
|
|
|
|
|
|
auto page = json_object();
|
|
|
|
json_array_append_new(pages, page);
|
|
|
|
|
|
|
|
json_object_set_new
|
|
|
|
(page, "startedDateTime",
|
|
|
|
json_string(util::format_iso8601(stat.started_system_time).c_str()));
|
|
|
|
json_object_set_new(page, "id", json_string(PAGE_ID));
|
|
|
|
json_object_set_new(page, "title", json_string(""));
|
|
|
|
|
|
|
|
json_object_set_new(page, "pageTimings", json_object());
|
|
|
|
|
|
|
|
auto entries = json_array();
|
|
|
|
json_object_set_new(log, "entries", entries);
|
|
|
|
|
|
|
|
auto dns_delta = std::chrono::duration_cast
|
|
|
|
<std::chrono::microseconds>
|
|
|
|
(stat.on_dns_complete_time - stat.on_started_time).count() / 1000.0;
|
|
|
|
auto connect_delta = std::chrono::duration_cast
|
|
|
|
<std::chrono::microseconds>
|
|
|
|
(stat.on_connect_time - stat.on_dns_complete_time).count() / 1000.0;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < reqvec.size(); ++i) {
|
|
|
|
auto& req = reqvec[i];
|
|
|
|
|
|
|
|
if(req->stat.stage != STAT_ON_COMPLETE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto entry = json_object();
|
|
|
|
json_array_append_new(entries, entry);
|
|
|
|
|
|
|
|
auto& req_stat = req->stat;
|
|
|
|
auto request_time = (i == 0) ? stat.started_system_time :
|
|
|
|
stat.started_system_time +
|
|
|
|
(req_stat.on_request_time - stat.on_started_time);
|
|
|
|
|
|
|
|
auto wait_delta = std::chrono::duration_cast
|
|
|
|
<std::chrono::microseconds>
|
|
|
|
(req_stat.on_response_time - req_stat.on_request_time)
|
|
|
|
.count() / 1000.0;
|
|
|
|
auto receive_delta = std::chrono::duration_cast
|
|
|
|
<std::chrono::microseconds>
|
|
|
|
(req_stat.on_complete_time - req_stat.on_response_time)
|
|
|
|
.count() / 1000.0;
|
|
|
|
|
|
|
|
auto time_sum = std::chrono::duration_cast<std::chrono::microseconds>
|
|
|
|
((i == 0) ? (req_stat.on_complete_time - stat.on_started_time) :
|
|
|
|
(req_stat.on_complete_time - req_stat.on_request_time))
|
|
|
|
.count() / 1000.0;
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
auto request = json_object();
|
|
|
|
json_object_set_new(entry, "request", request);
|
|
|
|
|
|
|
|
auto method_ptr = http2::get_header(req->req_nva, ":method");
|
|
|
|
|
|
|
|
const char *method = "GET";
|
|
|
|
if(method_ptr) {
|
|
|
|
method = (*method_ptr).value.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto req_headers = json_array();
|
|
|
|
json_object_set_new(request, "headers", req_headers);
|
|
|
|
|
|
|
|
for(auto& nv : req->req_nva) {
|
|
|
|
auto hd = json_object();
|
|
|
|
json_array_append_new(req_headers, hd);
|
|
|
|
|
|
|
|
json_object_set_new(hd, "name", json_string(nv.name.c_str()));
|
|
|
|
json_object_set_new(hd, "value", json_string(nv.value.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_set_new(request, "method", json_string(method));
|
|
|
|
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));
|
|
|
|
|
|
|
|
auto response = json_object();
|
|
|
|
json_object_set_new(entry, "response", response);
|
|
|
|
|
|
|
|
auto res_headers = json_array();
|
|
|
|
json_object_set_new(response, "headers", res_headers);
|
|
|
|
|
|
|
|
for(auto& nv : req->res_nva) {
|
|
|
|
auto hd = json_object();
|
|
|
|
json_array_append_new(res_headers, hd);
|
|
|
|
|
|
|
|
json_object_set_new(hd, "name", json_string(nv.name.c_str()));
|
|
|
|
json_object_set_new(hd, "value", json_string(nv.value.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
auto content = json_object();
|
|
|
|
json_object_set_new(response, "content", content);
|
|
|
|
|
|
|
|
json_object_set_new(content, "size", json_integer(req->response_len));
|
|
|
|
|
|
|
|
auto content_type_ptr = http2::get_header(req->res_nva, "content-type");
|
|
|
|
|
|
|
|
const char *content_type = "";
|
|
|
|
if(content_type_ptr) {
|
|
|
|
content_type = content_type_ptr->value.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_set_new(content, "mimeType", json_string(content_type));
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
auto timings = json_object();
|
|
|
|
json_object_set_new(entry, "timings", timings);
|
|
|
|
|
|
|
|
auto dns_timing = (i == 0) ? dns_delta : 0;
|
|
|
|
auto connect_timing = (i == 0) ? connect_delta : 0;
|
|
|
|
|
|
|
|
json_object_set_new(timings, "dns", json_real(dns_timing));
|
|
|
|
json_object_set_new(timings, "connect", json_real(connect_timing));
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
json_object_set_new(entry, "pageref", json_string(PAGE_ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
json_dumpf(root, outfile, JSON_PRESERVE_ORDER | JSON_INDENT(2));
|
|
|
|
json_decref(root);
|
|
|
|
}
|
|
|
|
#endif // HAVE_JANSSON
|
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-08-03 11:51:01 +02:00
|
|
|
namespace {
|
|
|
|
int htp_msg_begincb(http_parser *htp)
|
|
|
|
{
|
|
|
|
if(config.verbose) {
|
|
|
|
print_timer();
|
|
|
|
std::cout << " HTTP Upgrade response" << std::endl;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-05-15 17:23:03 +02:00
|
|
|
int htp_statuscb(http_parser *htp, const char *at, size_t length)
|
2013-08-03 11:51:01 +02:00
|
|
|
{
|
2014-01-19 11:46:58 +01:00
|
|
|
auto client = static_cast<HttpClient*>(htp->data);
|
2013-08-03 11:51:01 +02:00
|
|
|
client->upgrade_response_status_code = htp->status_code;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_msg_completecb(http_parser *htp)
|
|
|
|
{
|
2014-01-19 11:46:58 +01:00
|
|
|
auto client = static_cast<HttpClient*>(htp->data);
|
2013-08-03 11:51:01 +02:00
|
|
|
client->upgrade_response_complete = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
http_parser_settings htp_hooks = {
|
2014-05-14 16:22:23 +02:00
|
|
|
htp_msg_begincb, // http_cb on_message_begin;
|
|
|
|
nullptr, // http_data_cb on_url;
|
2014-05-15 17:23:03 +02:00
|
|
|
htp_statuscb, // http_data_cb on_status;
|
2014-05-14 16:22:23 +02: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-08-03 11:51:01 +02:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
2013-11-16 13:13:09 +01:00
|
|
|
int submit_request
|
2014-10-21 16:24:50 +02:00
|
|
|
(HttpClient *client, const Headers& headers, Request *req)
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
|
|
|
auto path = req->make_reqpath();
|
2014-02-27 13:47:04 +01:00
|
|
|
auto scheme = util::get_uri_field(req->uri.c_str(), req->u, UF_SCHEMA);
|
2014-04-03 04:22:11 +02:00
|
|
|
auto build_headers = Headers
|
2013-12-06 16:32:14 +01:00
|
|
|
{{":method", req->data_prd ? "POST" : "GET"},
|
|
|
|
{":path", path},
|
|
|
|
{":scheme", scheme},
|
|
|
|
{":authority", client->hostport},
|
|
|
|
{"accept", "*/*"},
|
|
|
|
{"accept-encoding", "gzip, deflate"},
|
|
|
|
{"user-agent", "nghttp2/" NGHTTP2_VERSION}};
|
2014-02-07 16:22:19 +01:00
|
|
|
if(config.continuation) {
|
2014-06-27 14:16:36 +02:00
|
|
|
for(size_t i = 0; i < 6; ++i) {
|
2014-02-11 07:28:44 +01:00
|
|
|
build_headers.emplace_back("continuation-test-" + util::utos(i+1),
|
|
|
|
std::string(4096, '-'));
|
|
|
|
}
|
2014-02-07 16:22:19 +01:00
|
|
|
}
|
2014-01-20 13:54:03 +01:00
|
|
|
auto num_initial_headers = build_headers.size();
|
2014-07-03 15:48:43 +02:00
|
|
|
if(!config.no_content_length && req->data_prd) {
|
2013-12-06 16:32:14 +01:00
|
|
|
build_headers.emplace_back("content-length", util::utos(req->data_length));
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2013-11-16 13:13:09 +01:00
|
|
|
for(auto& kv : headers) {
|
2014-01-20 13:54:03 +01:00
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < num_initial_headers; ++i) {
|
2014-10-21 16:24:50 +02:00
|
|
|
if(kv.name == build_headers[i].name) {
|
|
|
|
build_headers[i].value = kv.value;
|
2014-01-20 13:54:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-01-20 13:54:03 +01:00
|
|
|
if(i < num_initial_headers) {
|
|
|
|
continue;
|
2013-12-06 16:32:14 +01:00
|
|
|
}
|
2014-04-03 04:33:15 +02:00
|
|
|
|
2014-10-21 16:24:50 +02:00
|
|
|
build_headers.emplace_back(kv.name, kv.value, kv.no_index);
|
2013-12-06 16:32:14 +01:00
|
|
|
}
|
|
|
|
std::stable_sort(std::begin(build_headers), std::end(build_headers),
|
2014-08-07 17:27:04 +02:00
|
|
|
http2::name_less);
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2013-12-06 16:32:14 +01:00
|
|
|
auto nva = std::vector<nghttp2_nv>();
|
|
|
|
nva.reserve(build_headers.size());
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2013-12-06 16:32:14 +01:00
|
|
|
for(auto& kv : build_headers) {
|
2014-04-03 04:33:15 +02:00
|
|
|
nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index));
|
2013-12-06 16:32:14 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-05-07 16:24:07 +02:00
|
|
|
auto stream_id = nghttp2_submit_request(client->session, &req->pri_spec,
|
|
|
|
nva.data(), nva.size(),
|
|
|
|
req->data_prd, req);
|
|
|
|
if(stream_id < 0) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] nghttp2_submit_request() returned error: "
|
2014-05-07 16:24:07 +02:00
|
|
|
<< nghttp2_strerror(stream_id) << std::endl;
|
2013-10-24 16:04:28 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-05-07 16:24:07 +02:00
|
|
|
|
|
|
|
req->stream_id = stream_id;
|
|
|
|
client->on_request(req);
|
|
|
|
|
2014-11-15 14:06:16 +01:00
|
|
|
req->req_nva = std::move(build_headers);
|
|
|
|
|
2013-10-24 16:04:28 +02:00
|
|
|
return 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
void update_html_parser(HttpClient *client, Request *req,
|
|
|
|
const uint8_t *data, size_t len, int fin)
|
|
|
|
{
|
|
|
|
if(!req->html_parser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
req->update_html_parser(data, len, fin);
|
|
|
|
|
2013-09-29 11:58:46 +02:00
|
|
|
for(auto& p : req->html_parser->get_links()) {
|
|
|
|
auto uri = strip_fragment(p.first.c_str());
|
2014-05-07 16:24:07 +02:00
|
|
|
auto pri = p.second;
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
http_parser_url u;
|
2014-02-27 14:11:31 +01:00
|
|
|
memset(&u, 0, sizeof(u));
|
2013-07-21 16:49:12 +02:00
|
|
|
if(http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) == 0 &&
|
2014-02-27 13:47:04 +01:00
|
|
|
util::fieldeq(uri.c_str(), u, req->uri.c_str(), req->u, UF_SCHEMA) &&
|
|
|
|
util::fieldeq(uri.c_str(), u, req->uri.c_str(), req->u, UF_HOST) &&
|
|
|
|
util::porteq(uri.c_str(), u, req->uri.c_str(), req->u)) {
|
2013-07-21 16:49:12 +02:00
|
|
|
// No POST data for assets
|
2014-05-07 16:24:07 +02:00
|
|
|
auto pri_spec = req->resolve_dep(pri);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
if ( client->add_request(uri, nullptr, 0, pri_spec, req->dep,
|
2014-05-07 16:24:07 +02:00
|
|
|
pri, req->level+1) ) {
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
submit_request(client, config.headers,
|
|
|
|
client->reqvec.back().get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
HttpClient* get_session(void *user_data)
|
|
|
|
{
|
2014-01-19 11:46:58 +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 {
|
2013-08-29 14:18:40 +02:00
|
|
|
int on_data_chunk_recv_callback
|
2013-07-21 16:49:12 +02:00
|
|
|
(nghttp2_session *session, uint8_t flags, int32_t stream_id,
|
|
|
|
const uint8_t *data, size_t len, void *user_data)
|
|
|
|
{
|
2013-09-03 16:09:56 +02:00
|
|
|
auto client = get_session(user_data);
|
2014-10-30 15:31:28 +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-05-07 17:29:46 +02:00
|
|
|
if(!req) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-04 16:42:12 +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);
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:03:12 +02:00
|
|
|
if(req->status == 0) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
stream_id, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-07 17:29:46 +02:00
|
|
|
if(req->inflater) {
|
|
|
|
while(len > 0) {
|
2014-05-02 16:34:57 +02:00
|
|
|
const size_t MAX_OUTLEN = 4096;
|
|
|
|
uint8_t out[MAX_OUTLEN];
|
2014-05-07 17:29:46 +02:00
|
|
|
size_t outlen = MAX_OUTLEN;
|
|
|
|
size_t tlen = len;
|
|
|
|
int rv = nghttp2_gzip_inflate(req->inflater, out, &outlen, data, &tlen);
|
|
|
|
if(rv != 0) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
|
|
|
req->response_len += outlen;
|
|
|
|
|
2014-05-07 17:29:46 +02:00
|
|
|
if(!config.null_out) {
|
|
|
|
std::cout.write(reinterpret_cast<const char*>(out), outlen);
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
2014-05-07 17:29:46 +02:00
|
|
|
update_html_parser(client, req, out, outlen, 0);
|
|
|
|
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-15 14:06:16 +01:00
|
|
|
req->response_len += len;
|
|
|
|
|
2014-06-07 09:04:43 +02: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
|
|
|
|
2013-10-27 15:02:39 +01:00
|
|
|
namespace {
|
|
|
|
void settings_timeout_cb(evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
2014-02-19 15:12:32 +01:00
|
|
|
int rv;
|
2013-10-27 15:02:39 +01:00
|
|
|
auto client = get_session(arg);
|
2013-12-25 16:23:07 +01:00
|
|
|
nghttp2_session_terminate_session(client->session, NGHTTP2_SETTINGS_TIMEOUT);
|
2014-02-19 15:12:32 +01:00
|
|
|
rv = client->on_write();
|
|
|
|
if(rv != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-02-11 07:28:44 +01:00
|
|
|
namespace {
|
|
|
|
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-01-16 15:41:13 +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;
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
for(auto& nv : req->res_nva) {
|
2014-04-03 04:22:11 +02:00
|
|
|
if("content-encoding" == nv.name) {
|
|
|
|
gzip = util::strieq("gzip", nv.value) ||
|
|
|
|
util::strieq("deflate", nv.value);
|
2014-01-16 15:41:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-03 04:22:11 +02:00
|
|
|
if(":status" == nv.name) {
|
2014-08-08 16:03:12 +02:00
|
|
|
int status;
|
|
|
|
if(req->status != 0 ||
|
|
|
|
(status = http2::parse_http_status_code(nv.value)) == -1) {
|
|
|
|
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
req->stream_id, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->status = status;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
|
|
|
if(req->status == 0) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, req->stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(req->status / 100 == 1) {
|
|
|
|
req->expect_final_response = true;
|
|
|
|
req->status = 0;
|
|
|
|
req->res_nva.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
if(gzip) {
|
|
|
|
if(!req->inflater) {
|
|
|
|
req->init_inflater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(config.get_assets && req->level == 0) {
|
|
|
|
if(!req->html_parser) {
|
|
|
|
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,
|
|
|
|
const nghttp2_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
auto client = get_session(user_data);
|
|
|
|
switch(frame->hd.type) {
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
|
|
|
auto stream_id = frame->push_promise.promised_stream_id;
|
|
|
|
http_parser_url u;
|
|
|
|
memset(&u, 0, sizeof(u));
|
|
|
|
// 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
|
|
|
|
|
|
|
auto req = util::make_unique<Request>("", u, nullptr, 0, pri_spec,
|
|
|
|
nullptr);
|
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
|
|
|
|
|
|
|
client->on_request(req.get());
|
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;
|
|
|
|
}
|
|
|
|
} //namespace
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
namespace {
|
|
|
|
int on_header_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame,
|
|
|
|
const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen,
|
2014-04-01 19:10:35 +02:00
|
|
|
uint8_t flags,
|
2014-01-16 15:41:13 +01:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if(config.verbose) {
|
|
|
|
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
|
|
|
|
|
|
|
if(!http2::check_nv(name, namelen, value, valuelen)) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, frame->hd.stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
switch(frame->hd.type) {
|
|
|
|
case NGHTTP2_HEADERS: {
|
2014-10-30 15:31:28 +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-02-24 17:16:47 +01:00
|
|
|
if(!req) {
|
|
|
|
break;
|
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
|
|
|
if(frame->headers.cat != NGHTTP2_HCAT_RESPONSE &&
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_PUSH_RESPONSE &&
|
|
|
|
(frame->headers.cat != NGHTTP2_HCAT_HEADERS ||
|
|
|
|
!req->expect_final_response)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(namelen > 0 && name[0] == ':') {
|
|
|
|
if(!req->response_pseudo_header_allowed() ||
|
|
|
|
!http2::check_http2_response_pseudo_header(name, namelen)) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->hd.stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 11:55:08 +02:00
|
|
|
http2::add_header(req->res_nva, name, namelen, value, valuelen,
|
|
|
|
flags & NGHTTP2_NV_FLAG_NO_INDEX);
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
2014-10-30 15:31:28 +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-02-24 17:16:47 +01:00
|
|
|
if(!req) {
|
|
|
|
break;
|
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
|
|
|
if(namelen > 0 && name[0] == ':') {
|
|
|
|
if(!req->push_request_pseudo_header_allowed() ||
|
|
|
|
!http2::check_http2_request_pseudo_header(name, namelen)) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 14:06:16 +01:00
|
|
|
http2::add_header(req->req_nva, name, namelen, value, valuelen,
|
2014-07-12 11:55:08 +02:00
|
|
|
flags & NGHTTP2_NV_FLAG_NO_INDEX);
|
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 {
|
2013-08-29 14:03:39 +02:00
|
|
|
int on_frame_recv_callback2
|
2013-09-03 14:24:14 +02:00
|
|
|
(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
|
2013-07-21 16:49:12 +02:00
|
|
|
{
|
2014-05-02 16:34:57 +02:00
|
|
|
int rv = 0;
|
|
|
|
|
2014-08-08 16:03:12 +02:00
|
|
|
if(config.verbose) {
|
|
|
|
verbose_on_frame_recv_callback(session, frame, user_data);
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
auto client = get_session(user_data);
|
|
|
|
switch(frame->hd.type) {
|
|
|
|
case NGHTTP2_HEADERS: {
|
2014-10-30 15:31:28 +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-08-08 16:03:12 +02:00
|
|
|
if(!req) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(frame->headers.cat == NGHTTP2_HCAT_RESPONSE ||
|
|
|
|
frame->headers.cat == NGHTTP2_HCAT_PUSH_RESPONSE) {
|
2013-09-16 10:36:24 +02:00
|
|
|
req->record_response_time();
|
2014-01-29 13:23:13 +01:00
|
|
|
check_response_header(session, req);
|
2014-08-08 16:03:12 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(frame->headers.cat == NGHTTP2_HCAT_HEADERS) {
|
|
|
|
if(req->expect_final_response) {
|
|
|
|
check_response_header(session, req);
|
|
|
|
} else {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
2014-08-08 16:03:12 +02:00
|
|
|
|
|
|
|
if(req->status == 0 && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGHTTP2_SETTINGS:
|
|
|
|
if((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
if(client->settings_timerev) {
|
|
|
|
evtimer_del(client->settings_timerev);
|
|
|
|
event_free(client->settings_timerev);
|
|
|
|
client->settings_timerev = nullptr;
|
|
|
|
}
|
2014-02-24 17:16:47 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
2014-10-30 15:31:28 +01:00
|
|
|
auto req = static_cast<Request*>
|
|
|
|
(nghttp2_session_get_stream_user_data
|
|
|
|
(session, frame->push_promise.promised_stream_id));
|
2014-02-24 17:16:47 +01:00
|
|
|
if(!req) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string scheme, authority, method, path;
|
2014-11-15 14:06:16 +01:00
|
|
|
for(auto& nv : req->req_nva) {
|
2014-04-03 04:22:11 +02:00
|
|
|
if(nv.name == ":scheme") {
|
|
|
|
scheme = nv.value;
|
2014-02-24 17:16:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-03 04:22:11 +02:00
|
|
|
if(nv.name == ":authority" || nv.name == "host") {
|
|
|
|
authority = nv.value;
|
2014-02-24 17:16:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-03 04:22:11 +02:00
|
|
|
if(nv.name == ":method") {
|
|
|
|
method = nv.value;
|
2014-02-24 17:16:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-03 04:22:11 +02:00
|
|
|
if(nv.name == ":path") {
|
|
|
|
path = nv.value;
|
2014-02-24 17:16:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(scheme.empty() || authority.empty() || method.empty() || path.empty() ||
|
|
|
|
path[0] != '/') {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string uri = scheme;
|
|
|
|
uri += "://";
|
|
|
|
uri += authority;
|
|
|
|
uri += path;
|
|
|
|
http_parser_url u;
|
2014-02-27 14:11:31 +01:00
|
|
|
memset(&u, 0, sizeof(u));
|
2014-02-24 17:16:47 +01:00
|
|
|
if(http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req->uri = uri;
|
|
|
|
req->u = u;
|
|
|
|
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
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-08-29 15:58:05 +02:00
|
|
|
int on_stream_close_callback
|
2014-08-23 10:34:56 +02:00
|
|
|
(nghttp2_session *session, int32_t stream_id, uint32_t error_code,
|
2013-07-21 16:49:12 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
2013-09-03 16:09:56 +02:00
|
|
|
auto client = get_session(user_data);
|
2014-10-30 15:31:28 +01:00
|
|
|
auto req = static_cast<Request*>
|
|
|
|
(nghttp2_session_get_stream_user_data(session, stream_id));
|
2014-05-07 17:29:46 +02:00
|
|
|
|
|
|
|
if(!req) {
|
|
|
|
return 0;
|
2014-02-24 17:16:47 +01:00
|
|
|
}
|
2014-05-07 17:29:46 +02:00
|
|
|
|
|
|
|
update_html_parser(client, req, nullptr, 0, 1);
|
|
|
|
req->record_complete_time();
|
|
|
|
++client->complete;
|
|
|
|
|
2014-02-24 17:16:47 +01:00
|
|
|
if(client->all_requests_processed()) {
|
|
|
|
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
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
void print_stats(const HttpClient& client)
|
|
|
|
{
|
|
|
|
std::cout << "***** Statistics *****" << std::endl;
|
|
|
|
int i = 0;
|
|
|
|
for(auto& req : client.reqvec) {
|
|
|
|
std::cout << "#" << ++i << ": " << req->uri << std::endl;
|
|
|
|
std::cout << " Status: " << req->status << std::endl;
|
|
|
|
std::cout << " Delta (ms) from handshake(HEADERS):"
|
|
|
|
<< std::endl;
|
2013-09-16 10:36:24 +02:00
|
|
|
if(req->stat.stage >= STAT_ON_RESPONSE) {
|
|
|
|
std::cout << " response HEADERS: "
|
|
|
|
<< time_delta(req->stat.on_response_time,
|
|
|
|
client.stat.on_handshake_time).count()
|
2013-07-21 16:49:12 +02:00
|
|
|
<< "("
|
2013-09-16 10:36:24 +02:00
|
|
|
<< time_delta(req->stat.on_response_time,
|
|
|
|
req->stat.on_request_time).count()
|
2013-07-21 16:49:12 +02:00
|
|
|
<< ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2013-09-16 10:36:24 +02:00
|
|
|
if(req->stat.stage >= STAT_ON_COMPLETE) {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cout << " Completed: "
|
|
|
|
<< time_delta(req->stat.on_complete_time,
|
2013-09-16 10:36:24 +02:00
|
|
|
client.stat.on_handshake_time).count()
|
2013-07-21 16:49:12 +02:00
|
|
|
<< "("
|
|
|
|
<< time_delta(req->stat.on_complete_time,
|
2013-09-16 10:36:24 +02:00
|
|
|
req->stat.on_request_time).count()
|
2013-07-21 16:49:12 +02:00
|
|
|
<< ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-01-01 15:54:28 +01:00
|
|
|
namespace {
|
|
|
|
void print_protocol_nego_error()
|
|
|
|
{
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] HTTP/2 protocol was not selected."
|
2014-01-01 15:54:28 +01:00
|
|
|
<< " (nghttp2 expects " << NGHTTP2_PROTO_VERSION_ID << ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
|
|
|
int client_select_next_proto_cb(SSL* ssl,
|
|
|
|
unsigned char **out, unsigned char *outlen,
|
|
|
|
const unsigned char *in, unsigned int inlen,
|
|
|
|
void *arg)
|
|
|
|
{
|
2013-07-22 15:08:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
for(unsigned int i = 0; i < inlen; i += in[i]+1) {
|
2013-07-22 15:08:52 +02:00
|
|
|
if(config.verbose) {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cout << " * ";
|
|
|
|
std::cout.write(reinterpret_cast<const char*>(&in[i+1]), in[i]);
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) {
|
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
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
namespace {
|
|
|
|
void upgrade_readcb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
|
|
|
int rv;
|
2014-01-19 11:46:58 +01:00
|
|
|
auto client = static_cast<HttpClient*>(ptr);
|
2013-08-03 11:51:01 +02:00
|
|
|
rv = client->on_upgrade_read();
|
|
|
|
if(rv != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
namespace {
|
|
|
|
void readcb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
|
|
|
int rv;
|
2014-01-19 11:46:58 +01:00
|
|
|
auto client = static_cast<HttpClient*>(ptr);
|
2013-07-21 16:49:12 +02:00
|
|
|
rv = client->on_read();
|
|
|
|
if(rv != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void writecb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
|
|
|
if(evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int rv;
|
2014-01-19 11:46:58 +01:00
|
|
|
auto client = static_cast<HttpClient*>(ptr);
|
2013-07-21 16:49:12 +02:00
|
|
|
rv = client->on_write();
|
|
|
|
if(rv != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void eventcb(bufferevent *bev, short events, void *ptr)
|
|
|
|
{
|
2013-08-03 11:51:01 +02:00
|
|
|
int rv;
|
2014-01-19 11:46:58 +01:00
|
|
|
auto client = static_cast<HttpClient*>(ptr);
|
2013-07-21 16:49:12 +02:00
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
2014-11-15 14:06:16 +01:00
|
|
|
client->record_connect_time();
|
2013-07-21 16:49:12 +02:00
|
|
|
client->state = STATE_CONNECTED;
|
|
|
|
int fd = bufferevent_getfd(bev);
|
|
|
|
int val = 1;
|
|
|
|
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
reinterpret_cast<char *>(&val), sizeof(val)) == -1) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Setting option TCP_NODELAY failed: errno="
|
2013-07-21 16:49:12 +02:00
|
|
|
<< errno << std::endl;
|
|
|
|
}
|
2013-08-12 15:33:51 +02:00
|
|
|
if(client->need_upgrade()) {
|
2013-08-03 11:51:01 +02:00
|
|
|
rv = client->on_upgrade_connect();
|
|
|
|
} else {
|
2014-01-07 18:15:46 +01:00
|
|
|
if(client->ssl) {
|
|
|
|
// Check NPN or ALPN result
|
|
|
|
const unsigned char *next_proto = nullptr;
|
|
|
|
unsigned int next_proto_len;
|
|
|
|
SSL_get0_next_proto_negotiated(client->ssl,
|
|
|
|
&next_proto, &next_proto_len);
|
|
|
|
for(int i = 0; i < 2; ++i) {
|
|
|
|
if(next_proto) {
|
|
|
|
if(config.verbose) {
|
|
|
|
std::cout << "The negotiated protocol: ";
|
|
|
|
std::cout.write(reinterpret_cast<const char*>(next_proto),
|
|
|
|
next_proto_len);
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2014-11-14 15:14:39 +01:00
|
|
|
if(!util::check_h2_is_selected(next_proto, next_proto_len)) {
|
2014-01-07 18:15:46 +01:00
|
|
|
next_proto = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
2014-01-01 15:54:28 +01:00
|
|
|
}
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2014-01-07 18:15:46 +01:00
|
|
|
SSL_get0_alpn_selected(client->ssl, &next_proto, &next_proto_len);
|
2014-01-01 15:54:28 +01:00
|
|
|
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2014-01-07 18:15:46 +01:00
|
|
|
break;
|
2014-01-01 15:54:28 +01:00
|
|
|
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2014-01-07 18:15:46 +01:00
|
|
|
}
|
|
|
|
if(!next_proto) {
|
|
|
|
print_protocol_nego_error();
|
|
|
|
client->disconnect();
|
|
|
|
return;
|
|
|
|
}
|
2014-01-01 15:54:28 +01:00
|
|
|
}
|
2013-08-03 11:51:01 +02:00
|
|
|
rv = client->on_connect();
|
|
|
|
}
|
|
|
|
if(rv != 0) {
|
|
|
|
client->disconnect();
|
|
|
|
return;
|
|
|
|
}
|
2014-01-22 16:29:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_EOF) {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cerr << "EOF" << std::endl;
|
2014-01-22 16:29:09 +01:00
|
|
|
auto state = client->state;
|
2013-07-21 16:49:12 +02:00
|
|
|
client->disconnect();
|
2014-01-22 16:29:09 +01:00
|
|
|
if(state == STATE_IDLE) {
|
2014-01-27 17:40:00 +01:00
|
|
|
if(client->initiate_connection() == 0) {
|
|
|
|
std::cerr << "Trying next address" << std::endl;
|
|
|
|
}
|
2014-01-22 16:29:09 +01:00
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
return;
|
2014-01-22 16:29:09 +01:00
|
|
|
}
|
|
|
|
if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
|
2013-07-21 16:49:12 +02:00
|
|
|
if(events & BEV_EVENT_ERROR) {
|
|
|
|
if(client->state == STATE_IDLE) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Could not connect to the host" << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
} else {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Network error" << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Timeout" << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-01-22 16:29:09 +01:00
|
|
|
auto state = client->state;
|
2013-07-21 16:49:12 +02:00
|
|
|
client->disconnect();
|
2014-01-22 16:29:09 +01:00
|
|
|
if(state == STATE_IDLE) {
|
2014-01-27 17:40:00 +01:00
|
|
|
if(client->initiate_connection() == 0) {
|
|
|
|
std::cerr << "Trying next address" << std::endl;
|
|
|
|
}
|
2014-01-22 16:29:09 +01:00
|
|
|
}
|
2013-07-21 16:49:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-08-12 15:33:51 +02:00
|
|
|
int communicate(const std::string& scheme, const std::string& host,
|
|
|
|
uint16_t port,
|
2013-07-21 16:49:12 +02:00
|
|
|
std::vector<std::tuple<std::string,
|
|
|
|
nghttp2_data_provider*,
|
|
|
|
int64_t>> requests,
|
|
|
|
const nghttp2_session_callbacks *callbacks)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
auto evbase = event_base_new();
|
|
|
|
SSL_CTX *ssl_ctx = nullptr;
|
2013-08-12 15:33:51 +02:00
|
|
|
if(scheme == "https") {
|
2013-07-21 16:49:12 +02:00
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
SSL_CTX_set_options(ssl_ctx,
|
2014-10-22 16:14:07 +02:00
|
|
|
SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
|
|
|
|
SSL_OP_NO_COMPRESSION |
|
2013-07-21 16:49:12 +02:00
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
|
|
|
if(!config.keyfile.empty()) {
|
|
|
|
if(SSL_CTX_use_PrivateKey_file(ssl_ctx, config.keyfile.c_str(),
|
|
|
|
SSL_FILETYPE_PEM) != 1) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!config.certfile.empty()) {
|
|
|
|
if(SSL_CTX_use_certificate_chain_file(ssl_ctx,
|
|
|
|
config.certfile.c_str()) != 1) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
result = -1;
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SSL_CTX_set_next_proto_select_cb(ssl_ctx,
|
|
|
|
client_select_next_proto_cb, nullptr);
|
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
|
|
|
}
|
|
|
|
{
|
|
|
|
HttpClient client{callbacks, evbase, ssl_ctx};
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec pri_spec;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
if(config.weight != NGHTTP2_DEFAULT_WEIGHT) {
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, 0, config.weight, 0);
|
|
|
|
} else {
|
|
|
|
nghttp2_priority_spec_default_init(&pri_spec);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
for(auto req : requests) {
|
|
|
|
for(int i = 0; i < config.multiply; ++i) {
|
2014-03-25 18:04:24 +01:00
|
|
|
auto dep = std::make_shared<Dependency>();
|
2013-07-21 16:49:12 +02:00
|
|
|
client.add_request(std::get<0>(req), std::get<1>(req),
|
2014-03-25 18:04:24 +01:00
|
|
|
std::get<2>(req), pri_spec, std::move(dep));
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
client.update_hostport();
|
2014-11-15 14:06:16 +01:00
|
|
|
|
|
|
|
client.record_started_time();
|
|
|
|
|
2014-01-22 16:29:09 +01:00
|
|
|
if(client.resolve_host(host, port) != 0) {
|
|
|
|
goto fin;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
|
|
|
|
client.record_dns_complete_time();
|
|
|
|
|
2014-01-22 16:29:09 +01:00
|
|
|
if(client.initiate_connection() != 0) {
|
2013-07-21 16:49:12 +02:00
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
event_base_loop(evbase, 0);
|
|
|
|
|
2014-11-15 14:06:16 +01:00
|
|
|
#ifdef HAVE_JANSSON
|
|
|
|
if(!config.harfile.empty()) {
|
|
|
|
FILE *outfile;
|
|
|
|
if(config.harfile == "-") {
|
|
|
|
outfile = stdout;
|
|
|
|
} else {
|
|
|
|
outfile = fopen(config.harfile.c_str(), "wb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(outfile) {
|
|
|
|
client.output_har(outfile);
|
|
|
|
|
|
|
|
if(outfile != stdout) {
|
|
|
|
fclose(outfile);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << "Cannot open file " << config.harfile << ". "
|
|
|
|
<< "har file could not be created."
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAVE_JANSSON
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
if(!client.all_requests_processed()) {
|
|
|
|
std::cerr << "Some requests were not processed. total="
|
|
|
|
<< client.reqvec.size()
|
|
|
|
<< ", processed=" << client.complete << std::endl;
|
|
|
|
}
|
|
|
|
if(config.stat) {
|
|
|
|
print_stats(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fin:
|
|
|
|
if(ssl_ctx) {
|
|
|
|
SSL_CTX_free(ssl_ctx);
|
|
|
|
}
|
|
|
|
if(evbase) {
|
|
|
|
event_base_free(evbase);
|
|
|
|
}
|
|
|
|
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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
ssize_t file_read_callback
|
|
|
|
(nghttp2_session *session, int32_t stream_id,
|
2014-04-05 10:59:24 +02:00
|
|
|
uint8_t *buf, size_t length, uint32_t *data_flags,
|
2013-07-21 16:49:12 +02:00
|
|
|
nghttp2_data_source *source, void *user_data)
|
|
|
|
{
|
2014-10-30 15:31:28 +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-06-07 09:04:43 +02:00
|
|
|
while((nread = pread(fd, buf, length, req->data_offset)) == -1 &&
|
|
|
|
errno == EINTR);
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2014-06-07 09:04:43 +02:00
|
|
|
if(nread == -1) {
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
2014-05-02 16:34:57 +02:00
|
|
|
|
|
|
|
if(nread == 0) {
|
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
|
|
|
} else {
|
|
|
|
req->data_offset += nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
int run(char **uris, int n)
|
|
|
|
{
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks *callbacks;
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_new(&callbacks);
|
2014-09-16 16:39:38 +02:00
|
|
|
auto cbsdel = util::defer(callbacks, nghttp2_session_callbacks_del);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback
|
|
|
|
(callbacks, on_stream_close_callback);
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback
|
|
|
|
(callbacks, on_frame_recv_callback2);
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
if(config.verbose) {
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks_set_on_frame_send_callback
|
|
|
|
(callbacks, verbose_on_frame_send_callback);
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback
|
|
|
|
(callbacks, verbose_on_invalid_frame_recv_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback
|
|
|
|
(callbacks, on_data_chunk_recv_callback);
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_on_begin_headers_callback
|
|
|
|
(callbacks, on_begin_headers_callback);
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_on_header_callback
|
|
|
|
(callbacks, on_header_callback);
|
|
|
|
|
2014-02-15 08:40:32 +01:00
|
|
|
if(config.padding) {
|
2014-08-22 13:59:50 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
if(!config.datafile.empty()) {
|
|
|
|
data_fd = open(config.datafile.c_str(), O_RDONLY | O_BINARY);
|
|
|
|
if(data_fd == -1) {
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Could not open file "
|
|
|
|
<< config.datafile << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(fstat(data_fd, &data_stat) == -1) {
|
|
|
|
close(data_fd);
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[ERROR] Could not stat file "
|
|
|
|
<< config.datafile << std::endl;
|
2013-07-21 16:49:12 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
data_prd.source.fd = data_fd;
|
|
|
|
data_prd.read_callback = file_read_callback;
|
|
|
|
}
|
|
|
|
std::vector<std::tuple<std::string, nghttp2_data_provider*, int64_t>>
|
|
|
|
requests;
|
|
|
|
for(int i = 0; i < n; ++i) {
|
|
|
|
http_parser_url u;
|
2014-02-27 14:11:31 +01:00
|
|
|
memset(&u, 0, sizeof(u));
|
2013-09-03 16:09:56 +02:00
|
|
|
auto uri = strip_fragment(uris[i]);
|
2013-07-21 16:49:12 +02:00
|
|
|
if(http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) == 0 &&
|
2014-02-27 13:47:04 +01:00
|
|
|
util::has_uri_field(u, UF_SCHEMA)) {
|
|
|
|
uint16_t port = util::has_uri_field(u, UF_PORT) ?
|
|
|
|
u.port : util::get_default_port(uri.c_str(), u);
|
|
|
|
if(!util::fieldeq(uri.c_str(), u, UF_SCHEMA, prev_scheme.c_str()) ||
|
|
|
|
!util::fieldeq(uri.c_str(), u, UF_HOST, prev_host.c_str()) ||
|
2014-01-09 16:59:14 +01:00
|
|
|
port != prev_port) {
|
2013-07-21 16:49:12 +02:00
|
|
|
if(!requests.empty()) {
|
2013-08-12 15:33:51 +02:00
|
|
|
if (communicate(prev_scheme, prev_host, prev_port,
|
2014-08-22 13:59:50 +02:00
|
|
|
std::move(requests), callbacks) != 0) {
|
2013-07-21 16:49:12 +02:00
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
requests.clear();
|
|
|
|
}
|
2014-02-27 13:47:04 +01:00
|
|
|
prev_scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA);
|
|
|
|
prev_host = util::get_uri_field(uri.c_str(), u, UF_HOST);
|
2013-07-21 16:49:12 +02:00
|
|
|
prev_port = port;
|
|
|
|
}
|
|
|
|
requests.emplace_back(uri, data_fd == -1 ? nullptr : &data_prd,
|
|
|
|
data_stat.st_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
void print_version(std::ostream& out)
|
|
|
|
{
|
|
|
|
out << "nghttp nghttp2/" NGHTTP2_VERSION << std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-16 09:54:11 +02:00
|
|
|
namespace {
|
2013-07-21 16:49:12 +02:00
|
|
|
void print_usage(std::ostream& out)
|
|
|
|
{
|
2014-03-13 17:53:28 +01:00
|
|
|
out << R"(Usage: nghttp [OPTIONS]... <URI>...
|
|
|
|
HTTP/2 experimental 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 {
|
2013-07-21 16:49:12 +02:00
|
|
|
void print_help(std::ostream& out)
|
|
|
|
{
|
|
|
|
print_usage(out);
|
2014-03-13 17:53:28 +01:00
|
|
|
out << R"(
|
|
|
|
<URI> Specify URI to access.
|
|
|
|
Options:
|
|
|
|
-v, --verbose Print debug information such as reception and
|
2014-11-04 16:42:12 +01:00
|
|
|
transmission of frames and name/value pairs.
|
|
|
|
Specifying this option multiple times increases
|
|
|
|
verbosity.
|
2014-03-13 17:53:28 +01:00
|
|
|
-n, --null-out Discard downloaded data.
|
|
|
|
-O, --remote-name Save download data in the current directory. The
|
|
|
|
filename is dereived from URI. If URI ends with
|
|
|
|
'/', 'index.html' is used as a filename. Not
|
|
|
|
implemented yet.
|
|
|
|
-t, --timeout=<N> Timeout each request after <N> seconds.
|
|
|
|
-w, --window-bits=<N>
|
|
|
|
Sets the stream level initial window size to
|
|
|
|
2**<N>-1.
|
|
|
|
-W, --connection-window-bits=<N>
|
|
|
|
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
|
2014-10-16 16:17:19 +02:00
|
|
|
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.
|
2014-03-13 17:53:28 +01:00
|
|
|
-s, --stat Print statistics.
|
2014-10-21 15:46:47 +02:00
|
|
|
-H, --header=<HEADER>
|
|
|
|
Add a header to the requests. Example:
|
|
|
|
-H':method: PUT'
|
2014-03-13 17:53:28 +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.
|
|
|
|
-d, --data=<FILE> 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.
|
2014-03-30 12:09:21 +02:00
|
|
|
-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>
|
2014-04-05 12:15:45 +02:00
|
|
|
Sets priority group weight. The valid value
|
|
|
|
range is [)"
|
|
|
|
<< NGHTTP2_MIN_WEIGHT << ", " << NGHTTP2_MAX_WEIGHT << R"(], inclusive.
|
|
|
|
Default: )"
|
2014-03-25 18:04:24 +01:00
|
|
|
<< NGHTTP2_DEFAULT_WEIGHT << R"(
|
2014-03-13 17:53:28 +01:00
|
|
|
-M, --peer-max-concurrent-streams=<N>
|
|
|
|
Use <N> as SETTINGS_MAX_CONCURRENT_STREAMS value
|
|
|
|
of remote endpoint as if it is received in
|
|
|
|
SETTINGS frame. The default is large enough as
|
|
|
|
it is seen as unlimited.
|
|
|
|
-c, --header-table-size=<N>
|
|
|
|
Specify decoder header table size.
|
|
|
|
-b, --padding=<N> Add at most <N> bytes to a frame payload as
|
|
|
|
padding. Specify 0 to disable padding.
|
2014-11-15 14:06:16 +01:00
|
|
|
-r, --har=<FILE> Output HTTP transactions <FILE> in HAR format.
|
|
|
|
If '-' is given, data is written to stdout.
|
2014-03-13 17:53:28 +01:00
|
|
|
--color Force colored log output.
|
|
|
|
--continuation Send large header to test CONTINUATION.
|
2014-07-03 15:48:43 +02:00
|
|
|
--no-content-length
|
|
|
|
Don't send content-length header field.
|
2014-03-13 17:53:28 +01:00
|
|
|
--version Display version information and exit.
|
|
|
|
-h, --help Display this help and exit.)"
|
2013-07-21 16:49:12 +02:00
|
|
|
<< std::endl;
|
|
|
|
}
|
2013-09-16 09:54:11 +02:00
|
|
|
} // namespace
|
2013-07-21 16:49:12 +02:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-11-01 15:06:53 +01:00
|
|
|
bool color = false;
|
2013-07-21 16:49:12 +02:00
|
|
|
while(1) {
|
2014-04-30 15:44:51 +02:00
|
|
|
static int flag = 0;
|
2013-07-21 16:49:12 +02:00
|
|
|
static option long_options[] = {
|
2013-08-18 15:08:40 +02: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'},
|
2013-10-10 15:04:37 +02:00
|
|
|
{"connection-window-bits", required_argument, nullptr, 'W'},
|
2013-08-18 15:08:40 +02:00
|
|
|
{"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'},
|
2014-03-25 18:04:24 +01:00
|
|
|
{"weight", required_argument, nullptr, 'p'},
|
2013-09-16 09:44:13 +02:00
|
|
|
{"peer-max-concurrent-streams", required_argument, nullptr, 'M'},
|
2013-10-29 13:34:22 +01:00
|
|
|
{"header-table-size", required_argument, nullptr, 'c'},
|
2014-02-09 13:46:15 +01:00
|
|
|
{"padding", required_argument, nullptr, 'b'},
|
2014-11-15 14:06:16 +01:00
|
|
|
{"har", required_argument, nullptr, 'r'},
|
2013-11-01 15:06:53 +01:00
|
|
|
{"cert", required_argument, &flag, 1},
|
|
|
|
{"key", required_argument, &flag, 2},
|
|
|
|
{"color", no_argument, &flag, 3},
|
2014-02-07 16:22:19 +01:00
|
|
|
{"continuation", no_argument, &flag, 4},
|
2014-02-16 11:39:41 +01:00
|
|
|
{"version", no_argument, &flag, 5},
|
2014-07-03 15:48:43 +02:00
|
|
|
{"no-content-length", no_argument, &flag, 6},
|
2013-08-18 15:08:40 +02:00
|
|
|
{nullptr, 0, nullptr, 0 }
|
2013-07-21 16:49:12 +02:00
|
|
|
};
|
|
|
|
int option_index = 0;
|
2014-11-15 14:06:16 +01:00
|
|
|
int c = getopt_long(argc, argv, "M:Oab:c:d:gm:np:r:hH:vst:uw:W:",
|
2014-05-02 16:34:57 +02:00
|
|
|
long_options, &option_index);
|
2013-10-29 13:34:22 +01:00
|
|
|
char *end;
|
2013-07-21 16:49:12 +02:00
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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-04-05 12:15:45 +02:00
|
|
|
if(errno == 0 && NGHTTP2_MIN_WEIGHT <= n && n <= NGHTTP2_MAX_WEIGHT) {
|
2014-03-25 18:04:24 +01:00
|
|
|
config.weight = 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 ["
|
|
|
|
<< NGHTTP2_MIN_WEIGHT << ", "
|
2014-03-25 18:04:24 +01:00
|
|
|
<< NGHTTP2_MAX_WEIGHT << "], inclusive"
|
2013-08-04 11:55:26 +02:00
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-15 14:06:16 +01:00
|
|
|
case 'r':
|
|
|
|
#ifdef HAVE_JANSSON
|
|
|
|
config.harfile = optarg;
|
|
|
|
#else // !HAVE_JANSSON
|
|
|
|
std::cerr << "[WARNING]: -r, --har option is ignored because\n"
|
|
|
|
<< "the binary was not compiled with libjansson."
|
|
|
|
<< std::endl;
|
|
|
|
#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':
|
|
|
|
config.timeout = atoi(optarg) * 1000;
|
|
|
|
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);
|
|
|
|
if(errno == 0 && *endptr == '\0' && n < 31) {
|
|
|
|
if(c == 'w') {
|
|
|
|
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
|
|
|
|
char *value = strchr( optarg + 1, ':' );
|
2014-04-26 07:53:03 +02:00
|
|
|
if ( ! value || (header[0] == ':' && header + 1 == value)) {
|
2013-07-21 16:49:12 +02:00
|
|
|
std::cerr << "-H: 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 << "-H: invalid header - value missing: " << optarg
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-10-21 16:24:50 +02:00
|
|
|
// To test "never index" repr, don't index authorization header
|
|
|
|
// field unconditionally.
|
|
|
|
auto no_index = util::strieq("authorization", header);
|
|
|
|
config.headers.emplace_back(header, value, no_index);
|
|
|
|
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;
|
|
|
|
#else // !HAVE_LIBXML2
|
2014-08-08 17:03:33 +02:00
|
|
|
std::cerr << "[WARNING]: -a, --get-assets option cannot be used because\n"
|
2013-07-21 16:49:12 +02:00
|
|
|
<< "the binary was not compiled with libxml2."
|
|
|
|
<< std::endl;
|
|
|
|
#endif // !HAVE_LIBXML2
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
config.stat = true;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
config.datafile = strcmp("-", optarg) == 0 ? "/dev/stdin" : optarg;
|
|
|
|
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;
|
2013-10-29 13:34:22 +01:00
|
|
|
case 'c':
|
2014-01-18 07:32:50 +01:00
|
|
|
errno = 0;
|
2013-10-29 13:34:22 +01:00
|
|
|
config.header_table_size = strtol(optarg, &end, 10);
|
|
|
|
if(errno == ERANGE || *end != '\0') {
|
|
|
|
std::cerr << "-c: Bad option value: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
switch(flag) {
|
|
|
|
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;
|
2013-07-21 16:49:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 15:06:53 +01:00
|
|
|
set_color_output(color || isatty(fileno(stdout)));
|
2013-07-21 16:49:12 +02:00
|
|
|
|
2014-04-04 14:57:47 +02:00
|
|
|
nghttp2_option_set_peer_max_concurrent_streams
|
|
|
|
(config.http2_option, config.peer_max_concurrent_streams);
|
|
|
|
|
2013-07-21 16:49:12 +02:00
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_handler = SIG_IGN;
|
2013-09-03 16:09:56 +02:00
|
|
|
sigaction(SIGPIPE, &act, nullptr);
|
2014-08-02 03:11:45 +02:00
|
|
|
OPENSSL_config(nullptr);
|
|
|
|
OpenSSL_add_all_algorithms();
|
2013-07-21 16:49:12 +02:00
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
reset_timer();
|
|
|
|
return run(argv+optind, argc-optind);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return nghttp2::main(argc, argv);
|
|
|
|
}
|