nghttp2/src/shrpx_client_handler.cc

721 lines
21 KiB
C++
Raw Normal View History

/*
2014-03-30 12:09:21 +02:00
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2012 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 "shrpx_client_handler.h"
#include <unistd.h>
#include <cerrno>
#include "shrpx_upstream.h"
2013-07-26 12:38:54 +02:00
#include "shrpx_http2_upstream.h"
#include "shrpx_https_upstream.h"
#include "shrpx_config.h"
#include "shrpx_http_downstream_connection.h"
#include "shrpx_http2_downstream_connection.h"
#include "shrpx_ssl.h"
#include "shrpx_worker.h"
#include "shrpx_worker_config.h"
#include "shrpx_downstream_connection_pool.h"
#include "shrpx_downstream.h"
#ifdef HAVE_SPDYLAY
#include "shrpx_spdy_upstream.h"
#endif // HAVE_SPDYLAY
#include "util.h"
#include "libevent_util.h"
using namespace nghttp2;
namespace shrpx {
namespace {
2014-11-27 15:39:04 +01:00
void upstream_readcb(bufferevent *bev, void *arg) {
auto handler = static_cast<ClientHandler *>(arg);
auto upstream = handler->get_upstream();
2014-11-27 15:39:04 +01:00
if (upstream) {
upstream->reset_timeouts();
}
int rv = handler->on_read();
2014-11-27 15:39:04 +01:00
if (rv != 0) {
delete handler;
}
}
} // namespace
namespace {
2014-11-27 15:39:04 +01:00
void upstream_writecb(bufferevent *bev, void *arg) {
auto handler = static_cast<ClientHandler *>(arg);
auto upstream = handler->get_upstream();
2014-11-27 15:39:04 +01:00
if (upstream) {
upstream->reset_timeouts();
}
handler->update_last_write_time();
2014-05-31 19:29:01 +02:00
// We actually depend on write low-water mark == 0.
2014-11-27 15:39:04 +01:00
if (handler->get_outbuf_length() > 0) {
// Possibly because of deferred callback, we may get this callback
// when the output buffer is not empty.
return;
}
2014-11-27 15:39:04 +01:00
if (handler->get_should_close_after_write()) {
delete handler;
return;
}
2014-11-27 15:39:04 +01:00
if (!upstream) {
return;
}
int rv = upstream->on_write();
2014-11-27 15:39:04 +01:00
if (rv != 0) {
delete handler;
}
}
} // namespace
namespace {
2014-11-27 15:39:04 +01:00
void upstream_eventcb(bufferevent *bev, short events, void *arg) {
auto handler = static_cast<ClientHandler *>(arg);
bool finish = false;
2014-11-27 15:39:04 +01:00
if (events & BEV_EVENT_EOF) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, handler) << "EOF";
}
finish = true;
}
2014-11-27 15:39:04 +01:00
if (events & BEV_EVENT_ERROR) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, handler) << "Network error: " << evutil_socket_error_to_string(
EVUTIL_SOCKET_ERROR());
}
finish = true;
}
2014-11-27 15:39:04 +01:00
if (events & BEV_EVENT_TIMEOUT) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, handler) << "Time out";
}
finish = true;
}
2014-11-27 15:39:04 +01:00
if (finish) {
delete handler;
} else {
2014-11-27 15:39:04 +01:00
if (events & BEV_EVENT_CONNECTED) {
2014-01-18 11:53:52 +01:00
handler->set_tls_handshake(true);
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
2014-01-18 11:53:52 +01:00
CLOG(INFO, handler) << "SSL/TLS handshake completed";
}
2014-11-27 15:39:04 +01:00
if (handler->validate_next_proto() != 0) {
delete handler;
return;
}
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
if (SSL_session_reused(handler->get_ssl())) {
CLOG(INFO, handler) << "SSL/TLS session reused";
}
}
}
}
}
} // namespace
namespace {
2014-11-27 15:39:04 +01:00
void upstream_http2_connhd_readcb(bufferevent *bev, void *arg) {
// This callback assumes upstream is Http2Upstream.
2014-11-27 15:39:04 +01:00
auto handler = static_cast<ClientHandler *>(arg);
if (handler->on_http2_connhd_read() != 0) {
delete handler;
}
}
} // namespace
namespace {
2014-11-27 15:39:04 +01:00
void upstream_http1_connhd_readcb(bufferevent *bev, void *arg) {
// This callback assumes upstream is HttpsUpstream.
2014-11-27 15:39:04 +01:00
auto handler = static_cast<ClientHandler *>(arg);
if (handler->on_http1_connhd_read() != 0) {
delete handler;
}
}
} // namespace
ClientHandler::ClientHandler(bufferevent *bev,
bufferevent_rate_limit_group *rate_limit_group,
2014-11-27 15:39:04 +01:00
int fd, SSL *ssl, const char *ipaddr,
const char *port, WorkerStat *worker_stat,
DownstreamConnectionPool *dconn_pool)
2014-11-27 15:39:04 +01:00
: ipaddr_(ipaddr), port_(port), dconn_pool_(dconn_pool), bev_(bev),
http2session_(nullptr), ssl_(ssl), reneg_shutdown_timerev_(nullptr),
worker_stat_(worker_stat), last_write_time_(0), warmup_writelen_(0),
left_connhd_len_(NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN), fd_(fd),
should_close_after_write_(false), tls_handshake_(false),
tls_renegotiation_(false) {
int rv;
++worker_stat->num_connections;
rv = bufferevent_set_rate_limit(bev_, get_config()->rate_limit_cfg);
2014-11-27 15:39:04 +01:00
if (rv == -1) {
CLOG(FATAL, this) << "bufferevent_set_rate_limit() failed";
}
rv = bufferevent_add_to_rate_limit_group(bev_, rate_limit_group);
2014-11-27 15:39:04 +01:00
if (rv == -1) {
CLOG(FATAL, this) << "bufferevent_add_to_rate_limit_group() failed";
}
2014-09-18 16:56:01 +02:00
util::bev_enable_unless(bev_, EV_READ | EV_WRITE);
2014-05-31 19:29:01 +02:00
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
set_upstream_timeouts(&get_config()->upstream_read_timeout,
&get_config()->upstream_write_timeout);
2014-11-27 15:39:04 +01:00
if (ssl_) {
SSL_set_app_data(ssl_, reinterpret_cast<char *>(this));
2013-07-26 12:33:25 +02:00
set_bev_cb(nullptr, upstream_writecb, upstream_eventcb);
} else {
// For non-TLS version, first create HttpsUpstream. It may be
2014-03-30 12:09:21 +02:00
// upgraded to HTTP/2 through HTTP Upgrade or direct HTTP/2
// connection.
upstream_ = util::make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1";
set_bev_cb(upstream_http1_connhd_readcb, nullptr, upstream_eventcb);
}
}
2014-11-27 15:39:04 +01:00
ClientHandler::~ClientHandler() {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Deleting";
}
2014-11-27 15:39:04 +01:00
if (upstream_) {
upstream_->on_handler_delete();
}
--worker_stat_->num_connections;
// TODO If backend is http/2, and it is in CONNECTED state, signal
// it and make it loopbreak when output is zero.
2014-11-27 15:39:04 +01:00
if (worker_config->graceful_shutdown && worker_stat_->num_connections == 0) {
event_base_loopbreak(get_evbase());
}
2014-11-27 15:39:04 +01:00
if (reneg_shutdown_timerev_) {
event_free(reneg_shutdown_timerev_);
}
2014-11-27 15:39:04 +01:00
if (ssl_) {
2014-01-18 11:53:52 +01:00
SSL_set_app_data(ssl_, nullptr);
SSL_set_shutdown(ssl_, SSL_RECEIVED_SHUTDOWN);
SSL_shutdown(ssl_);
}
bufferevent_remove_from_rate_limit_group(bev_);
2014-09-18 16:56:01 +02:00
util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
bufferevent_free(bev_);
2014-11-27 15:39:04 +01:00
if (ssl_) {
SSL_free(ssl_);
}
shutdown(fd_, SHUT_WR);
close(fd_);
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Deleted";
}
}
2014-11-27 15:39:04 +01:00
Upstream *ClientHandler::get_upstream() { return upstream_.get(); }
2014-11-27 15:39:04 +01:00
bufferevent *ClientHandler::get_bev() const { return bev_; }
2014-11-27 15:39:04 +01:00
event_base *ClientHandler::get_evbase() const {
return bufferevent_get_base(bev_);
}
2014-11-27 15:39:04 +01:00
void ClientHandler::set_bev_cb(bufferevent_data_cb readcb,
bufferevent_data_cb writecb,
bufferevent_event_cb eventcb) {
bufferevent_setcb(bev_, readcb, writecb, eventcb, this);
}
void ClientHandler::set_upstream_timeouts(const timeval *read_timeout,
2014-11-27 15:39:04 +01:00
const timeval *write_timeout) {
bufferevent_set_timeouts(bev_, read_timeout, write_timeout);
}
2014-11-27 15:39:04 +01:00
int ClientHandler::validate_next_proto() {
2013-07-26 12:33:25 +02:00
const unsigned char *next_proto = nullptr;
unsigned int next_proto_len;
int rv;
// First set callback for catch all cases
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
SSL_get0_next_proto_negotiated(ssl_, &next_proto, &next_proto_len);
2014-11-27 15:39:04 +01:00
for (int i = 0; i < 2; ++i) {
if (next_proto) {
if (LOG_ENABLED(INFO)) {
std::string proto(next_proto, next_proto + next_proto_len);
CLOG(INFO, this) << "The negotiated next protocol: " << proto;
}
2014-11-27 15:39:04 +01:00
if (!ssl::in_proto_list(get_config()->npn_list, next_proto,
next_proto_len)) {
break;
}
2014-11-27 15:39:04 +01:00
if (util::check_h2_is_selected(next_proto, next_proto_len)) {
set_bev_cb(upstream_http2_connhd_readcb, upstream_writecb,
upstream_eventcb);
auto http2_upstream = util::make_unique<Http2Upstream>(this);
2014-11-27 15:39:04 +01:00
if (!ssl::check_http2_requirement(ssl_)) {
rv = http2_upstream->terminate_session(NGHTTP2_INADEQUATE_SECURITY);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
return -1;
}
}
upstream_ = std::move(http2_upstream);
alpn_.assign(next_proto, next_proto + next_proto_len);
// At this point, input buffer is already filled with some
// bytes. The read callback is not called until new data
// come. So consume input buffer here.
2014-11-27 15:39:04 +01:00
if (on_http2_connhd_read() != 0) {
return -1;
}
return 0;
} else {
#ifdef HAVE_SPDYLAY
uint16_t version = spdylay_npn_get_version(next_proto, next_proto_len);
2014-11-27 15:39:04 +01:00
if (version) {
upstream_ = util::make_unique<SpdyUpstream>(version, this);
2014-11-27 15:39:04 +01:00
switch (version) {
case SPDYLAY_PROTO_SPDY2:
alpn_ = "spdy/2";
break;
case SPDYLAY_PROTO_SPDY3:
alpn_ = "spdy/3";
break;
case SPDYLAY_PROTO_SPDY3_1:
alpn_ = "spdy/3.1";
break;
default:
alpn_ = "spdy/unknown";
}
// At this point, input buffer is already filled with some
// bytes. The read callback is not called until new data
// come. So consume input buffer here.
2014-11-27 15:39:04 +01:00
if (upstream_->on_read() != 0) {
return -1;
}
return 0;
}
#endif // HAVE_SPDYLAY
2014-11-27 15:39:04 +01:00
if (next_proto_len == 8 && memcmp("http/1.1", next_proto, 8) == 0) {
upstream_ = util::make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1";
// At this point, input buffer is already filled with some
// bytes. The read callback is not called until new data
// come. So consume input buffer here.
2014-11-27 15:39:04 +01:00
if (upstream_->on_read() != 0) {
return -1;
}
return 0;
}
}
break;
}
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
SSL_get0_alpn_selected(ssl_, &next_proto, &next_proto_len);
2014-11-27 15:39:04 +01:00
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
break;
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
}
2014-11-27 15:39:04 +01:00
if (!next_proto) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "No protocol negotiated. Fallback to HTTP/1.1";
}
upstream_ = util::make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1";
// At this point, input buffer is already filled with some bytes.
// The read callback is not called until new data come. So consume
// input buffer here.
2014-11-27 15:39:04 +01:00
if (upstream_->on_read() != 0) {
return -1;
}
return 0;
}
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "The negotiated protocol is not supported";
2012-06-05 18:26:04 +02:00
}
return -1;
}
2014-11-27 15:39:04 +01:00
int ClientHandler::on_read() { return upstream_->on_read(); }
2014-11-27 15:39:04 +01:00
int ClientHandler::on_event() { return upstream_->on_event(); }
2014-11-27 15:39:04 +01:00
int ClientHandler::on_http2_connhd_read() {
// This callback assumes upstream is Http2Upstream.
uint8_t data[NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN];
auto input = bufferevent_get_input(bev_);
auto readlen = evbuffer_remove(input, data, left_connhd_len_);
2014-11-27 15:39:04 +01:00
if (readlen == -1) {
return -1;
}
2014-11-27 15:39:04 +01:00
if (memcmp(NGHTTP2_CLIENT_CONNECTION_PREFACE +
NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN - left_connhd_len_,
data, readlen) != 0) {
// There is no downgrade path here. Just drop the connection.
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "invalid client connection header";
}
return -1;
}
left_connhd_len_ -= readlen;
2014-11-27 15:39:04 +01:00
if (left_connhd_len_ > 0) {
return 0;
}
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
// Run on_read to process data left in buffer since they are not
// notified further
2014-11-27 15:39:04 +01:00
if (on_read() != 0) {
return -1;
}
return 0;
}
2014-11-27 15:39:04 +01:00
int ClientHandler::on_http1_connhd_read() {
uint8_t data[NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN];
auto input = bufferevent_get_input(bev_);
auto readlen = evbuffer_copyout(input, data, left_connhd_len_);
2014-11-27 15:39:04 +01:00
if (readlen == -1) {
return -1;
}
2014-11-27 15:39:04 +01:00
if (memcmp(NGHTTP2_CLIENT_CONNECTION_PREFACE +
NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN - left_connhd_len_,
data, readlen) != 0) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "This is HTTP/1.1 connection, "
<< "but may be upgraded to HTTP/2 later.";
}
// Reset header length for later HTTP/2 upgrade
left_connhd_len_ = NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN;
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
2014-11-27 15:39:04 +01:00
if (on_read() != 0) {
return -1;
}
return 0;
}
2014-11-27 15:39:04 +01:00
if (evbuffer_drain(input, readlen) == -1) {
return -1;
}
left_connhd_len_ -= readlen;
2014-11-27 15:39:04 +01:00
if (left_connhd_len_ > 0) {
return 0;
}
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "direct HTTP/2 connection";
}
direct_http2_upgrade();
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
// Run on_read to process data left in buffer since they are not
// notified further
2014-11-27 15:39:04 +01:00
if (on_read() != 0) {
return -1;
}
return 0;
}
2014-11-27 15:39:04 +01:00
const std::string &ClientHandler::get_ipaddr() const { return ipaddr_; }
2014-11-27 15:39:04 +01:00
bool ClientHandler::get_should_close_after_write() const {
return should_close_after_write_;
}
2014-11-27 15:39:04 +01:00
void ClientHandler::set_should_close_after_write(bool f) {
should_close_after_write_ = f;
}
2014-11-27 15:39:04 +01:00
void ClientHandler::pool_downstream_connection(
std::unique_ptr<DownstreamConnection> dconn) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Pooling downstream connection DCONN:" << dconn.get();
2012-06-09 16:14:00 +02:00
}
dconn->set_client_handler(nullptr);
dconn_pool_->add_downstream_connection(std::move(dconn));
2012-06-09 16:14:00 +02:00
}
2014-11-27 15:39:04 +01:00
void ClientHandler::remove_downstream_connection(DownstreamConnection *dconn) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Removing downstream connection DCONN:" << dconn
<< " from pool";
2012-06-09 16:14:00 +02:00
}
dconn_pool_->remove_downstream_connection(dconn);
2012-06-09 16:14:00 +02:00
}
std::unique_ptr<DownstreamConnection>
2014-11-27 15:39:04 +01:00
ClientHandler::get_downstream_connection() {
auto dconn = dconn_pool_->pop_downstream_connection();
2014-11-27 15:39:04 +01:00
if (!dconn) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Downstream connection pool is empty."
<< " Create new one";
2012-06-09 16:14:00 +02:00
}
2014-11-27 15:39:04 +01:00
if (http2session_) {
dconn = util::make_unique<Http2DownstreamConnection>(dconn_pool_,
http2session_);
} else {
dconn = util::make_unique<HttpDownstreamConnection>(dconn_pool_);
}
dconn->set_client_handler(this);
return dconn;
2012-06-09 16:14:00 +02:00
}
dconn->set_client_handler(this);
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Reuse downstream connection DCONN:" << dconn.get()
<< " from pool";
}
return dconn;
2012-06-09 16:14:00 +02:00
}
2014-11-27 15:39:04 +01:00
size_t ClientHandler::get_outbuf_length() {
return evbuffer_get_length(bufferevent_get_output(bev_));
}
2014-11-27 15:39:04 +01:00
SSL *ClientHandler::get_ssl() const { return ssl_; }
2014-11-27 15:39:04 +01:00
void ClientHandler::set_http2_session(Http2Session *http2session) {
http2session_ = http2session;
}
2014-11-27 15:39:04 +01:00
Http2Session *ClientHandler::get_http2_session() const { return http2session_; }
2014-11-27 15:39:04 +01:00
void ClientHandler::set_http1_connect_blocker(
ConnectBlocker *http1_connect_blocker) {
http1_connect_blocker_ = http1_connect_blocker;
}
2014-11-27 15:39:04 +01:00
ConnectBlocker *ClientHandler::get_http1_connect_blocker() const {
return http1_connect_blocker_;
}
2014-11-27 15:39:04 +01:00
void ClientHandler::direct_http2_upgrade() {
upstream_ = util::make_unique<Http2Upstream>(this);
// TODO We don't know exact h2 draft version in direct upgrade. We
// just use library default for now.
alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID;
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
}
2014-11-27 15:39:04 +01:00
int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) {
int rv;
auto upstream = util::make_unique<Http2Upstream>(this);
2014-11-27 15:39:04 +01:00
if (upstream->upgrade_upstream(http) != 0) {
return -1;
}
2013-09-26 14:39:19 +02:00
// http pointer is now owned by upstream.
upstream_.release();
upstream_ = std::move(upstream);
// TODO We might get other version id in HTTP2-settings, if we
// support aliasing for h2, but we just use library default for now.
alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID;
set_bev_cb(upstream_http2_connhd_readcb, upstream_writecb, upstream_eventcb);
static char res[] = "HTTP/1.1 101 Switching Protocols\r\n"
2014-11-27 15:39:04 +01:00
"Connection: Upgrade\r\n"
"Upgrade: " NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "\r\n"
"\r\n";
rv = bufferevent_write(bev_, res, sizeof(res) - 1);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
CLOG(FATAL, this) << "bufferevent_write() faild";
return -1;
}
return 0;
}
2014-11-27 15:39:04 +01:00
bool ClientHandler::get_http2_upgrade_allowed() const { return !ssl_; }
2014-11-27 15:39:04 +01:00
std::string ClientHandler::get_upstream_scheme() const {
if (ssl_) {
return "https";
} else {
return "http";
}
}
2014-11-27 15:39:04 +01:00
void ClientHandler::set_tls_handshake(bool f) { tls_handshake_ = f; }
2014-06-12 16:37:33 +02:00
2014-11-27 15:39:04 +01:00
bool ClientHandler::get_tls_handshake() const { return tls_handshake_; }
2014-06-12 16:37:33 +02:00
namespace {
2014-11-27 15:39:04 +01:00
void shutdown_cb(evutil_socket_t fd, short what, void *arg) {
auto handler = static_cast<ClientHandler *>(arg);
2014-11-27 15:39:04 +01:00
if (LOG_ENABLED(INFO)) {
CLOG(INFO, handler) << "Close connection due to TLS renegotiation";
}
delete handler;
}
} // namespace
2014-11-27 15:39:04 +01:00
void ClientHandler::set_tls_renegotiation(bool f) {
if (tls_renegotiation_ == false) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "TLS renegotiation detected. "
<< "Start shutdown timer now.";
}
reneg_shutdown_timerev_ = evtimer_new(get_evbase(), shutdown_cb, this);
event_priority_set(reneg_shutdown_timerev_, 0);
timeval timeout = {0, 0};
// TODO What to do if this failed?
evtimer_add(reneg_shutdown_timerev_, &timeout);
}
2014-01-18 11:53:52 +01:00
tls_renegotiation_ = f;
}
2014-11-27 15:39:04 +01:00
bool ClientHandler::get_tls_renegotiation() const { return tls_renegotiation_; }
2014-01-18 11:53:52 +01:00
namespace {
const size_t SHRPX_SMALL_WRITE_LIMIT = 1300;
const size_t SHRPX_WARMUP_THRESHOLD = 1 << 20;
} // namespace
2014-11-27 15:39:04 +01:00
ssize_t ClientHandler::get_write_limit() {
if (!ssl_) {
return -1;
}
timeval tv;
2014-11-27 15:39:04 +01:00
if (event_base_gettimeofday_cached(get_evbase(), &tv) == 0) {
auto now = util::to_time64(tv);
2014-11-27 15:39:04 +01:00
if (now - last_write_time_ > 1000000) {
// Time out, use small record size
warmup_writelen_ = 0;
return SHRPX_SMALL_WRITE_LIMIT;
}
}
// If event_base_gettimeofday_cached() failed, we just skip timer
// checking. Don't know how to treat this.
2014-11-27 15:39:04 +01:00
if (warmup_writelen_ >= SHRPX_WARMUP_THRESHOLD) {
return -1;
}
return SHRPX_SMALL_WRITE_LIMIT;
}
2014-11-27 15:39:04 +01:00
void ClientHandler::update_warmup_writelen(size_t n) {
if (warmup_writelen_ < SHRPX_WARMUP_THRESHOLD) {
warmup_writelen_ += n;
}
}
2014-11-27 15:39:04 +01:00
void ClientHandler::update_last_write_time() {
timeval tv;
2014-11-27 15:39:04 +01:00
if (event_base_gettimeofday_cached(get_evbase(), &tv) == 0) {
last_write_time_ = util::to_time64(tv);
}
}
2014-11-27 15:39:04 +01:00
void ClientHandler::write_accesslog(Downstream *downstream) {
LogSpec lgsp = {
2014-11-27 15:39:04 +01:00
downstream, ipaddr_.c_str(), downstream->get_request_method().c_str(),
downstream->get_request_path().empty()
? downstream->get_request_http2_authority().c_str()
: downstream->get_request_path().c_str(),
alpn_.c_str(),
downstream->get_request_start_time(),
std::chrono::high_resolution_clock::now(),
downstream->get_request_major(), downstream->get_request_minor(),
downstream->get_response_http_status(),
downstream->get_response_sent_bodylen(), port_.c_str(),
get_config()->port, get_config()->pid,
};
upstream_accesslog(get_config()->accesslog_format, &lgsp);
}
2014-11-27 15:39:04 +01:00
void ClientHandler::write_accesslog(int major, int minor, unsigned int status,
int64_t body_bytes_sent) {
LogSpec lgsp = {
2014-11-27 15:39:04 +01:00
nullptr, ipaddr_.c_str(),
"-", // method
"-", // path,
alpn_.c_str(),
std::chrono::high_resolution_clock::now(), // request_start_time TODO is
// there a better value?
std::chrono::high_resolution_clock::now(), // time_now
major, minor, // major, minor
status, body_bytes_sent,
port_.c_str(), get_config()->port,
get_config()->pid,
};
upstream_accesslog(get_config()->accesslog_format, &lgsp);
}
} // namespace shrpx