2015-09-14 15:33:48 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 British Broadcasting Corporation
|
|
|
|
*
|
|
|
|
* 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 "h2load_http1_session.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cerrno>
|
|
|
|
|
|
|
|
#include "h2load.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "template.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "http-parser/http_parser.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
|
|
|
namespace h2load {
|
|
|
|
|
|
|
|
Http1Session::Http1Session(Client *client)
|
2016-01-27 13:14:07 +01:00
|
|
|
: stream_req_counter_(1),
|
|
|
|
stream_resp_counter_(1),
|
|
|
|
client_(client),
|
|
|
|
htp_(),
|
2015-09-14 15:33:48 +02:00
|
|
|
complete_(false) {
|
|
|
|
http_parser_init(&htp_, HTTP_RESPONSE);
|
|
|
|
htp_.data = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Http1Session::~Http1Session() {}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// HTTP response message begin
|
2015-11-26 13:26:00 +01:00
|
|
|
int htp_msg_begincb(http_parser *htp) {
|
|
|
|
auto session = static_cast<Http1Session *>(htp->data);
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
if (session->stream_resp_counter_ > session->stream_req_counter_) {
|
2015-11-26 13:26:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-14 15:33:48 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// HTTP response status code
|
|
|
|
int htp_statuscb(http_parser *htp, const char *at, size_t length) {
|
|
|
|
auto session = static_cast<Http1Session *>(htp->data);
|
|
|
|
auto client = session->get_client();
|
|
|
|
client->on_status_code(session->stream_resp_counter_, htp->status_code);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// HTTP response message complete
|
|
|
|
int htp_msg_completecb(http_parser *htp) {
|
|
|
|
auto session = static_cast<Http1Session *>(htp->data);
|
|
|
|
auto client = session->get_client();
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
client->final = http_should_keep_alive(htp) == 0;
|
2016-01-05 14:36:37 +01:00
|
|
|
auto req_stat = client->get_req_stat(session->stream_resp_counter_);
|
|
|
|
|
|
|
|
assert(req_stat);
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
auto config = client->worker->config;
|
|
|
|
if (req_stat->data_offset >= config->data_length) {
|
|
|
|
client->on_stream_close(session->stream_resp_counter_, true, client->final);
|
|
|
|
}
|
2015-09-14 15:33:48 +02:00
|
|
|
|
|
|
|
session->stream_resp_counter_ += 2;
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
if (client->final) {
|
|
|
|
session->stream_req_counter_ = session->stream_resp_counter_;
|
|
|
|
|
2015-09-28 16:50:44 +02:00
|
|
|
http_parser_pause(htp, 1);
|
|
|
|
// Connection is going down. If we have still request to do,
|
|
|
|
// create new connection and keep on doing the job.
|
2017-01-25 16:16:12 +01:00
|
|
|
if (client->req_left) {
|
2015-09-28 16:50:44 +02:00
|
|
|
client->try_new_connection();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:33:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdr_keycb(http_parser *htp, const char *data, size_t len) {
|
|
|
|
auto session = static_cast<Http1Session *>(htp->data);
|
|
|
|
auto client = session->get_client();
|
|
|
|
|
|
|
|
client->worker->stats.bytes_head += len;
|
2015-11-03 17:03:35 +01:00
|
|
|
client->worker->stats.bytes_head_decomp += len;
|
2015-09-14 15:33:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdr_valcb(http_parser *htp, const char *data, size_t len) {
|
|
|
|
auto session = static_cast<Http1Session *>(htp->data);
|
|
|
|
auto client = session->get_client();
|
|
|
|
|
|
|
|
client->worker->stats.bytes_head += len;
|
2015-11-03 17:03:35 +01:00
|
|
|
client->worker->stats.bytes_head_decomp += len;
|
2015-09-14 15:33:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_body_cb(http_parser *htp, const char *data, size_t len) {
|
|
|
|
auto session = static_cast<Http1Session *>(htp->data);
|
|
|
|
auto client = session->get_client();
|
|
|
|
|
|
|
|
client->record_ttfb();
|
|
|
|
client->worker->stats.bytes_body += len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2017-01-09 09:11:37 +01:00
|
|
|
constexpr http_parser_settings htp_hooks = {
|
2015-09-14 15:33:48 +02:00
|
|
|
htp_msg_begincb, // http_cb on_message_begin;
|
|
|
|
nullptr, // http_data_cb on_url;
|
|
|
|
htp_statuscb, // http_data_cb on_status;
|
|
|
|
htp_hdr_keycb, // http_data_cb on_header_field;
|
|
|
|
htp_hdr_valcb, // http_data_cb on_header_value;
|
|
|
|
nullptr, // http_cb on_headers_complete;
|
|
|
|
htp_body_cb, // http_data_cb on_body;
|
|
|
|
htp_msg_completecb // http_cb on_message_complete;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Http1Session::on_connect() { client_->signal_write(); }
|
|
|
|
|
2016-01-05 14:36:37 +01:00
|
|
|
int Http1Session::submit_request() {
|
2015-09-14 15:33:48 +02:00
|
|
|
auto config = client_->worker->config;
|
2015-11-26 14:04:31 +01:00
|
|
|
const auto &req = config->h1reqs[client_->reqidx];
|
2015-09-14 15:33:48 +02:00
|
|
|
client_->reqidx++;
|
|
|
|
|
|
|
|
if (client_->reqidx == config->h1reqs.size()) {
|
|
|
|
client_->reqidx = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:36:37 +01:00
|
|
|
client_->on_request(stream_req_counter_);
|
|
|
|
|
|
|
|
auto req_stat = client_->get_req_stat(stream_req_counter_);
|
|
|
|
|
2015-09-14 15:33:48 +02:00
|
|
|
client_->record_request_time(req_stat);
|
2016-06-12 11:50:52 +02:00
|
|
|
client_->wb.append(req);
|
2015-09-14 15:33:48 +02:00
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
if (config->data_fd == -1 || config->data_length == 0) {
|
|
|
|
// increment for next request
|
|
|
|
stream_req_counter_ += 2;
|
2016-06-12 11:54:06 +02:00
|
|
|
|
|
|
|
return 0;
|
2016-06-12 10:42:12 +02:00
|
|
|
}
|
2015-10-29 14:31:03 +01:00
|
|
|
|
2016-06-12 11:54:06 +02:00
|
|
|
return on_write();
|
2015-09-14 15:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Http1Session::on_read(const uint8_t *data, size_t len) {
|
|
|
|
auto nread = http_parser_execute(&htp_, &htp_hooks,
|
|
|
|
reinterpret_cast<const char *>(data), len);
|
|
|
|
|
|
|
|
if (client_->worker->config->verbose) {
|
|
|
|
std::cout.write(reinterpret_cast<const char *>(data), nread);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto htperr = HTTP_PARSER_ERRNO(&htp_);
|
|
|
|
|
2015-09-28 16:50:44 +02:00
|
|
|
if (htperr == HPE_PAUSED) {
|
|
|
|
// pause is done only when connection: close is requested
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:33:48 +02:00
|
|
|
if (htperr != HPE_OK) {
|
|
|
|
std::cerr << "[ERROR] HTTP parse error: "
|
|
|
|
<< "(" << http_errno_name(htperr) << ") "
|
|
|
|
<< http_errno_description(htperr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Http1Session::on_write() {
|
|
|
|
if (complete_) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-12 10:42:12 +02:00
|
|
|
|
|
|
|
auto config = client_->worker->config;
|
|
|
|
auto req_stat = client_->get_req_stat(stream_req_counter_);
|
|
|
|
if (!req_stat) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req_stat->data_offset < config->data_length) {
|
|
|
|
auto req_stat = client_->get_req_stat(stream_req_counter_);
|
|
|
|
auto &wb = client_->wb;
|
|
|
|
|
2016-06-12 11:50:52 +02:00
|
|
|
// TODO unfortunately, wb has no interface to use with read(2)
|
|
|
|
// family functions.
|
|
|
|
std::array<uint8_t, 16_k> buf;
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
ssize_t nread;
|
2016-06-12 11:50:52 +02:00
|
|
|
while ((nread = pread(config->data_fd, buf.data(), buf.size(),
|
2016-06-12 10:42:12 +02:00
|
|
|
req_stat->data_offset)) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (nread == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
req_stat->data_offset += nread;
|
|
|
|
|
2016-06-12 11:50:52 +02:00
|
|
|
wb.append(buf.data(), nread);
|
2016-06-12 10:42:12 +02:00
|
|
|
|
|
|
|
if (client_->worker->config->verbose) {
|
|
|
|
std::cout << "[send " << nread << " byte(s)]" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req_stat->data_offset == config->data_length) {
|
|
|
|
// increment for next request
|
|
|
|
stream_req_counter_ += 2;
|
|
|
|
|
|
|
|
if (stream_resp_counter_ == stream_req_counter_) {
|
|
|
|
// Response has already been received
|
|
|
|
client_->on_stream_close(stream_resp_counter_ - 2, true,
|
|
|
|
client_->final);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:33:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Http1Session::terminate() { complete_ = true; }
|
|
|
|
|
|
|
|
Client *Http1Session::get_client() { return client_; }
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
size_t Http1Session::max_concurrent_streams() {
|
|
|
|
auto config = client_->worker->config;
|
|
|
|
|
|
|
|
return config->data_fd == -1 ? config->max_concurrent_streams : 1;
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:33:48 +02:00
|
|
|
} // namespace h2load
|