2012-11-18 13:23:13 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-11-18 13:23:13 +01:00
|
|
|
*
|
|
|
|
* 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_http_downstream_connection.h"
|
|
|
|
|
2021-09-10 15:55:33 +02:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
#include "shrpx_client_handler.h"
|
|
|
|
#include "shrpx_upstream.h"
|
|
|
|
#include "shrpx_downstream.h"
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_error.h"
|
|
|
|
#include "shrpx_http.h"
|
2015-02-25 16:02:29 +01:00
|
|
|
#include "shrpx_log_config.h"
|
2014-08-19 16:36:04 +02:00
|
|
|
#include "shrpx_connect_blocker.h"
|
2014-10-13 14:09:00 +02:00
|
|
|
#include "shrpx_downstream_connection_pool.h"
|
2014-12-06 10:31:46 +01:00
|
|
|
#include "shrpx_worker.h"
|
2015-07-12 15:16:20 +02:00
|
|
|
#include "shrpx_http2_session.h"
|
2017-04-01 08:07:32 +02:00
|
|
|
#include "shrpx_tls.h"
|
2017-02-16 14:46:22 +01:00
|
|
|
#include "shrpx_log.h"
|
2013-08-27 19:47:22 +02:00
|
|
|
#include "http2.h"
|
2012-11-18 13:23:13 +01:00
|
|
|
#include "util.h"
|
2017-11-26 02:56:39 +01:00
|
|
|
#include "ssl_compat.h"
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
using namespace nghttp2;
|
2012-11-18 13:23:13 +01:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2014-01-19 10:07:50 +01:00
|
|
|
namespace {
|
2014-12-27 18:59:06 +01:00
|
|
|
void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-11-25 16:00:32 +01:00
|
|
|
if (w == &conn->rt && !conn->expired_rt()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, dconn) << "Time out";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
2016-01-13 16:37:45 +01:00
|
|
|
auto &resp = downstream->response();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
// Do this so that dconn is not pooled
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.connection_close = true;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
if (upstream->downstream_error(dconn, Downstream::EVENT_TIMEOUT) != 0) {
|
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-24 14:24:30 +02:00
|
|
|
namespace {
|
2017-11-04 07:55:25 +01:00
|
|
|
void retry_downstream_connection(Downstream *downstream,
|
|
|
|
unsigned int status_code) {
|
2016-05-24 14:24:30 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
assert(!downstream->get_request_header_sent());
|
|
|
|
|
2017-11-04 07:55:25 +01:00
|
|
|
downstream->add_retry();
|
|
|
|
|
|
|
|
if (downstream->no_more_retry()) {
|
|
|
|
delete handler;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-30 16:37:49 +02:00
|
|
|
downstream->pop_downstream_connection();
|
2019-01-19 03:12:05 +01:00
|
|
|
auto buf = downstream->get_request_buf();
|
|
|
|
buf->reset();
|
2016-05-24 14:24:30 +02:00
|
|
|
|
2017-02-18 10:23:06 +01:00
|
|
|
int rv;
|
2019-01-19 03:12:05 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto ndconn = handler->get_downstream_connection(rv, downstream);
|
|
|
|
if (!ndconn) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (downstream->attach_downstream_connection(std::move(ndconn)) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (downstream->push_request_headers() == 0) {
|
2016-08-30 16:37:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:03:32 +02:00
|
|
|
downstream->set_request_state(DownstreamState::CONNECT_FAIL);
|
2016-08-30 16:37:49 +02:00
|
|
|
|
2017-02-18 10:23:06 +01:00
|
|
|
if (rv == SHRPX_ERR_TLS_REQUIRED) {
|
|
|
|
rv = upstream->on_downstream_abort_request_with_https_redirect(downstream);
|
|
|
|
} else {
|
2017-11-04 07:55:25 +01:00
|
|
|
rv = upstream->on_downstream_abort_request(downstream, status_code);
|
2017-02-18 10:23:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
2016-05-24 14:24:30 +02:00
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-11-04 07:55:25 +01:00
|
|
|
namespace {
|
|
|
|
void connect_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
|
|
|
auto addr = dconn->get_addr();
|
|
|
|
auto raddr = dconn->get_raddr();
|
|
|
|
|
|
|
|
DCLOG(WARN, dconn) << "Connect time out; addr="
|
|
|
|
<< util::to_numeric_addr(raddr);
|
|
|
|
|
|
|
|
downstream_failure(addr, raddr);
|
|
|
|
|
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
|
|
|
|
retry_downstream_connection(downstream, 504);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
namespace {
|
|
|
|
void backend_retry(Downstream *downstream) {
|
|
|
|
retry_downstream_connection(downstream, 502);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
namespace {
|
|
|
|
void readcb(struct ev_loop *loop, ev_io *w, int revents) {
|
2019-09-07 11:20:24 +02:00
|
|
|
int rv;
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
2014-12-27 18:59:06 +01:00
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
rv = upstream->downstream_read(dconn);
|
|
|
|
if (rv != 0) {
|
|
|
|
if (rv == SHRPX_ERR_RETRY) {
|
|
|
|
backend_retry(downstream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void writecb(struct ev_loop *loop, ev_io *w, int revents) {
|
2016-12-25 13:22:34 +01:00
|
|
|
int rv;
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
2014-12-27 18:59:06 +01:00
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
|
2016-12-25 13:22:34 +01:00
|
|
|
rv = upstream->downstream_write(dconn);
|
|
|
|
if (rv == SHRPX_ERR_RETRY) {
|
2016-12-25 14:18:25 +01:00
|
|
|
backend_retry(downstream);
|
2016-12-25 13:22:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
2014-12-27 18:59:06 +01:00
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void connectcb(struct ev_loop *loop, ev_io *w, int revents) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
2015-01-05 08:14:10 +01:00
|
|
|
auto downstream = dconn->get_downstream();
|
2016-02-06 11:50:21 +01:00
|
|
|
if (dconn->connected() != 0) {
|
2016-12-25 14:18:25 +01:00
|
|
|
backend_retry(downstream);
|
2015-01-05 08:14:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
writecb(loop, w, revents);
|
|
|
|
}
|
2014-01-19 10:07:50 +01:00
|
|
|
} // namespace
|
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
HttpDownstreamConnection::HttpDownstreamConnection(
|
2019-01-19 03:12:05 +01:00
|
|
|
const std::shared_ptr<DownstreamAddrGroup> &group, DownstreamAddr *addr,
|
2016-06-09 15:35:59 +02:00
|
|
|
struct ev_loop *loop, Worker *worker)
|
2016-02-27 15:24:14 +01:00
|
|
|
: conn_(loop, -1, nullptr, worker->get_mcpool(),
|
2018-09-30 05:23:34 +02:00
|
|
|
group->shared_addr->timeout.write, group->shared_addr->timeout.read,
|
2019-09-06 15:01:03 +02:00
|
|
|
{}, {}, connectcb, readcb, connect_timeoutcb, this,
|
2016-05-24 14:24:30 +02:00
|
|
|
get_config()->tls.dyn_rec.warmup_threshold,
|
2018-10-16 15:17:37 +02:00
|
|
|
get_config()->tls.dyn_rec.idle_timeout, Proto::HTTP1),
|
2017-01-08 15:09:00 +01:00
|
|
|
on_read_(&HttpDownstreamConnection::noop),
|
|
|
|
on_write_(&HttpDownstreamConnection::noop),
|
|
|
|
signal_write_(&HttpDownstreamConnection::noop),
|
2016-02-06 15:07:00 +01:00
|
|
|
worker_(worker),
|
2016-05-24 16:36:43 +02:00
|
|
|
ssl_ctx_(worker->get_cl_ssl_ctx()),
|
2016-02-27 15:24:14 +01:00
|
|
|
group_(group),
|
2019-01-19 03:12:05 +01:00
|
|
|
addr_(addr),
|
2016-12-18 14:16:52 +01:00
|
|
|
raddr_(nullptr),
|
2016-01-27 13:14:07 +01:00
|
|
|
ioctrl_(&conn_.rlimit),
|
2016-06-09 15:35:59 +02:00
|
|
|
response_htp_{0},
|
2019-01-19 03:12:05 +01:00
|
|
|
first_write_done_(false),
|
|
|
|
reusable_(true),
|
|
|
|
request_header_written_(false) {}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2016-06-04 05:16:31 +02:00
|
|
|
HttpDownstreamConnection::~HttpDownstreamConnection() {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Deleted";
|
|
|
|
}
|
2016-12-04 15:43:41 +01:00
|
|
|
|
|
|
|
if (dns_query_) {
|
|
|
|
auto dns_tracker = worker_->get_dns_tracker();
|
|
|
|
dns_tracker->cancel(dns_query_.get());
|
|
|
|
}
|
2016-06-04 05:16:31 +02:00
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::attach_downstream(Downstream *downstream) {
|
2016-12-04 15:43:41 +01:00
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "Attaching to DOWNSTREAM:" << downstream;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
downstream_ = downstream;
|
|
|
|
|
|
|
|
rv = initiate_connection();
|
|
|
|
if (rv != 0) {
|
|
|
|
downstream_ = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-23 12:44:36 +01:00
|
|
|
namespace {
|
|
|
|
int htp_msg_begincb(llhttp_t *htp);
|
|
|
|
int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len);
|
|
|
|
int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len);
|
|
|
|
int htp_hdrs_completecb(llhttp_t *htp);
|
|
|
|
int htp_bodycb(llhttp_t *htp, const char *data, size_t len);
|
|
|
|
int htp_msg_completecb(llhttp_t *htp);
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr llhttp_settings_t htp_hooks = {
|
|
|
|
htp_msg_begincb, // llhttp_cb on_message_begin;
|
|
|
|
nullptr, // llhttp_data_cb on_url;
|
|
|
|
nullptr, // llhttp_data_cb on_status;
|
|
|
|
htp_hdr_keycb, // llhttp_data_cb on_header_field;
|
|
|
|
htp_hdr_valcb, // llhttp_data_cb on_header_value;
|
|
|
|
htp_hdrs_completecb, // llhttp_cb on_headers_complete;
|
|
|
|
htp_bodycb, // llhttp_data_cb on_body;
|
|
|
|
htp_msg_completecb, // llhttp_cb on_message_complete;
|
|
|
|
nullptr, // llhttp_cb on_chunk_header
|
|
|
|
nullptr, // llhttp_cb on_chunk_complete
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
int HttpDownstreamConnection::initiate_connection() {
|
|
|
|
int rv;
|
|
|
|
|
2016-02-21 07:27:19 +01:00
|
|
|
auto worker_blocker = worker_->get_connect_blocker();
|
|
|
|
if (worker_blocker->blocked()) {
|
2016-02-21 08:11:50 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this)
|
|
|
|
<< "Worker wide backend connection was blocked temporarily";
|
|
|
|
}
|
2016-02-21 07:27:19 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
auto &downstreamconf = *worker_->get_downstream_config();
|
2016-01-19 08:56:12 +01:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
if (conn_.fd == -1) {
|
2019-01-19 03:12:05 +01:00
|
|
|
auto check_dns_result = dns_query_.get() != nullptr;
|
|
|
|
|
|
|
|
if (check_dns_result) {
|
|
|
|
assert(addr_->dns);
|
|
|
|
}
|
2016-02-07 10:22:57 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
auto &connect_blocker = addr_->connect_blocker;
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (connect_blocker->blocked()) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Backend server " << addr_->host << ":"
|
|
|
|
<< addr_->port << " was not available temporarily";
|
2016-05-24 16:36:43 +02:00
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2016-02-21 06:53:06 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
Address *raddr;
|
2016-02-21 06:53:06 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (addr_->dns) {
|
|
|
|
if (!check_dns_result) {
|
|
|
|
auto dns_query = std::make_unique<DNSQuery>(
|
|
|
|
addr_->host,
|
|
|
|
[this](DNSResolverStatus status, const Address *result) {
|
|
|
|
int rv;
|
2016-02-21 06:53:06 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (status == DNSResolverStatus::OK) {
|
|
|
|
*this->resolved_addr_ = *result;
|
|
|
|
}
|
2016-02-21 06:53:06 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
rv = this->initiate_connection();
|
|
|
|
if (rv != 0) {
|
|
|
|
// This callback destroys |this|.
|
|
|
|
auto downstream = this->downstream_;
|
|
|
|
backend_retry(downstream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
auto dns_tracker = worker_->get_dns_tracker();
|
2016-12-04 15:43:41 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (!resolved_addr_) {
|
|
|
|
resolved_addr_ = std::make_unique<Address>();
|
|
|
|
}
|
|
|
|
switch (dns_tracker->resolve(resolved_addr_.get(), dns_query.get())) {
|
|
|
|
case DNSResolverStatus::ERROR:
|
|
|
|
downstream_failure(addr_, nullptr);
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
case DNSResolverStatus::RUNNING:
|
|
|
|
dns_query_ = std::move(dns_query);
|
|
|
|
return 0;
|
|
|
|
case DNSResolverStatus::OK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2016-12-04 15:43:41 +01:00
|
|
|
} else {
|
2019-01-19 03:12:05 +01:00
|
|
|
switch (dns_query_->status) {
|
|
|
|
case DNSResolverStatus::ERROR:
|
|
|
|
dns_query_.reset();
|
|
|
|
downstream_failure(addr_, nullptr);
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
case DNSResolverStatus::OK:
|
|
|
|
dns_query_.reset();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2016-12-04 15:43:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
raddr = resolved_addr_.get();
|
|
|
|
util::set_port(*resolved_addr_, addr_->port);
|
|
|
|
} else {
|
|
|
|
raddr = &addr_->addr;
|
|
|
|
}
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
conn_.fd = util::create_nonblock_socket(raddr->su.storage.ss_family);
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (conn_.fd == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
DCLOG(WARN, this) << "socket() failed; addr="
|
|
|
|
<< util::to_numeric_addr(raddr) << ", errno=" << error;
|
2016-02-21 07:27:19 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
worker_blocker->on_failure();
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2016-05-21 03:34:47 +02:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
worker_blocker->on_success();
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
rv = connect(conn_.fd, &raddr->su.sa, raddr->len);
|
|
|
|
if (rv != 0 && errno != EINPROGRESS) {
|
|
|
|
auto error = errno;
|
|
|
|
DCLOG(WARN, this) << "connect() failed; addr="
|
|
|
|
<< util::to_numeric_addr(raddr) << ", errno=" << error;
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
downstream_failure(addr_, raddr);
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Connecting to downstream server";
|
|
|
|
}
|
2015-02-03 17:15:56 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
raddr_ = raddr;
|
2016-05-24 16:36:43 +02:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
if (addr_->tls) {
|
|
|
|
assert(ssl_ctx_);
|
2016-05-24 16:36:43 +02:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
auto ssl = tls::create_ssl(ssl_ctx_);
|
|
|
|
if (!ssl) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-05-24 16:36:43 +02:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
tls::setup_downstream_http1_alpn(ssl);
|
2016-05-24 16:36:43 +02:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
conn_.set_ssl(ssl);
|
|
|
|
conn_.tls.client_session_cache = &addr_->tls_session_cache;
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
auto sni_name =
|
|
|
|
addr_->sni.empty() ? StringRef{addr_->host} : StringRef{addr_->sni};
|
|
|
|
if (!util::numeric_host(sni_name.c_str())) {
|
|
|
|
SSL_set_tlsext_host_name(conn_.tls.ssl, sni_name.c_str());
|
|
|
|
}
|
2016-02-06 15:07:00 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
auto session = tls::reuse_tls_session(addr_->tls_session_cache);
|
|
|
|
if (session) {
|
|
|
|
SSL_set_session(conn_.tls.ssl, session);
|
|
|
|
SSL_SESSION_free(session);
|
2016-02-06 11:50:21 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
conn_.prepare_client_handshake();
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
ev_io_set(&conn_.wev, conn_.fd, EV_WRITE);
|
|
|
|
ev_io_set(&conn_.rev, conn_.fd, EV_READ);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
conn_.wlimit.startw();
|
2015-07-26 18:18:52 +02:00
|
|
|
|
2016-10-10 15:50:41 +02:00
|
|
|
conn_.wt.repeat = downstreamconf.timeout.connect;
|
2015-07-26 18:18:52 +02:00
|
|
|
ev_timer_again(conn_.loop, &conn_.wt);
|
|
|
|
} else {
|
|
|
|
// we may set read timer cb to idle_timeoutcb. Reset again.
|
|
|
|
ev_set_cb(&conn_.rt, timeoutcb);
|
2018-09-30 05:23:34 +02:00
|
|
|
if (conn_.read_timeout < group_->shared_addr->timeout.read) {
|
|
|
|
conn_.read_timeout = group_->shared_addr->timeout.read;
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
|
|
|
} else {
|
2018-09-30 05:23:34 +02:00
|
|
|
conn_.again_rt(group_->shared_addr->timeout.read);
|
2016-11-25 16:00:32 +01:00
|
|
|
}
|
2016-06-23 17:04:39 +02:00
|
|
|
|
2019-09-06 15:01:03 +02:00
|
|
|
ev_set_cb(&conn_.rev, readcb);
|
2016-12-25 13:22:34 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::write_first;
|
|
|
|
first_write_done_ = false;
|
|
|
|
request_header_written_ = false;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-08-18 17:16:51 +02:00
|
|
|
|
2018-11-23 12:44:36 +01:00
|
|
|
llhttp_init(&response_htp_, HTTP_RESPONSE, &htp_hooks);
|
2013-09-23 17:13:40 +02:00
|
|
|
response_htp_.data = downstream_;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::push_request_headers() {
|
2019-01-19 03:12:05 +01:00
|
|
|
if (request_header_written_) {
|
2017-11-04 07:46:08 +01:00
|
|
|
signal_write();
|
2016-12-25 13:22:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-07 10:22:57 +01:00
|
|
|
const auto &downstream_hostport = addr_->hostport;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream_->request();
|
|
|
|
|
2016-03-12 11:07:48 +01:00
|
|
|
auto &balloc = downstream_->get_block_allocator();
|
|
|
|
|
2018-03-11 04:02:18 +01:00
|
|
|
auto connect_method = req.regular_connect_method();
|
2015-03-13 14:52:09 +01:00
|
|
|
|
2016-10-08 04:34:23 +02:00
|
|
|
auto config = get_config();
|
|
|
|
auto &httpconf = config->http;
|
2016-01-18 09:00:20 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
request_header_written_ = true;
|
2016-09-14 17:25:41 +02:00
|
|
|
|
2015-09-03 16:36:49 +02:00
|
|
|
// For HTTP/1.0 request, there is no authority in request. In that
|
|
|
|
// case, we use backend server's host nonetheless.
|
2016-01-17 03:33:45 +01:00
|
|
|
auto authority = StringRef(downstream_hostport);
|
2016-02-28 13:35:26 +01:00
|
|
|
auto no_host_rewrite =
|
2016-10-08 04:34:23 +02:00
|
|
|
httpconf.no_host_rewrite || config->http2_proxy || connect_method;
|
2015-02-03 17:15:56 +01:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
if (no_host_rewrite && !req.authority.empty()) {
|
2016-03-10 14:42:07 +01:00
|
|
|
authority = req.authority;
|
2015-02-03 17:15:56 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
downstream_->set_request_downstream_host(authority);
|
2015-02-08 11:41:45 +01:00
|
|
|
|
2015-09-11 16:07:00 +02:00
|
|
|
auto buf = downstream_->get_request_buf();
|
|
|
|
|
2013-09-11 16:24:32 +02:00
|
|
|
// Assume that method and request path do not contain \r\n.
|
2018-03-11 04:02:18 +01:00
|
|
|
auto meth = http2::to_method_string(
|
2018-10-17 02:50:29 +02:00
|
|
|
req.connect_proto == ConnectProto::WEBSOCKET ? HTTP_GET : req.method);
|
2016-02-28 13:35:26 +01:00
|
|
|
buf->append(meth);
|
2016-11-04 13:13:22 +01:00
|
|
|
buf->append(' ');
|
2015-06-16 14:31:33 +02:00
|
|
|
|
2015-05-26 15:26:17 +02:00
|
|
|
if (connect_method) {
|
2016-01-17 09:04:16 +01:00
|
|
|
buf->append(authority);
|
2016-10-08 04:34:23 +02:00
|
|
|
} else if (config->http2_proxy) {
|
2013-10-25 14:50:56 +02:00
|
|
|
// Construct absolute-form request target because we are going to
|
|
|
|
// send a request to a HTTP/1 proxy.
|
2016-01-13 14:45:52 +01:00
|
|
|
assert(!req.scheme.empty());
|
|
|
|
buf->append(req.scheme);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("://");
|
2016-01-17 09:04:16 +01:00
|
|
|
buf->append(authority);
|
2016-01-13 14:45:52 +01:00
|
|
|
buf->append(req.path);
|
|
|
|
} else if (req.method == HTTP_OPTIONS && req.path.empty()) {
|
2015-09-03 17:14:09 +02:00
|
|
|
// Server-wide OPTIONS
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("*");
|
2013-10-25 14:50:56 +02:00
|
|
|
} else {
|
2016-01-13 14:45:52 +01:00
|
|
|
buf->append(req.path);
|
2013-10-25 14:50:56 +02:00
|
|
|
}
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append(" HTTP/1.1\r\nHost: ");
|
2016-01-17 09:04:16 +01:00
|
|
|
buf->append(authority);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("\r\n");
|
2015-02-03 17:15:56 +01:00
|
|
|
|
2017-04-25 16:41:56 +02:00
|
|
|
auto &fwdconf = httpconf.forwarded;
|
|
|
|
auto &xffconf = httpconf.xff;
|
|
|
|
auto &xfpconf = httpconf.xfp;
|
2018-09-09 15:37:22 +02:00
|
|
|
auto &earlydataconf = httpconf.early_data;
|
2017-04-25 16:41:56 +02:00
|
|
|
|
|
|
|
uint32_t build_flags =
|
|
|
|
(fwdconf.strip_incoming ? http2::HDOP_STRIP_FORWARDED : 0) |
|
|
|
|
(xffconf.strip_incoming ? http2::HDOP_STRIP_X_FORWARDED_FOR : 0) |
|
2018-09-09 15:37:22 +02:00
|
|
|
(xfpconf.strip_incoming ? http2::HDOP_STRIP_X_FORWARDED_PROTO : 0) |
|
2018-03-11 04:02:18 +01:00
|
|
|
(earlydataconf.strip_incoming ? http2::HDOP_STRIP_EARLY_DATA : 0) |
|
2021-09-11 10:56:25 +02:00
|
|
|
((req.http_major == 3 || req.http_major == 2)
|
|
|
|
? http2::HDOP_STRIP_SEC_WEBSOCKET_KEY
|
|
|
|
: 0);
|
2017-04-25 16:41:56 +02:00
|
|
|
|
|
|
|
http2::build_http1_headers_from_headers(buf, req.fs.headers(), build_flags);
|
2013-08-27 17:09:46 +02:00
|
|
|
|
2016-01-13 16:44:10 +01:00
|
|
|
auto cookie = downstream_->assemble_request_cookie();
|
|
|
|
if (!cookie.empty()) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Cookie: ");
|
2016-01-13 16:44:10 +01:00
|
|
|
buf->append(cookie);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("\r\n");
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-28 09:44:04 +02:00
|
|
|
// set transfer-encoding only when content-length is unknown and
|
|
|
|
// request body is expected.
|
2018-03-11 04:02:18 +01:00
|
|
|
if (req.method != HTTP_CONNECT && req.http2_expect_body &&
|
|
|
|
req.fs.content_length == -1) {
|
2014-07-03 12:59:10 +02:00
|
|
|
downstream_->set_chunked_request(true);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Transfer-Encoding: chunked\r\n");
|
2014-07-03 12:59:10 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 02:50:29 +02:00
|
|
|
if (req.connect_proto == ConnectProto::WEBSOCKET) {
|
2021-09-11 10:56:25 +02:00
|
|
|
if (req.http_major == 3 || req.http_major == 2) {
|
2018-03-11 04:02:18 +01:00
|
|
|
std::array<uint8_t, 16> nonce;
|
2021-09-10 15:43:03 +02:00
|
|
|
if (RAND_bytes(nonce.data(), nonce.size()) != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-11 04:02:18 +01:00
|
|
|
auto iov = make_byte_ref(balloc, base64::encode_length(nonce.size()) + 1);
|
|
|
|
auto p = base64::encode(std::begin(nonce), std::end(nonce), iov.base);
|
|
|
|
*p = '\0';
|
|
|
|
auto key = StringRef{iov.base, p};
|
|
|
|
downstream_->set_ws_key(key);
|
|
|
|
|
|
|
|
buf->append("Sec-Websocket-Key: ");
|
|
|
|
buf->append(key);
|
|
|
|
buf->append("\r\n");
|
|
|
|
}
|
2015-05-26 15:26:17 +02:00
|
|
|
|
2018-03-11 04:02:18 +01:00
|
|
|
buf->append("Upgrade: websocket\r\nConnection: Upgrade\r\n");
|
|
|
|
} else if (!connect_method && req.upgrade_request) {
|
2016-01-13 14:45:52 +01:00
|
|
|
auto connection = req.fs.header(http2::HD_CONNECTION);
|
2015-05-26 15:26:17 +02:00
|
|
|
if (connection) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Connection: ");
|
|
|
|
buf->append((*connection).value);
|
|
|
|
buf->append("\r\n");
|
2015-05-26 15:26:17 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
auto upgrade = req.fs.header(http2::HD_UPGRADE);
|
2015-05-26 15:26:17 +02:00
|
|
|
if (upgrade) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Upgrade: ");
|
|
|
|
buf->append((*upgrade).value);
|
|
|
|
buf->append("\r\n");
|
2015-05-26 15:26:17 +02:00
|
|
|
}
|
2018-03-11 04:02:18 +01:00
|
|
|
} else if (req.connection_close) {
|
|
|
|
buf->append("Connection: close\r\n");
|
2015-05-26 15:26:17 +02:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:04:58 +01:00
|
|
|
auto upstream = downstream_->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
|
2017-11-26 02:56:39 +01:00
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
auto conn = handler->get_connection();
|
|
|
|
|
2018-09-15 05:16:23 +02:00
|
|
|
if (conn->tls.ssl && !SSL_is_init_finished(conn->tls.ssl)) {
|
2017-11-26 02:56:39 +01:00
|
|
|
buf->append("Early-Data: 1\r\n");
|
|
|
|
}
|
|
|
|
#endif // OPENSSL_1_1_1_API
|
|
|
|
|
2016-01-18 09:00:20 +01:00
|
|
|
auto fwd =
|
|
|
|
fwdconf.strip_incoming ? nullptr : req.fs.header(http2::HD_FORWARDED);
|
|
|
|
|
|
|
|
if (fwdconf.params) {
|
|
|
|
auto params = fwdconf.params;
|
2016-01-15 15:04:58 +01:00
|
|
|
|
2016-10-08 04:34:23 +02:00
|
|
|
if (config->http2_proxy || connect_method) {
|
2016-01-15 15:04:58 +01:00
|
|
|
params &= ~FORWARDED_PROTO;
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:07:48 +01:00
|
|
|
auto value = http::create_forwarded(
|
|
|
|
balloc, params, handler->get_forwarded_by(),
|
|
|
|
handler->get_forwarded_for(), req.authority, req.scheme);
|
|
|
|
|
2016-01-15 15:04:58 +01:00
|
|
|
if (fwd || !value.empty()) {
|
|
|
|
buf->append("Forwarded: ");
|
|
|
|
if (fwd) {
|
|
|
|
buf->append(fwd->value);
|
|
|
|
|
|
|
|
if (!value.empty()) {
|
|
|
|
buf->append(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf->append(value);
|
|
|
|
buf->append("\r\n");
|
|
|
|
}
|
|
|
|
} else if (fwd) {
|
|
|
|
buf->append("Forwarded: ");
|
|
|
|
buf->append(fwd->value);
|
|
|
|
buf->append("\r\n");
|
|
|
|
}
|
|
|
|
|
2016-01-18 09:00:20 +01:00
|
|
|
auto xff = xffconf.strip_incoming ? nullptr
|
|
|
|
: req.fs.header(http2::HD_X_FORWARDED_FOR);
|
2016-01-16 08:48:41 +01:00
|
|
|
|
2016-01-18 09:00:20 +01:00
|
|
|
if (xffconf.add) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("X-Forwarded-For: ");
|
2016-01-16 08:48:41 +01:00
|
|
|
if (xff) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append((*xff).value);
|
|
|
|
buf->append(", ");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append(client_handler_->get_ipaddr());
|
|
|
|
buf->append("\r\n");
|
2016-01-16 08:48:41 +01:00
|
|
|
} else if (xff) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("X-Forwarded-For: ");
|
|
|
|
buf->append((*xff).value);
|
|
|
|
buf->append("\r\n");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2016-10-08 04:34:23 +02:00
|
|
|
if (!config->http2_proxy && !connect_method) {
|
2017-04-05 15:50:18 +02:00
|
|
|
auto xfp = xfpconf.strip_incoming
|
|
|
|
? nullptr
|
|
|
|
: req.fs.header(http2::HD_X_FORWARDED_PROTO);
|
|
|
|
|
|
|
|
if (xfpconf.add) {
|
|
|
|
buf->append("X-Forwarded-Proto: ");
|
|
|
|
if (xfp) {
|
|
|
|
buf->append((*xfp).value);
|
|
|
|
buf->append(", ");
|
|
|
|
}
|
|
|
|
assert(!req.scheme.empty());
|
|
|
|
buf->append(req.scheme);
|
|
|
|
buf->append("\r\n");
|
|
|
|
} else if (xfp) {
|
|
|
|
buf->append("X-Forwarded-Proto: ");
|
|
|
|
buf->append((*xfp).value);
|
|
|
|
buf->append("\r\n");
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2016-01-13 14:45:52 +01:00
|
|
|
auto via = req.fs.header(http2::HD_VIA);
|
2016-01-18 09:00:20 +01:00
|
|
|
if (httpconf.no_via) {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (via) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Via: ");
|
|
|
|
buf->append((*via).value);
|
|
|
|
buf->append("\r\n");
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Via: ");
|
2015-01-04 15:22:39 +01:00
|
|
|
if (via) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append((*via).value);
|
|
|
|
buf->append(", ");
|
2013-01-09 14:01:25 +01:00
|
|
|
}
|
2016-03-12 10:36:05 +01:00
|
|
|
std::array<char, 16> viabuf;
|
|
|
|
auto end = http::create_via_header_value(viabuf.data(), req.http_major,
|
|
|
|
req.http_minor);
|
|
|
|
buf->append(viabuf.data(), end - viabuf.data());
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("\r\n");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2016-01-18 09:00:20 +01:00
|
|
|
for (auto &p : httpconf.add_request_headers) {
|
2016-02-13 14:19:05 +01:00
|
|
|
buf->append(p.name);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append(": ");
|
2016-02-13 14:19:05 +01:00
|
|
|
buf->append(p.value);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("\r\n");
|
2015-06-05 16:04:20 +02:00
|
|
|
}
|
|
|
|
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("\r\n");
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2012-12-09 13:36:02 +01:00
|
|
|
std::string nhdrs;
|
2015-09-11 16:07:00 +02:00
|
|
|
for (auto chunk = buf->head; chunk; chunk = chunk->next) {
|
|
|
|
nhdrs.append(chunk->pos, chunk->last);
|
|
|
|
}
|
2015-03-03 17:09:15 +01:00
|
|
|
if (log_config()->errorlog_tty) {
|
2015-09-11 16:07:00 +02:00
|
|
|
nhdrs = http::colorizeHeaders(nhdrs.c_str());
|
2012-12-09 13:36:02 +01:00
|
|
|
}
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "HTTP request headers. stream_id="
|
2016-10-15 11:36:04 +02:00
|
|
|
<< downstream_->get_stream_id() << "\n"
|
|
|
|
<< nhdrs;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-09-22 12:02:57 +02:00
|
|
|
// Don't call signal_write() if we anticipate request body. We call
|
|
|
|
// signal_write() when we received request body chunk, and it
|
|
|
|
// enables us to send headers and data in one writev system call.
|
2018-03-11 04:02:18 +01:00
|
|
|
if (req.method == HTTP_CONNECT ||
|
|
|
|
downstream_->get_blocked_request_buf()->rleft() ||
|
2019-08-06 13:48:50 +02:00
|
|
|
(!req.http2_expect_body && req.fs.content_length == 0) ||
|
|
|
|
downstream_->get_expect_100_continue()) {
|
2016-09-22 12:02:57 +02:00
|
|
|
signal_write();
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
return 0;
|
2018-08-22 15:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::process_blocked_request_buf() {
|
|
|
|
auto src = downstream_->get_blocked_request_buf();
|
|
|
|
|
|
|
|
if (src->rleft()) {
|
|
|
|
auto dest = downstream_->get_request_buf();
|
|
|
|
auto chunked = downstream_->get_chunked_request();
|
|
|
|
if (chunked) {
|
|
|
|
auto chunk_size_hex = util::utox(src->rleft());
|
|
|
|
dest->append(chunk_size_hex);
|
|
|
|
dest->append("\r\n");
|
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
src->copy(*dest);
|
2018-08-22 15:32:25 +02:00
|
|
|
|
|
|
|
if (chunked) {
|
|
|
|
dest->append("\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:43:03 +02:00
|
|
|
if (downstream_->get_blocked_request_data_eof() &&
|
|
|
|
downstream_->get_chunked_request()) {
|
|
|
|
end_upload_data_chunk();
|
2018-08-22 15:32:25 +02:00
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::push_upload_data_chunk(const uint8_t *data,
|
|
|
|
size_t datalen) {
|
2019-01-23 09:57:12 +01:00
|
|
|
if (!downstream_->get_request_header_sent()) {
|
|
|
|
auto output = downstream_->get_blocked_request_buf();
|
|
|
|
auto &req = downstream_->request();
|
|
|
|
output->append(data, datalen);
|
|
|
|
req.unconsumed_body_length += datalen;
|
|
|
|
if (request_header_written_) {
|
|
|
|
signal_write();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
auto chunked = downstream_->get_chunked_request();
|
|
|
|
auto output = downstream_->get_request_buf();
|
2014-05-14 15:39:28 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (chunked) {
|
2014-05-14 15:39:28 +02:00
|
|
|
auto chunk_size_hex = util::utox(datalen);
|
2016-01-17 09:04:16 +01:00
|
|
|
output->append(chunk_size_hex);
|
2015-01-22 15:54:30 +01:00
|
|
|
output->append("\r\n");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-05-14 15:39:28 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
output->append(data, datalen);
|
2014-05-14 15:39:28 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (chunked) {
|
2015-01-22 15:54:30 +01:00
|
|
|
output->append("\r\n");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-05-14 15:39:28 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
signal_write();
|
|
|
|
|
2014-05-14 15:39:28 +02:00
|
|
|
return 0;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::end_upload_data() {
|
2019-01-23 09:57:12 +01:00
|
|
|
if (!downstream_->get_request_header_sent()) {
|
|
|
|
downstream_->set_blocked_request_data_eof(true);
|
|
|
|
if (request_header_written_) {
|
|
|
|
signal_write();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-10 16:48:32 +01:00
|
|
|
signal_write();
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (!downstream_->get_chunked_request()) {
|
|
|
|
return 0;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2019-04-15 16:43:03 +02:00
|
|
|
end_upload_data_chunk();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpDownstreamConnection::end_upload_data_chunk() {
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream_->request();
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
auto output = downstream_->get_request_buf();
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &trailers = req.fs.trailers();
|
2015-03-08 09:58:00 +01:00
|
|
|
if (trailers.empty()) {
|
|
|
|
output->append("0\r\n\r\n");
|
|
|
|
} else {
|
|
|
|
output->append("0\r\n");
|
2017-04-25 16:41:56 +02:00
|
|
|
http2::build_http1_headers_from_headers(output, trailers,
|
|
|
|
http2::HDOP_STRIP_ALL);
|
2015-03-08 09:58:00 +01:00
|
|
|
output->append("\r\n");
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-09 16:17:41 +02:00
|
|
|
namespace {
|
|
|
|
void remove_from_pool(HttpDownstreamConnection *dconn) {
|
|
|
|
auto addr = dconn->get_addr();
|
|
|
|
auto &dconn_pool = addr->dconn_pool;
|
|
|
|
dconn_pool->remove_downstream_connection(dconn);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-10-13 14:09:00 +02:00
|
|
|
namespace {
|
2014-12-27 18:59:06 +01:00
|
|
|
void idle_readcb(struct ev_loop *loop, ev_io *w, int revents) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
2014-12-27 18:59:06 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, dconn) << "Idle connection EOF";
|
|
|
|
}
|
2016-06-09 16:17:41 +02:00
|
|
|
|
|
|
|
remove_from_pool(dconn);
|
2014-10-13 14:09:00 +02:00
|
|
|
// dconn was deleted
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
2014-12-27 18:59:06 +01:00
|
|
|
void idle_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(w->data);
|
|
|
|
auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
|
2016-11-25 16:00:32 +01:00
|
|
|
|
2018-02-02 13:09:04 +01:00
|
|
|
if (w == &conn->rt && !conn->expired_rt()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, dconn) << "Idle connection timeout";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2016-06-09 16:17:41 +02:00
|
|
|
|
|
|
|
remove_from_pool(dconn);
|
2014-08-18 17:16:51 +02:00
|
|
|
// dconn was deleted
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void HttpDownstreamConnection::detach_downstream(Downstream *downstream) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "Detaching from DOWNSTREAM:" << downstream;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream_ = nullptr;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ev_set_cb(&conn_.rev, idle_readcb);
|
2015-07-26 18:18:52 +02:00
|
|
|
ioctrl_.force_resume_read();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
auto &downstreamconf = *worker_->get_downstream_config();
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ev_set_cb(&conn_.rt, idle_timeoutcb);
|
2016-11-25 16:00:32 +01:00
|
|
|
if (conn_.read_timeout < downstreamconf.timeout.idle_read) {
|
|
|
|
conn_.read_timeout = downstreamconf.timeout.idle_read;
|
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
|
|
|
} else {
|
|
|
|
conn_.again_rt(downstreamconf.timeout.idle_read);
|
|
|
|
}
|
2015-07-26 18:18:52 +02:00
|
|
|
|
|
|
|
conn_.wlimit.stopw();
|
|
|
|
ev_timer_stop(conn_.loop, &conn_.wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void HttpDownstreamConnection::pause_read(IOCtrlReason reason) {
|
2012-11-20 17:29:39 +01:00
|
|
|
ioctrl_.pause_read(reason);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::resume_read(IOCtrlReason reason,
|
|
|
|
size_t consumed) {
|
2016-06-03 12:13:02 +02:00
|
|
|
auto &downstreamconf = *worker_->get_downstream_config();
|
|
|
|
|
2015-10-04 03:36:20 +02:00
|
|
|
if (downstream_->get_response_buf()->rleft() <=
|
2016-06-03 12:13:02 +02:00
|
|
|
downstreamconf.request_buffer_size / 2) {
|
2014-12-27 18:59:06 +01:00
|
|
|
ioctrl_.resume_read(reason);
|
|
|
|
}
|
|
|
|
|
2014-01-18 16:38:11 +01:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void HttpDownstreamConnection::force_resume_read() {
|
2012-11-20 17:29:39 +01:00
|
|
|
ioctrl_.force_resume_read();
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:57:07 +02:00
|
|
|
namespace {
|
2018-11-23 12:44:36 +01:00
|
|
|
int htp_msg_begincb(llhttp_t *htp) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2014-07-03 13:57:07 +02:00
|
|
|
|
2018-10-17 03:03:32 +02:00
|
|
|
if (downstream->get_response_state() != DownstreamState::INITIAL) {
|
2021-05-13 06:59:44 +02:00
|
|
|
return -1;
|
2014-07-03 13:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
2018-11-23 12:44:36 +01:00
|
|
|
int htp_hdrs_completecb(llhttp_t *htp) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2014-07-23 16:32:57 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
2017-01-11 11:27:08 +01:00
|
|
|
auto handler = upstream->get_client_handler();
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
2016-01-13 16:37:45 +01:00
|
|
|
auto &resp = downstream->response();
|
2014-07-23 16:32:57 +02:00
|
|
|
int rv;
|
|
|
|
|
2022-06-24 16:33:55 +02:00
|
|
|
auto &balloc = downstream->get_block_allocator();
|
|
|
|
|
|
|
|
for (auto &kv : resp.fs.headers()) {
|
|
|
|
kv.value = util::rstrip(balloc, kv.value);
|
|
|
|
}
|
|
|
|
|
2017-01-11 11:27:08 +01:00
|
|
|
auto config = get_config();
|
|
|
|
auto &loggingconf = config->logging;
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.http_status = htp->status_code;
|
|
|
|
resp.http_major = htp->http_major;
|
|
|
|
resp.http_minor = htp->http_minor;
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2017-02-11 10:42:29 +01:00
|
|
|
if (resp.http_major > 1 || req.http_minor > 1) {
|
2016-04-27 16:00:36 +02:00
|
|
|
resp.http_major = 1;
|
|
|
|
resp.http_minor = 1;
|
2017-02-11 10:42:29 +01:00
|
|
|
return -1;
|
2016-04-27 16:00:36 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 17:04:47 +02:00
|
|
|
auto dconn = downstream->get_downstream_connection();
|
|
|
|
|
|
|
|
downstream->set_downstream_addr_group(dconn->get_downstream_addr_group());
|
|
|
|
downstream->set_addr(dconn->get_addr());
|
|
|
|
|
2016-11-03 09:00:05 +01:00
|
|
|
// Server MUST NOT send Transfer-Encoding with a status code 1xx or
|
|
|
|
// 204. Also server MUST NOT send Transfer-Encoding with a status
|
2018-10-04 05:04:15 +02:00
|
|
|
// code 2xx to a CONNECT request. Same holds true with
|
2016-11-03 09:00:05 +01:00
|
|
|
// Content-Length.
|
2016-12-03 06:57:48 +01:00
|
|
|
if (resp.http_status == 204) {
|
|
|
|
if (resp.fs.header(http2::HD_TRANSFER_ENCODING)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Some server send content-length: 0 for 204. Until they get
|
|
|
|
// fixed, we accept, but ignore it.
|
|
|
|
|
|
|
|
// Calling parse_content_length() detects duplicated
|
|
|
|
// content-length header fields.
|
|
|
|
if (resp.fs.parse_content_length() != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (resp.fs.content_length == 0) {
|
2019-05-17 15:04:46 +02:00
|
|
|
resp.fs.erase_content_length_and_transfer_encoding();
|
2017-04-07 14:45:13 +02:00
|
|
|
} else if (resp.fs.content_length != -1) {
|
|
|
|
return -1;
|
2016-12-03 06:57:48 +01:00
|
|
|
}
|
|
|
|
} else if (resp.http_status / 100 == 1 ||
|
2018-10-04 05:04:15 +02:00
|
|
|
(resp.http_status / 100 == 2 && req.method == HTTP_CONNECT)) {
|
2019-05-17 15:04:46 +02:00
|
|
|
// Server MUST NOT send Content-Length and Transfer-Encoding in
|
|
|
|
// these responses.
|
|
|
|
resp.fs.erase_content_length_and_transfer_encoding();
|
2016-12-03 06:57:48 +01:00
|
|
|
} else if (resp.fs.parse_content_length() != 0) {
|
2018-10-17 03:03:32 +02:00
|
|
|
downstream->set_response_state(DownstreamState::MSG_BAD_HEADER);
|
2015-01-20 15:11:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
|
2015-05-26 15:26:17 +02:00
|
|
|
// Check upgrade before processing non-final response, since if
|
|
|
|
// upgrade succeeded, 101 response is treated as final in nghttpx.
|
2018-03-11 04:02:18 +01:00
|
|
|
downstream->check_upgrade_fulfilled_http1();
|
2015-05-26 15:26:17 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_non_final_response()) {
|
2015-04-10 15:30:20 +02:00
|
|
|
// Reset content-length because we reuse same Downstream for the
|
|
|
|
// next response.
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.fs.content_length = -1;
|
2014-07-23 16:32:57 +02:00
|
|
|
// For non-final response code, we just call
|
|
|
|
// on_downstream_header_complete() without changing response
|
|
|
|
// state.
|
|
|
|
rv = upstream->on_downstream_header_complete(downstream);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-07-23 16:32:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:26:17 +02:00
|
|
|
// Ignore response body for non-final response.
|
|
|
|
return 1;
|
2014-07-23 16:32:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-23 12:44:36 +01:00
|
|
|
resp.connection_close = !llhttp_should_keep_alive(htp);
|
2018-10-17 03:03:32 +02:00
|
|
|
downstream->set_response_state(DownstreamState::HEADER_COMPLETE);
|
2014-06-15 09:14:00 +02:00
|
|
|
downstream->inspect_http1_response();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_upgraded()) {
|
2015-04-10 15:30:20 +02:00
|
|
|
// content-length must be ignored for upgraded connection.
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.fs.content_length = -1;
|
|
|
|
resp.connection_close = true;
|
2015-04-10 15:30:20 +02:00
|
|
|
// transfer-encoding not applied to upgraded connection
|
|
|
|
downstream->set_chunked_response(false);
|
2018-10-14 15:57:54 +02:00
|
|
|
} else if (http2::legacy_http1(req.http_major, req.http_minor)) {
|
|
|
|
if (resp.fs.content_length == -1) {
|
|
|
|
resp.connection_close = true;
|
|
|
|
}
|
|
|
|
downstream->set_chunked_response(false);
|
2016-05-14 10:47:58 +02:00
|
|
|
} else if (!downstream->expect_response_body()) {
|
|
|
|
downstream->set_chunked_response(false);
|
2013-02-11 09:20:52 +01:00
|
|
|
}
|
2016-05-14 10:47:58 +02:00
|
|
|
|
2017-01-11 11:27:08 +01:00
|
|
|
if (loggingconf.access.write_early && downstream->accesslog_ready()) {
|
|
|
|
handler->write_accesslog(downstream);
|
|
|
|
downstream->set_accesslog_written(true);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->on_downstream_header_complete(downstream) != 0) {
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_upgraded()) {
|
2013-07-31 14:48:37 +02:00
|
|
|
// Upgrade complete, read until EOF in both ends
|
2015-02-13 14:41:50 +01:00
|
|
|
if (upstream->resume_read(SHRPX_NO_BUFFER, downstream, 0) != 0) {
|
2013-07-31 15:14:25 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-10-17 03:03:32 +02:00
|
|
|
downstream->set_request_state(DownstreamState::HEADER_COMPLETE);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-31 14:48:37 +02:00
|
|
|
LOG(INFO) << "HTTP upgrade success. stream_id="
|
|
|
|
<< downstream->get_stream_id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
// Ignore the response body. HEAD response may contain
|
|
|
|
// Content-Length or Transfer-Encoding: chunked. Some server send
|
|
|
|
// 304 status code with nonzero Content-Length, but without response
|
|
|
|
// body. See
|
2015-04-28 16:05:00 +02:00
|
|
|
// https://tools.ietf.org/html/rfc7230#section-3.3
|
2014-07-23 16:32:57 +02:00
|
|
|
|
|
|
|
// TODO It seems that the cases other than HEAD are handled by
|
2018-11-23 12:44:36 +01:00
|
|
|
// llhttp. Need test.
|
2016-05-15 14:05:20 +02:00
|
|
|
return !http2::expect_response_body(req.method, resp.http_status);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-02-06 04:25:34 +01:00
|
|
|
namespace {
|
|
|
|
int ensure_header_field_buffer(const Downstream *downstream,
|
|
|
|
const HttpConfig &httpconf, size_t len) {
|
|
|
|
auto &resp = downstream->response();
|
|
|
|
|
|
|
|
if (resp.fs.buffer_size() + len > httpconf.response_header_field_buffer) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, downstream) << "Too large header header field size="
|
|
|
|
<< resp.fs.buffer_size() + len;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int ensure_max_header_fields(const Downstream *downstream,
|
|
|
|
const HttpConfig &httpconf) {
|
|
|
|
auto &resp = downstream->response();
|
|
|
|
|
|
|
|
if (resp.fs.num_fields() >= httpconf.max_response_header_fields) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2017-04-30 08:45:53 +02:00
|
|
|
DLOG(INFO, downstream)
|
|
|
|
<< "Too many header field num=" << resp.fs.num_fields() + 1;
|
2016-02-06 04:25:34 +01:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
2018-11-23 12:44:36 +01:00
|
|
|
int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2016-01-13 16:37:45 +01:00
|
|
|
auto &resp = downstream->response();
|
2016-02-06 04:25:34 +01:00
|
|
|
auto &httpconf = get_config()->http;
|
|
|
|
|
|
|
|
if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-13 16:37:45 +01:00
|
|
|
|
2018-10-17 03:03:32 +02:00
|
|
|
if (downstream->get_response_state() == DownstreamState::INITIAL) {
|
2016-01-13 16:37:45 +01:00
|
|
|
if (resp.fs.header_key_prev()) {
|
|
|
|
resp.fs.append_last_header_key(data, len);
|
2015-03-08 09:19:52 +01:00
|
|
|
} else {
|
2016-02-06 04:25:34 +01:00
|
|
|
if (ensure_max_header_fields(downstream, httpconf) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-18 14:33:29 +01:00
|
|
|
resp.fs.alloc_add_header_name(StringRef{data, len});
|
2015-03-08 09:19:52 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
} else {
|
2015-03-08 09:19:52 +01:00
|
|
|
// trailer part
|
2016-01-13 16:37:45 +01:00
|
|
|
if (resp.fs.trailer_key_prev()) {
|
|
|
|
resp.fs.append_last_trailer_key(data, len);
|
2015-03-08 09:19:52 +01:00
|
|
|
} else {
|
2016-02-06 04:25:34 +01:00
|
|
|
if (ensure_max_header_fields(downstream, httpconf) != 0) {
|
|
|
|
// Could not ignore this trailer field easily, since we may
|
|
|
|
// get its value in htp_hdr_valcb, and it will be added to
|
|
|
|
// wrong place or crash if trailer fields are currently empty.
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-18 14:33:29 +01:00
|
|
|
resp.fs.alloc_add_trailer_name(StringRef{data, len});
|
2015-03-08 09:19:52 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2018-11-23 12:44:36 +01:00
|
|
|
int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2016-01-13 16:37:45 +01:00
|
|
|
auto &resp = downstream->response();
|
2016-02-06 04:25:34 +01:00
|
|
|
auto &httpconf = get_config()->http;
|
|
|
|
|
|
|
|
if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-13 16:37:45 +01:00
|
|
|
|
2018-10-17 03:03:32 +02:00
|
|
|
if (downstream->get_response_state() == DownstreamState::INITIAL) {
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.fs.append_last_header_value(data, len);
|
2012-11-20 17:29:39 +01:00
|
|
|
} else {
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.fs.append_last_trailer_value(data, len);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2018-11-23 12:44:36 +01:00
|
|
|
int htp_bodycb(llhttp_t *htp, const char *data, size_t len) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2016-01-14 15:20:44 +01:00
|
|
|
auto &resp = downstream->response();
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2016-01-14 15:20:44 +01:00
|
|
|
resp.recv_body_length += len;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
return downstream->get_upstream()->on_downstream_body(
|
|
|
|
downstream, reinterpret_cast<const uint8_t *>(data), len, true);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2018-11-23 12:44:36 +01:00
|
|
|
int htp_msg_completecb(llhttp_t *htp) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2022-06-24 16:33:55 +02:00
|
|
|
auto &resp = downstream->response();
|
|
|
|
auto &balloc = downstream->get_block_allocator();
|
|
|
|
|
|
|
|
for (auto &kv : resp.fs.trailers()) {
|
|
|
|
kv.value = util::rstrip(balloc, kv.value);
|
|
|
|
}
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2018-11-23 12:44:36 +01:00
|
|
|
// llhttp does not treat "200 connection established" response
|
2015-05-26 15:26:17 +02:00
|
|
|
// against CONNECT request, and in that case, this function is not
|
|
|
|
// called. But if HTTP Upgrade is made (e.g., WebSocket), this
|
2018-11-23 12:44:36 +01:00
|
|
|
// function is called, and llhttp_execute() returns just after that.
|
2015-05-26 15:26:17 +02:00
|
|
|
if (downstream->get_upgraded()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_non_final_response()) {
|
2014-07-23 16:32:57 +02:00
|
|
|
downstream->reset_response();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:03:32 +02:00
|
|
|
downstream->set_response_state(DownstreamState::MSG_COMPLETE);
|
2014-01-18 10:39:25 +01:00
|
|
|
// Block reading another response message from (broken?)
|
|
|
|
// server. This callback is not called if the connection is
|
|
|
|
// tunneled.
|
|
|
|
downstream->pause_read(SHRPX_MSG_BLOCK);
|
2012-11-20 17:29:39 +01:00
|
|
|
return downstream->get_upstream()->on_downstream_body_complete(downstream);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
int HttpDownstreamConnection::write_first() {
|
2016-12-25 13:22:34 +01:00
|
|
|
int rv;
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
process_blocked_request_buf();
|
|
|
|
|
2016-12-25 13:22:34 +01:00
|
|
|
if (conn_.tls.ssl) {
|
|
|
|
rv = write_tls();
|
|
|
|
} else {
|
|
|
|
rv = write_clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return SHRPX_ERR_RETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn_.tls.ssl) {
|
2017-01-08 15:09:00 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::write_tls;
|
2016-12-25 13:22:34 +01:00
|
|
|
} else {
|
2017-01-08 15:09:00 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::write_clear;
|
2016-12-25 13:22:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
first_write_done_ = true;
|
|
|
|
downstream_->set_request_header_sent(true);
|
|
|
|
|
|
|
|
auto buf = downstream_->get_blocked_request_buf();
|
|
|
|
buf->reset();
|
2016-12-25 13:22:34 +01:00
|
|
|
|
2019-08-06 13:48:50 +02:00
|
|
|
// upstream->resume_read() might be called in
|
|
|
|
// write_tls()/write_clear(), but before blocked_request_buf_ is
|
|
|
|
// reset. So upstream read might still be blocked. Let's do it
|
|
|
|
// again here.
|
|
|
|
auto input = downstream_->get_request_buf();
|
|
|
|
if (input->rleft() == 0) {
|
|
|
|
auto upstream = downstream_->get_upstream();
|
|
|
|
auto &req = downstream_->request();
|
|
|
|
|
|
|
|
upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
|
|
|
|
req.unconsumed_body_length);
|
|
|
|
}
|
|
|
|
|
2016-12-25 13:22:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
int HttpDownstreamConnection::read_clear() {
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
|
|
|
|
2016-02-07 13:07:27 +01:00
|
|
|
std::array<uint8_t, 16_k> buf;
|
2016-02-06 17:07:44 +01:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto nread = conn_.read_clear(buf.data(), buf.size());
|
|
|
|
if (nread == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread < 0) {
|
2021-09-16 15:00:36 +02:00
|
|
|
if (nread == SHRPX_ERR_EOF && !downstream_->get_upgraded()) {
|
|
|
|
auto htperr = llhttp_finish(&response_htp_);
|
|
|
|
if (htperr != HPE_OK) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "HTTP response ended prematurely: "
|
|
|
|
<< llhttp_errno_name(htperr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = process_input(buf.data(), nread);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
if (!ev_is_active(&conn_.rev)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 11:50:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
int HttpDownstreamConnection::write_clear() {
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
auto upstream = downstream_->get_upstream();
|
|
|
|
auto input = downstream_->get_request_buf();
|
|
|
|
|
|
|
|
std::array<struct iovec, MAX_WR_IOVCNT> iov;
|
|
|
|
|
|
|
|
while (input->rleft() > 0) {
|
|
|
|
auto iovcnt = input->riovec(iov.data(), iov.size());
|
|
|
|
|
|
|
|
auto nwrite = conn_.writev_clear(iov.data(), iovcnt);
|
|
|
|
|
|
|
|
if (nwrite == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nwrite < 0) {
|
2019-01-19 03:12:05 +01:00
|
|
|
if (!first_write_done_) {
|
2016-12-25 13:22:34 +01:00
|
|
|
return nwrite;
|
|
|
|
}
|
2016-12-24 14:50:02 +01:00
|
|
|
// We may have pending data in receive buffer which may contain
|
|
|
|
// part of response body. So keep reading. Invoke read event
|
|
|
|
// to get read(2) error just in case.
|
|
|
|
ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
|
2017-01-08 15:09:00 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::noop;
|
2016-12-25 15:03:57 +01:00
|
|
|
reusable_ = false;
|
2016-12-24 14:50:02 +01:00
|
|
|
break;
|
2016-02-06 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
input->drain(nwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_.wlimit.stopw();
|
|
|
|
ev_timer_stop(conn_.loop, &conn_.wt);
|
|
|
|
|
|
|
|
if (input->rleft() == 0) {
|
|
|
|
auto &req = downstream_->request();
|
|
|
|
|
|
|
|
upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
|
|
|
|
req.unconsumed_body_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::tls_handshake() {
|
2016-02-06 11:50:21 +01:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
2016-02-06 17:07:44 +01:00
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
auto rv = conn_.tls_handshake();
|
|
|
|
if (rv == SHRPX_ERR_INPROGRESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv < 0) {
|
2016-12-04 15:43:41 +01:00
|
|
|
downstream_failure(addr_, raddr_);
|
2016-06-16 17:53:38 +02:00
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "SSL/TLS handshake completed";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!get_config()->tls.insecure &&
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::check_cert(conn_.tls.ssl, addr_, raddr_) != 0) {
|
2016-12-04 15:43:41 +01:00
|
|
|
downstream_failure(addr_, raddr_);
|
2016-06-16 17:53:38 +02:00
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:24:30 +02:00
|
|
|
auto &connect_blocker = addr_->connect_blocker;
|
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
signal_write_ = &HttpDownstreamConnection::actual_signal_write;
|
2016-08-23 14:36:43 +02:00
|
|
|
|
2016-05-24 14:24:30 +02:00
|
|
|
connect_blocker->on_success();
|
|
|
|
|
2016-05-28 15:41:24 +02:00
|
|
|
ev_set_cb(&conn_.rt, timeoutcb);
|
|
|
|
ev_set_cb(&conn_.wt, timeoutcb);
|
2016-05-24 14:24:30 +02:00
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
on_read_ = &HttpDownstreamConnection::read_tls;
|
2019-01-19 03:12:05 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::write_first;
|
2016-02-06 11:50:21 +01:00
|
|
|
|
|
|
|
// TODO Check negotiated ALPN
|
|
|
|
|
|
|
|
return on_write();
|
|
|
|
}
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
int HttpDownstreamConnection::read_tls() {
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
ERR_clear_error();
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2016-02-07 13:07:27 +01:00
|
|
|
std::array<uint8_t, 16_k> buf;
|
2014-12-27 18:59:06 +01:00
|
|
|
int rv;
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2016-02-06 17:07:44 +01:00
|
|
|
auto nread = conn_.read_tls(buf.data(), buf.size());
|
2014-12-27 18:59:06 +01:00
|
|
|
if (nread == 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread < 0) {
|
2021-09-16 15:00:36 +02:00
|
|
|
if (nread == SHRPX_ERR_EOF && !downstream_->get_upgraded()) {
|
|
|
|
auto htperr = llhttp_finish(&response_htp_);
|
|
|
|
if (htperr != HPE_OK) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "HTTP response ended prematurely: "
|
|
|
|
<< llhttp_errno_name(htperr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
return nread;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
rv = process_input(buf.data(), nread);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
2015-05-26 15:26:17 +02:00
|
|
|
}
|
2016-01-27 07:26:46 +01:00
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
if (!ev_is_active(&conn_.rev)) {
|
2016-01-27 07:26:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
int HttpDownstreamConnection::write_tls() {
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.last_read = ev_now(conn_.loop);
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
ERR_clear_error();
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2014-08-21 14:22:16 +02:00
|
|
|
auto upstream = downstream_->get_upstream();
|
2014-12-27 18:59:06 +01:00
|
|
|
auto input = downstream_->get_request_buf();
|
|
|
|
|
2016-02-07 08:52:40 +01:00
|
|
|
struct iovec iov;
|
2015-01-10 15:04:54 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
while (input->rleft() > 0) {
|
2016-02-07 08:52:40 +01:00
|
|
|
auto iovcnt = input->riovec(&iov, 1);
|
2017-05-18 16:10:44 +02:00
|
|
|
if (iovcnt != 1) {
|
|
|
|
assert(0);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-07 08:52:40 +01:00
|
|
|
auto nwrite = conn_.write_tls(iov.iov_base, iov.iov_len);
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
if (nwrite == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nwrite < 0) {
|
2019-01-19 03:12:05 +01:00
|
|
|
if (!first_write_done_) {
|
2016-12-25 13:22:34 +01:00
|
|
|
return nwrite;
|
|
|
|
}
|
2016-12-24 14:50:02 +01:00
|
|
|
// We may have pending data in receive buffer which may contain
|
|
|
|
// part of response body. So keep reading. Invoke read event
|
|
|
|
// to get read(2) error just in case.
|
|
|
|
ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
|
2017-01-08 15:09:00 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::noop;
|
2016-12-25 15:03:57 +01:00
|
|
|
reusable_ = false;
|
2016-12-24 14:50:02 +01:00
|
|
|
break;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2015-02-04 13:15:58 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
input->drain(nwrite);
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
conn_.wlimit.stopw();
|
|
|
|
ev_timer_stop(conn_.loop, &conn_.wt);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
if (input->rleft() == 0) {
|
2016-01-14 15:36:47 +01:00
|
|
|
auto &req = downstream_->request();
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
|
2016-01-14 15:36:47 +01:00
|
|
|
req.unconsumed_body_length);
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-06 17:07:44 +01:00
|
|
|
int HttpDownstreamConnection::process_input(const uint8_t *data,
|
|
|
|
size_t datalen) {
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (downstream_->get_upgraded()) {
|
|
|
|
// For upgraded connection, just pass data to the upstream.
|
|
|
|
rv = downstream_->get_upstream()->on_downstream_body(downstream_, data,
|
|
|
|
datalen, true);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downstream_->response_buf_full()) {
|
|
|
|
downstream_->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-07 09:59:38 +01:00
|
|
|
|
|
|
|
return 0;
|
2016-02-06 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
2018-11-23 12:44:36 +01:00
|
|
|
auto htperr = llhttp_execute(&response_htp_,
|
|
|
|
reinterpret_cast<const char *>(data), datalen);
|
2019-04-20 17:07:11 +02:00
|
|
|
auto nproc =
|
|
|
|
htperr == HPE_OK
|
|
|
|
? datalen
|
|
|
|
: static_cast<size_t>(reinterpret_cast<const uint8_t *>(
|
|
|
|
llhttp_get_error_pos(&response_htp_)) -
|
|
|
|
data);
|
2016-02-06 17:07:44 +01:00
|
|
|
|
2018-11-23 12:44:36 +01:00
|
|
|
if (htperr != HPE_OK &&
|
|
|
|
(!downstream_->get_upgraded() || htperr != HPE_PAUSED_UPGRADE)) {
|
2016-02-07 08:52:40 +01:00
|
|
|
// Handling early return (in other words, response was hijacked by
|
|
|
|
// mruby scripting).
|
2018-10-17 03:03:32 +02:00
|
|
|
if (downstream_->get_response_state() == DownstreamState::MSG_COMPLETE) {
|
2016-02-06 17:07:44 +01:00
|
|
|
return SHRPX_ERR_DCONN_CANCELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "HTTP parser failure: "
|
2018-11-23 12:44:36 +01:00
|
|
|
<< "(" << llhttp_errno_name(htperr) << ") "
|
|
|
|
<< llhttp_get_error_reason(&response_htp_);
|
2016-02-06 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downstream_->get_upgraded()) {
|
|
|
|
if (nproc < datalen) {
|
|
|
|
// Data from data + nproc are for upgraded protocol.
|
|
|
|
rv = downstream_->get_upstream()->on_downstream_body(
|
|
|
|
downstream_, data + nproc, datalen - nproc, true);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downstream_->response_buf_full()) {
|
|
|
|
downstream_->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downstream_->response_buf_full()) {
|
|
|
|
downstream_->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
int HttpDownstreamConnection::connected() {
|
2016-02-27 15:24:14 +01:00
|
|
|
auto &connect_blocker = addr_->connect_blocker;
|
2015-01-23 15:00:18 +01:00
|
|
|
|
2016-08-24 17:25:03 +02:00
|
|
|
auto sock_error = util::get_socket_error(conn_.fd);
|
|
|
|
if (sock_error != 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
conn_.wlimit.stopw();
|
2015-01-22 13:13:34 +01:00
|
|
|
|
2016-08-19 09:25:05 +02:00
|
|
|
DCLOG(WARN, this) << "Backend connect failed; addr="
|
2016-12-04 15:43:41 +01:00
|
|
|
<< util::to_numeric_addr(raddr_)
|
2016-08-24 17:25:03 +02:00
|
|
|
<< ": errno=" << sock_error;
|
2015-03-10 15:21:48 +01:00
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
downstream_failure(addr_, raddr_);
|
2016-02-21 06:53:06 +01:00
|
|
|
|
2015-01-05 08:14:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-02-21 08:11:50 +01:00
|
|
|
DCLOG(INFO, this) << "Connected to downstream host";
|
2016-02-06 11:50:21 +01:00
|
|
|
}
|
|
|
|
|
2016-10-10 15:50:41 +02:00
|
|
|
// Reset timeout for write. Previously, we set timeout for connect.
|
2018-09-30 05:23:34 +02:00
|
|
|
conn_.wt.repeat = group_->shared_addr->timeout.write;
|
2016-10-10 15:50:41 +02:00
|
|
|
ev_timer_again(conn_.loop, &conn_.wt);
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
conn_.rlimit.startw();
|
2016-11-25 16:00:32 +01:00
|
|
|
conn_.again_rt();
|
2015-07-26 18:18:52 +02:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ev_set_cb(&conn_.wev, writecb);
|
2015-01-05 08:14:10 +01:00
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
if (conn_.tls.ssl) {
|
2017-01-08 15:09:00 +01:00
|
|
|
on_read_ = &HttpDownstreamConnection::tls_handshake;
|
|
|
|
on_write_ = &HttpDownstreamConnection::tls_handshake;
|
2016-02-06 11:50:21 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
signal_write_ = &HttpDownstreamConnection::actual_signal_write;
|
2016-08-23 14:36:43 +02:00
|
|
|
|
2016-05-24 14:24:30 +02:00
|
|
|
connect_blocker->on_success();
|
|
|
|
|
2016-05-28 15:41:24 +02:00
|
|
|
ev_set_cb(&conn_.rt, timeoutcb);
|
|
|
|
ev_set_cb(&conn_.wt, timeoutcb);
|
2016-05-24 14:24:30 +02:00
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
on_read_ = &HttpDownstreamConnection::read_clear;
|
2019-01-19 03:12:05 +01:00
|
|
|
on_write_ = &HttpDownstreamConnection::write_first;
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2015-01-05 08:14:10 +01:00
|
|
|
return 0;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
int HttpDownstreamConnection::on_read() { return on_read_(*this); }
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
int HttpDownstreamConnection::on_write() { return on_write_(*this); }
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
void HttpDownstreamConnection::on_upstream_change(Upstream *upstream) {}
|
|
|
|
|
2017-01-08 15:09:00 +01:00
|
|
|
void HttpDownstreamConnection::signal_write() { signal_write_(*this); }
|
2016-04-25 05:49:38 +02:00
|
|
|
|
|
|
|
int HttpDownstreamConnection::actual_signal_write() {
|
2015-10-28 15:26:41 +01:00
|
|
|
ev_feed_event(conn_.loop, &conn_.wev, EV_WRITE);
|
2016-04-25 05:49:38 +02:00
|
|
|
return 0;
|
2015-10-28 15:26:41 +01:00
|
|
|
}
|
2014-09-18 16:19:28 +02:00
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
int HttpDownstreamConnection::noop() { return 0; }
|
|
|
|
|
2016-08-04 17:04:47 +02:00
|
|
|
const std::shared_ptr<DownstreamAddrGroup> &
|
2016-02-27 15:24:14 +01:00
|
|
|
HttpDownstreamConnection::get_downstream_addr_group() const {
|
2016-08-04 17:04:47 +02:00
|
|
|
return group_;
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:24:30 +02:00
|
|
|
DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; }
|
|
|
|
|
2016-12-25 15:03:57 +01:00
|
|
|
bool HttpDownstreamConnection::poolable() const {
|
|
|
|
return !group_->retired && reusable_;
|
|
|
|
}
|
2016-06-04 05:16:31 +02:00
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
const Address *HttpDownstreamConnection::get_raddr() const { return raddr_; }
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
} // namespace shrpx
|