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.
|
|
|
|
*/
|
2013-11-04 09:53:57 +01:00
|
|
|
#include "shrpx_http2_downstream_connection.h"
|
2012-11-18 13:23:13 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include <event2/bufferevent_ssl.h>
|
|
|
|
|
2013-01-05 15:21:09 +01:00
|
|
|
#include "http-parser/http_parser.h"
|
2012-11-20 17:29:39 +01:00
|
|
|
|
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"
|
2013-11-04 09:53:57 +01: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 {
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
Http2DownstreamConnection::Http2DownstreamConnection
|
2012-11-18 13:23:13 +01:00
|
|
|
(ClientHandler *client_handler)
|
|
|
|
: DownstreamConnection(client_handler),
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_(client_handler->get_http2_session()),
|
2012-11-20 17:29:39 +01:00
|
|
|
request_body_buf_(0),
|
2013-10-29 16:10:28 +01:00
|
|
|
sd_(0)
|
2012-11-18 13:23:13 +01:00
|
|
|
{}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
Http2DownstreamConnection::~Http2DownstreamConnection()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
2013-02-08 13:46:58 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Deleting";
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
if(request_body_buf_) {
|
|
|
|
evbuffer_free(request_body_buf_);
|
|
|
|
}
|
2013-02-07 16:22:22 +01:00
|
|
|
if(downstream_) {
|
|
|
|
if(submit_rst_stream(downstream_) == 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2013-02-07 16:22:22 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->remove_downstream_connection(this);
|
2012-11-20 17:29:39 +01:00
|
|
|
// Downstream and DownstreamConnection may be deleted
|
|
|
|
// asynchronously.
|
|
|
|
if(downstream_) {
|
|
|
|
downstream_->set_downstream_connection(0);
|
|
|
|
}
|
2013-02-08 13:46:58 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Deleted";
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::init_request_body_buf()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
if(request_body_buf_) {
|
|
|
|
rv = evbuffer_drain(request_body_buf_,
|
|
|
|
evbuffer_get_length(request_body_buf_));
|
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request_body_buf_ = evbuffer_new();
|
|
|
|
if(request_body_buf_ == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-18 18:11:46 +01:00
|
|
|
evbuffer_setcb(request_body_buf_, 0, this);
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::attach_downstream(Downstream *downstream)
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
if(init_request_body_buf() == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->add_downstream_connection(this);
|
|
|
|
if(http2session_->get_state() == Http2Session::DISCONNECTED) {
|
|
|
|
http2session_->notify();
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
downstream->set_downstream_connection(this);
|
|
|
|
downstream_ = downstream;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2DownstreamConnection::detach_downstream(Downstream *downstream)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
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
|
|
|
}
|
2013-02-07 16:22:22 +01:00
|
|
|
if(submit_rst_stream(downstream) == 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2013-02-07 16:22:22 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
downstream_ = 0;
|
|
|
|
|
|
|
|
client_handler_->pool_downstream_connection(this);
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::submit_rst_stream(Downstream *downstream)
|
2013-02-07 16:22:22 +01:00
|
|
|
{
|
|
|
|
int rv = -1;
|
2013-11-04 09:53:57 +01:00
|
|
|
if(http2session_->get_state() == Http2Session::CONNECTED &&
|
2013-02-07 16:22:22 +01:00
|
|
|
downstream->get_downstream_stream_id() != -1) {
|
|
|
|
switch(downstream->get_response_state()) {
|
|
|
|
case Downstream::MSG_RESET:
|
|
|
|
case Downstream::MSG_COMPLETE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "Submit RST_STREAM for DOWNSTREAM:"
|
2013-02-09 09:56:44 +01:00
|
|
|
<< downstream << ", stream_id="
|
|
|
|
<< downstream->get_downstream_stream_id();
|
2013-02-07 16:22:22 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
rv = http2session_->submit_rst_stream
|
|
|
|
(downstream->get_downstream_stream_id(),
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
2013-02-07 16:22:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
namespace {
|
2013-11-04 09:53:57 +01:00
|
|
|
ssize_t http2_data_read_callback(nghttp2_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length,
|
|
|
|
int *eof,
|
|
|
|
nghttp2_data_source *source,
|
|
|
|
void *user_data)
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
2013-07-12 17:19:03 +02:00
|
|
|
(nghttp2_session_get_stream_user_data(session, stream_id));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!sd || !sd->dconn) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
auto dconn = reinterpret_cast<Http2DownstreamConnection*>(source->ptr);
|
|
|
|
auto downstream = dconn->get_downstream();
|
2012-11-18 13:23:13 +01:00
|
|
|
if(!downstream) {
|
|
|
|
// In this case, RST_STREAM should have been issued. But depending
|
|
|
|
// on the priority, DATA frame may come first.
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
auto body = dconn->get_request_body_buf();
|
2012-11-20 17:29:39 +01:00
|
|
|
int nread = 0;
|
|
|
|
for(;;) {
|
|
|
|
nread = evbuffer_remove(body, buf, length);
|
|
|
|
if(nread == 0) {
|
|
|
|
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
|
2013-08-23 17:28:50 +02:00
|
|
|
if(!downstream->get_upgrade_request() ||
|
|
|
|
(downstream->get_response_state() == Downstream::HEADER_COMPLETE &&
|
|
|
|
!downstream->get_upgraded())) {
|
2013-07-31 14:48:37 +02:00
|
|
|
*eof = 1;
|
2013-08-23 17:28:50 +02:00
|
|
|
} else {
|
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2013-02-08 13:46:58 +01:00
|
|
|
// This is important because it will handle flow control
|
|
|
|
// stuff.
|
|
|
|
if(downstream->get_upstream()->resume_read(SHRPX_NO_BUFFER,
|
|
|
|
downstream) == -1) {
|
2012-11-20 17:29:39 +01:00
|
|
|
// In this case, downstream may be deleted.
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-02-27 14:55:44 +01:00
|
|
|
// Check dconn is still alive because Upstream::resume_read()
|
|
|
|
// may delete downstream which will delete dconn.
|
|
|
|
if(sd->dconn == 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2013-02-27 14:55:44 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
if(evbuffer_get_length(body) == 0) {
|
2013-03-07 17:50:46 +01:00
|
|
|
// Check get_request_state() == MSG_COMPLETE just in case
|
|
|
|
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
*eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::push_request_headers()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
if(http2session_->get_state() != Http2Session::CONNECTED) {
|
|
|
|
// The HTTP2 session to the backend has not been established.
|
|
|
|
// This function will be called again just after it is
|
|
|
|
// established.
|
2012-11-18 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!downstream_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t nheader = downstream_->get_request_headers().size();
|
2013-11-17 16:03:55 +01:00
|
|
|
if(!get_config()->http2_no_cookie_crumbling) {
|
|
|
|
downstream_->crumble_request_cookie();
|
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
downstream_->normalize_request_headers();
|
|
|
|
auto end_headers = std::end(downstream_->get_request_headers());
|
2013-10-25 14:50:56 +02:00
|
|
|
// 12 means:
|
|
|
|
// 1. :method
|
|
|
|
// 2. :scheme
|
|
|
|
// 3. :path
|
|
|
|
// 4. :authority (optional)
|
|
|
|
// 5. via (optional)
|
|
|
|
// 6. x-forwarded-for (optional)
|
2013-10-23 16:18:24 +02:00
|
|
|
auto nv = std::vector<const char*>();
|
|
|
|
nv.reserve(nheader * 2 + 10 + 1);
|
2012-11-18 13:23:13 +01:00
|
|
|
std::string via_value;
|
2012-11-18 15:04:14 +01:00
|
|
|
std::string xff_value;
|
2013-10-25 14:50:56 +02:00
|
|
|
std::string scheme, authority, path, query;
|
2013-02-07 13:53:20 +01:00
|
|
|
if(downstream_->get_request_method() == "CONNECT") {
|
2013-10-25 14:50:56 +02:00
|
|
|
// The upstream may be HTTP/2 or HTTP/1
|
|
|
|
nv.push_back(":authority");
|
|
|
|
if(!downstream_->get_request_http2_authority().empty()) {
|
|
|
|
nv.push_back(downstream_->get_request_http2_authority().c_str());
|
|
|
|
} else {
|
|
|
|
nv.push_back(downstream_->get_request_path().c_str());
|
|
|
|
}
|
|
|
|
} else if(!downstream_->get_request_http2_scheme().empty()) {
|
|
|
|
// Here the upstream is HTTP/2
|
|
|
|
nv.push_back(":scheme");
|
|
|
|
nv.push_back(downstream_->get_request_http2_scheme().c_str());
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(":path");
|
|
|
|
nv.push_back(downstream_->get_request_path().c_str());
|
2013-10-25 14:50:56 +02:00
|
|
|
if(!downstream_->get_request_http2_authority().empty()) {
|
|
|
|
nv.push_back(":authority");
|
|
|
|
nv.push_back(downstream_->get_request_http2_authority().c_str());
|
|
|
|
} else if(downstream_->get_norm_request_header("host") == end_headers) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "host header field missing";
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-07 13:53:20 +01:00
|
|
|
} else {
|
2013-10-25 14:50:56 +02:00
|
|
|
// The upstream is HTTP/1
|
2012-11-19 13:40:59 +01:00
|
|
|
http_parser_url u;
|
|
|
|
const char *url = downstream_->get_request_path().c_str();
|
|
|
|
memset(&u, 0, sizeof(u));
|
|
|
|
rv = http_parser_parse_url(url,
|
|
|
|
downstream_->get_request_path().size(),
|
|
|
|
0, &u);
|
|
|
|
if(rv == 0) {
|
2013-08-27 19:47:22 +02:00
|
|
|
http2::copy_url_component(scheme, &u, UF_SCHEMA, url);
|
2013-10-25 14:50:56 +02:00
|
|
|
http2::copy_url_component(authority, &u, UF_HOST, url);
|
2013-08-27 19:47:22 +02:00
|
|
|
http2::copy_url_component(path, &u, UF_PATH, url);
|
|
|
|
http2::copy_url_component(query, &u, UF_QUERY, url);
|
2013-02-07 13:53:20 +01:00
|
|
|
if(path.empty()) {
|
|
|
|
path = "/";
|
|
|
|
}
|
2012-11-19 13:40:59 +01:00
|
|
|
if(!query.empty()) {
|
|
|
|
path += "?";
|
|
|
|
path += query;
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(":scheme");
|
2013-02-07 13:53:20 +01:00
|
|
|
if(scheme.empty()) {
|
2013-11-04 09:53:57 +01:00
|
|
|
// The default scheme is http. For HTTP2 upstream, the path must
|
2013-02-07 13:53:20 +01:00
|
|
|
// be absolute URI, so scheme should be provided.
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back("http");
|
2013-02-07 13:53:20 +01:00
|
|
|
} else {
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(scheme.c_str());
|
2013-02-07 13:53:20 +01:00
|
|
|
}
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(":path");
|
2013-02-07 13:53:20 +01:00
|
|
|
if(path.empty()) {
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(downstream_->get_request_path().c_str());
|
2013-02-07 13:53:20 +01:00
|
|
|
} else {
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(path.c_str());
|
2013-02-07 13:53:20 +01:00
|
|
|
}
|
2013-10-25 14:50:56 +02:00
|
|
|
if(!authority.empty()) {
|
|
|
|
// TODO properly check IPv6 numeric address
|
|
|
|
if(authority.find(":") != std::string::npos) {
|
|
|
|
authority = "[" + authority;
|
|
|
|
authority += "]";
|
|
|
|
}
|
|
|
|
if(u.field_set & (1 << UF_PORT)) {
|
|
|
|
authority += ":";
|
|
|
|
authority += util::utos(u.port);
|
|
|
|
}
|
|
|
|
nv.push_back(":authority");
|
|
|
|
nv.push_back(authority.c_str());
|
|
|
|
} else if(downstream_->get_norm_request_header("host") == end_headers) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, this) << "host header field missing";
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-19 13:40:59 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(":method");
|
|
|
|
nv.push_back(downstream_->get_request_method().c_str());
|
2013-02-07 13:53:20 +01:00
|
|
|
|
2013-10-23 16:18:24 +02:00
|
|
|
http2::copy_norm_headers_to_nv(nv, downstream_->get_request_headers());
|
2013-08-27 17:09:46 +02:00
|
|
|
|
|
|
|
bool content_length = false;
|
|
|
|
if(downstream_->get_norm_request_header("content-length") != end_headers) {
|
|
|
|
content_length = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-23 16:18:24 +02:00
|
|
|
nv.push_back("expect");
|
|
|
|
nv.push_back((*expect).second.c_str());
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2012-11-18 15:04:14 +01:00
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
bool chunked_encoding = false;
|
|
|
|
auto transfer_encoding =
|
|
|
|
downstream_->get_norm_request_header("transfer-encoding");
|
|
|
|
if(transfer_encoding != end_headers &&
|
|
|
|
util::strieq((*transfer_encoding).second.c_str(), "chunked")) {
|
|
|
|
chunked_encoding = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto xff = downstream_->get_norm_request_header("x-forwarded-for");
|
2012-11-18 15:04:14 +01:00
|
|
|
if(get_config()->add_x_forwarded_for) {
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back("x-forwarded-for");
|
2013-08-27 17:09:46 +02:00
|
|
|
if(xff != end_headers) {
|
|
|
|
xff_value = (*xff).second;
|
2012-11-18 15:04:14 +01:00
|
|
|
xff_value += ", ";
|
|
|
|
}
|
|
|
|
xff_value += downstream_->get_upstream()->get_client_handler()->
|
|
|
|
get_ipaddr();
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(xff_value.c_str());
|
2013-08-27 17:09:46 +02:00
|
|
|
} else if(xff != end_headers) {
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back("x-forwarded-for");
|
|
|
|
nv.push_back((*xff).second.c_str());
|
2012-11-18 15:04:14 +01:00
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
|
|
|
|
auto via = downstream_->get_norm_request_header("via");
|
|
|
|
if(get_config()->no_via) {
|
|
|
|
if(via != end_headers) {
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back("via");
|
|
|
|
nv.push_back((*via).second.c_str());
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(via != end_headers) {
|
|
|
|
via_value = (*via).second;
|
2013-01-09 14:01:25 +01:00
|
|
|
via_value += ", ";
|
|
|
|
}
|
|
|
|
via_value += http::create_via_header_value
|
|
|
|
(downstream_->get_request_major(), downstream_->get_request_minor());
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back("via");
|
|
|
|
nv.push_back(via_value.c_str());
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2013-10-23 16:18:24 +02:00
|
|
|
nv.push_back(nullptr);
|
2013-08-27 17:09:46 +02:00
|
|
|
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-11-18 13:23:13 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
for(size_t i = 0; nv[i]; i += 2) {
|
2012-12-09 13:36:02 +01:00
|
|
|
ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\n";
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, this) << "HTTP request headers\n" << ss.str();
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2012-11-19 13:40:59 +01:00
|
|
|
if(downstream_->get_request_method() == "CONNECT" ||
|
|
|
|
chunked_encoding || content_length) {
|
2012-11-18 13:23:13 +01:00
|
|
|
// Request-body is expected.
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_data_provider data_prd;
|
2012-11-18 13:23:13 +01:00
|
|
|
data_prd.source.ptr = this;
|
2013-11-04 09:53:57 +01:00
|
|
|
data_prd.read_callback = http2_data_read_callback;
|
2013-11-16 07:41:24 +01:00
|
|
|
rv = http2session_->submit_request(this, downstream_->get_priorty(),
|
|
|
|
nv.data(), &data_prd);
|
2012-11-18 13:23:13 +01:00
|
|
|
} else {
|
2013-11-16 07:41:24 +01:00
|
|
|
rv = http2session_->submit_request(this, downstream_->get_priorty(),
|
|
|
|
nv.data(), nullptr);
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
if(rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
DCLOG(FATAL, this) << "nghttp2_submit_request() failed";
|
2012-11-18 13:23:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2012-11-20 17:29:39 +01:00
|
|
|
return 0;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::push_upload_data_chunk(const uint8_t *data,
|
2012-11-18 13:23:13 +01:00
|
|
|
size_t datalen)
|
|
|
|
{
|
|
|
|
int rv = evbuffer_add(request_body_buf_, data, datalen);
|
|
|
|
if(rv != 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;
|
|
|
|
}
|
2012-11-18 15:48:55 +01:00
|
|
|
if(downstream_->get_downstream_stream_id() != -1) {
|
2013-11-04 09:53:57 +01:00
|
|
|
rv = http2session_->resume_data(this);
|
2012-11-18 18:11:46 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2012-11-18 18:11:46 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::end_upload_data()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
|
|
|
int rv;
|
2012-11-20 17:29:39 +01:00
|
|
|
if(downstream_->get_downstream_stream_id() != -1) {
|
2013-11-04 09:53:57 +01:00
|
|
|
rv = http2session_->resume_data(this);
|
2012-11-18 13:23:13 +01:00
|
|
|
if(rv != 0) {
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
return 0;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::resume_read(IOCtrlReason reason)
|
2012-11-21 15:47:48 +01:00
|
|
|
{
|
2013-10-29 16:51:01 +01:00
|
|
|
int rv1 = 0, rv2 = 0;
|
2013-11-04 09:53:57 +01:00
|
|
|
if(http2session_->get_state() == Http2Session::CONNECTED &&
|
|
|
|
http2session_->get_flow_control()) {
|
2013-10-29 16:51:01 +01:00
|
|
|
int32_t window_size_increment;
|
|
|
|
window_size_increment = http2::determine_window_update_transmission
|
2013-11-04 09:53:57 +01:00
|
|
|
(http2session_->get_session(), 0);
|
2013-10-29 16:51:01 +01:00
|
|
|
if(window_size_increment != -1) {
|
2013-11-04 09:53:57 +01:00
|
|
|
rv1 = http2session_->submit_window_update(nullptr, window_size_increment);
|
2013-10-29 16:51:01 +01:00
|
|
|
if(rv1 == 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2013-10-29 16:51:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(downstream_ && downstream_->get_downstream_stream_id() != -1) {
|
|
|
|
window_size_increment = http2::determine_window_update_transmission
|
2013-11-04 09:53:57 +01:00
|
|
|
(http2session_->get_session(), downstream_->get_downstream_stream_id());
|
2013-10-29 16:51:01 +01:00
|
|
|
if(window_size_increment != -1) {
|
2013-11-04 09:53:57 +01:00
|
|
|
rv2 = http2session_->submit_window_update(this, window_size_increment);
|
2013-10-29 16:51:01 +01:00
|
|
|
if(rv2 == 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session_->notify();
|
2013-10-29 16:51:01 +01:00
|
|
|
}
|
2013-10-29 16:07:35 +01:00
|
|
|
}
|
2012-11-21 15:47:48 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 16:51:01 +01:00
|
|
|
return (rv1 == 0 && rv2 == 0) ? 0 : -1;
|
2012-11-21 15:47:48 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::on_read()
|
2013-02-08 13:46:58 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2DownstreamConnection::on_write()
|
2013-02-08 13:46:58 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
evbuffer* Http2DownstreamConnection::get_request_body_buf() const
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
2012-11-20 17:29:39 +01:00
|
|
|
return request_body_buf_;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2DownstreamConnection::attach_stream_data(StreamData *sd)
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
2013-03-07 13:32:10 +01:00
|
|
|
// It is possible sd->dconn is not NULL. sd is detached when
|
|
|
|
// on_stream_close_callback. Before that, after MSG_COMPLETE is set
|
|
|
|
// to Downstream::set_response_state(), upstream's readcb is called
|
|
|
|
// and execution path eventually could reach here. Since the
|
|
|
|
// response was already handled, we just detach sd.
|
|
|
|
detach_stream_data();
|
2012-11-20 17:29:39 +01:00
|
|
|
sd_ = sd;
|
|
|
|
sd_->dconn = this;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
StreamData* Http2DownstreamConnection::detach_stream_data()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
2012-11-20 17:29:39 +01:00
|
|
|
if(sd_) {
|
|
|
|
StreamData *sd = sd_;
|
|
|
|
sd_ = 0;
|
|
|
|
sd->dconn = 0;
|
|
|
|
return sd;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
bool Http2DownstreamConnection::get_output_buffer_full()
|
2012-11-18 13:23:13 +01:00
|
|
|
{
|
2012-11-20 17:29:39 +01:00
|
|
|
if(request_body_buf_) {
|
|
|
|
return
|
|
|
|
evbuffer_get_length(request_body_buf_) >= Downstream::OUTPUT_UPPER_THRES;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|