2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2013-07-12 17:19:03 +02:00
|
|
|
* nghttp2 - HTTP/2.0 C Library
|
2012-06-04 16:48:31 +02: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_downstream.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2013-12-21 09:49:31 +01:00
|
|
|
#include "http-parser/http_parser.h"
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "shrpx_upstream.h"
|
|
|
|
#include "shrpx_client_handler.h"
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_error.h"
|
2012-06-09 16:14:00 +02:00
|
|
|
#include "shrpx_downstream_connection.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "util.h"
|
2013-12-06 16:32:14 +01:00
|
|
|
#include "http2.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
Downstream::Downstream(Upstream *upstream, int stream_id, int priority)
|
2013-12-06 15:17:38 +01:00
|
|
|
: request_bodylen_(0),
|
|
|
|
upstream_(upstream),
|
2013-07-26 12:33:25 +02:00
|
|
|
dconn_(nullptr),
|
2013-12-06 15:17:38 +01:00
|
|
|
response_body_buf_(nullptr),
|
2012-06-04 16:48:31 +02:00
|
|
|
stream_id_(stream_id),
|
|
|
|
priority_(priority),
|
2012-11-18 13:23:13 +01:00
|
|
|
downstream_stream_id_(-1),
|
2013-12-06 15:17:38 +01:00
|
|
|
response_rst_stream_error_code_(NGHTTP2_NO_ERROR),
|
2012-06-04 16:48:31 +02:00
|
|
|
request_state_(INITIAL),
|
2012-06-06 17:43:18 +02:00
|
|
|
request_major_(1),
|
|
|
|
request_minor_(1),
|
2012-06-04 16:48:31 +02:00
|
|
|
response_state_(INITIAL),
|
|
|
|
response_http_status_(0),
|
2012-06-06 17:43:18 +02:00
|
|
|
response_major_(1),
|
|
|
|
response_minor_(1),
|
2013-12-06 15:17:38 +01:00
|
|
|
upgrade_request_(false),
|
|
|
|
upgraded_(false),
|
|
|
|
chunked_request_(false),
|
|
|
|
request_connection_close_(false),
|
|
|
|
request_expect_100_continue_(false),
|
|
|
|
request_header_key_prev_(false),
|
2012-06-04 16:48:31 +02:00
|
|
|
chunked_response_(false),
|
2012-06-07 16:42:11 +02:00
|
|
|
response_connection_close_(false),
|
2013-12-06 15:17:38 +01:00
|
|
|
response_header_key_prev_(false)
|
2012-11-20 17:29:39 +01:00
|
|
|
{}
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
Downstream::~Downstream()
|
|
|
|
{
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DLOG(INFO, this) << "Deleting";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
if(response_body_buf_) {
|
|
|
|
// Passing NULL to evbuffer_free() causes segmentation fault.
|
|
|
|
evbuffer_free(response_body_buf_);
|
|
|
|
}
|
2012-06-09 16:14:00 +02:00
|
|
|
if(dconn_) {
|
|
|
|
delete dconn_;
|
|
|
|
}
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DLOG(INFO, this) << "Deleted";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
void Downstream::set_downstream_connection(DownstreamConnection *dconn)
|
2012-06-07 16:42:11 +02:00
|
|
|
{
|
2012-06-09 16:14:00 +02:00
|
|
|
dconn_ = dconn;
|
2012-06-07 16:42:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection* Downstream::get_downstream_connection()
|
2012-06-07 16:42:11 +02:00
|
|
|
{
|
2012-06-09 16:14:00 +02:00
|
|
|
return dconn_;
|
2012-06-07 16:42:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
void Downstream::pause_read(IOCtrlReason reason)
|
|
|
|
{
|
2012-11-20 17:29:39 +01:00
|
|
|
if(dconn_) {
|
|
|
|
dconn_->pause_read(reason);
|
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 13:46:58 +01:00
|
|
|
int Downstream::resume_read(IOCtrlReason reason)
|
2012-06-04 20:11:43 +02:00
|
|
|
{
|
2012-11-20 17:29:39 +01:00
|
|
|
if(dconn_) {
|
|
|
|
return dconn_->resume_read(reason);
|
|
|
|
} else {
|
2013-02-08 13:46:58 +01:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 14:39:55 +02:00
|
|
|
void Downstream::force_resume_read()
|
|
|
|
{
|
2012-11-20 17:29:39 +01:00
|
|
|
if(dconn_) {
|
|
|
|
dconn_->force_resume_read();
|
|
|
|
}
|
2012-06-06 14:39:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2012-06-09 19:33:34 +02:00
|
|
|
void check_header_field(bool *result, const Headers::value_type &item,
|
|
|
|
const char *name, const char *value)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-06-09 19:33:34 +02:00
|
|
|
if(util::strieq(item.first.c_str(), name)) {
|
|
|
|
if(util::strifind(item.second.c_str(), value)) {
|
|
|
|
*result = true;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-09 19:33:34 +02:00
|
|
|
namespace {
|
|
|
|
void check_transfer_encoding_chunked(bool *chunked,
|
|
|
|
const Headers::value_type &item)
|
|
|
|
{
|
|
|
|
return check_header_field(chunked, item, "transfer-encoding", "chunked");
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void check_expect_100_continue(bool *res,
|
|
|
|
const Headers::value_type& item)
|
|
|
|
{
|
|
|
|
return check_header_field(res, item, "expect", "100-continue");
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
namespace {
|
|
|
|
Headers::const_iterator get_norm_header(const Headers& headers,
|
|
|
|
const std::string& name)
|
|
|
|
{
|
|
|
|
auto i = std::lower_bound(std::begin(headers), std::end(headers),
|
2014-01-16 15:41:13 +01:00
|
|
|
std::make_pair(name, ""), http2::name_less);
|
2013-08-27 17:09:46 +02:00
|
|
|
if(i != std::end(headers) && (*i).first == name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return std::end(headers);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-12-21 09:49:31 +01:00
|
|
|
namespace {
|
2014-01-16 15:41:13 +01:00
|
|
|
Headers::iterator get_norm_header(Headers& headers, const std::string& name)
|
2013-12-21 09:49:31 +01:00
|
|
|
{
|
|
|
|
auto i = std::lower_bound(std::begin(headers), std::end(headers),
|
2014-01-16 15:41:13 +01:00
|
|
|
std::make_pair(name, ""), http2::name_less);
|
2013-12-21 09:49:31 +01:00
|
|
|
if(i != std::end(headers) && (*i).first == name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return std::end(headers);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
const Headers& Downstream::get_request_headers() const
|
|
|
|
{
|
|
|
|
return request_headers_;
|
|
|
|
}
|
|
|
|
|
2013-11-16 13:15:55 +01:00
|
|
|
void Downstream::assemble_request_cookie()
|
|
|
|
{
|
|
|
|
std::string& cookie = assembled_request_cookie_;
|
|
|
|
cookie = "";
|
|
|
|
for(auto& kv : request_headers_) {
|
|
|
|
if(util::strieq("cookie", kv.first.c_str())) {
|
|
|
|
auto end = kv.second.find_last_not_of(" ;");
|
|
|
|
if(end == std::string::npos) {
|
|
|
|
cookie += kv.second;
|
|
|
|
} else {
|
|
|
|
cookie.append(std::begin(kv.second), std::begin(kv.second) + end + 1);
|
|
|
|
}
|
|
|
|
cookie += "; ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cookie.size() >= 2) {
|
|
|
|
cookie.erase(cookie.size() - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::crumble_request_cookie()
|
|
|
|
{
|
|
|
|
Headers cookie_hdrs;
|
|
|
|
for(auto& kv : request_headers_) {
|
|
|
|
if(util::strieq("cookie", kv.first.c_str())) {
|
|
|
|
size_t last = kv.second.size();
|
|
|
|
size_t num = 0;
|
|
|
|
std::string rep_cookie;
|
|
|
|
|
|
|
|
for(size_t j = 0; j < last;) {
|
|
|
|
j = kv.second.find_first_not_of("\t ;", j);
|
|
|
|
if(j == std::string::npos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto first = j;
|
|
|
|
|
|
|
|
j = kv.second.find(';', j);
|
|
|
|
if(j == std::string::npos) {
|
|
|
|
j = last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(num == 0) {
|
2013-12-22 07:42:12 +01:00
|
|
|
if(first == 0 && j == last) {
|
|
|
|
break;
|
|
|
|
}
|
2013-11-16 13:15:55 +01:00
|
|
|
rep_cookie = kv.second.substr(first, j - first);
|
|
|
|
} else {
|
|
|
|
cookie_hdrs.push_back
|
|
|
|
(std::make_pair("cookie", kv.second.substr(first, j - first)));
|
|
|
|
}
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
if(num > 0) {
|
|
|
|
kv.second = std::move(rep_cookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request_headers_.insert(std::end(request_headers_),
|
|
|
|
std::begin(cookie_hdrs), std::end(cookie_hdrs));
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Downstream::get_assembled_request_cookie() const
|
|
|
|
{
|
|
|
|
return assembled_request_cookie_;
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
void Downstream::normalize_request_headers()
|
|
|
|
{
|
2014-01-16 15:41:13 +01:00
|
|
|
http2::normalize_headers(request_headers_);
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Headers::const_iterator Downstream::get_norm_request_header
|
|
|
|
(const std::string& name) const
|
|
|
|
{
|
|
|
|
return get_norm_header(request_headers_, name);
|
|
|
|
}
|
|
|
|
|
2013-12-21 09:49:31 +01:00
|
|
|
void Downstream::concat_norm_request_headers()
|
|
|
|
{
|
|
|
|
request_headers_ = http2::concat_norm_headers(std::move(request_headers_));
|
|
|
|
}
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void Downstream::add_request_header(std::string name, std::string value)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-07-11 11:32:04 +02:00
|
|
|
request_header_key_prev_ = true;
|
2013-09-24 16:53:55 +02:00
|
|
|
request_headers_.emplace_back(std::move(name), std::move(value));
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void Downstream::set_last_request_header_value(std::string value)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-07-11 11:32:04 +02:00
|
|
|
request_header_key_prev_ = false;
|
2012-06-04 16:48:31 +02:00
|
|
|
Headers::value_type &item = request_headers_.back();
|
2013-09-24 16:53:55 +02:00
|
|
|
item.second = std::move(value);
|
2012-06-04 16:48:31 +02:00
|
|
|
check_transfer_encoding_chunked(&chunked_request_, item);
|
2012-06-09 19:33:34 +02:00
|
|
|
check_expect_100_continue(&request_expect_100_continue_, item);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
void Downstream::split_add_request_header
|
|
|
|
(const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen)
|
|
|
|
{
|
|
|
|
http2::split_add_header(request_headers_, name, namelen, value, valuelen);
|
|
|
|
}
|
|
|
|
|
2012-07-11 11:32:04 +02:00
|
|
|
bool Downstream::get_request_header_key_prev() const
|
|
|
|
{
|
|
|
|
return request_header_key_prev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::append_last_request_header_key(const char *data, size_t len)
|
|
|
|
{
|
|
|
|
assert(request_header_key_prev_);
|
2013-09-24 16:53:55 +02:00
|
|
|
auto& item = request_headers_.back();
|
2012-07-11 11:32:04 +02:00
|
|
|
item.first.append(data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::append_last_request_header_value(const char *data, size_t len)
|
|
|
|
{
|
|
|
|
assert(!request_header_key_prev_);
|
2013-09-24 16:53:55 +02:00
|
|
|
auto& item = request_headers_.back();
|
2012-07-11 11:32:04 +02:00
|
|
|
item.second.append(data, len);
|
|
|
|
}
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void Downstream::set_request_method(std::string method)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-09-24 16:53:55 +02:00
|
|
|
request_method_ = std::move(method);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 09:20:16 +02:00
|
|
|
const std::string& Downstream::get_request_method() const
|
|
|
|
{
|
|
|
|
return request_method_;
|
|
|
|
}
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void Downstream::set_request_path(std::string path)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-09-24 16:53:55 +02:00
|
|
|
request_path_ = std::move(path);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 11:32:04 +02:00
|
|
|
void Downstream::append_request_path(const char *data, size_t len)
|
|
|
|
{
|
|
|
|
request_path_.append(data, len);
|
|
|
|
}
|
|
|
|
|
2012-07-11 09:20:16 +02:00
|
|
|
const std::string& Downstream::get_request_path() const
|
|
|
|
{
|
|
|
|
return request_path_;
|
|
|
|
}
|
|
|
|
|
2013-10-25 14:50:56 +02:00
|
|
|
const std::string& Downstream::get_request_http2_scheme() const
|
|
|
|
{
|
|
|
|
return request_http2_scheme_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_request_http2_scheme(std::string scheme)
|
|
|
|
{
|
|
|
|
request_http2_scheme_ = std::move(scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Downstream::get_request_http2_authority() const
|
|
|
|
{
|
|
|
|
return request_http2_authority_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_request_http2_authority(std::string authority)
|
|
|
|
{
|
|
|
|
request_http2_authority_ = std::move(authority);
|
|
|
|
}
|
|
|
|
|
2012-06-06 17:43:18 +02:00
|
|
|
void Downstream::set_request_major(int major)
|
|
|
|
{
|
|
|
|
request_major_ = major;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_request_minor(int minor)
|
|
|
|
{
|
|
|
|
request_minor_ = minor;
|
|
|
|
}
|
|
|
|
|
2012-06-07 15:40:42 +02:00
|
|
|
int Downstream::get_request_major() const
|
|
|
|
{
|
|
|
|
return request_major_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::get_request_minor() const
|
|
|
|
{
|
|
|
|
return request_minor_;
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
void Downstream::reset_upstream(Upstream* upstream)
|
|
|
|
{
|
|
|
|
upstream_ = upstream;
|
|
|
|
if(dconn_) {
|
|
|
|
dconn_->on_upstream_change(upstream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
Upstream* Downstream::get_upstream() const
|
|
|
|
{
|
|
|
|
return upstream_;
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
void Downstream::set_stream_id(int32_t stream_id)
|
|
|
|
{
|
|
|
|
stream_id_ = stream_id;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
int32_t Downstream::get_stream_id() const
|
|
|
|
{
|
|
|
|
return stream_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_request_state(int state)
|
|
|
|
{
|
|
|
|
request_state_ = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::get_request_state() const
|
|
|
|
{
|
|
|
|
return request_state_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Downstream::get_chunked_request() const
|
|
|
|
{
|
|
|
|
return chunked_request_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Downstream::get_request_connection_close() const
|
|
|
|
{
|
|
|
|
return request_connection_close_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_request_connection_close(bool f)
|
|
|
|
{
|
|
|
|
request_connection_close_ = f;
|
|
|
|
}
|
|
|
|
|
2012-06-09 19:33:34 +02:00
|
|
|
bool Downstream::get_expect_100_continue() const
|
|
|
|
{
|
|
|
|
return request_expect_100_continue_;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:49:33 +02:00
|
|
|
bool Downstream::get_output_buffer_full()
|
|
|
|
{
|
|
|
|
if(dconn_) {
|
2012-11-20 17:29:39 +01:00
|
|
|
return dconn_->get_output_buffer_full();
|
2012-06-09 17:49:33 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 16:02:01 +02:00
|
|
|
// Call this function after this object is attached to
|
|
|
|
// Downstream. Otherwise, the program will crash.
|
2012-06-04 16:48:31 +02:00
|
|
|
int Downstream::push_request_headers()
|
|
|
|
{
|
2013-11-20 18:06:28 +01:00
|
|
|
if(!dconn_) {
|
|
|
|
DLOG(INFO, this) << "dconn_ is NULL";
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
return dconn_->push_request_headers();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::push_upload_data_chunk(const uint8_t *data, size_t datalen)
|
|
|
|
{
|
|
|
|
// Assumes that request headers have already been pushed to output
|
|
|
|
// buffer using push_request_headers().
|
2012-06-09 17:49:33 +02:00
|
|
|
if(!dconn_) {
|
2013-10-29 17:26:28 +01:00
|
|
|
DLOG(INFO, this) << "dconn_ is NULL";
|
2013-08-03 16:45:28 +02:00
|
|
|
return -1;
|
2012-06-09 17:49:33 +02:00
|
|
|
}
|
2013-08-03 11:51:01 +02:00
|
|
|
request_bodylen_ += datalen;
|
2012-11-18 13:23:13 +01:00
|
|
|
return dconn_->push_upload_data_chunk(data, datalen);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::end_upload_data()
|
|
|
|
{
|
2013-11-20 18:06:28 +01:00
|
|
|
if(!dconn_) {
|
|
|
|
DLOG(INFO, this) << "dconn_ is NULL";
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
return dconn_->end_upload_data();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Headers& Downstream::get_response_headers() const
|
|
|
|
{
|
|
|
|
return response_headers_;
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
void Downstream::normalize_response_headers()
|
|
|
|
{
|
2014-01-16 15:41:13 +01:00
|
|
|
http2::normalize_headers(response_headers_);
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 16:32:14 +01:00
|
|
|
void Downstream::concat_norm_response_headers()
|
|
|
|
{
|
|
|
|
response_headers_ = http2::concat_norm_headers(std::move(response_headers_));
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
Headers::const_iterator Downstream::get_norm_response_header
|
|
|
|
(const std::string& name) const
|
|
|
|
{
|
|
|
|
return get_norm_header(response_headers_, name);
|
|
|
|
}
|
|
|
|
|
2013-12-21 09:49:31 +01:00
|
|
|
void Downstream::rewrite_norm_location_response_header
|
|
|
|
(const std::string& upstream_scheme,
|
2013-12-21 10:35:53 +01:00
|
|
|
uint16_t upstream_port)
|
2013-12-21 09:49:31 +01:00
|
|
|
{
|
|
|
|
auto hd = get_norm_header(response_headers_, "location");
|
|
|
|
if(hd == std::end(response_headers_)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
http_parser_url u;
|
|
|
|
int rv = http_parser_parse_url((*hd).second.c_str(), (*hd).second.size(),
|
|
|
|
0, &u);
|
|
|
|
if(rv != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string new_uri;
|
|
|
|
if(!request_http2_authority_.empty()) {
|
|
|
|
new_uri = http2::rewrite_location_uri((*hd).second, u,
|
|
|
|
request_http2_authority_,
|
2013-12-21 10:35:53 +01:00
|
|
|
upstream_scheme, upstream_port);
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
|
|
|
if(new_uri.empty()) {
|
|
|
|
auto host = get_norm_request_header("host");
|
|
|
|
if(host == std::end(request_headers_)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_uri = http2::rewrite_location_uri((*hd).second, u, (*host).second,
|
2013-12-21 10:35:53 +01:00
|
|
|
upstream_scheme, upstream_port);
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
|
|
|
if(!new_uri.empty()) {
|
|
|
|
(*hd).second = std::move(new_uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void Downstream::add_response_header(std::string name, std::string value)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-07-11 11:32:04 +02:00
|
|
|
response_header_key_prev_ = true;
|
2013-09-24 16:53:55 +02:00
|
|
|
response_headers_.emplace_back(std::move(name), std::move(value));
|
2012-11-18 13:23:13 +01:00
|
|
|
check_transfer_encoding_chunked(&chunked_response_,
|
|
|
|
response_headers_.back());
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void Downstream::set_last_response_header_value(std::string value)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-07-11 11:32:04 +02:00
|
|
|
response_header_key_prev_ = false;
|
2013-09-24 16:53:55 +02:00
|
|
|
auto& item = response_headers_.back();
|
|
|
|
item.second = std::move(value);
|
2012-06-04 16:48:31 +02:00
|
|
|
check_transfer_encoding_chunked(&chunked_response_, item);
|
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
void Downstream::split_add_response_header
|
|
|
|
(const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen)
|
|
|
|
{
|
|
|
|
http2::split_add_header(response_headers_, name, namelen, value, valuelen);
|
|
|
|
}
|
|
|
|
|
2012-07-11 11:32:04 +02:00
|
|
|
bool Downstream::get_response_header_key_prev() const
|
|
|
|
{
|
|
|
|
return response_header_key_prev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::append_last_response_header_key(const char *data, size_t len)
|
|
|
|
{
|
|
|
|
assert(response_header_key_prev_);
|
2013-09-24 16:53:55 +02:00
|
|
|
auto& item = response_headers_.back();
|
2012-07-11 11:32:04 +02:00
|
|
|
item.first.append(data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::append_last_response_header_value(const char *data,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
assert(!response_header_key_prev_);
|
2013-09-24 16:53:55 +02:00
|
|
|
auto& item = response_headers_.back();
|
2012-07-11 11:32:04 +02:00
|
|
|
item.second.append(data, len);
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
unsigned int Downstream::get_response_http_status() const
|
|
|
|
{
|
|
|
|
return response_http_status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_response_http_status(unsigned int status)
|
|
|
|
{
|
|
|
|
response_http_status_ = status;
|
|
|
|
}
|
|
|
|
|
2012-06-06 17:43:18 +02:00
|
|
|
void Downstream::set_response_major(int major)
|
|
|
|
{
|
|
|
|
response_major_ = major;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_response_minor(int minor)
|
|
|
|
{
|
|
|
|
response_minor_ = minor;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::get_response_major() const
|
|
|
|
{
|
|
|
|
return response_major_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::get_response_minor() const
|
|
|
|
{
|
|
|
|
return response_minor_;
|
|
|
|
}
|
|
|
|
|
2012-07-11 09:20:16 +02:00
|
|
|
int Downstream::get_response_version() const
|
|
|
|
{
|
|
|
|
return response_major_*100+response_minor_;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
bool Downstream::get_chunked_response() const
|
|
|
|
{
|
|
|
|
return chunked_response_;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
void Downstream::set_chunked_response(bool f)
|
|
|
|
{
|
|
|
|
chunked_response_ = f;
|
|
|
|
}
|
|
|
|
|
2012-06-07 16:42:11 +02:00
|
|
|
bool Downstream::get_response_connection_close() const
|
|
|
|
{
|
|
|
|
return response_connection_close_;
|
|
|
|
}
|
|
|
|
|
2012-07-11 09:20:16 +02:00
|
|
|
void Downstream::set_response_connection_close(bool f)
|
|
|
|
{
|
|
|
|
response_connection_close_ = f;
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
int Downstream::on_read()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-11-20 18:06:28 +01:00
|
|
|
if(!dconn_) {
|
|
|
|
DLOG(INFO, this) << "dconn_ is NULL";
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
return dconn_->on_read();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_response_state(int state)
|
|
|
|
{
|
|
|
|
response_state_ = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Downstream::get_response_state() const
|
|
|
|
{
|
|
|
|
return response_state_;
|
|
|
|
}
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
namespace {
|
|
|
|
void body_buf_cb(evbuffer *body, size_t oldlen, size_t newlen, void *arg)
|
|
|
|
{
|
|
|
|
Downstream *downstream = reinterpret_cast<Downstream*>(arg);
|
|
|
|
if(newlen == 0) {
|
2013-02-08 13:46:58 +01:00
|
|
|
if(downstream->resume_read(SHRPX_NO_BUFFER) == -1) {
|
|
|
|
DLOG(WARNING, downstream) << "Sending WINDOW_UPDATE failed";
|
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
int Downstream::init_response_body_buf()
|
|
|
|
{
|
2012-06-06 14:39:55 +02:00
|
|
|
if(!response_body_buf_) {
|
|
|
|
response_body_buf_ = evbuffer_new();
|
2013-12-20 15:28:54 +01:00
|
|
|
if(response_body_buf_ == nullptr) {
|
2012-06-06 14:39:55 +02:00
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
evbuffer_setcb(response_body_buf_, body_buf_cb, this);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
evbuffer* Downstream::get_response_body_buf()
|
|
|
|
{
|
|
|
|
return response_body_buf_;
|
|
|
|
}
|
|
|
|
|
2013-11-16 07:41:24 +01:00
|
|
|
void Downstream::set_priority(int32_t pri)
|
2012-06-07 17:36:19 +02:00
|
|
|
{
|
|
|
|
priority_ = pri;
|
|
|
|
}
|
|
|
|
|
2013-11-16 07:41:24 +01:00
|
|
|
int32_t Downstream::get_priorty() const
|
|
|
|
{
|
|
|
|
return priority_;
|
|
|
|
}
|
|
|
|
|
2013-07-31 14:48:37 +02:00
|
|
|
void Downstream::check_upgrade_fulfilled()
|
2012-07-11 09:20:16 +02:00
|
|
|
{
|
2013-07-31 14:48:37 +02:00
|
|
|
if(request_method_ == "CONNECT") {
|
|
|
|
upgraded_ = 200 <= response_http_status_ && response_http_status_ < 300;
|
|
|
|
} else {
|
|
|
|
// TODO Do more strict checking for upgrade headers
|
2013-08-03 11:51:01 +02:00
|
|
|
if(response_http_status_ == 101) {
|
|
|
|
for(auto& hd : request_headers_) {
|
|
|
|
if(util::strieq("upgrade", hd.first.c_str())) {
|
|
|
|
upgraded_ = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Downstream::get_upgraded() const
|
|
|
|
{
|
|
|
|
return upgraded_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::check_upgrade_request()
|
|
|
|
{
|
|
|
|
if(request_method_ == "CONNECT") {
|
|
|
|
upgrade_request_ = true;
|
|
|
|
} else {
|
|
|
|
// TODO Do more strict checking for upgrade headers
|
|
|
|
for(auto& hd : request_headers_) {
|
|
|
|
if(util::strieq("upgrade", hd.first.c_str())) {
|
|
|
|
upgrade_request_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Downstream::get_upgrade_request() const
|
|
|
|
{
|
|
|
|
return upgrade_request_;
|
2012-07-11 09:20:16 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
bool Downstream::http2_upgrade_request() const
|
|
|
|
{
|
|
|
|
if(request_bodylen_ != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool upgrade_seen = false;
|
|
|
|
bool http2_settings_seen = false;
|
|
|
|
for(auto& hd : request_headers_) {
|
|
|
|
// For now just check NGHTTP2_PROTO_VERSION_ID in Upgrade header
|
|
|
|
// field and existence of HTTP2-Settings header field.
|
|
|
|
if(util::strieq(hd.first.c_str(), "upgrade")) {
|
|
|
|
if(util::strieq(hd.second.c_str(), NGHTTP2_PROTO_VERSION_ID)) {
|
|
|
|
upgrade_seen = true;
|
|
|
|
}
|
|
|
|
} else if(util::strieq(hd.first.c_str(), "http2-settings")) {
|
|
|
|
http2_settings_seen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return upgrade_seen && http2_settings_seen;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
void Downstream::set_downstream_stream_id(int32_t stream_id)
|
|
|
|
{
|
|
|
|
downstream_stream_id_ = stream_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Downstream::get_downstream_stream_id() const
|
|
|
|
{
|
|
|
|
return downstream_stream_id_;
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
nghttp2_error_code Downstream::get_response_rst_stream_error_code() const
|
2013-02-27 14:39:44 +01:00
|
|
|
{
|
2013-07-26 12:33:25 +02:00
|
|
|
return response_rst_stream_error_code_;
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
void Downstream::set_response_rst_stream_error_code
|
|
|
|
(nghttp2_error_code error_code)
|
2013-02-27 14:39:44 +01:00
|
|
|
{
|
2013-07-26 12:33:25 +02:00
|
|
|
response_rst_stream_error_code_ = error_code;
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|