2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 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 {
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
namespace {
|
|
|
|
void upstream_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto downstream = static_cast<Downstream *>(w->data);
|
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
|
|
|
|
auto which = revents == EV_READ ? "read" : "write";
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, downstream) << "upstream timeout stream_id="
|
|
|
|
<< downstream->get_stream_id() << " event=" << which;
|
|
|
|
}
|
|
|
|
|
|
|
|
downstream->disable_upstream_rtimer();
|
|
|
|
downstream->disable_upstream_wtimer();
|
|
|
|
|
|
|
|
upstream->on_timeout(downstream);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void upstream_rtimeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
upstream_timeoutcb(loop, w, EV_READ);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void upstream_wtimeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
upstream_timeoutcb(loop, w, EV_WRITE);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void downstream_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto downstream = static_cast<Downstream *>(w->data);
|
|
|
|
|
|
|
|
auto which = revents == EV_READ ? "read" : "write";
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, downstream) << "downstream timeout stream_id="
|
|
|
|
<< downstream->get_downstream_stream_id()
|
|
|
|
<< " event=" << which;
|
|
|
|
}
|
|
|
|
|
|
|
|
downstream->disable_downstream_rtimer();
|
|
|
|
downstream->disable_downstream_wtimer();
|
|
|
|
|
|
|
|
auto dconn = downstream->get_downstream_connection();
|
|
|
|
|
|
|
|
if (dconn) {
|
|
|
|
dconn->on_timeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void downstream_rtimeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
downstream_timeoutcb(loop, w, EV_READ);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void downstream_wtimeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
downstream_timeoutcb(loop, w, EV_WRITE);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-01-02 03:16:37 +01:00
|
|
|
// upstream could be nullptr for unittests
|
2014-08-18 15:59:31 +02:00
|
|
|
Downstream::Downstream(Upstream *upstream, int32_t stream_id, int32_t priority)
|
2015-01-21 14:28:15 +01:00
|
|
|
: request_start_time_(std::chrono::high_resolution_clock::now()),
|
|
|
|
request_buf_(upstream ? upstream->get_mcpool() : nullptr),
|
2015-01-02 03:16:37 +01:00
|
|
|
response_buf_(upstream ? upstream->get_mcpool() : nullptr),
|
|
|
|
request_bodylen_(0), response_bodylen_(0), response_sent_bodylen_(0),
|
2015-01-17 13:31:28 +01:00
|
|
|
request_content_length_(-1), response_content_length_(-1),
|
|
|
|
upstream_(upstream), request_headers_sum_(0), response_headers_sum_(0),
|
2015-02-02 17:47:04 +01:00
|
|
|
request_datalen_(0), response_datalen_(0), num_retry_(0),
|
|
|
|
stream_id_(stream_id), priority_(priority), downstream_stream_id_(-1),
|
2014-11-27 15:39:04 +01:00
|
|
|
response_rst_stream_error_code_(NGHTTP2_NO_ERROR),
|
|
|
|
request_state_(INITIAL), request_major_(1), request_minor_(1),
|
|
|
|
response_state_(INITIAL), response_http_status_(0), response_major_(1),
|
|
|
|
response_minor_(1), upgrade_request_(false), upgraded_(false),
|
2015-01-04 15:22:39 +01:00
|
|
|
http2_upgrade_seen_(false), chunked_request_(false),
|
|
|
|
request_connection_close_(false), request_header_key_prev_(false),
|
|
|
|
request_http2_expect_body_(false), chunked_response_(false),
|
|
|
|
response_connection_close_(false), response_header_key_prev_(false),
|
|
|
|
expect_final_response_(false) {
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
ev_timer_init(&upstream_rtimer_, &upstream_rtimeoutcb, 0.,
|
|
|
|
get_config()->stream_read_timeout);
|
|
|
|
ev_timer_init(&upstream_wtimer_, &upstream_wtimeoutcb, 0.,
|
|
|
|
get_config()->stream_write_timeout);
|
|
|
|
ev_timer_init(&downstream_rtimer_, &downstream_rtimeoutcb, 0.,
|
|
|
|
get_config()->stream_read_timeout);
|
|
|
|
ev_timer_init(&downstream_wtimer_, &downstream_wtimeoutcb, 0.,
|
|
|
|
get_config()->stream_write_timeout);
|
|
|
|
|
|
|
|
upstream_rtimer_.data = this;
|
|
|
|
upstream_wtimer_.data = this;
|
|
|
|
downstream_rtimer_.data = this;
|
|
|
|
downstream_wtimer_.data = this;
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
http2::init_hdidx(request_hdidx_);
|
|
|
|
http2::init_hdidx(response_hdidx_);
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
Downstream::~Downstream() {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DLOG(INFO, this) << "Deleting";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-02 03:16:37 +01:00
|
|
|
// check nullptr for unittest
|
|
|
|
if (upstream_) {
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
|
|
|
|
ev_timer_stop(loop, &upstream_rtimer_);
|
|
|
|
ev_timer_stop(loop, &upstream_wtimer_);
|
|
|
|
ev_timer_stop(loop, &downstream_rtimer_);
|
|
|
|
ev_timer_stop(loop, &downstream_wtimer_);
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2014-11-27 15:39:04 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::attach_downstream_connection(
|
|
|
|
std::unique_ptr<DownstreamConnection> dconn) {
|
|
|
|
if (dconn->attach_downstream(this) != 0) {
|
2014-08-18 17:16:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dconn_ = std::move(dconn);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::detach_downstream_connection() {
|
|
|
|
if (!dconn_) {
|
2014-08-18 17:16:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dconn_->detach_downstream(this);
|
|
|
|
|
|
|
|
auto handler = dconn_->get_client_handler();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
handler->pool_downstream_connection(
|
|
|
|
std::unique_ptr<DownstreamConnection>(dconn_.release()));
|
2014-08-18 17:16:51 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::release_downstream_connection() { dconn_.release(); }
|
2012-06-07 16:42:11 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
DownstreamConnection *Downstream::get_downstream_connection() {
|
2014-08-18 17:16:51 +02:00
|
|
|
return dconn_.get();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
std::unique_ptr<DownstreamConnection> Downstream::pop_downstream_connection() {
|
2014-08-18 17:16:51 +02:00
|
|
|
return std::unique_ptr<DownstreamConnection>(dconn_.release());
|
2012-06-07 16:42:11 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::pause_read(IOCtrlReason reason) {
|
|
|
|
if (dconn_) {
|
2012-11-20 17:29:39 +01:00
|
|
|
dconn_->pause_read(reason);
|
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::resume_read(IOCtrlReason reason, size_t consumed) {
|
|
|
|
if (dconn_) {
|
2014-08-21 14:22:16 +02:00
|
|
|
return dconn_->resume_read(reason, consumed);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2014-08-21 14:22:16 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::force_resume_read() {
|
|
|
|
if (dconn_) {
|
2012-11-20 17:29:39 +01:00
|
|
|
dconn_->force_resume_read();
|
|
|
|
}
|
2012-06-06 14:39:55 +02:00
|
|
|
}
|
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
namespace {
|
2015-01-04 15:22:39 +01:00
|
|
|
const Headers::value_type *get_header_linear(const Headers &headers,
|
|
|
|
const std::string &name) {
|
|
|
|
const Headers::value_type *res = nullptr;
|
|
|
|
for (auto &kv : headers) {
|
|
|
|
if (kv.name == name) {
|
|
|
|
res = &kv;
|
|
|
|
}
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
return res;
|
2014-11-18 16:56:44 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const Headers &Downstream::get_request_headers() const {
|
2012-11-18 13:23:13 +01:00
|
|
|
return request_headers_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::assemble_request_cookie() {
|
|
|
|
std::string &cookie = assembled_request_cookie_;
|
2013-11-16 13:15:55 +01:00
|
|
|
cookie = "";
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &kv : request_headers_) {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (kv.name.size() != 6 || kv.name[5] != 'e' ||
|
|
|
|
!util::streq("cooki", kv.name.c_str(), 5)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto end = kv.value.find_last_not_of(" ;");
|
|
|
|
if (end == std::string::npos) {
|
|
|
|
cookie += kv.value;
|
|
|
|
} else {
|
|
|
|
cookie.append(std::begin(kv.value), std::begin(kv.value) + end + 1);
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
cookie += "; ";
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (cookie.size() >= 2) {
|
2013-11-16 13:15:55 +01:00
|
|
|
cookie.erase(cookie.size() - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
Headers Downstream::crumble_request_cookie() {
|
2013-11-16 13:15:55 +01:00
|
|
|
Headers cookie_hdrs;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &kv : request_headers_) {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (kv.name.size() != 6 || kv.name[5] != 'e' ||
|
|
|
|
!util::streq("cooki", kv.name.c_str(), 5)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t last = kv.value.size();
|
|
|
|
|
|
|
|
for (size_t j = 0; j < last;) {
|
|
|
|
j = kv.value.find_first_not_of("\t ;", j);
|
|
|
|
if (j == std::string::npos) {
|
|
|
|
break;
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
auto first = j;
|
|
|
|
|
|
|
|
j = kv.value.find(';', j);
|
|
|
|
if (j == std::string::npos) {
|
|
|
|
j = last;
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
cookie_hdrs.push_back(
|
|
|
|
Header("cookie", kv.value.substr(first, j - first), kv.no_index));
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
return cookie_hdrs;
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &Downstream::get_assembled_request_cookie() const {
|
2013-11-16 13:15:55 +01:00
|
|
|
return assembled_request_cookie_;
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:11:54 +01:00
|
|
|
namespace {
|
2015-02-05 15:06:56 +01:00
|
|
|
int index_headers(http2::HeaderIndex &hdidx, Headers &headers,
|
|
|
|
int64_t &content_length) {
|
2015-01-20 15:11:54 +01:00
|
|
|
for (size_t i = 0; i < headers.size(); ++i) {
|
|
|
|
auto &kv = headers[i];
|
2015-01-04 15:22:39 +01:00
|
|
|
util::inp_strlower(kv.name);
|
2015-01-20 14:55:01 +01:00
|
|
|
|
|
|
|
auto token = http2::lookup_token(
|
|
|
|
reinterpret_cast<const uint8_t *>(kv.name.c_str()), kv.name.size());
|
|
|
|
if (token < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
kv.token = token;
|
2015-01-20 15:11:54 +01:00
|
|
|
http2::index_header(hdidx, token, i);
|
2015-01-20 14:55:01 +01:00
|
|
|
|
|
|
|
if (token == http2::HD_CONTENT_LENGTH) {
|
|
|
|
auto len = util::parse_uint(kv.value);
|
|
|
|
if (len == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-23 16:07:28 +01:00
|
|
|
if (content_length != -1) {
|
2015-01-20 14:55:01 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-20 15:11:54 +01:00
|
|
|
content_length = len;
|
2015-01-20 14:55:01 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
}
|
2015-01-20 14:55:01 +01:00
|
|
|
return 0;
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
2015-01-20 15:11:54 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int Downstream::index_request_headers() {
|
|
|
|
return index_headers(request_hdidx_, request_headers_,
|
|
|
|
request_content_length_);
|
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
const Headers::value_type *Downstream::get_request_header(int16_t token) const {
|
2015-01-04 15:22:39 +01:00
|
|
|
return http2::get_header(request_hdidx_, token, request_headers_);
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
const Headers::value_type *
|
2014-11-27 15:39:04 +01:00
|
|
|
Downstream::get_request_header(const std::string &name) const {
|
2014-11-18 16:56:44 +01:00
|
|
|
return get_header_linear(request_headers_, name);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::add_request_header(std::string name, std::string value) {
|
2012-07-11 11:32:04 +02:00
|
|
|
request_header_key_prev_ = true;
|
2014-01-27 17:17:54 +01:00
|
|
|
request_headers_sum_ += name.size() + value.size();
|
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
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_last_request_header_value(std::string value) {
|
2012-07-11 11:32:04 +02:00
|
|
|
request_header_key_prev_ = false;
|
2014-01-27 17:17:54 +01:00
|
|
|
request_headers_sum_ += value.size();
|
2012-06-04 16:48:31 +02:00
|
|
|
Headers::value_type &item = request_headers_.back();
|
2014-04-03 04:22:11 +02:00
|
|
|
item.value = std::move(value);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
void Downstream::add_request_header(const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
bool no_index, int16_t token) {
|
2015-01-04 15:22:39 +01:00
|
|
|
http2::index_header(request_hdidx_, token, request_headers_.size());
|
2014-01-27 17:17:54 +01:00
|
|
|
request_headers_sum_ += namelen + valuelen;
|
2015-02-08 06:07:01 +01:00
|
|
|
http2::add_header(request_headers_, name, namelen, value, valuelen, no_index,
|
|
|
|
token);
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_request_header_key_prev() const {
|
2012-07-11 11:32:04 +02:00
|
|
|
return request_header_key_prev_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::append_last_request_header_key(const char *data, size_t len) {
|
2012-07-11 11:32:04 +02:00
|
|
|
assert(request_header_key_prev_);
|
2014-01-27 17:17:54 +01:00
|
|
|
request_headers_sum_ += len;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &item = request_headers_.back();
|
2014-04-03 04:22:11 +02:00
|
|
|
item.name.append(data, len);
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::append_last_request_header_value(const char *data,
|
|
|
|
size_t len) {
|
2012-07-11 11:32:04 +02:00
|
|
|
assert(!request_header_key_prev_);
|
2014-01-27 17:17:54 +01:00
|
|
|
request_headers_sum_ += len;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &item = request_headers_.back();
|
2014-04-03 04:22:11 +02:00
|
|
|
item.value.append(data, len);
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
void Downstream::clear_request_headers() {
|
|
|
|
Headers().swap(request_headers_);
|
|
|
|
http2::init_hdidx(request_hdidx_);
|
|
|
|
}
|
2014-06-15 09:14:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t Downstream::get_request_headers_sum() const {
|
2014-01-27 17:17:54 +01:00
|
|
|
return request_headers_sum_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_method(std::string method) {
|
2013-09-24 16:53:55 +02:00
|
|
|
request_method_ = std::move(method);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &Downstream::get_request_method() const {
|
2012-07-11 09:20:16 +02:00
|
|
|
return request_method_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_path(std::string path) {
|
2013-09-24 16:53:55 +02:00
|
|
|
request_path_ = std::move(path);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::append_request_path(const char *data, size_t len) {
|
2012-07-11 11:32:04 +02:00
|
|
|
request_path_.append(data, len);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &Downstream::get_request_path() const {
|
2012-07-11 09:20:16 +02:00
|
|
|
return request_path_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_start_time(
|
|
|
|
std::chrono::high_resolution_clock::time_point time) {
|
2014-11-19 17:53:30 +01:00
|
|
|
request_start_time_ = std::move(time);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::chrono::high_resolution_clock::time_point &
|
|
|
|
Downstream::get_request_start_time() const {
|
2014-11-19 17:53:30 +01:00
|
|
|
return request_start_time_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &Downstream::get_request_http2_scheme() const {
|
2013-10-25 14:50:56 +02:00
|
|
|
return request_http2_scheme_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_http2_scheme(std::string scheme) {
|
2013-10-25 14:50:56 +02:00
|
|
|
request_http2_scheme_ = std::move(scheme);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &Downstream::get_request_http2_authority() const {
|
2013-10-25 14:50:56 +02:00
|
|
|
return request_http2_authority_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_http2_authority(std::string authority) {
|
2013-10-25 14:50:56 +02:00
|
|
|
request_http2_authority_ = std::move(authority);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_major(int major) { request_major_ = major; }
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_minor(int minor) { request_minor_ = minor; }
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_request_major() const { return request_major_; }
|
2012-06-07 15:40:42 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_request_minor() const { return request_minor_; }
|
2012-06-07 15:40:42 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_upstream(Upstream *upstream) {
|
2013-08-03 11:51:01 +02:00
|
|
|
upstream_ = upstream;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dconn_) {
|
2013-08-03 11:51:01 +02:00
|
|
|
dconn_->on_upstream_change(upstream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Upstream *Downstream::get_upstream() const { return upstream_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_stream_id(int32_t stream_id) { stream_id_ = stream_id; }
|
2013-08-03 11:51:01 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t Downstream::get_stream_id() const { return stream_id_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_state(int state) { request_state_ = state; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_request_state() const { return request_state_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_chunked_request() const { return chunked_request_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_chunked_request(bool f) { chunked_request_ = f; }
|
2014-07-03 12:59:10 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_request_connection_close() const {
|
2012-06-04 16:48:31 +02:00
|
|
|
return request_connection_close_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_connection_close(bool f) {
|
2012-06-04 16:48:31 +02:00
|
|
|
request_connection_close_ = f;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_request_http2_expect_body() const {
|
2014-07-03 12:59:10 +02:00
|
|
|
return request_http2_expect_body_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_request_http2_expect_body(bool f) {
|
2014-07-03 12:59:10 +02:00
|
|
|
request_http2_expect_body_ = f;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
bool Downstream::request_buf_full() {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dconn_) {
|
2015-01-13 15:30:28 +01:00
|
|
|
return request_buf_.rleft() >= get_config()->downstream_request_buffer_size;
|
2012-06-09 17:49:33 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:47:43 +01:00
|
|
|
DefaultMemchunks *Downstream::get_request_buf() { return &request_buf_; }
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2012-06-12 16:02:01 +02:00
|
|
|
// Call this function after this object is attached to
|
|
|
|
// Downstream. Otherwise, the program will crash.
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::push_request_headers() {
|
|
|
|
if (!dconn_) {
|
2013-11-20 18:06:28 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::push_upload_data_chunk(const uint8_t *data, size_t datalen) {
|
2012-06-04 16:48:31 +02:00
|
|
|
// Assumes that request headers have already been pushed to output
|
|
|
|
// buffer using push_request_headers().
|
2014-11-27 15:39:04 +01: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;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dconn_->push_upload_data_chunk(data, datalen) != 0) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
request_datalen_ += datalen;
|
|
|
|
|
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::end_upload_data() {
|
|
|
|
if (!dconn_) {
|
2013-11-20 18:06:28 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const Headers &Downstream::get_response_headers() const {
|
2012-06-04 16:48:31 +02:00
|
|
|
return response_headers_;
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:11:54 +01:00
|
|
|
int Downstream::index_response_headers() {
|
|
|
|
return index_headers(response_hdidx_, response_headers_,
|
|
|
|
response_content_length_);
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
const Headers::value_type *
|
|
|
|
Downstream::get_response_header(int16_t token) const {
|
2015-01-04 15:22:39 +01:00
|
|
|
return http2::get_header(response_hdidx_, token, response_headers_);
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
void
|
|
|
|
Downstream::rewrite_location_response_header(const std::string &upstream_scheme,
|
|
|
|
uint16_t upstream_port) {
|
|
|
|
auto hd =
|
|
|
|
http2::get_header(response_hdidx_, http2::HD_LOCATION, response_headers_);
|
|
|
|
if (!hd) {
|
2013-12-21 09:49:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
http_parser_url u;
|
2014-02-27 14:11:31 +01:00
|
|
|
memset(&u, 0, sizeof(u));
|
2014-11-27 15:39:04 +01:00
|
|
|
int rv =
|
|
|
|
http_parser_parse_url((*hd).value.c_str(), (*hd).value.size(), 0, &u);
|
|
|
|
if (rv != 0) {
|
2013-12-21 09:49:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string new_uri;
|
2015-02-08 10:54:24 +01:00
|
|
|
if (get_config()->no_host_rewrite) {
|
|
|
|
if (!request_http2_authority_.empty()) {
|
|
|
|
new_uri =
|
|
|
|
http2::rewrite_location_uri((*hd).value, u, request_http2_authority_,
|
|
|
|
upstream_scheme, upstream_port);
|
|
|
|
}
|
|
|
|
if (new_uri.empty()) {
|
|
|
|
auto host = get_request_header(http2::HD_HOST);
|
|
|
|
if (!host) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_uri = http2::rewrite_location_uri((*hd).value, u, (*host).value,
|
|
|
|
upstream_scheme, upstream_port);
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
2015-02-08 10:54:24 +01:00
|
|
|
} else {
|
|
|
|
assert(dconn_);
|
|
|
|
auto request_host =
|
|
|
|
get_config()->downstream_addrs[dconn_->get_addr_idx()].host.get();
|
|
|
|
new_uri = http2::rewrite_location_uri((*hd).value, u, request_host,
|
2013-12-21 10:35:53 +01:00
|
|
|
upstream_scheme, upstream_port);
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!new_uri.empty()) {
|
2015-01-04 15:22:39 +01:00
|
|
|
auto idx = response_hdidx_[http2::HD_LOCATION];
|
|
|
|
response_headers_[idx].value = std::move(new_uri);
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::add_response_header(std::string name, std::string value) {
|
2012-07-11 11:32:04 +02:00
|
|
|
response_header_key_prev_ = true;
|
2014-01-27 17:17:54 +01:00
|
|
|
response_headers_sum_ += name.size() + value.size();
|
2013-09-24 16:53:55 +02:00
|
|
|
response_headers_.emplace_back(std::move(name), std::move(value));
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_last_response_header_value(std::string value) {
|
2012-07-11 11:32:04 +02:00
|
|
|
response_header_key_prev_ = false;
|
2014-01-27 17:17:54 +01:00
|
|
|
response_headers_sum_ += value.size();
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &item = response_headers_.back();
|
2014-04-03 04:22:11 +02:00
|
|
|
item.value = std::move(value);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
void Downstream::add_response_header(std::string name, std::string value,
|
|
|
|
int16_t token) {
|
|
|
|
http2::index_header(response_hdidx_, token, response_headers_.size());
|
|
|
|
response_headers_sum_ += name.size() + value.size();
|
|
|
|
response_headers_.emplace_back(std::move(name), std::move(value), false,
|
|
|
|
token);
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
void Downstream::add_response_header(const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
bool no_index, int16_t token) {
|
2015-01-04 15:22:39 +01:00
|
|
|
http2::index_header(response_hdidx_, token, response_headers_.size());
|
2014-01-27 17:17:54 +01:00
|
|
|
response_headers_sum_ += namelen + valuelen;
|
2015-02-08 06:07:01 +01:00
|
|
|
http2::add_header(response_headers_, name, namelen, value, valuelen, no_index,
|
|
|
|
token);
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_response_header_key_prev() const {
|
2012-07-11 11:32:04 +02:00
|
|
|
return response_header_key_prev_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::append_last_response_header_key(const char *data, size_t len) {
|
2012-07-11 11:32:04 +02:00
|
|
|
assert(response_header_key_prev_);
|
2014-01-27 17:17:54 +01:00
|
|
|
response_headers_sum_ += len;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &item = response_headers_.back();
|
2014-04-03 04:22:11 +02:00
|
|
|
item.name.append(data, len);
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::append_last_response_header_value(const char *data,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t len) {
|
2012-07-11 11:32:04 +02:00
|
|
|
assert(!response_header_key_prev_);
|
2014-01-27 17:17:54 +01:00
|
|
|
response_headers_sum_ += len;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &item = response_headers_.back();
|
2014-04-03 04:22:11 +02:00
|
|
|
item.value.append(data, len);
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
void Downstream::clear_response_headers() {
|
|
|
|
Headers().swap(response_headers_);
|
|
|
|
http2::init_hdidx(response_hdidx_);
|
|
|
|
}
|
2014-06-15 09:14:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t Downstream::get_response_headers_sum() const {
|
2014-01-27 17:17:54 +01:00
|
|
|
return response_headers_sum_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
unsigned int Downstream::get_response_http_status() const {
|
2012-06-04 16:48:31 +02:00
|
|
|
return response_http_status_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_response_http_status(unsigned int status) {
|
2012-06-04 16:48:31 +02:00
|
|
|
response_http_status_ = status;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_response_major(int major) { response_major_ = major; }
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_response_minor(int minor) { response_minor_ = minor; }
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_response_major() const { return response_major_; }
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_response_minor() const { return response_minor_; }
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_response_version() const {
|
|
|
|
return response_major_ * 100 + response_minor_;
|
2012-07-11 09:20:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_chunked_response() const { return chunked_response_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_chunked_response(bool f) { chunked_response_ = f; }
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_response_connection_close() const {
|
2012-06-07 16:42:11 +02:00
|
|
|
return response_connection_close_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_response_connection_close(bool f) {
|
2012-07-11 09:20:16 +02:00
|
|
|
response_connection_close_ = f;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::on_read() {
|
|
|
|
if (!dconn_) {
|
2013-11-20 18:06:28 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::change_priority(int32_t pri) {
|
|
|
|
if (!dconn_) {
|
2014-01-18 08:12:03 +01:00
|
|
|
DLOG(INFO, this) << "dconn_ is NULL";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return dconn_->on_priority_change(pri);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_response_state(int state) { response_state_ = state; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Downstream::get_response_state() const { return response_state_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2015-01-20 17:47:43 +01:00
|
|
|
DefaultMemchunks *Downstream::get_response_buf() { return &response_buf_; }
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
bool Downstream::response_buf_full() {
|
|
|
|
if (dconn_) {
|
2015-01-13 15:20:06 +01:00
|
|
|
return response_buf_.rleft() >=
|
|
|
|
get_config()->downstream_response_buffer_size;
|
2014-12-27 18:59:06 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::add_response_bodylen(size_t amount) {
|
2014-07-05 11:22:40 +02:00
|
|
|
response_bodylen_ += amount;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int64_t Downstream::get_response_bodylen() const { return response_bodylen_; }
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::add_response_sent_bodylen(size_t amount) {
|
2014-11-18 16:56:44 +01:00
|
|
|
response_sent_bodylen_ += amount;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int64_t Downstream::get_response_sent_bodylen() const {
|
2014-11-18 16:56:44 +01:00
|
|
|
return response_sent_bodylen_;
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:44:23 +01:00
|
|
|
int64_t Downstream::get_response_content_length() const {
|
|
|
|
return response_content_length_;
|
|
|
|
}
|
|
|
|
|
2015-01-17 11:33:30 +01:00
|
|
|
void Downstream::set_response_content_length(int64_t len) {
|
|
|
|
response_content_length_ = len;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:40:37 +01:00
|
|
|
int64_t Downstream::get_request_content_length() const {
|
|
|
|
return request_content_length_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_request_content_length(int64_t len) {
|
|
|
|
request_content_length_ = len;
|
|
|
|
}
|
|
|
|
|
2015-01-17 13:31:28 +01:00
|
|
|
bool Downstream::validate_request_bodylen() const {
|
|
|
|
if (request_content_length_ == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request_content_length_ != request_bodylen_) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, this) << "request invalid bodylen: content-length="
|
|
|
|
<< request_content_length_
|
|
|
|
<< ", received=" << request_bodylen_;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-17 11:33:30 +01:00
|
|
|
bool Downstream::validate_response_bodylen() const {
|
|
|
|
if (!expect_response_body() || response_content_length_ == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-17 13:31:28 +01:00
|
|
|
if (response_content_length_ != response_bodylen_) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, this) << "response invalid bodylen: content-length="
|
|
|
|
<< response_content_length_
|
|
|
|
<< ", received=" << response_bodylen_;
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-17 11:33:30 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 13:31:28 +01:00
|
|
|
return true;
|
2015-01-17 11:33:30 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_priority(int32_t pri) { priority_ = pri; }
|
2012-06-07 17:36:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t Downstream::get_priority() const { return priority_; }
|
2013-11-16 07:41:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::check_upgrade_fulfilled() {
|
|
|
|
if (request_method_ == "CONNECT") {
|
2013-07-31 14:48:37 +02:00
|
|
|
upgraded_ = 200 <= response_http_status_ && response_http_status_ < 300;
|
2014-06-15 09:14:00 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (response_http_status_ == 101) {
|
2013-07-31 14:48:37 +02:00
|
|
|
// TODO Do more strict checking for upgrade headers
|
2014-06-15 09:14:00 +02:00
|
|
|
upgraded_ = upgrade_request_;
|
|
|
|
|
|
|
|
return;
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::inspect_http2_request() {
|
|
|
|
if (request_method_ == "CONNECT") {
|
2014-06-15 09:14:00 +02:00
|
|
|
upgrade_request_ = true;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::inspect_http1_request() {
|
|
|
|
if (request_method_ == "CONNECT") {
|
2013-07-31 14:48:37 +02:00
|
|
|
upgrade_request_ = true;
|
2014-06-15 09:14:00 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
if (!upgrade_request_) {
|
|
|
|
auto idx = request_hdidx_[http2::HD_UPGRADE];
|
|
|
|
if (idx != -1) {
|
2014-06-15 09:14:00 +02:00
|
|
|
upgrade_request_ = true;
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto &val = request_headers_[idx].value;
|
|
|
|
// TODO Perform more strict checking for upgrade headers
|
|
|
|
if (util::streq(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, val.c_str(),
|
|
|
|
val.size())) {
|
2014-06-15 09:14:00 +02:00
|
|
|
http2_upgrade_seen_ = true;
|
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
auto idx = request_hdidx_[http2::HD_TRANSFER_ENCODING];
|
2015-01-20 14:55:01 +01:00
|
|
|
if (idx != -1) {
|
|
|
|
request_content_length_ = -1;
|
|
|
|
if (util::strifind(request_headers_[idx].value.c_str(), "chunked")) {
|
|
|
|
chunked_request_ = true;
|
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::inspect_http1_response() {
|
2015-01-04 15:22:39 +01:00
|
|
|
auto idx = response_hdidx_[http2::HD_TRANSFER_ENCODING];
|
2015-01-20 15:11:54 +01:00
|
|
|
if (idx != -1) {
|
|
|
|
response_content_length_ = -1;
|
|
|
|
if (util::strifind(response_headers_[idx].value.c_str(), "chunked")) {
|
|
|
|
chunked_response_ = true;
|
2015-01-17 11:33:30 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-15 09:14:00 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_response() {
|
2014-07-23 16:32:57 +02:00
|
|
|
response_http_status_ = 0;
|
|
|
|
response_major_ = 1;
|
|
|
|
response_minor_ = 1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_non_final_response() const {
|
2014-07-23 16:32:57 +02:00
|
|
|
return response_http_status_ / 100 == 1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_upgraded() const { return upgraded_; }
|
2014-06-15 09:14:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_upgrade_request() const { return upgrade_request_; }
|
2012-07-11 09:20:16 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_http2_upgrade_request() const {
|
2015-01-04 15:22:39 +01:00
|
|
|
return request_bodylen_ == 0 && http2_upgrade_seen_ &&
|
|
|
|
request_hdidx_[http2::HD_HTTP2_SETTINGS] != -1;
|
2014-06-15 09:14:00 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
namespace {
|
|
|
|
const std::string EMPTY;
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &Downstream::get_http2_settings() const {
|
2015-01-04 15:22:39 +01:00
|
|
|
auto idx = request_hdidx_[http2::HD_HTTP2_SETTINGS];
|
|
|
|
if (idx == -1) {
|
|
|
|
return EMPTY;
|
|
|
|
}
|
|
|
|
return request_headers_[idx].value;
|
2013-08-03 11:51:01 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_downstream_stream_id(int32_t stream_id) {
|
2012-11-18 13:23:13 +01:00
|
|
|
downstream_stream_id_ = stream_id;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t Downstream::get_downstream_stream_id() const {
|
2012-11-18 13:23:13 +01:00
|
|
|
return downstream_stream_id_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t Downstream::get_response_rst_stream_error_code() const {
|
2013-07-26 12:33:25 +02:00
|
|
|
return response_rst_stream_error_code_;
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_response_rst_stream_error_code(uint32_t error_code) {
|
2013-07-26 12:33:25 +02:00
|
|
|
response_rst_stream_error_code_ = error_code;
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::set_expect_final_response(bool f) {
|
2014-07-23 16:32:57 +02:00
|
|
|
expect_final_response_ = f;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_expect_final_response() const {
|
2014-07-23 16:32:57 +02:00
|
|
|
return expect_final_response_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t Downstream::get_request_datalen() const { return request_datalen_; }
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::dec_request_datalen(size_t len) {
|
2014-08-21 14:22:16 +02:00
|
|
|
assert(request_datalen_ >= len);
|
|
|
|
request_datalen_ -= len;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_request_datalen() { request_datalen_ = 0; }
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::add_response_datalen(size_t len) { response_datalen_ += len; }
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::dec_response_datalen(size_t len) {
|
2014-08-21 14:22:16 +02:00
|
|
|
assert(response_datalen_ >= len);
|
|
|
|
response_datalen_ -= len;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t Downstream::get_response_datalen() const { return response_datalen_; }
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_response_datalen() { response_datalen_ = 0; }
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::expect_response_body() const {
|
|
|
|
return request_method_ != "HEAD" && response_http_status_ / 100 != 1 &&
|
|
|
|
response_http_status_ != 304 && response_http_status_ != 204;
|
2014-07-25 16:13:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 13:52:32 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
bool pseudo_header_allowed(const Headers &headers) {
|
|
|
|
if (headers.empty()) {
|
2014-08-08 13:52:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers.back().name.c_str()[0] == ':';
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
bool Downstream::request_pseudo_header_allowed(int16_t token) const {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (!pseudo_header_allowed(request_headers_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return http2::check_http2_request_pseudo_header(request_hdidx_, token);
|
2014-08-08 13:52:32 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
bool Downstream::response_pseudo_header_allowed(int16_t token) const {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (!pseudo_header_allowed(response_headers_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return http2::check_http2_response_pseudo_header(response_hdidx_, token);
|
2014-08-08 13:52:32 +02:00
|
|
|
}
|
|
|
|
|
2015-01-05 16:30:57 +01:00
|
|
|
namespace {
|
|
|
|
void reset_timer(struct ev_loop *loop, ev_timer *w) { ev_timer_again(loop, w); }
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void try_reset_timer(struct ev_loop *loop, ev_timer *w) {
|
|
|
|
if (!ev_is_active(w)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ev_timer_again(loop, w);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void ensure_timer(struct ev_loop *loop, ev_timer *w) {
|
|
|
|
if (ev_is_active(w)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ev_timer_again(loop, w);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void disable_timer(struct ev_loop *loop, ev_timer *w) {
|
|
|
|
ev_timer_stop(loop, w);
|
|
|
|
}
|
|
|
|
} // namespace
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_upstream_rtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_read_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
reset_timer(loop, &upstream_rtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_upstream_wtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
if (get_config()->stream_write_timeout != 0.) {
|
|
|
|
reset_timer(loop, &upstream_wtimer_);
|
|
|
|
}
|
|
|
|
if (get_config()->stream_read_timeout != 0.) {
|
|
|
|
try_reset_timer(loop, &upstream_rtimer_);
|
|
|
|
}
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::ensure_upstream_wtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_write_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
ensure_timer(loop, &upstream_wtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::disable_upstream_rtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_read_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
disable_timer(loop, &upstream_rtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::disable_upstream_wtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_write_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
disable_timer(loop, &upstream_wtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_downstream_rtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_read_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
reset_timer(loop, &downstream_rtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::reset_downstream_wtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
if (get_config()->stream_write_timeout != 0.) {
|
|
|
|
reset_timer(loop, &downstream_wtimer_);
|
|
|
|
}
|
|
|
|
if (get_config()->stream_read_timeout != 0.) {
|
|
|
|
try_reset_timer(loop, &downstream_rtimer_);
|
|
|
|
}
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::ensure_downstream_wtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_write_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
ensure_timer(loop, &downstream_wtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::disable_downstream_rtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_read_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
disable_timer(loop, &downstream_rtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::disable_downstream_wtimer() {
|
2015-01-05 16:30:57 +01:00
|
|
|
if (get_config()->stream_write_timeout == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
disable_timer(loop, &downstream_wtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::accesslog_ready() const { return response_http_status_ > 0; }
|
2014-11-23 09:24:23 +01:00
|
|
|
|
2015-02-02 17:47:04 +01:00
|
|
|
void Downstream::add_retry() { ++num_retry_; }
|
|
|
|
|
|
|
|
bool Downstream::no_more_retry() const { return num_retry_ > 5; }
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|