2012-11-18 13:23:13 +01:00
|
|
|
/*
|
2013-07-12 17:19:03 +02:00
|
|
|
* nghttp2 - HTTP/2.0 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"
|
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 {
|
|
|
|
|
|
|
|
// Workaround for the inability for Bufferevent to remove timeout from
|
|
|
|
// bufferevent. Specify this long timeout instead of removing.
|
|
|
|
namespace {
|
|
|
|
timeval max_timeout = { 86400, 0 };
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
HttpDownstreamConnection::HttpDownstreamConnection
|
|
|
|
(ClientHandler *client_handler)
|
2012-11-20 17:29:39 +01:00
|
|
|
: DownstreamConnection(client_handler),
|
2013-09-24 14:42:50 +02:00
|
|
|
bev_(nullptr),
|
|
|
|
ioctrl_(nullptr)
|
2012-11-18 13:23:13 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
HttpDownstreamConnection::~HttpDownstreamConnection()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
if(bev_) {
|
|
|
|
bufferevent_disable(bev_, EV_READ | EV_WRITE);
|
|
|
|
bufferevent_free(bev_);
|
|
|
|
}
|
|
|
|
// Downstream and DownstreamConnection may be deleted
|
|
|
|
// asynchronously.
|
|
|
|
if(downstream_) {
|
2013-09-24 14:42:50 +02:00
|
|
|
downstream_->set_downstream_connection(nullptr);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
|
|
|
|
int HttpDownstreamConnection::attach_downstream(Downstream *downstream)
|
|
|
|
{
|
2013-01-21 14:42:49 +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
|
|
|
}
|
2013-09-24 14:42:50 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
2012-11-18 13:23:13 +01:00
|
|
|
if(!bev_) {
|
2013-09-24 14:42:50 +02:00
|
|
|
auto evbase = client_handler_->get_evbase();
|
2012-11-18 13:23:13 +01:00
|
|
|
bev_ = bufferevent_socket_new
|
|
|
|
(evbase, -1,
|
|
|
|
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
|
2013-09-24 16:17:53 +02:00
|
|
|
if(!bev_) {
|
|
|
|
DCLOG(INFO, this) << "bufferevent_socket_new() failed";
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
int rv = bufferevent_socket_connect
|
|
|
|
(bev_,
|
|
|
|
// TODO maybe not thread-safe?
|
|
|
|
const_cast<sockaddr*>(&get_config()->downstream_addr.sa),
|
|
|
|
get_config()->downstream_addrlen);
|
|
|
|
if(rv != 0) {
|
|
|
|
bufferevent_free(bev_);
|
2013-09-24 16:17:53 +02:00
|
|
|
bev_ = nullptr;
|
2012-11-18 13:23:13 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "Connecting to downstream server";
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
downstream->set_downstream_connection(this);
|
|
|
|
downstream_ = downstream;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
|
|
|
ioctrl_.set_bev(bev_);
|
|
|
|
|
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
|
|
|
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WARTER_MARK);
|
|
|
|
bufferevent_enable(bev_, EV_READ);
|
|
|
|
bufferevent_setcb(bev_,
|
|
|
|
upstream->get_downstream_readcb(),
|
|
|
|
upstream->get_downstream_writecb(),
|
|
|
|
upstream->get_downstream_eventcb(), this);
|
|
|
|
// HTTP request/response model, we first issue request to downstream
|
|
|
|
// server, so just enable write timeout here.
|
|
|
|
bufferevent_set_timeouts(bev_,
|
|
|
|
&max_timeout,
|
|
|
|
&get_config()->downstream_write_timeout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::push_request_headers()
|
|
|
|
{
|
2013-11-16 13:15:55 +01:00
|
|
|
downstream_->assemble_request_cookie();
|
2013-10-25 14:50:56 +02:00
|
|
|
downstream_->normalize_request_headers();
|
|
|
|
auto end_headers = std::end(downstream_->get_request_headers());
|
2013-09-11 16:24:32 +02:00
|
|
|
// Assume that method and request path do not contain \r\n.
|
2012-11-18 13:23:13 +01:00
|
|
|
std::string hdrs = downstream_->get_request_method();
|
|
|
|
hdrs += " ";
|
2013-10-25 14:50:56 +02:00
|
|
|
if(downstream_->get_request_method() == "CONNECT") {
|
|
|
|
if(!downstream_->get_request_http2_authority().empty()) {
|
|
|
|
hdrs += downstream_->get_request_http2_authority();
|
|
|
|
} else {
|
|
|
|
hdrs += downstream_->get_request_path();
|
|
|
|
}
|
2013-11-04 10:14:05 +01:00
|
|
|
} else if(get_config()->http2_proxy &&
|
2013-10-25 14:50:56 +02:00
|
|
|
!downstream_->get_request_http2_scheme().empty() &&
|
|
|
|
!downstream_->get_request_http2_authority().empty() &&
|
|
|
|
downstream_->get_request_path().c_str()[0] == '/') {
|
|
|
|
// Construct absolute-form request target because we are going to
|
|
|
|
// send a request to a HTTP/1 proxy.
|
|
|
|
hdrs += downstream_->get_request_http2_scheme();
|
|
|
|
hdrs += "://";
|
|
|
|
hdrs += downstream_->get_request_http2_authority();
|
|
|
|
hdrs += downstream_->get_request_path();
|
|
|
|
} else {
|
|
|
|
// No proxy case. get_request_path() may be absolute-form but we
|
|
|
|
// don't care.
|
|
|
|
hdrs += downstream_->get_request_path();
|
|
|
|
}
|
2013-09-11 16:24:32 +02:00
|
|
|
hdrs += " HTTP/1.1\r\n";
|
2013-10-25 14:50:56 +02:00
|
|
|
if(downstream_->get_norm_request_header("host") == end_headers &&
|
|
|
|
!downstream_->get_request_http2_authority().empty()) {
|
|
|
|
hdrs += "Host: ";
|
|
|
|
hdrs += downstream_->get_request_http2_authority();
|
|
|
|
hdrs += "\r\n";
|
|
|
|
}
|
2013-08-27 19:47:22 +02:00
|
|
|
http2::build_http1_headers_from_norm_headers
|
2013-08-27 17:09:46 +02:00
|
|
|
(hdrs, downstream_->get_request_headers());
|
|
|
|
|
2013-11-16 13:15:55 +01:00
|
|
|
if(!downstream_->get_assembled_request_cookie().empty()) {
|
|
|
|
hdrs += "Cookie: ";
|
|
|
|
hdrs += downstream_->get_assembled_request_cookie();
|
|
|
|
hdrs += "\r\n";
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
if(downstream_->get_request_connection_close()) {
|
|
|
|
hdrs += "Connection: close\r\n";
|
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
auto xff = downstream_->get_norm_request_header("x-forwarded-for");
|
2012-11-18 13:23:13 +01:00
|
|
|
if(get_config()->add_x_forwarded_for) {
|
|
|
|
hdrs += "X-Forwarded-For: ";
|
2013-08-27 17:09:46 +02:00
|
|
|
if(xff != end_headers) {
|
|
|
|
hdrs += (*xff).second;
|
2013-09-11 16:24:32 +02:00
|
|
|
http2::sanitize_header_value(hdrs, hdrs.size() - (*xff).second.size());
|
2012-11-18 13:23:13 +01:00
|
|
|
hdrs += ", ";
|
|
|
|
}
|
|
|
|
hdrs += downstream_->get_upstream()->get_client_handler()->get_ipaddr();
|
|
|
|
hdrs += "\r\n";
|
2013-08-27 17:09:46 +02:00
|
|
|
} else if(xff != end_headers) {
|
2012-11-18 13:23:13 +01:00
|
|
|
hdrs += "X-Forwarded-For: ";
|
2013-08-27 17:09:46 +02:00
|
|
|
hdrs += (*xff).second;
|
2013-09-11 16:24:32 +02:00
|
|
|
http2::sanitize_header_value(hdrs, hdrs.size() - (*xff).second.size());
|
2012-11-18 13:23:13 +01:00
|
|
|
hdrs += "\r\n";
|
|
|
|
}
|
|
|
|
if(downstream_->get_request_method() != "CONNECT") {
|
|
|
|
hdrs += "X-Forwarded-Proto: ";
|
2013-10-25 14:50:56 +02:00
|
|
|
if(!downstream_->get_request_http2_scheme().empty()) {
|
|
|
|
hdrs += downstream_->get_request_http2_scheme();
|
|
|
|
hdrs += "\r\n";
|
|
|
|
} else if(util::istartsWith(downstream_->get_request_path(), "https:")) {
|
2013-09-11 16:24:32 +02:00
|
|
|
hdrs += "https\r\n";
|
2013-10-25 14:50:56 +02:00
|
|
|
} else {
|
|
|
|
hdrs += "http\r\n";
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
auto expect = downstream_->get_norm_request_header("expect");
|
|
|
|
if(expect != end_headers &&
|
2013-08-28 17:03:26 +02:00
|
|
|
!util::strifind((*expect).second.c_str(), "100-continue")) {
|
2013-08-27 17:09:46 +02:00
|
|
|
hdrs += "Expect: ";
|
|
|
|
hdrs += (*expect).second;
|
2013-09-11 16:24:32 +02:00
|
|
|
http2::sanitize_header_value(hdrs, hdrs.size() - (*expect).second.size());
|
2013-08-27 17:09:46 +02:00
|
|
|
hdrs += "\r\n";
|
|
|
|
}
|
|
|
|
auto via = downstream_->get_norm_request_header("via");
|
|
|
|
if(get_config()->no_via) {
|
|
|
|
if(via != end_headers) {
|
|
|
|
hdrs += "Via: ";
|
|
|
|
hdrs += (*via).second;
|
2013-09-11 16:24:32 +02:00
|
|
|
http2::sanitize_header_value(hdrs, hdrs.size() - (*via).second.size());
|
2013-08-27 17:09:46 +02:00
|
|
|
hdrs += "\r\n";
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-09 14:01:25 +01:00
|
|
|
hdrs += "Via: ";
|
2013-08-27 17:09:46 +02:00
|
|
|
if(via != end_headers) {
|
|
|
|
hdrs += (*via).second;
|
2013-09-11 16:24:32 +02:00
|
|
|
http2::sanitize_header_value(hdrs, hdrs.size() - (*via).second.size());
|
2013-01-09 14:01:25 +01:00
|
|
|
hdrs += ", ";
|
|
|
|
}
|
|
|
|
hdrs += http::create_via_header_value(downstream_->get_request_major(),
|
|
|
|
downstream_->get_request_minor());
|
|
|
|
hdrs += "\r\n";
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hdrs += "\r\n";
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 13:36:02 +01:00
|
|
|
const char *hdrp;
|
|
|
|
std::string nhdrs;
|
|
|
|
if(get_config()->tty) {
|
|
|
|
nhdrs = http::colorizeHeaders(hdrs.c_str());
|
|
|
|
hdrp = nhdrs.c_str();
|
|
|
|
} else {
|
|
|
|
hdrp = hdrs.c_str();
|
|
|
|
}
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "HTTP request headers. stream_id="
|
2012-12-09 13:36:02 +01:00
|
|
|
<< downstream_->get_stream_id() << "\n" << hdrp;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2013-09-24 14:42:50 +02:00
|
|
|
auto output = bufferevent_get_output(bev_);
|
2012-11-18 13:23:13 +01:00
|
|
|
int rv;
|
|
|
|
rv = evbuffer_add(output, hdrs.c_str(), hdrs.size());
|
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
|
|
|
|
// When downstream request is issued, set read timeout. We don't
|
|
|
|
// know when the request is completely received by the downstream
|
|
|
|
// server. This function may be called before that happens. Overall
|
|
|
|
// it does not cause problem for most of the time. If the
|
|
|
|
// downstream server is too slow to recv/send, the connection will
|
|
|
|
// be dropped by read timeout.
|
|
|
|
bufferevent_set_timeouts(bev_,
|
|
|
|
&get_config()->downstream_read_timeout,
|
|
|
|
&get_config()->downstream_write_timeout);
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::push_upload_data_chunk
|
|
|
|
(const uint8_t *data, size_t datalen)
|
|
|
|
{
|
|
|
|
ssize_t res = 0;
|
|
|
|
int rv;
|
|
|
|
int chunked = downstream_->get_chunked_request();
|
2013-09-24 14:42:50 +02:00
|
|
|
auto output = bufferevent_get_output(bev_);
|
2012-11-18 13:23:13 +01:00
|
|
|
if(chunked) {
|
|
|
|
char chunk_size_hex[16];
|
|
|
|
rv = snprintf(chunk_size_hex, sizeof(chunk_size_hex), "%X\r\n",
|
|
|
|
static_cast<unsigned int>(datalen));
|
|
|
|
res += rv;
|
|
|
|
rv = evbuffer_add(output, chunk_size_hex, rv);
|
|
|
|
if(rv == -1) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(FATAL, this) << "evbuffer_add() failed";
|
2012-11-18 13:23:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = evbuffer_add(output, data, datalen);
|
|
|
|
if(rv == -1) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(FATAL, this) << "evbuffer_add() failed";
|
2012-11-18 13:23:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res += rv;
|
|
|
|
if(chunked) {
|
|
|
|
rv = evbuffer_add(output, "\r\n", 2);
|
|
|
|
if(rv == -1) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(FATAL, this) << "evbuffer_add() failed";
|
2012-11-18 13:23:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res += 2;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::end_upload_data()
|
|
|
|
{
|
|
|
|
if(downstream_->get_chunked_request()) {
|
2013-09-24 14:42:50 +02:00
|
|
|
auto output = bufferevent_get_output(bev_);
|
2012-11-18 13:23:13 +01:00
|
|
|
if(evbuffer_add(output, "0\r\n\r\n", 5) != 0) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(FATAL, this) << "evbuffer_add() failed";
|
2012-11-18 13:23:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
|
|
|
// Gets called when DownstreamConnection is pooled in ClientHandler.
|
|
|
|
void idle_eventcb(bufferevent *bev, short events, void *arg)
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto dconn = reinterpret_cast<HttpDownstreamConnection*>(arg);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
|
|
|
// Downstream was detached before connection established?
|
|
|
|
// This may be safe to be left.
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Idle connection connected?";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_EOF) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Idle connection EOF";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} else if(events & BEV_EVENT_TIMEOUT) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Idle connection timeout";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} else if(events & BEV_EVENT_ERROR) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Idle connection network error";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
2013-09-24 14:42:50 +02:00
|
|
|
auto client_handler = dconn->get_client_handler();
|
2012-11-20 17:29:39 +01:00
|
|
|
client_handler->remove_downstream_connection(dconn);
|
|
|
|
delete dconn;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void HttpDownstreamConnection::detach_downstream(Downstream *downstream)
|
|
|
|
{
|
2013-01-21 14:42:49 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
downstream_ = 0;
|
|
|
|
ioctrl_.force_resume_read();
|
|
|
|
bufferevent_enable(bev_, EV_READ);
|
|
|
|
bufferevent_setcb(bev_, 0, 0, idle_eventcb, this);
|
|
|
|
// On idle state, just enable read timeout. Normally idle downstream
|
|
|
|
// connection will get EOF from the downstream server and closed.
|
|
|
|
bufferevent_set_timeouts(bev_,
|
|
|
|
&get_config()->downstream_idle_read_timeout,
|
|
|
|
&get_config()->downstream_write_timeout);
|
|
|
|
client_handler_->pool_downstream_connection(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent* HttpDownstreamConnection::get_bev()
|
|
|
|
{
|
|
|
|
return bev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpDownstreamConnection::pause_read(IOCtrlReason reason)
|
|
|
|
{
|
|
|
|
ioctrl_.pause_read(reason);
|
|
|
|
}
|
|
|
|
|
2013-02-08 13:46:58 +01:00
|
|
|
int HttpDownstreamConnection::resume_read(IOCtrlReason reason)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-02-08 13:46:58 +01:00
|
|
|
return ioctrl_.resume_read(reason) ? 0 : -1;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpDownstreamConnection::force_resume_read()
|
|
|
|
{
|
|
|
|
ioctrl_.force_resume_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HttpDownstreamConnection::get_output_buffer_full()
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto output = bufferevent_get_output(bev_);
|
2012-11-20 17:29:39 +01:00
|
|
|
return evbuffer_get_length(output) >= Downstream::OUTPUT_UPPER_THRES;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdrs_completecb(http_parser *htp)
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto downstream = reinterpret_cast<Downstream*>(htp->data);
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_http_status(htp->status_code);
|
|
|
|
downstream->set_response_major(htp->http_major);
|
|
|
|
downstream->set_response_minor(htp->http_minor);
|
|
|
|
downstream->set_response_connection_close(!http_should_keep_alive(htp));
|
|
|
|
downstream->set_response_state(Downstream::HEADER_COMPLETE);
|
2013-07-31 14:48:37 +02:00
|
|
|
downstream->check_upgrade_fulfilled();
|
|
|
|
if(downstream->get_upgraded()) {
|
2013-02-11 09:20:52 +01:00
|
|
|
downstream->set_response_connection_close(true);
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
if(downstream->get_upstream()->on_downstream_header_complete(downstream)
|
|
|
|
!= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
|
|
|
|
if(downstream->get_upgraded()) {
|
|
|
|
// Upgrade complete, read until EOF in both ends
|
2013-07-31 15:14:25 +02:00
|
|
|
if(downstream->get_upstream()->resume_read(SHRPX_MSG_BLOCK,
|
|
|
|
downstream) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "HTTP upgrade success. stream_id="
|
|
|
|
<< downstream->get_stream_id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
unsigned int status = downstream->get_response_http_status();
|
|
|
|
// 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
|
|
|
|
// http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-20#section-3.3
|
|
|
|
return downstream->get_request_method() == "HEAD" ||
|
|
|
|
(100 <= status && status <= 199) || status == 204 ||
|
|
|
|
status == 304 ? 1 : 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdr_keycb(http_parser *htp, const char *data, size_t len)
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto downstream = reinterpret_cast<Downstream*>(htp->data);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(downstream->get_response_header_key_prev()) {
|
|
|
|
downstream->append_last_response_header_key(data, len);
|
|
|
|
} else {
|
|
|
|
downstream->add_response_header(std::string(data, len), "");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdr_valcb(http_parser *htp, const char *data, size_t len)
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto downstream = reinterpret_cast<Downstream*>(htp->data);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(downstream->get_response_header_key_prev()) {
|
|
|
|
downstream->set_last_response_header_value(std::string(data, len));
|
|
|
|
} else {
|
|
|
|
downstream->append_last_response_header_value(data, len);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_bodycb(http_parser *htp, const char *data, size_t len)
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto downstream = reinterpret_cast<Downstream*>(htp->data);
|
2012-11-20 17:29:39 +01:00
|
|
|
return downstream->get_upstream()->on_downstream_body
|
|
|
|
(downstream, reinterpret_cast<const uint8_t*>(data), len);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_msg_completecb(http_parser *htp)
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto downstream = reinterpret_cast<Downstream*>(htp->data);
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
return downstream->get_upstream()->on_downstream_body_complete(downstream);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
http_parser_settings htp_hooks = {
|
2013-09-24 14:42:50 +02:00
|
|
|
nullptr, /*http_cb on_message_begin;*/
|
|
|
|
nullptr, /*http_data_cb on_url;*/
|
|
|
|
nullptr, /*http_cb on_status_complete */
|
2012-11-20 17:29:39 +01:00
|
|
|
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;*/
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::on_read()
|
|
|
|
{
|
2013-09-24 14:42:50 +02:00
|
|
|
auto input = bufferevent_get_input(bev_);
|
2013-07-31 14:48:37 +02:00
|
|
|
size_t inputlen = evbuffer_get_length(input);
|
2013-09-24 14:42:50 +02:00
|
|
|
auto mem = evbuffer_pullup(input, -1);
|
2013-07-31 14:48:37 +02:00
|
|
|
if(downstream_->get_upgraded()) {
|
|
|
|
// For upgraded connection, just pass data to the upstream.
|
|
|
|
int rv;
|
|
|
|
rv = downstream_->get_upstream()->on_downstream_body
|
|
|
|
(downstream_, reinterpret_cast<const uint8_t*>(mem), inputlen);
|
2013-12-20 15:28:54 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if(evbuffer_drain(input, inputlen) != 0) {
|
|
|
|
DCLOG(FATAL, this) << "evbuffer_drain() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
2013-09-23 17:13:40 +02:00
|
|
|
size_t nread = http_parser_execute(&response_htp_, &htp_hooks,
|
2012-11-20 17:29:39 +01:00
|
|
|
reinterpret_cast<const char*>(mem),
|
2013-07-31 14:48:37 +02:00
|
|
|
inputlen);
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-12-20 15:28:54 +01:00
|
|
|
if(evbuffer_drain(input, nread) != 0) {
|
|
|
|
DCLOG(FATAL, this) << "evbuffer_drain() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-24 14:42:50 +02:00
|
|
|
auto htperr = HTTP_PARSER_ERRNO(&response_htp_);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(htperr == HPE_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "HTTP parser failure: "
|
|
|
|
<< "(" << http_errno_name(htperr) << ") "
|
|
|
|
<< http_errno_description(htperr);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
return SHRPX_ERR_HTTP_PARSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpDownstreamConnection::on_write()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
void HttpDownstreamConnection::on_upstream_change(Upstream *upstream)
|
|
|
|
{
|
|
|
|
bufferevent_setcb(bev_,
|
|
|
|
upstream->get_downstream_readcb(),
|
|
|
|
upstream->get_downstream_writecb(),
|
|
|
|
upstream->get_downstream_eventcb(), this);
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
} // namespace shrpx
|