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"
|
|
|
|
|
|
|
|
#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"
|
2013-08-27 19:47:22 +02:00
|
|
|
#include "http2.h"
|
2012-11-18 13:23:13 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void 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
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
|
|
|
|
if (upstream->downstream_read(dconn) != 0) {
|
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void writecb(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
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
|
|
|
|
if (upstream->downstream_write(dconn) != 0) {
|
|
|
|
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();
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
if (dconn->on_connect() != 0) {
|
2015-01-21 15:08:13 +01:00
|
|
|
if (upstream->on_downstream_abort_request(downstream, 503) != 0) {
|
2015-01-20 15:26:09 +01:00
|
|
|
delete handler;
|
|
|
|
}
|
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
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
HttpDownstreamConnection::HttpDownstreamConnection(
|
2015-07-09 19:52:11 +02:00
|
|
|
DownstreamConnectionPool *dconn_pool, size_t group, struct ev_loop *loop)
|
2015-02-04 13:15:58 +01:00
|
|
|
: DownstreamConnection(dconn_pool),
|
2015-08-12 17:04:41 +02:00
|
|
|
conn_(loop, -1, nullptr, nullptr, get_config()->downstream_write_timeout,
|
2015-02-04 13:15:58 +01:00
|
|
|
get_config()->downstream_read_timeout, 0, 0, 0, 0, connectcb,
|
2015-10-25 09:03:53 +01:00
|
|
|
readcb, timeoutcb, this, get_config()->tls_dyn_rec_warmup_threshold,
|
2015-10-21 12:22:46 +02:00
|
|
|
get_config()->tls_dyn_rec_idle_timeout),
|
2015-07-09 19:52:11 +02:00
|
|
|
ioctrl_(&conn_.rlimit), response_htp_{0}, group_(group), addr_idx_(0),
|
2015-03-28 17:47:22 +01:00
|
|
|
connected_(false) {}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2015-07-15 16:31:32 +02:00
|
|
|
HttpDownstreamConnection::~HttpDownstreamConnection() {}
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::attach_downstream(Downstream *downstream) {
|
|
|
|
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
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
if (conn_.fd == -1) {
|
2015-03-10 15:11:22 +01:00
|
|
|
auto connect_blocker = client_handler_->get_connect_blocker();
|
2014-08-19 16:36:04 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (connect_blocker->blocked()) {
|
2015-01-22 16:15:38 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this)
|
|
|
|
<< "Downstream connection was blocked by connect_blocker";
|
|
|
|
}
|
2014-08-19 16:36:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
auto worker = client_handler_->get_worker();
|
2015-07-12 15:16:20 +02:00
|
|
|
auto &next_downstream = worker->get_dgrp(group_)->next;
|
2015-07-09 19:52:11 +02:00
|
|
|
auto end = next_downstream;
|
|
|
|
auto &addrs = get_config()->downstream_addr_groups[group_].addrs;
|
2014-12-06 10:31:46 +01:00
|
|
|
for (;;) {
|
2015-07-09 19:52:11 +02:00
|
|
|
auto &addr = addrs[next_downstream];
|
|
|
|
auto i = next_downstream;
|
|
|
|
if (++next_downstream >= addrs.size()) {
|
|
|
|
next_downstream = 0;
|
|
|
|
}
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2015-07-26 18:41:10 +02:00
|
|
|
conn_.fd = util::create_nonblock_socket(addr.addr.su.storage.ss_family);
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
if (conn_.fd == -1) {
|
2014-12-06 10:31:46 +01:00
|
|
|
auto error = errno;
|
|
|
|
DCLOG(WARN, this) << "socket() failed; errno=" << error;
|
|
|
|
|
|
|
|
connect_blocker->on_failure();
|
|
|
|
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int rv;
|
2015-07-26 18:41:10 +02:00
|
|
|
rv = connect(conn_.fd, &addr.addr.su.sa, addr.addr.len);
|
2014-12-27 18:59:06 +01:00
|
|
|
if (rv != 0 && errno != EINPROGRESS) {
|
2014-12-06 10:31:46 +01:00
|
|
|
auto error = errno;
|
2014-12-27 18:59:06 +01:00
|
|
|
DCLOG(WARN, this) << "connect() failed; errno=" << error;
|
2014-12-06 10:31:46 +01:00
|
|
|
|
|
|
|
connect_blocker->on_failure();
|
2015-02-04 13:15:58 +01:00
|
|
|
close(conn_.fd);
|
|
|
|
conn_.fd = -1;
|
2014-12-06 10:31:46 +01:00
|
|
|
|
2015-07-09 19:52:11 +02:00
|
|
|
if (end == next_downstream) {
|
2014-12-06 10:31:46 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try again with the next downstream server
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Connecting to downstream server";
|
|
|
|
}
|
|
|
|
|
2015-02-03 17:15:56 +01:00
|
|
|
addr_idx_ = i;
|
|
|
|
|
2015-02-04 13:15:58 +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
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
conn_.wlimit.startw();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2014-12-06 10:31:46 +01:00
|
|
|
break;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2015-07-26 18:18:52 +02:00
|
|
|
|
|
|
|
// TODO we should have timeout for connection establishment
|
|
|
|
ev_timer_again(conn_.loop, &conn_.wt);
|
|
|
|
} else {
|
|
|
|
// we may set read timer cb to idle_timeoutcb. Reset again.
|
|
|
|
conn_.rt.repeat = get_config()->downstream_read_timeout;
|
|
|
|
ev_set_cb(&conn_.rt, timeoutcb);
|
|
|
|
ev_timer_again(conn_.loop, &conn_.rt);
|
|
|
|
ev_set_cb(&conn_.rev, readcb);
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-08-18 17:16:51 +02:00
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
downstream_ = downstream;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-09-23 17:13:40 +02:00
|
|
|
http_parser_init(&response_htp_, HTTP_RESPONSE);
|
|
|
|
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() {
|
2015-07-09 19:52:11 +02:00
|
|
|
auto downstream_hostport = get_config()
|
|
|
|
->downstream_addr_groups[group_]
|
|
|
|
.addrs[addr_idx_]
|
|
|
|
.hostport.get();
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream_->request();
|
|
|
|
|
|
|
|
auto connect_method = req.method == HTTP_CONNECT;
|
2015-03-13 14:52:09 +01: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.
|
|
|
|
const char *authority = downstream_hostport;
|
|
|
|
auto no_host_rewrite = get_config()->no_host_rewrite ||
|
|
|
|
get_config()->http2_proxy ||
|
|
|
|
get_config()->client_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()) {
|
|
|
|
authority = req.authority.c_str();
|
2015-02-03 17:15:56 +01:00
|
|
|
}
|
2015-09-11 16:07:00 +02:00
|
|
|
auto authoritylen = strlen(authority);
|
2015-02-03 17:15:56 +01:00
|
|
|
|
2015-09-03 16:36:49 +02: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.
|
2016-01-13 14:45:52 +01:00
|
|
|
auto meth = http2::to_method_string(req.method);
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append(meth, strlen(meth));
|
|
|
|
buf->append(" ");
|
2015-06-16 14:31:33 +02:00
|
|
|
|
2015-05-26 15:26:17 +02:00
|
|
|
if (connect_method) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append(authority, authoritylen);
|
2015-03-13 14:40:41 +01:00
|
|
|
} else if (get_config()->http2_proxy || get_config()->client_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("://");
|
|
|
|
buf->append(authority, authoritylen);
|
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: ");
|
|
|
|
buf->append(authority, authoritylen);
|
|
|
|
buf->append("\r\n");
|
2015-02-03 17:15:56 +01:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
http2::build_http1_headers_from_headers(buf, req.fs.headers());
|
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-01-13 14:45:52 +01:00
|
|
|
if (!connect_method && req.http2_expect_body &&
|
|
|
|
!req.fs.header(http2::HD_CONTENT_LENGTH)) {
|
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
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req.connection_close) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("Connection: close\r\n");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2015-05-26 15:26:17 +02:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
if (!connect_method && req.upgrade_request) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
auto xff = req.fs.header(http2::HD_X_FORWARDED_FOR);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->add_x_forwarded_for) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("X-Forwarded-For: ");
|
2015-01-04 15:22:39 +01:00
|
|
|
if (xff && !get_config()->strip_incoming_x_forwarded_for) {
|
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");
|
2015-01-04 15:22:39 +01:00
|
|
|
} else if (xff && !get_config()->strip_incoming_x_forwarded_for) {
|
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
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->http2_proxy && !get_config()->client_proxy &&
|
2015-05-26 15:26:17 +02:00
|
|
|
!connect_method) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("X-Forwarded-Proto: ");
|
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("\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);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->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-01-13 14:45:52 +01:00
|
|
|
buf->append(http::create_via_header_value(req.http_major, req.http_minor));
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append("\r\n");
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:04:20 +02:00
|
|
|
for (auto &p : get_config()->add_request_headers) {
|
2015-09-11 16:07:00 +02:00
|
|
|
buf->append(p.first);
|
|
|
|
buf->append(": ");
|
|
|
|
buf->append(p.second);
|
|
|
|
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="
|
2015-09-11 16:07:00 +02:00
|
|
|
<< downstream_->get_stream_id() << "\n" << nhdrs;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
signal_write();
|
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_upload_data_chunk(const uint8_t *data,
|
|
|
|
size_t datalen) {
|
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);
|
2014-12-27 18:59:06 +01:00
|
|
|
output->append(chunk_size_hex.c_str(), chunk_size_hex.size());
|
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() {
|
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
|
|
|
|
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");
|
2015-09-11 16:07:00 +02:00
|
|
|
http2::build_http1_headers_from_headers(output, trailers);
|
2015-03-08 09:58:00 +01:00
|
|
|
output->append("\r\n");
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
signal_write();
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
}
|
2014-10-13 14:09:00 +02:00
|
|
|
auto dconn_pool = dconn->get_dconn_pool();
|
|
|
|
dconn_pool->remove_downstream_connection(dconn);
|
|
|
|
// 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);
|
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
|
|
|
}
|
2014-10-13 14:09:00 +02:00
|
|
|
auto dconn_pool = dconn->get_dconn_pool();
|
|
|
|
dconn_pool->remove_downstream_connection(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
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
conn_.rt.repeat = get_config()->downstream_idle_read_timeout;
|
|
|
|
ev_set_cb(&conn_.rt, idle_timeoutcb);
|
|
|
|
ev_timer_again(conn_.loop, &conn_.rt);
|
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) {
|
2015-10-04 03:36:20 +02:00
|
|
|
if (downstream_->get_response_buf()->rleft() <=
|
|
|
|
get_config()->downstream_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 {
|
2014-11-27 15:39:04 +01:00
|
|
|
int htp_msg_begincb(http_parser *htp) {
|
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2014-07-03 13:57:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() != Downstream::INITIAL) {
|
2014-07-03 13:57:07 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int htp_hdrs_completecb(http_parser *htp) {
|
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2014-07-23 16:32:57 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
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;
|
|
|
|
|
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
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
if (resp.fs.index_headers() != 0) {
|
2015-01-20 15:11:54 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_BAD_HEADER);
|
|
|
|
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.
|
|
|
|
downstream->check_upgrade_fulfilled();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
resp.connection_close = !http_should_keep_alive(htp);
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::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);
|
2013-02-11 09:20:52 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
downstream->set_request_state(Downstream::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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
auto status = resp.http_status;
|
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
|
|
|
|
// http-parser. Need test.
|
2016-01-13 14:45:52 +01:00
|
|
|
return req.method == HTTP_HEAD || (100 <= status && status <= 199) ||
|
|
|
|
status == 204 || status == 304
|
2014-11-27 15:39:04 +01:00
|
|
|
? 1
|
|
|
|
: 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int htp_hdr_keycb(http_parser *htp, const char *data, size_t len) {
|
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2016-01-13 16:37:45 +01:00
|
|
|
auto &resp = downstream->response();
|
|
|
|
|
2015-03-08 09:19:52 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::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-01-13 16:37:45 +01:00
|
|
|
resp.fs.add_header(std::string(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-01-13 16:37:45 +01:00
|
|
|
resp.fs.add_trailer(std::string(data, len), "");
|
2015-03-08 09:19:52 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int htp_hdr_valcb(http_parser *htp, const char *data, size_t len) {
|
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2016-01-13 16:37:45 +01:00
|
|
|
auto &resp = downstream->response();
|
|
|
|
|
2015-03-08 09:19:52 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::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 {
|
2014-11-27 15:39:04 +01:00
|
|
|
int htp_bodycb(http_parser *htp, const char *data, size_t len) {
|
|
|
|
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 {
|
2014-11-27 15:39:04 +01:00
|
|
|
int htp_msg_completecb(http_parser *htp) {
|
|
|
|
auto downstream = static_cast<Downstream *>(htp->data);
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2015-05-26 15:26:17 +02:00
|
|
|
// http-parser does not treat "200 connection established" response
|
|
|
|
// against CONNECT request, and in that case, this function is not
|
|
|
|
// called. But if HTTP Upgrade is made (e.g., WebSocket), this
|
|
|
|
// function is called, and http_parser_execute() returns just after
|
|
|
|
// that.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::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
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
http_parser_settings htp_hooks = {
|
2014-11-27 15:39:04 +01:00
|
|
|
htp_msg_begincb, // http_cb on_message_begin;
|
|
|
|
nullptr, // http_data_cb on_url;
|
|
|
|
nullptr, // http_data_cb on_status;
|
|
|
|
htp_hdr_keycb, // http_data_cb on_header_field;
|
|
|
|
htp_hdr_valcb, // http_data_cb on_header_value;
|
|
|
|
htp_hdrs_completecb, // http_cb on_headers_complete;
|
|
|
|
htp_bodycb, // http_data_cb on_body;
|
|
|
|
htp_msg_completecb // http_cb on_message_complete;
|
2012-11-20 17:29:39 +01:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::on_read() {
|
2015-03-28 17:47:22 +01:00
|
|
|
if (!connected_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ev_timer_again(conn_.loop, &conn_.rt);
|
2015-06-21 07:32:47 +02:00
|
|
|
std::array<uint8_t, 8_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
|
|
|
if (downstream_->get_upgraded()) {
|
2013-07-31 14:48:37 +02:00
|
|
|
// For upgraded connection, just pass data to the upstream.
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2015-02-05 16:06:01 +01:00
|
|
|
auto nread = conn_.read_clear(buf.data(), buf.size());
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (nread == 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread < 0) {
|
|
|
|
return nread;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
rv = downstream_->get_upstream()->on_downstream_body(
|
|
|
|
downstream_, buf.data(), nread, true);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-06-01 14:01:01 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
if (downstream_->response_buf_full()) {
|
|
|
|
downstream_->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
return 0;
|
2014-06-01 14:01:01 +02:00
|
|
|
}
|
2013-12-20 15:28:54 +01:00
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2015-02-05 16:06:01 +01:00
|
|
|
auto nread = conn_.read_clear(buf.data(), buf.size());
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (nread == 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread < 0) {
|
|
|
|
return nread;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
auto nproc =
|
|
|
|
http_parser_execute(&response_htp_, &htp_hooks,
|
|
|
|
reinterpret_cast<char *>(buf.data()), nread);
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2014-06-01 14:01:01 +02:00
|
|
|
auto htperr = HTTP_PARSER_ERRNO(&response_htp_);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (htperr != HPE_OK) {
|
2015-09-02 18:32:15 +02:00
|
|
|
// Handling early return (in other words, response was hijacked
|
|
|
|
// by mruby scripting).
|
|
|
|
if (downstream_->get_response_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
return SHRPX_ERR_DCONN_CANCELED;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-06-01 14:01:01 +02:00
|
|
|
DCLOG(INFO, this) << "HTTP parser failure: "
|
|
|
|
<< "(" << http_errno_name(htperr) << ") "
|
|
|
|
<< http_errno_description(htperr);
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downstream_->response_buf_full()) {
|
|
|
|
downstream_->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2015-05-26 15:26:17 +02:00
|
|
|
|
|
|
|
if (downstream_->get_upgraded()) {
|
2015-06-06 16:37:46 +02:00
|
|
|
if (nproc < static_cast<size_t>(nread)) {
|
2015-05-26 15:26:17 +02:00
|
|
|
// Data from buf.data() + nproc are for upgraded protocol.
|
|
|
|
rv = downstream_->get_upstream()->on_downstream_body(
|
|
|
|
downstream_, buf.data() + nproc, nread - nproc, true);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downstream_->response_buf_full()) {
|
|
|
|
downstream_->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// call on_read(), so that we can process data left in buffer as
|
|
|
|
// upgrade.
|
|
|
|
return on_read();
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpDownstreamConnection::on_write() {
|
2015-03-28 17:47:22 +01:00
|
|
|
if (!connected_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ev_timer_again(conn_.loop, &conn_.rt);
|
2014-09-18 16:19:28 +02: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();
|
|
|
|
|
2015-02-06 14:44:09 +01:00
|
|
|
std::array<struct iovec, MAX_WR_IOVCNT> iov;
|
2015-01-10 15:04:54 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
while (input->rleft() > 0) {
|
2015-02-06 14:44:09 +01:00
|
|
|
auto iovcnt = input->riovec(iov.data(), iov.size());
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-06 14:44:09 +01:00
|
|
|
auto nwrite = conn_.writev_clear(iov.data(), iovcnt);
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
if (nwrite == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nwrite < 0) {
|
|
|
|
return nwrite;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-05 08:14:10 +01:00
|
|
|
int HttpDownstreamConnection::on_connect() {
|
2015-03-10 15:11:22 +01:00
|
|
|
auto connect_blocker = client_handler_->get_connect_blocker();
|
2015-01-23 15:00:18 +01:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
if (!util::check_socket_connected(conn_.fd)) {
|
|
|
|
conn_.wlimit.stopw();
|
2015-01-22 13:13:34 +01:00
|
|
|
|
2015-01-20 15:26:09 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, this) << "downstream connect failed";
|
|
|
|
}
|
2015-03-10 15:21:48 +01:00
|
|
|
|
2015-07-15 12:46:48 +02:00
|
|
|
downstream_->set_request_state(Downstream::CONNECT_FAIL);
|
|
|
|
|
2015-01-05 08:14:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-28 17:47:22 +01:00
|
|
|
connected_ = true;
|
|
|
|
|
2015-01-23 15:00:18 +01:00
|
|
|
connect_blocker->on_success();
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
conn_.rlimit.startw();
|
2015-07-26 18:18:52 +02:00
|
|
|
ev_timer_again(conn_.loop, &conn_.rt);
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ev_set_cb(&conn_.wev, writecb);
|
2015-01-05 08:14:10 +01:00
|
|
|
|
|
|
|
return 0;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
void HttpDownstreamConnection::on_upstream_change(Upstream *upstream) {}
|
|
|
|
|
2015-10-28 15:26:41 +01:00
|
|
|
void HttpDownstreamConnection::signal_write() {
|
|
|
|
ev_feed_event(conn_.loop, &conn_.wev, EV_WRITE);
|
|
|
|
}
|
2014-09-18 16:19:28 +02:00
|
|
|
|
2015-07-09 19:52:11 +02:00
|
|
|
size_t HttpDownstreamConnection::get_group() const { return group_; }
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
} // namespace shrpx
|