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"
|
2015-03-11 16:17:05 +01:00
|
|
|
#include "shrpx_downstream_queue.h"
|
2015-09-04 16:56:07 +02:00
|
|
|
#include "shrpx_worker.h"
|
|
|
|
#include "shrpx_http2_session.h"
|
|
|
|
#ifdef HAVE_MRUBY
|
|
|
|
#include "shrpx_mruby.h"
|
|
|
|
#endif // HAVE_MRUBY
|
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
|
2015-04-07 15:13:01 +02:00
|
|
|
Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool,
|
2016-01-14 16:09:53 +01:00
|
|
|
int32_t stream_id)
|
2016-01-27 13:14:07 +01:00
|
|
|
: dlnext(nullptr),
|
|
|
|
dlprev(nullptr),
|
|
|
|
response_sent_body_length(0),
|
2016-03-09 13:25:11 +01:00
|
|
|
balloc_(1024, 1024),
|
2016-03-09 13:15:32 +01:00
|
|
|
req_(balloc_),
|
|
|
|
resp_(balloc_),
|
2015-03-11 16:17:05 +01:00
|
|
|
request_start_time_(std::chrono::high_resolution_clock::now()),
|
2016-01-27 13:14:07 +01:00
|
|
|
request_buf_(mcpool),
|
|
|
|
response_buf_(mcpool),
|
|
|
|
upstream_(upstream),
|
|
|
|
blocked_link_(nullptr),
|
2016-08-04 17:04:47 +02:00
|
|
|
addr_(nullptr),
|
2016-01-27 13:14:07 +01:00
|
|
|
num_retry_(0),
|
|
|
|
stream_id_(stream_id),
|
|
|
|
assoc_stream_id_(-1),
|
|
|
|
downstream_stream_id_(-1),
|
2016-01-13 14:45:52 +01:00
|
|
|
response_rst_stream_error_code_(NGHTTP2_NO_ERROR),
|
2016-01-27 13:14:07 +01:00
|
|
|
request_state_(INITIAL),
|
|
|
|
response_state_(INITIAL),
|
|
|
|
dispatch_state_(DISPATCH_NONE),
|
|
|
|
upgraded_(false),
|
|
|
|
chunked_request_(false),
|
|
|
|
chunked_response_(false),
|
|
|
|
expect_final_response_(false),
|
2016-01-13 16:37:45 +01:00
|
|
|
request_pending_(false) {
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-01-19 08:56:12 +01:00
|
|
|
auto &timeoutconf = get_config()->http2.timeout;
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_init(&upstream_rtimer_, &upstream_rtimeoutcb, 0.,
|
2016-01-19 08:56:12 +01:00
|
|
|
timeoutconf.stream_read);
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_init(&upstream_wtimer_, &upstream_wtimeoutcb, 0.,
|
2016-01-19 08:56:12 +01:00
|
|
|
timeoutconf.stream_write);
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_init(&downstream_rtimer_, &downstream_rtimeoutcb, 0.,
|
2016-01-19 08:56:12 +01:00
|
|
|
timeoutconf.stream_read);
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_init(&downstream_wtimer_, &downstream_wtimeoutcb, 0.,
|
2016-01-19 08:56:12 +01:00
|
|
|
timeoutconf.stream_write);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
upstream_rtimer_.data = this;
|
|
|
|
upstream_wtimer_.data = this;
|
|
|
|
downstream_rtimer_.data = this;
|
|
|
|
downstream_wtimer_.data = this;
|
|
|
|
}
|
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_);
|
2015-09-04 16:56:07 +02:00
|
|
|
|
2015-09-04 18:32:26 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-04 16:56:07 +02:00
|
|
|
auto handler = upstream_->get_client_handler();
|
|
|
|
auto worker = handler->get_worker();
|
|
|
|
auto mruby_ctx = worker->get_mruby_context();
|
|
|
|
|
|
|
|
mruby_ctx->delete_downstream(this);
|
2015-09-04 18:32:26 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2015-01-02 03:16:37 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-07-15 16:31:32 +02:00
|
|
|
// DownstreamConnection may refer to this object. Delete it now
|
|
|
|
// explicitly.
|
|
|
|
dconn_.reset();
|
|
|
|
|
2016-03-12 07:05:20 +01:00
|
|
|
for (auto rcbuf : rcbufs_) {
|
|
|
|
nghttp2_rcbuf_decref(rcbuf);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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 {
|
2016-03-09 13:15:32 +01:00
|
|
|
const HeaderRefs::value_type *
|
|
|
|
search_header_linear_backwards(const HeaderRefs &headers,
|
|
|
|
const StringRef &name) {
|
2016-02-20 11:02:19 +01:00
|
|
|
for (auto it = headers.rbegin(); it != headers.rend(); ++it) {
|
|
|
|
auto &kv = *it;
|
2015-01-04 15:22:39 +01:00
|
|
|
if (kv.name == name) {
|
2016-02-20 11:02:19 +01:00
|
|
|
return &kv;
|
2015-01-04 15:22:39 +01:00
|
|
|
}
|
2013-12-21 09:49:31 +01:00
|
|
|
}
|
2016-02-20 11:02:19 +01:00
|
|
|
return nullptr;
|
2014-11-18 16:56:44 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-03-18 15:50:04 +01:00
|
|
|
StringRef Downstream::assemble_request_cookie() {
|
|
|
|
size_t len = 0;
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
for (auto &kv : req_.fs.headers()) {
|
2016-03-18 15:50:04 +01:00
|
|
|
if (kv.token != http2::HD_COOKIE || kv.value.empty()) {
|
2015-01-04 15:22:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:50:04 +01:00
|
|
|
len += kv.value.size() + str_size("; ");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iov = make_byte_ref(balloc_, len + 1);
|
|
|
|
auto p = iov.base;
|
|
|
|
|
|
|
|
for (auto &kv : req_.fs.headers()) {
|
|
|
|
if (kv.token != http2::HD_COOKIE || kv.value.empty()) {
|
2016-03-09 13:15:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto end = std::end(kv.value);
|
|
|
|
for (auto it = std::begin(kv.value) + kv.value.size();
|
|
|
|
it != std::begin(kv.value); --it) {
|
|
|
|
auto c = *(it - 1);
|
|
|
|
if (c == ' ' || c == ';') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-10 15:50:04 +01:00
|
|
|
end = it;
|
2016-03-09 13:15:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:50:04 +01:00
|
|
|
p = std::copy(std::begin(kv.value), end, p);
|
|
|
|
p = util::copy_lit(p, "; ");
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2016-03-18 15:50:04 +01:00
|
|
|
|
|
|
|
// cut trailing "; "
|
|
|
|
if (p - iov.base >= 2) {
|
|
|
|
p -= 2;
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2016-01-13 16:44:10 +01:00
|
|
|
|
2016-03-18 15:50:04 +01:00
|
|
|
return StringRef{iov.base, p};
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-05 14:48:54 +01:00
|
|
|
size_t Downstream::count_crumble_request_cookie() {
|
|
|
|
size_t n = 0;
|
2016-01-13 14:45:52 +01:00
|
|
|
for (auto &kv : req_.fs.headers()) {
|
2015-11-05 14:48:54 +01:00
|
|
|
if (kv.name.size() != 6 || kv.name[5] != 'e' ||
|
|
|
|
!util::streq_l("cooki", kv.name.c_str(), 5)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
for (auto it = std::begin(kv.value); it != std::end(kv.value);) {
|
|
|
|
if (*it == '\t' || *it == ' ' || *it == ';') {
|
|
|
|
++it;
|
|
|
|
continue;
|
2015-11-05 14:48:54 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
it = std::find(it, std::end(kv.value), ';');
|
2015-11-05 14:48:54 +01:00
|
|
|
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::crumble_request_cookie(std::vector<nghttp2_nv> &nva) {
|
2016-01-13 14:45:52 +01:00
|
|
|
for (auto &kv : req_.fs.headers()) {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (kv.name.size() != 6 || kv.name[5] != 'e' ||
|
2015-02-20 15:57:40 +01:00
|
|
|
!util::streq_l("cooki", kv.name.c_str(), 5)) {
|
2015-01-04 15:22:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
for (auto it = std::begin(kv.value); it != std::end(kv.value);) {
|
|
|
|
if (*it == '\t' || *it == ' ' || *it == ';') {
|
|
|
|
++it;
|
|
|
|
continue;
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
2015-01-04 15:22:39 +01:00
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
auto first = it;
|
2015-01-04 15:22:39 +01:00
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
it = std::find(it, std::end(kv.value), ';');
|
|
|
|
|
|
|
|
nva.push_back({(uint8_t *)"cookie", (uint8_t *)first, str_size("cookie"),
|
|
|
|
(size_t)(it - first),
|
2015-11-05 14:48:54 +01:00
|
|
|
(uint8_t)(NGHTTP2_NV_FLAG_NO_COPY_NAME |
|
|
|
|
NGHTTP2_NV_FLAG_NO_COPY_VALUE |
|
|
|
|
(kv.no_index ? NGHTTP2_NV_FLAG_NO_INDEX : 0))});
|
2013-11-16 13:15:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 10:32:01 +01:00
|
|
|
namespace {
|
2016-03-09 13:15:32 +01:00
|
|
|
void add_header(bool &key_prev, size_t &sum, HeaderRefs &headers,
|
2016-02-20 14:56:55 +01:00
|
|
|
const StringRef &name, const StringRef &value, bool no_index,
|
2016-02-21 08:44:00 +01:00
|
|
|
int32_t token) {
|
2015-03-08 10:32:01 +01:00
|
|
|
key_prev = true;
|
|
|
|
sum += name.size() + value.size();
|
2016-03-09 13:15:32 +01:00
|
|
|
headers.emplace_back(name, value, no_index, token);
|
2015-03-08 10:32:01 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2016-03-09 13:25:11 +01:00
|
|
|
void append_last_header_key(BlockAllocator &balloc, bool &key_prev, size_t &sum,
|
|
|
|
HeaderRefs &headers, const char *data, size_t len) {
|
2015-03-08 10:32:01 +01:00
|
|
|
assert(key_prev);
|
|
|
|
sum += len;
|
|
|
|
auto &item = headers.back();
|
2016-03-10 16:50:27 +01:00
|
|
|
auto iov = make_byte_ref(balloc, item.name.size() + len + 1);
|
|
|
|
auto p = iov.base;
|
|
|
|
p = std::copy(std::begin(item.name), std::end(item.name), p);
|
|
|
|
p = std::copy_n(data, len, p);
|
|
|
|
util::inp_strlower(p - len, p);
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
item.name = StringRef{iov.base, p};
|
|
|
|
|
2016-02-20 11:02:19 +01:00
|
|
|
item.token = http2::lookup_token(item.name);
|
2015-03-08 10:32:01 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2016-03-09 13:25:11 +01:00
|
|
|
void append_last_header_value(BlockAllocator &balloc, bool &key_prev,
|
2016-03-09 13:15:32 +01:00
|
|
|
size_t &sum, HeaderRefs &headers,
|
2015-03-08 10:32:01 +01:00
|
|
|
const char *data, size_t len) {
|
2016-01-13 15:01:29 +01:00
|
|
|
key_prev = false;
|
2015-03-08 10:32:01 +01:00
|
|
|
sum += len;
|
|
|
|
auto &item = headers.back();
|
2016-03-09 13:15:32 +01:00
|
|
|
item.value = concat_string_ref(balloc, item.value, StringRef{data, len});
|
2015-03-08 10:32:01 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-02-20 13:44:08 +01:00
|
|
|
int FieldStore::parse_content_length() {
|
2016-01-13 14:45:52 +01:00
|
|
|
content_length = -1;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2016-02-20 11:02:19 +01:00
|
|
|
for (auto &kv : headers_) {
|
|
|
|
if (kv.token != http2::HD_CONTENT_LENGTH) {
|
2016-01-13 14:45:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-13 14:40:41 +01:00
|
|
|
|
2016-02-20 11:02:19 +01:00
|
|
|
auto len = util::parse_uint(kv.value);
|
|
|
|
if (len == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (content_length != -1) {
|
|
|
|
return -1;
|
2016-01-13 14:45:52 +01:00
|
|
|
}
|
2016-02-20 11:02:19 +01:00
|
|
|
content_length = len;
|
2016-01-13 14:45:52 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
const HeaderRefs::value_type *FieldStore::header(int32_t token) const {
|
2016-02-20 11:02:19 +01:00
|
|
|
for (auto it = headers_.rbegin(); it != headers_.rend(); ++it) {
|
|
|
|
auto &kv = *it;
|
|
|
|
if (kv.token == token) {
|
|
|
|
return &kv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
HeaderRefs::value_type *FieldStore::header(int32_t token) {
|
2016-02-20 11:02:19 +01:00
|
|
|
for (auto it = headers_.rbegin(); it != headers_.rend(); ++it) {
|
|
|
|
auto &kv = *it;
|
|
|
|
if (kv.token == token) {
|
|
|
|
return &kv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2016-01-13 16:37:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
const HeaderRefs::value_type *FieldStore::header(const StringRef &name) const {
|
2016-02-20 11:02:19 +01:00
|
|
|
return search_header_linear_backwards(headers_, name);
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
void FieldStore::add_header_token(const StringRef &name, const StringRef &value,
|
2016-02-21 08:44:00 +01:00
|
|
|
bool no_index, int32_t token) {
|
2016-03-10 16:50:27 +01:00
|
|
|
shrpx::add_header(header_key_prev_, buffer_size_, headers_, name, value,
|
|
|
|
no_index, token);
|
2015-03-08 08:29:26 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
void FieldStore::append_last_header_key(const char *data, size_t len) {
|
2016-03-09 13:15:32 +01:00
|
|
|
shrpx::append_last_header_key(balloc_, header_key_prev_, buffer_size_,
|
|
|
|
headers_, data, len);
|
2015-03-08 10:32:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
void FieldStore::append_last_header_value(const char *data, size_t len) {
|
2016-03-09 13:15:32 +01:00
|
|
|
shrpx::append_last_header_value(balloc_, header_key_prev_, buffer_size_,
|
|
|
|
headers_, data, len);
|
2015-03-08 10:32:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:02:19 +01:00
|
|
|
void FieldStore::clear_headers() { headers_.clear(); }
|
2015-03-08 10:32:01 +01:00
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
void FieldStore::add_trailer_token(const StringRef &name,
|
|
|
|
const StringRef &value, bool no_index,
|
2016-02-21 08:44:00 +01:00
|
|
|
int32_t token) {
|
2016-02-20 13:41:23 +01:00
|
|
|
// Header size limit should be applied to all header and trailer
|
|
|
|
// fields combined.
|
2016-03-10 16:50:27 +01:00
|
|
|
shrpx::add_header(trailer_key_prev_, buffer_size_, trailers_, name, value,
|
|
|
|
no_index, token);
|
2015-03-08 10:32:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
void FieldStore::append_last_trailer_key(const char *data, size_t len) {
|
2016-03-09 13:15:32 +01:00
|
|
|
shrpx::append_last_header_key(balloc_, trailer_key_prev_, buffer_size_,
|
|
|
|
trailers_, data, len);
|
2012-07-11 11:32:04 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
void FieldStore::append_last_trailer_value(const char *data, size_t len) {
|
2016-03-09 13:15:32 +01:00
|
|
|
shrpx::append_last_header_value(balloc_, trailer_key_prev_, buffer_size_,
|
|
|
|
trailers_, data, len);
|
2012-07-11 09:20:16 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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-12-27 18:59:06 +01:00
|
|
|
bool Downstream::request_buf_full() {
|
2016-06-02 16:47:41 +02:00
|
|
|
auto handler = upstream_->get_client_handler();
|
|
|
|
auto faddr = handler->get_upstream_addr();
|
2016-06-03 12:13:02 +02:00
|
|
|
auto worker = handler->get_worker();
|
2016-06-02 16:47:41 +02:00
|
|
|
|
|
|
|
// We don't check buffer size here for API endpoint.
|
2016-06-16 16:30:35 +02:00
|
|
|
if (faddr->alt_mode == ALTMODE_API) {
|
2016-06-02 16:47:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dconn_) {
|
2016-06-04 09:23:50 +02:00
|
|
|
auto &downstreamconf = *worker->get_downstream_config();
|
|
|
|
return request_buf_.rleft() >= downstreamconf.request_buffer_size;
|
2012-06-09 17:49:33 +02:00
|
|
|
}
|
2016-06-03 12:13:02 +02:00
|
|
|
|
|
|
|
return false;
|
2012-06-09 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-06-17 15:32:15 +02:00
|
|
|
req_.recv_body_length += 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
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:36:47 +01:00
|
|
|
req_.unconsumed_body_length += datalen;
|
2014-07-25 14:26:03 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-08 11:41:45 +01:00
|
|
|
void Downstream::rewrite_location_response_header(
|
2016-03-10 14:42:07 +01:00
|
|
|
const StringRef &upstream_scheme) {
|
2016-01-13 16:37:45 +01:00
|
|
|
auto hd = resp_.fs.header(http2::HD_LOCATION);
|
2015-01-04 15:22:39 +01:00
|
|
|
if (!hd) {
|
2013-12-21 09:49:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-16 04:49:18 +01:00
|
|
|
|
|
|
|
if (request_downstream_host_.empty() || req_.authority.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-19 10:55:37 +02:00
|
|
|
http_parser_url u{};
|
2016-01-16 04:49:18 +01:00
|
|
|
auto rv = http_parser_parse_url(hd->value.c_str(), hd->value.size(), 0, &u);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-12-21 09:49:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-16 04:49:18 +01:00
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
auto new_uri = http2::rewrite_location_uri(balloc_, hd->value, u,
|
|
|
|
request_downstream_host_,
|
|
|
|
req_.authority, upstream_scheme);
|
2015-02-08 06:07:01 +01:00
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
if (new_uri.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-06 17:43:18 +02:00
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
hd->value = new_uri;
|
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
|
|
|
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
|
|
|
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_) {
|
2016-06-03 12:13:02 +02:00
|
|
|
auto handler = upstream_->get_client_handler();
|
|
|
|
auto worker = handler->get_worker();
|
2016-06-04 09:23:50 +02:00
|
|
|
auto &downstreamconf = *worker->get_downstream_config();
|
2016-06-03 12:13:02 +02:00
|
|
|
|
2016-06-04 09:23:50 +02:00
|
|
|
return response_buf_.rleft() >= downstreamconf.response_buffer_size;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2016-06-03 12:13:02 +02:00
|
|
|
|
|
|
|
return false;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2016-01-14 15:14:58 +01:00
|
|
|
bool Downstream::validate_request_recv_body_length() const {
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req_.fs.content_length == -1) {
|
2015-01-17 13:31:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:14:58 +01:00
|
|
|
if (req_.fs.content_length != req_.recv_body_length) {
|
2015-01-17 13:31:28 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, this) << "request invalid bodylen: content-length="
|
2016-01-13 14:45:52 +01:00
|
|
|
<< req_.fs.content_length
|
2016-01-14 15:14:58 +01:00
|
|
|
<< ", received=" << req_.recv_body_length;
|
2015-01-17 13:31:28 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:20:44 +01:00
|
|
|
bool Downstream::validate_response_recv_body_length() const {
|
2016-01-13 16:37:45 +01:00
|
|
|
if (!expect_response_body() || resp_.fs.content_length == -1) {
|
2015-01-17 11:33:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:20:44 +01:00
|
|
|
if (resp_.fs.content_length != resp_.recv_body_length) {
|
2015-01-17 13:31:28 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DLOG(INFO, this) << "response invalid bodylen: content-length="
|
2016-01-13 16:37:45 +01:00
|
|
|
<< resp_.fs.content_length
|
2016-01-14 15:20:44 +01:00
|
|
|
<< ", received=" << resp_.recv_body_length;
|
2015-01-17 13:31:28 +01:00
|
|
|
}
|
|
|
|
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::check_upgrade_fulfilled() {
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req_.method == HTTP_CONNECT) {
|
2016-01-13 16:37:45 +01:00
|
|
|
upgraded_ = 200 <= resp_.http_status && resp_.http_status < 300;
|
2014-06-15 09:14:00 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
if (resp_.http_status == 101) {
|
2013-07-31 14:48:37 +02:00
|
|
|
// TODO Do more strict checking for upgrade headers
|
2016-01-13 14:45:52 +01:00
|
|
|
upgraded_ = req_.upgrade_request;
|
2014-06-15 09:14:00 +02:00
|
|
|
|
|
|
|
return;
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::inspect_http2_request() {
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req_.method == HTTP_CONNECT) {
|
|
|
|
req_.upgrade_request = true;
|
2014-06-15 09:14:00 +02:00
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Downstream::inspect_http1_request() {
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req_.method == HTTP_CONNECT) {
|
|
|
|
req_.upgrade_request = true;
|
2016-01-14 16:22:11 +01:00
|
|
|
} else {
|
2016-01-13 14:45:52 +01:00
|
|
|
auto upgrade = req_.fs.header(http2::HD_UPGRADE);
|
|
|
|
if (upgrade) {
|
|
|
|
const auto &val = upgrade->value;
|
2015-01-04 15:22:39 +01:00
|
|
|
// TODO Perform more strict checking for upgrade headers
|
2015-02-20 15:57:40 +01:00
|
|
|
if (util::streq_l(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, val.c_str(),
|
|
|
|
val.size())) {
|
2016-01-13 14:45:52 +01:00
|
|
|
req_.http2_upgrade_seen = true;
|
2015-05-25 17:00:11 +02:00
|
|
|
} else {
|
2016-01-13 14:45:52 +01:00
|
|
|
req_.upgrade_request = true;
|
2014-06-15 09:14:00 +02:00
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 14:45:52 +01:00
|
|
|
auto transfer_encoding = req_.fs.header(http2::HD_TRANSFER_ENCODING);
|
|
|
|
if (transfer_encoding) {
|
|
|
|
req_.fs.content_length = -1;
|
2016-01-17 08:34:56 +01:00
|
|
|
if (util::iends_with_l(transfer_encoding->value, "chunked")) {
|
2015-01-20 14:55:01 +01:00
|
|
|
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() {
|
2016-01-13 16:37:45 +01:00
|
|
|
auto transfer_encoding = resp_.fs.header(http2::HD_TRANSFER_ENCODING);
|
|
|
|
if (transfer_encoding) {
|
|
|
|
resp_.fs.content_length = -1;
|
2016-01-17 08:34:56 +01:00
|
|
|
if (util::iends_with_l(transfer_encoding->value, "chunked")) {
|
2015-01-20 15:11:54 +01:00
|
|
|
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() {
|
2016-01-13 16:37:45 +01:00
|
|
|
resp_.http_status = 0;
|
|
|
|
resp_.http_major = 1;
|
|
|
|
resp_.http_minor = 1;
|
2014-07-23 16:32:57 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Downstream::get_non_final_response() const {
|
2016-01-13 16:37:45 +01:00
|
|
|
return !upgraded_ && resp_.http_status / 100 == 1;
|
2014-07-23 16:32:57 +02:00
|
|
|
}
|
|
|
|
|
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_http2_upgrade_request() const {
|
2016-01-13 14:45:52 +01:00
|
|
|
return req_.http2_upgrade_seen && req_.fs.header(http2::HD_HTTP2_SETTINGS) &&
|
2015-05-21 18:59:40 +02:00
|
|
|
response_state_ == INITIAL;
|
2014-06-15 09:14:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
StringRef Downstream::get_http2_settings() const {
|
2016-01-13 14:45:52 +01:00
|
|
|
auto http2_settings = req_.fs.header(http2::HD_HTTP2_SETTINGS);
|
|
|
|
if (!http2_settings) {
|
2016-03-09 13:15:32 +01:00
|
|
|
return StringRef{};
|
2015-01-04 15:22:39 +01:00
|
|
|
}
|
2016-01-13 14:45:52 +01:00
|
|
|
return http2_settings->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
|
|
|
bool Downstream::expect_response_body() const {
|
2016-04-27 14:19:28 +02:00
|
|
|
return !resp_.headers_only &&
|
|
|
|
http2::expect_response_body(req_.method, resp_.http_status);
|
2014-08-08 13:52:32 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:00:36 +02:00
|
|
|
bool Downstream::expect_response_trailer() const {
|
|
|
|
// In HTTP/2, if final response HEADERS does not bear END_STREAM it
|
|
|
|
// is possible trailer fields might come, regardless of request
|
|
|
|
// method or status code.
|
|
|
|
return !resp_.headers_only && resp_.http_major == 2;
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_read == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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();
|
2016-01-19 08:56:12 +01:00
|
|
|
auto &timeoutconf = get_config()->http2.timeout;
|
|
|
|
|
|
|
|
if (timeoutconf.stream_write != 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
reset_timer(loop, &upstream_wtimer_);
|
|
|
|
}
|
2016-01-19 08:56:12 +01:00
|
|
|
if (timeoutconf.stream_read != 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_write == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_read == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_write == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_read == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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();
|
2016-01-19 08:56:12 +01:00
|
|
|
auto &timeoutconf = get_config()->http2.timeout;
|
|
|
|
|
|
|
|
if (timeoutconf.stream_write != 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
reset_timer(loop, &downstream_wtimer_);
|
|
|
|
}
|
2016-01-19 08:56:12 +01:00
|
|
|
if (timeoutconf.stream_read != 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_write == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_read == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
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() {
|
2016-01-19 08:56:12 +01:00
|
|
|
if (get_config()->http2.timeout.stream_write == 0.) {
|
2015-01-05 16:30:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto loop = upstream_->get_client_handler()->get_loop();
|
|
|
|
disable_timer(loop, &downstream_wtimer_);
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
bool Downstream::accesslog_ready() const { return resp_.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; }
|
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
void Downstream::set_request_downstream_host(const StringRef &host) {
|
|
|
|
request_downstream_host_ = host;
|
2015-02-08 11:41:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:15:53 +01:00
|
|
|
void Downstream::set_request_pending(bool f) { request_pending_ = f; }
|
|
|
|
|
|
|
|
bool Downstream::get_request_pending() const { return request_pending_; }
|
|
|
|
|
|
|
|
bool Downstream::request_submission_ready() const {
|
|
|
|
return (request_state_ == Downstream::HEADER_COMPLETE ||
|
|
|
|
request_state_ == Downstream::MSG_COMPLETE) &&
|
|
|
|
request_pending_ && response_state_ == Downstream::INITIAL;
|
|
|
|
}
|
|
|
|
|
2015-03-11 16:17:05 +01:00
|
|
|
int Downstream::get_dispatch_state() const { return dispatch_state_; }
|
|
|
|
|
|
|
|
void Downstream::set_dispatch_state(int s) { dispatch_state_ = s; }
|
|
|
|
|
|
|
|
void Downstream::attach_blocked_link(BlockedLink *l) {
|
|
|
|
assert(!blocked_link_);
|
|
|
|
|
|
|
|
l->downstream = this;
|
|
|
|
blocked_link_ = l;
|
|
|
|
}
|
|
|
|
|
2015-07-15 13:44:44 +02:00
|
|
|
BlockedLink *Downstream::detach_blocked_link() {
|
|
|
|
auto link = blocked_link_;
|
2015-03-11 16:17:05 +01:00
|
|
|
blocked_link_ = nullptr;
|
2015-07-15 13:44:44 +02:00
|
|
|
return link;
|
2015-03-11 16:17:05 +01:00
|
|
|
}
|
|
|
|
|
2015-07-22 14:41:16 +02:00
|
|
|
bool Downstream::can_detach_downstream_connection() const {
|
2016-05-14 10:16:50 +02:00
|
|
|
// We should check request and response buffer. If request buffer
|
|
|
|
// is not empty, then we might leave downstream connection in weird
|
|
|
|
// state, especially for HTTP/1.1
|
2015-07-22 14:41:16 +02:00
|
|
|
return dconn_ && response_state_ == Downstream::MSG_COMPLETE &&
|
|
|
|
request_state_ == Downstream::MSG_COMPLETE && !upgraded_ &&
|
2016-05-14 10:16:50 +02:00
|
|
|
!resp_.connection_close && request_buf_.rleft() == 0;
|
2015-07-22 14:41:16 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 04:10:07 +02:00
|
|
|
DefaultMemchunks Downstream::pop_response_buf() {
|
|
|
|
return std::move(response_buf_);
|
|
|
|
}
|
|
|
|
|
2016-01-20 03:16:49 +01:00
|
|
|
void Downstream::set_assoc_stream_id(int32_t stream_id) {
|
|
|
|
assoc_stream_id_ = stream_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Downstream::get_assoc_stream_id() const { return assoc_stream_id_; }
|
|
|
|
|
2016-03-09 13:25:11 +01:00
|
|
|
BlockAllocator &Downstream::get_block_allocator() { return balloc_; }
|
2016-03-09 13:15:32 +01:00
|
|
|
|
2016-03-12 07:05:20 +01:00
|
|
|
void Downstream::add_rcbuf(nghttp2_rcbuf *rcbuf) {
|
|
|
|
nghttp2_rcbuf_incref(rcbuf);
|
|
|
|
rcbufs_.push_back(rcbuf);
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:04:47 +02:00
|
|
|
void Downstream::set_downstream_addr_group(
|
|
|
|
const std::shared_ptr<DownstreamAddrGroup> &group) {
|
|
|
|
group_ = group;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Downstream::set_addr(const DownstreamAddr *addr) { addr_ = addr; }
|
|
|
|
|
|
|
|
const DownstreamAddr *Downstream::get_addr() const { return addr_; }
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|