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.
|
|
|
|
*/
|
2013-07-26 12:38:54 +02:00
|
|
|
#include "shrpx_http2_upstream.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-01-10 16:11:41 +01:00
|
|
|
#include <netinet/tcp.h>
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <assert.h>
|
2012-07-15 14:15:28 +02:00
|
|
|
#include <cerrno>
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "shrpx_client_handler.h"
|
2013-08-03 11:51:01 +02:00
|
|
|
#include "shrpx_https_upstream.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "shrpx_downstream.h"
|
2012-06-09 16:14:00 +02:00
|
|
|
#include "shrpx_downstream_connection.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_http.h"
|
2014-07-05 11:22:40 +02:00
|
|
|
#include "shrpx_worker_config.h"
|
2013-08-27 19:47:22 +02:00
|
|
|
#include "http2.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "util.h"
|
2013-08-03 11:51:01 +02:00
|
|
|
#include "base64.h"
|
2014-02-09 10:47:26 +01:00
|
|
|
#include "app_helper.h"
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
using namespace nghttp2;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint32_t error_code, void *user_data) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "Stream stream_id=" << stream_id
|
|
|
|
<< " is being closed";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2013-08-31 17:23:07 +02:00
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream) {
|
2014-06-01 16:44:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:26:03 +02:00
|
|
|
upstream->consume(stream_id, downstream->get_request_datalen());
|
|
|
|
|
|
|
|
downstream->reset_request_datalen();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_request_state() == Downstream::CONNECT_FAIL) {
|
2014-06-01 16:44:32 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstream was deleted
|
2014-06-01 16:44:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
downstream->set_request_state(Downstream::STREAM_CLOSED);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2014-06-01 16:44:32 +02:00
|
|
|
// At this point, downstream response was read
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream->get_upgraded() &&
|
|
|
|
!downstream->get_response_connection_close()) {
|
2014-06-01 16:44:32 +02:00
|
|
|
// Keep-alive
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream->detach_downstream_connection();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
|
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstream was deleted
|
2014-06-01 16:44:32 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
|
|
|
|
// At this point, downstream read may be paused.
|
|
|
|
|
|
|
|
// If shrpx_downstream::push_request_headers() failed, the
|
|
|
|
// error is handled here.
|
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstream was deleted
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
// How to test this case? Request sufficient large download
|
|
|
|
// and make client send RST_STREAM after it gets first DATA
|
|
|
|
// frame chunk.
|
|
|
|
|
2013-08-29 15:58:05 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::upgrade_upstream(HttpsUpstream *http) {
|
2013-08-03 11:51:01 +02:00
|
|
|
int rv;
|
2014-06-15 09:14:00 +02:00
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
auto http2_settings = http->get_downstream()->get_http2_settings();
|
2014-06-15 09:14:00 +02:00
|
|
|
util::to_base64(http2_settings);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
auto settings_payload =
|
|
|
|
base64::decode(std::begin(http2_settings), std::end(http2_settings));
|
2014-06-15 09:14:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_upgrade(
|
|
|
|
session_, reinterpret_cast<const uint8_t *>(settings_payload.c_str()),
|
|
|
|
settings_payload.size(), nullptr);
|
|
|
|
if (rv != 0) {
|
2014-11-08 02:51:56 +01:00
|
|
|
ULOG(WARN, this) << "nghttp2_session_upgrade() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2013-08-03 11:51:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-09-26 14:46:35 +02:00
|
|
|
pre_upstream_.reset(http);
|
2014-08-18 15:59:31 +02:00
|
|
|
auto downstream = http->pop_downstream();
|
2013-08-03 11:51:01 +02:00
|
|
|
downstream->reset_upstream(this);
|
2014-08-18 14:36:55 +02:00
|
|
|
downstream->set_stream_id(1);
|
2014-08-15 03:29:46 +02:00
|
|
|
downstream->reset_upstream_rtimer();
|
2013-08-03 11:51:01 +02:00
|
|
|
downstream->set_stream_id(1);
|
|
|
|
downstream->set_priority(0);
|
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
downstream_queue_.add_active(std::move(downstream));
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-04-05 11:59:22 +02:00
|
|
|
ULOG(INFO, this) << "Connection upgraded to HTTP/2";
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
void Http2Upstream::start_settings_timer() {
|
|
|
|
ev_timer_start(handler_->get_loop(), &settings_timer_);
|
2013-10-30 16:44:23 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Upstream::stop_settings_timer() {
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_stop(handler_->get_loop(), &settings_timer_);
|
2013-10-30 16:44:23 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
2014-01-16 15:41:13 +01:00
|
|
|
const uint8_t *name, size_t namelen,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *value, size_t valuelen, uint8_t flags,
|
|
|
|
void *user_data) {
|
|
|
|
if (get_config()->upstream_frame_debug) {
|
2014-02-09 10:47:26 +01:00
|
|
|
verbose_on_header_callback(session, frame, name, namelen, value, valuelen,
|
2014-04-01 19:10:35 +02:00
|
|
|
flags, user_data);
|
2014-02-09 10:47:26 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
2014-01-16 15:41:13 +01:00
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
2014-01-26 16:44:08 +01:00
|
|
|
}
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_request_headers_sum() > Downstream::MAX_HEADERS_SUM) {
|
|
|
|
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2014-06-27 15:53:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-01-27 17:17:54 +01:00
|
|
|
ULOG(INFO, upstream) << "Too large header block size="
|
|
|
|
<< downstream->get_request_headers_sum();
|
2014-01-26 16:44:08 +01:00
|
|
|
}
|
2014-06-27 15:53:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->error_reply(downstream, 431) != 0) {
|
2014-06-27 15:53:54 +02:00
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2015-01-25 14:46:12 +01:00
|
|
|
if (!http2::check_nv(name, namelen, value, valuelen)) {
|
2015-01-23 16:37:26 +01:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
2014-08-08 13:52:32 +02:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto token = http2::lookup_token(name, namelen);
|
2014-08-08 13:52:32 +02:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
if (name[0] == ':') {
|
|
|
|
if (!downstream->request_pseudo_header_allowed(token)) {
|
2014-08-08 13:52:32 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
if (!http2::http2_header_allowed(token)) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-01-24 07:31:59 +01:00
|
|
|
switch (token) {
|
|
|
|
case http2::HD_CONTENT_LENGTH: {
|
2015-01-19 14:40:37 +01:00
|
|
|
auto len = util::parse_uint(value, valuelen);
|
|
|
|
if (len == -1) {
|
|
|
|
if (upstream->error_reply(downstream, 400) != 0) {
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-23 16:07:28 +01:00
|
|
|
if (downstream->get_request_content_length() != -1) {
|
2015-01-19 14:40:37 +01:00
|
|
|
if (upstream->error_reply(downstream, 400) != 0) {
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
downstream->set_request_content_length(len);
|
2015-01-24 07:31:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case http2::HD_TE:
|
2015-01-25 14:43:48 +01:00
|
|
|
if (!util::strieq("trailers", value, valuelen)) {
|
2015-01-24 07:31:59 +01:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
2015-01-19 14:40:37 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
downstream->add_request_header(name, namelen, value, valuelen,
|
|
|
|
flags & NGHTTP2_NV_FLAG_NO_INDEX, token);
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-01-29 13:23:13 +01:00
|
|
|
int on_begin_headers_callback(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
2014-01-29 13:23:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-01-29 13:23:13 +01:00
|
|
|
ULOG(INFO, upstream) << "Received upstream request HEADERS stream_id="
|
|
|
|
<< frame->hd.stream_id;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
// TODO Use priority 0 for now
|
2015-02-05 15:21:53 +01:00
|
|
|
auto downstream = make_unique<Downstream>(upstream, frame->hd.stream_id, 0);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->reset_upstream_rtimer();
|
2014-01-29 13:23:13 +01:00
|
|
|
|
2014-03-21 11:25:46 +01:00
|
|
|
// Although, we deprecated minor version from HTTP/2, we supply
|
|
|
|
// minor version 0 to use via header field in a conventional way.
|
|
|
|
downstream->set_request_major(2);
|
|
|
|
downstream->set_request_minor(0);
|
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
upstream->add_pending_downstream(std::move(downstream));
|
|
|
|
|
2014-01-29 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_request_headers(Http2Upstream *upstream, Downstream *downstream,
|
|
|
|
nghttp2_session *session, const nghttp2_frame *frame) {
|
|
|
|
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2014-06-27 15:53:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &nva = downstream->get_request_headers();
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-01-16 15:41:13 +01:00
|
|
|
std::stringstream ss;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &nv : nva) {
|
2014-04-03 04:22:11 +02:00
|
|
|
ss << TTY_HTTP_HD << nv.name << TTY_RST << ": " << nv.value << "\n";
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
ULOG(INFO, upstream) << "HTTP request headers. stream_id="
|
2014-11-27 15:39:04 +01:00
|
|
|
<< downstream->get_stream_id() << "\n" << ss.str();
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->http2_upstream_dump_request_header) {
|
2014-01-16 15:41:13 +01:00
|
|
|
http2::dump_nv(get_config()->http2_upstream_dump_request_header, nva);
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto host = downstream->get_request_header(http2::HD_HOST);
|
|
|
|
auto authority = downstream->get_request_header(http2::HD__AUTHORITY);
|
|
|
|
auto path = downstream->get_request_header(http2::HD__PATH);
|
|
|
|
auto method = downstream->get_request_header(http2::HD__METHOD);
|
|
|
|
auto scheme = downstream->get_request_header(http2::HD__SCHEME);
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool is_connect = method && "CONNECT" == method->value;
|
2014-01-16 15:41:13 +01:00
|
|
|
bool having_host = http2::non_empty_value(host);
|
|
|
|
bool having_authority = http2::non_empty_value(authority);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (is_connect) {
|
2014-01-16 15:41:13 +01:00
|
|
|
// Here we strictly require :authority header field.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (scheme || path || !having_authority) {
|
2014-07-02 17:12:16 +02:00
|
|
|
|
2014-08-08 16:11:58 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
2014-07-02 17:12:16 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For proxy, :authority is required. Otherwise, we can accept
|
|
|
|
// :authority or host for methods.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!http2::non_empty_value(method) || !http2::non_empty_value(scheme) ||
|
|
|
|
(get_config()->http2_proxy && !having_authority) ||
|
|
|
|
(!get_config()->http2_proxy && !having_authority && !having_host) ||
|
|
|
|
!http2::non_empty_value(path)) {
|
2014-07-02 17:12:16 +02:00
|
|
|
|
2014-08-08 16:11:58 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
2014-07-02 17:12:16 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
downstream->set_request_method(http2::value_to_str(method));
|
|
|
|
downstream->set_request_http2_scheme(http2::value_to_str(scheme));
|
|
|
|
downstream->set_request_http2_authority(http2::value_to_str(authority));
|
|
|
|
downstream->set_request_path(http2::value_to_str(path));
|
2014-07-03 12:59:10 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
|
2014-07-03 12:59:10 +02:00
|
|
|
downstream->set_request_http2_expect_body(true);
|
|
|
|
}
|
|
|
|
|
2014-06-15 09:14:00 +02:00
|
|
|
downstream->inspect_http2_request();
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-08-16 14:29:20 +02:00
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2015-01-17 13:31:28 +01:00
|
|
|
if (!downstream->validate_request_bodylen()) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-16 14:29:20 +02:00
|
|
|
downstream->disable_upstream_rtimer();
|
|
|
|
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
upstream->start_downstream(downstream);
|
2014-08-16 14:29:20 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
void Http2Upstream::start_downstream(Downstream *downstream) {
|
|
|
|
auto next_downstream =
|
|
|
|
downstream_queue_.pop_pending(downstream->get_stream_id());
|
|
|
|
assert(next_downstream);
|
2014-10-06 17:31:35 +02:00
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
if (downstream_queue_.can_activate(
|
|
|
|
downstream->get_request_http2_authority())) {
|
|
|
|
initiate_downstream(std::move(next_downstream));
|
|
|
|
return;
|
2014-08-16 14:29:20 +02:00
|
|
|
}
|
2014-12-04 17:07:00 +01:00
|
|
|
|
|
|
|
downstream_queue_.add_blocked(std::move(next_downstream));
|
2014-08-16 14:29:20 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void
|
|
|
|
Http2Upstream::initiate_downstream(std::unique_ptr<Downstream> downstream) {
|
2014-08-16 14:29:20 +02:00
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = downstream->attach_downstream_connection(
|
|
|
|
handler_->get_downstream_connection());
|
|
|
|
if (rv != 0) {
|
2014-07-02 17:12:16 +02:00
|
|
|
// downstream connection fails, send error page
|
2014-11-27 15:39:04 +01:00
|
|
|
if (error_reply(downstream.get(), 503) != 0) {
|
2014-08-18 15:59:31 +02:00
|
|
|
rst_stream(downstream.get(), NGHTTP2_INTERNAL_ERROR);
|
2014-07-02 17:12:16 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
downstream->set_request_state(Downstream::CONNECT_FAIL);
|
2014-07-02 17:12:16 +02:00
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
downstream_queue_.add_failure(std::move(downstream));
|
|
|
|
|
2014-08-16 14:29:20 +02:00
|
|
|
return;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
rv = downstream->push_request_headers();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-08-16 14:29:20 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (error_reply(downstream.get(), 503) != 0) {
|
2014-08-18 15:59:31 +02:00
|
|
|
rst_stream(downstream.get(), NGHTTP2_INTERNAL_ERROR);
|
2014-07-02 17:12:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
downstream_queue_.add_failure(std::move(downstream));
|
|
|
|
|
2014-08-16 14:29:20 +02:00
|
|
|
return;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
downstream_queue_.add_active(std::move(downstream));
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-08-16 14:29:20 +02:00
|
|
|
return;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
|
|
|
void *user_data) {
|
2014-01-09 15:47:21 +01:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->upstream_frame_debug) {
|
2014-02-09 10:47:26 +01:00
|
|
|
verbose_on_frame_recv_callback(session, frame, user_data);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
2014-05-24 08:02:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2014-01-27 14:13:41 +01:00
|
|
|
case NGHTTP2_DATA: {
|
2014-08-09 11:47:45 +02:00
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream) {
|
2014-08-09 11:47:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->disable_upstream_rtimer();
|
2014-05-27 17:26:27 +02:00
|
|
|
|
2015-01-17 13:31:28 +01:00
|
|
|
if (!downstream->validate_request_bodylen()) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-27 14:13:41 +01:00
|
|
|
downstream->end_upload_data();
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2014-01-27 14:13:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-05-27 17:26:27 +02:00
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream) {
|
2014-05-27 17:26:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->reset_upstream_rtimer();
|
|
|
|
|
2014-05-24 08:02:46 +02:00
|
|
|
return on_request_headers(upstream, downstream, session, frame);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2015-01-17 13:31:28 +01:00
|
|
|
if (!downstream->validate_request_bodylen()) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->disable_upstream_rtimer();
|
|
|
|
|
2014-05-24 08:02:46 +02:00
|
|
|
downstream->end_upload_data();
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
2014-07-31 14:46:50 +02:00
|
|
|
} else {
|
2014-11-27 16:23:46 +01:00
|
|
|
rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->hd.stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2014-01-18 08:12:03 +01:00
|
|
|
}
|
2014-05-24 08:02:46 +02:00
|
|
|
|
|
|
|
break;
|
2014-05-27 17:26:27 +02:00
|
|
|
}
|
2014-05-24 08:02:46 +02:00
|
|
|
case NGHTTP2_PRIORITY: {
|
2014-03-25 18:04:24 +01:00
|
|
|
// TODO comment out for now
|
|
|
|
// rv = downstream->change_priority(frame->priority.pri);
|
|
|
|
// if(rv != 0) {
|
|
|
|
// return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
// }
|
2014-01-18 08:12:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-10-30 16:44:23 +01:00
|
|
|
case NGHTTP2_SETTINGS:
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
2013-10-30 16:44:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
upstream->stop_settings_timer();
|
|
|
|
break;
|
2014-07-12 16:30:13 +02:00
|
|
|
case NGHTTP2_GOAWAY:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-07-12 16:30:13 +02:00
|
|
|
auto debug_data = util::ascii_dump(frame->goaway.opaque_data,
|
|
|
|
frame->goaway.opaque_data_len);
|
|
|
|
|
|
|
|
ULOG(INFO, upstream) << "GOAWAY received: last-stream-id="
|
|
|
|
<< frame->goaway.last_stream_id
|
2014-11-27 15:39:04 +01:00
|
|
|
<< ", error_code=" << frame->goaway.error_code
|
|
|
|
<< ", debug_data=" << debug_data;
|
2014-07-12 16:30:13 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-06-04 16:48:31 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id, const uint8_t *data,
|
|
|
|
size_t len, void *user_data) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
2013-08-31 17:23:07 +02:00
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream || !downstream->get_downstream_connection()) {
|
|
|
|
if (upstream->consume(stream_id, len) != 0) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-07-02 16:56:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->reset_upstream_rtimer();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->push_upload_data_chunk(data, len) != 0) {
|
2014-06-01 16:44:32 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->consume(stream_id, len) != 0) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return 0;
|
2013-10-29 16:00:58 +01:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2013-10-29 16:00:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-10-30 16:44:23 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_send_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
|
|
|
void *user_data) {
|
|
|
|
if (get_config()->upstream_frame_debug) {
|
2014-02-09 10:47:26 +01:00
|
|
|
verbose_on_frame_send_callback(session, frame, user_data);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
2014-07-12 16:30:13 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2014-07-12 16:30:13 +02:00
|
|
|
case NGHTTP2_SETTINGS:
|
2014-12-27 18:59:06 +01:00
|
|
|
if ((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
|
|
|
upstream->start_settings_timer();
|
2013-10-30 16:44:23 +01:00
|
|
|
}
|
2014-07-12 16:30:13 +02:00
|
|
|
break;
|
2015-02-07 08:09:49 +01:00
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
|
|
|
auto downstream = make_unique<Downstream>(
|
|
|
|
upstream, frame->push_promise.promised_stream_id, 0);
|
|
|
|
|
|
|
|
downstream->disable_upstream_rtimer();
|
|
|
|
|
|
|
|
downstream->set_request_major(2);
|
|
|
|
downstream->set_request_minor(0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < frame->push_promise.nvlen; ++i) {
|
|
|
|
auto &nv = frame->push_promise.nva[i];
|
|
|
|
auto token = http2::lookup_token(nv.name, nv.namelen);
|
|
|
|
switch (token) {
|
|
|
|
case http2::HD__METHOD:
|
|
|
|
downstream->set_request_method({nv.value, nv.value + nv.valuelen});
|
|
|
|
break;
|
|
|
|
case http2::HD__SCHEME:
|
|
|
|
downstream->set_request_http2_scheme(
|
|
|
|
{nv.value, nv.value + nv.valuelen});
|
|
|
|
break;
|
|
|
|
case http2::HD__AUTHORITY:
|
|
|
|
downstream->set_request_http2_authority(
|
|
|
|
{nv.value, nv.value + nv.valuelen});
|
|
|
|
break;
|
|
|
|
case http2::HD__PATH:
|
|
|
|
downstream->set_request_path({nv.value, nv.value + nv.valuelen});
|
|
|
|
break;
|
|
|
|
}
|
2015-02-08 06:23:22 +01:00
|
|
|
downstream->add_request_header(nv.name, nv.namelen, nv.value, nv.valuelen,
|
|
|
|
nv.flags & NGHTTP2_NV_FLAG_NO_INDEX,
|
|
|
|
token);
|
2015-02-07 08:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
downstream->inspect_http2_request();
|
|
|
|
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
|
|
|
|
// a bit weird but start_downstream() expects that given
|
|
|
|
// downstream is in pending queue.
|
|
|
|
auto ptr = downstream.get();
|
|
|
|
upstream->add_pending_downstream(std::move(downstream));
|
|
|
|
upstream->start_downstream(ptr);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-07-12 16:30:13 +02:00
|
|
|
case NGHTTP2_GOAWAY:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-07-12 16:30:13 +02:00
|
|
|
auto debug_data = util::ascii_dump(frame->goaway.opaque_data,
|
|
|
|
frame->goaway.opaque_data_len);
|
|
|
|
|
|
|
|
ULOG(INFO, upstream) << "Sending GOAWAY: last-stream-id="
|
|
|
|
<< frame->goaway.last_stream_id
|
2014-11-27 15:39:04 +01:00
|
|
|
<< ", error_code=" << frame->goaway.error_code
|
|
|
|
<< ", debug_data=" << debug_data;
|
2014-07-12 16:30:13 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-10-30 16:44:23 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-09-15 10:19:58 +02:00
|
|
|
namespace {
|
2013-08-29 14:51:58 +02:00
|
|
|
int on_frame_not_send_callback(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_frame *frame, int lib_error_code,
|
|
|
|
void *user_data) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(user_data);
|
2015-01-21 14:49:00 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, upstream) << "Failed to send control frame type="
|
|
|
|
<< static_cast<uint32_t>(frame->hd.type)
|
|
|
|
<< ", lib_error_code=" << lib_error_code << ":"
|
|
|
|
<< nghttp2_strerror(lib_error_code);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type == NGHTTP2_HEADERS &&
|
2015-01-17 11:33:30 +01:00
|
|
|
lib_error_code != NGHTTP2_ERR_STREAM_CLOSED &&
|
|
|
|
lib_error_code != NGHTTP2_ERR_STREAM_CLOSING) {
|
2012-09-15 10:19:58 +02:00
|
|
|
// To avoid stream hanging around, issue RST_STREAM.
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream) {
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 14:51:58 +02:00
|
|
|
return 0;
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-02-27 14:39:44 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t infer_upstream_rst_stream_error_code(uint32_t downstream_error_code) {
|
2014-07-03 16:00:19 +02:00
|
|
|
// NGHTTP2_REFUSED_STREAM is important because it tells upstream
|
|
|
|
// client to retry.
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (downstream_error_code) {
|
2014-07-03 16:00:19 +02:00
|
|
|
case NGHTTP2_NO_ERROR:
|
|
|
|
case NGHTTP2_REFUSED_STREAM:
|
2014-08-23 10:34:56 +02:00
|
|
|
return downstream_error_code;
|
2014-07-03 16:00:19 +02:00
|
|
|
default:
|
|
|
|
return NGHTTP2_INTERNAL_ERROR;
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-23 09:38:03 +01:00
|
|
|
namespace {
|
2014-12-27 18:59:06 +01:00
|
|
|
void settings_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(w->data);
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
ULOG(INFO, upstream) << "SETTINGS timeout";
|
|
|
|
if (upstream->terminate_session(NGHTTP2_SETTINGS_TIMEOUT) != 0) {
|
|
|
|
delete handler;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handler->signal_write();
|
2014-12-23 09:38:03 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-01-21 17:43:56 +01:00
|
|
|
namespace {
|
|
|
|
void shutdown_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(w->data);
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
upstream->submit_goaway();
|
|
|
|
handler->signal_write();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void prepare_cb(struct ev_loop *loop, ev_prepare *w, int revents) {
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(w->data);
|
|
|
|
upstream->check_shutdown();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Http2Upstream::submit_goaway() {
|
|
|
|
auto last_stream_id = nghttp2_session_get_last_proc_stream_id(session_);
|
|
|
|
nghttp2_submit_goaway(session_, NGHTTP2_FLAG_NONE, last_stream_id,
|
|
|
|
NGHTTP2_NO_ERROR, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Http2Upstream::check_shutdown() {
|
|
|
|
int rv;
|
|
|
|
if (shutdown_handled_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (worker_config->graceful_shutdown) {
|
|
|
|
shutdown_handled_ = true;
|
|
|
|
rv = nghttp2_submit_shutdown_notice(session_);
|
|
|
|
if (rv != 0) {
|
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_shutdown_notice() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handler_->signal_write();
|
|
|
|
ev_timer_start(handler_->get_loop(), &shutdown_timer_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
Http2Upstream::Http2Upstream(ClientHandler *handler)
|
2015-01-02 04:53:27 +01:00
|
|
|
: downstream_queue_(
|
|
|
|
get_config()->http2_proxy
|
|
|
|
? get_config()->downstream_connections_per_host
|
|
|
|
: get_config()->downstream_proto == PROTO_HTTP
|
|
|
|
? get_config()->downstream_connections_per_frontend
|
|
|
|
: 0,
|
|
|
|
!get_config()->http2_proxy),
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_(handler), session_(nullptr), data_pending_(nullptr),
|
2015-01-21 17:43:56 +01:00
|
|
|
data_pendinglen_(0), shutdown_handled_(false) {
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-08-22 13:59:50 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
nghttp2_session_callbacks *callbacks;
|
|
|
|
rv = nghttp2_session_callbacks_new(&callbacks);
|
|
|
|
|
|
|
|
assert(rv == 0);
|
|
|
|
|
2015-02-06 15:27:15 +01:00
|
|
|
auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback(
|
|
|
|
callbacks, on_stream_close_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
|
|
|
|
on_frame_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
|
|
|
|
callbacks, on_data_chunk_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
|
|
|
|
on_frame_send_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_not_send_callback(
|
|
|
|
callbacks, on_frame_not_send_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_header_callback(callbacks,
|
|
|
|
on_header_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_begin_headers_callback(
|
|
|
|
callbacks, on_begin_headers_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->padding) {
|
|
|
|
nghttp2_session_callbacks_set_select_padding_callback(
|
|
|
|
callbacks, http::select_padding_callback);
|
2014-02-11 09:23:22 +01:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-08-22 13:59:50 +02:00
|
|
|
rv = nghttp2_session_server_new2(&session_, callbacks, this,
|
2014-04-04 14:57:47 +02:00
|
|
|
get_config()->http2_option);
|
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
assert(rv == 0);
|
|
|
|
|
|
|
|
flow_control_ = true;
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
// TODO Maybe call from outside?
|
2015-02-05 16:06:01 +01:00
|
|
|
std::array<nghttp2_settings_entry, 2> entry;
|
2013-07-12 17:19:03 +02:00
|
|
|
entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
|
2013-11-04 10:14:05 +01:00
|
|
|
entry[0].value = get_config()->http2_max_concurrent_streams;
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
entry[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
2013-11-12 03:08:43 +01:00
|
|
|
entry[1].value = (1 << get_config()->http2_upstream_window_bits) - 1;
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(),
|
|
|
|
entry.size());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-04-03 06:20:50 +02:00
|
|
|
ULOG(ERROR, this) << "nghttp2_submit_settings() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
}
|
2013-11-20 16:15:17 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->http2_upstream_connection_window_bits > 16) {
|
|
|
|
int32_t delta = (1 << get_config()->http2_upstream_connection_window_bits) -
|
|
|
|
1 - NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
|
2013-11-20 16:15:17 +01:00
|
|
|
rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE, 0, delta);
|
2014-04-03 06:20:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-04-03 06:20:50 +02:00
|
|
|
ULOG(ERROR, this) << "nghttp2_submit_window_update() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->altsvcs.empty()) {
|
2014-04-03 06:20:50 +02:00
|
|
|
// Set max_age to 24hrs, which is default for alt-svc header
|
|
|
|
// field.
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &altsvc : get_config()->altsvcs) {
|
|
|
|
rv = nghttp2_submit_altsvc(
|
|
|
|
session_, NGHTTP2_FLAG_NONE, 0, 86400, altsvc.port,
|
|
|
|
reinterpret_cast<const uint8_t *>(altsvc.protocol_id),
|
|
|
|
altsvc.protocol_id_len,
|
|
|
|
reinterpret_cast<const uint8_t *>(altsvc.host), altsvc.host_len,
|
|
|
|
reinterpret_cast<const uint8_t *>(altsvc.origin), altsvc.origin_len);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
2014-04-08 15:28:50 +02:00
|
|
|
ULOG(ERROR, this) << "nghttp2_submit_altsvc() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
}
|
2014-04-03 06:20:50 +02:00
|
|
|
}
|
2013-11-20 16:15:17 +01:00
|
|
|
}
|
2014-12-23 09:38:03 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
// We wait for SETTINGS ACK at least 10 seconds.
|
|
|
|
ev_timer_init(&settings_timer_, settings_timeout_cb, 10., 0.);
|
|
|
|
|
|
|
|
settings_timer_.data = this;
|
|
|
|
|
2015-01-21 17:43:56 +01:00
|
|
|
// timer for 2nd GOAWAY. HTTP/2 spec recommend 1 RTT. We wait for
|
|
|
|
// 2 seconds.
|
|
|
|
ev_timer_init(&shutdown_timer_, shutdown_timeout_cb, 2., 0);
|
|
|
|
shutdown_timer_.data = this;
|
|
|
|
|
|
|
|
ev_prepare_init(&prep_, prepare_cb);
|
|
|
|
prep_.data = this;
|
|
|
|
ev_prepare_start(handler_->get_loop(), &prep_);
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_->reset_upstream_read_timeout(
|
|
|
|
get_config()->http2_upstream_read_timeout);
|
|
|
|
|
|
|
|
handler_->signal_write();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Http2Upstream::~Http2Upstream() {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_del(session_);
|
2015-01-21 17:43:56 +01:00
|
|
|
ev_prepare_stop(handler_->get_loop(), &prep_);
|
|
|
|
ev_timer_stop(handler_->get_loop(), &shutdown_timer_);
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_stop(handler_->get_loop(), &settings_timer_);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::on_read() {
|
2014-01-18 08:50:52 +01:00
|
|
|
ssize_t rv = 0;
|
2014-12-27 18:59:06 +01:00
|
|
|
auto rb = handler_->get_rb();
|
2015-02-13 14:41:50 +01:00
|
|
|
auto rlimit = handler_->get_rlimit();
|
2014-01-18 08:50:52 +01:00
|
|
|
|
2015-01-29 14:47:37 +01:00
|
|
|
if (rb->rleft()) {
|
|
|
|
rv = nghttp2_session_mem_recv(session_, rb->pos, rb->rleft());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2015-02-01 16:20:44 +01:00
|
|
|
if (rv != NGHTTP2_ERR_BAD_PREFACE) {
|
|
|
|
ULOG(ERROR, this) << "nghttp2_session_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:37 +01:00
|
|
|
// nghttp2_session_mem_recv should consume all input bytes on
|
|
|
|
// success.
|
|
|
|
assert(static_cast<size_t>(rv) == rb->rleft());
|
|
|
|
rb->reset();
|
2015-02-13 14:41:50 +01:00
|
|
|
rlimit->startw();
|
2014-01-18 08:50:52 +01:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
auto wb = handler_->get_wb();
|
|
|
|
if (nghttp2_session_want_read(session_) == 0 &&
|
|
|
|
nghttp2_session_want_write(session_) == 0 && wb->rleft() == 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, this) << "No more read/write for this HTTP2 session";
|
|
|
|
}
|
2014-12-23 09:38:03 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_->signal_write();
|
2014-12-23 09:38:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After this function call, downstream may be deleted.
|
2014-12-27 18:59:06 +01:00
|
|
|
int Http2Upstream::on_write() {
|
|
|
|
auto wb = handler_->get_wb();
|
|
|
|
|
|
|
|
if (data_pending_) {
|
|
|
|
auto n = std::min(wb->wleft(), data_pendinglen_);
|
|
|
|
wb->write(data_pending_, n);
|
|
|
|
if (n < data_pendinglen_) {
|
2015-01-02 05:42:32 +01:00
|
|
|
data_pending_ += n;
|
2014-12-27 18:59:06 +01:00
|
|
|
data_pendinglen_ -= n;
|
|
|
|
return 0;
|
2014-02-18 15:23:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
data_pending_ = nullptr;
|
|
|
|
data_pendinglen_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2014-02-18 15:23:11 +01:00
|
|
|
const uint8_t *data;
|
|
|
|
auto datalen = nghttp2_session_mem_send(session_, &data);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (datalen < 0) {
|
2014-02-18 15:23:11 +01:00
|
|
|
ULOG(ERROR, this) << "nghttp2_session_mem_send() returned error: "
|
|
|
|
<< nghttp2_strerror(datalen);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (datalen == 0) {
|
2014-02-18 15:23:11 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
auto n = wb->write(data, datalen);
|
|
|
|
if (n < static_cast<decltype(n)>(datalen)) {
|
|
|
|
data_pending_ = data + n;
|
|
|
|
data_pendinglen_ = datalen - n;
|
|
|
|
return 0;
|
2014-12-23 09:38:03 +01:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-03-03 13:18:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_want_read(session_) == 0 &&
|
2014-12-27 18:59:06 +01:00
|
|
|
nghttp2_session_want_write(session_) == 0 && wb->rleft() == 0) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-02-18 15:23:11 +01:00
|
|
|
ULOG(INFO, this) << "No more read/write for this HTTP2 session";
|
2012-07-18 18:59:55 +02:00
|
|
|
}
|
2014-02-18 15:23:11 +01:00
|
|
|
return -1;
|
2012-07-18 18:59:55 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2014-02-18 15:23:11 +01:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ClientHandler *Http2Upstream::get_client_handler() const { return handler_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int Http2Upstream::downstream_read(DownstreamConnection *dconn) {
|
2013-08-31 17:23:07 +02:00
|
|
|
auto downstream = dconn->get_downstream();
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
2013-11-04 10:22:29 +01:00
|
|
|
// If upstream HTTP2 stream was closed, we just close downstream,
|
2012-06-09 16:14:00 +02:00
|
|
|
// because there is no consumer now. Downstream connection is also
|
|
|
|
// closed in this case.
|
2014-12-27 18:59:06 +01:00
|
|
|
remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstream was deleted
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
return 0;
|
2012-06-05 15:46:47 +02:00
|
|
|
}
|
2013-02-09 15:20:29 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::MSG_RESET) {
|
2013-02-09 15:20:29 +01:00
|
|
|
// The downstream stream was reset (canceled). In this case,
|
|
|
|
// RST_STREAM to the upstream and delete downstream connection
|
|
|
|
// here. Deleting downstream will be taken place at
|
|
|
|
// on_stream_close_callback.
|
2014-12-27 18:59:06 +01:00
|
|
|
rst_stream(downstream,
|
|
|
|
infer_upstream_rst_stream_error_code(
|
|
|
|
downstream->get_response_rst_stream_error_code()));
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
// dconn was deleted
|
2014-06-01 16:44:32 +02:00
|
|
|
dconn = nullptr;
|
2015-01-19 15:44:23 +01:00
|
|
|
} else if (downstream->get_response_state() == Downstream::MSG_BAD_HEADER) {
|
|
|
|
if (error_reply(downstream, 502) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
// dconn was deleted
|
|
|
|
dconn = nullptr;
|
2013-02-27 14:39:44 +01:00
|
|
|
} else {
|
2014-06-01 16:44:32 +02:00
|
|
|
auto rv = downstream->on_read();
|
2015-02-04 13:15:58 +01:00
|
|
|
if (rv == SHRPX_ERR_EOF) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return downstream_eof(dconn);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
if (rv != SHRPX_ERR_NETWORK) {
|
2014-12-27 18:59:06 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, dconn) << "HTTP parser failure";
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
2012-07-16 16:29:48 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
return downstream_error(dconn, Downstream::EVENT_ERROR);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2015-01-30 17:11:55 +01:00
|
|
|
// Detach downstream connection early so that it could be reused
|
|
|
|
// without hitting server's request timeout.
|
|
|
|
if (downstream->get_response_state() == Downstream::MSG_COMPLETE &&
|
|
|
|
!downstream->get_response_connection_close()) {
|
|
|
|
// Keep-alive
|
|
|
|
downstream->detach_downstream_connection();
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
handler_->signal_write();
|
|
|
|
|
2012-06-12 14:56:41 +02:00
|
|
|
// At this point, downstream may be deleted.
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int Http2Upstream::downstream_write(DownstreamConnection *dconn) {
|
|
|
|
int rv;
|
|
|
|
rv = dconn->on_write();
|
2015-02-04 13:15:58 +01:00
|
|
|
if (rv == SHRPX_ERR_NETWORK) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return downstream_error(dconn, Downstream::EVENT_ERROR);
|
2012-11-21 19:13:30 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int Http2Upstream::downstream_eof(DownstreamConnection *dconn) {
|
2013-08-31 17:23:07 +02:00
|
|
|
auto downstream = dconn->get_downstream();
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, dconn) << "EOF. stream_id=" << downstream->get_stream_id();
|
2014-06-01 16:44:32 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
|
|
|
// If stream was closed already, we don't need to send reply at
|
|
|
|
// the first place. We can delete downstream.
|
|
|
|
remove_downstream(downstream);
|
|
|
|
// downstream was deleted
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete downstream connection. If we don't delete it here, it will
|
|
|
|
// be pooled in on_stream_close_callback.
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
// dconn was deleted
|
|
|
|
dconn = nullptr;
|
|
|
|
// downstream wil be deleted in on_stream_close_callback.
|
|
|
|
if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
|
|
|
// Server may indicate the end of the request by EOF
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-12-27 18:59:06 +01:00
|
|
|
ULOG(INFO, this) << "Downstream body was ended by EOF";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
|
|
|
|
// For tunneled connection, MSG_COMPLETE signals
|
|
|
|
// downstream_data_read_callback to send RST_STREAM after pending
|
|
|
|
// response body is sent. This is needed to ensure that RST_STREAM
|
|
|
|
// is sent after all pending data are sent.
|
|
|
|
on_downstream_body_complete(downstream);
|
|
|
|
} else if (downstream->get_response_state() != Downstream::MSG_COMPLETE) {
|
|
|
|
// If stream was not closed, then we set MSG_COMPLETE and let
|
|
|
|
// on_stream_close_callback delete downstream.
|
|
|
|
if (error_reply(downstream, 502) != 0) {
|
|
|
|
return -1;
|
2014-06-01 16:44:32 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
handler_->signal_write();
|
|
|
|
// At this point, downstream may be deleted.
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int Http2Upstream::downstream_error(DownstreamConnection *dconn, int events) {
|
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
if (events & Downstream::EVENT_ERROR) {
|
|
|
|
DCLOG(INFO, dconn) << "Downstream network/general error";
|
|
|
|
} else {
|
|
|
|
DCLOG(INFO, dconn) << "Timeout";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
if (downstream->get_upgraded()) {
|
|
|
|
DCLOG(INFO, dconn) << "Note: this is tunnel connection";
|
2014-06-01 16:44:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
|
|
|
remove_downstream(downstream);
|
|
|
|
// downstream was deleted
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
// Delete downstream connection. If we don't delete it here, it will
|
|
|
|
// be pooled in on_stream_close_callback.
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
// dconn was deleted
|
|
|
|
dconn = nullptr;
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
// For SSL tunneling, we issue RST_STREAM. For other types of
|
|
|
|
// stream, we don't have to do anything since response was
|
|
|
|
// complete.
|
|
|
|
if (downstream->get_upgraded()) {
|
|
|
|
rst_stream(downstream, NGHTTP2_NO_ERROR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_upgraded()) {
|
2014-12-27 18:59:06 +01:00
|
|
|
on_downstream_body_complete(downstream);
|
|
|
|
} else {
|
|
|
|
rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2014-06-01 16:44:32 +02:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
} else {
|
2014-12-27 18:59:06 +01:00
|
|
|
unsigned int status;
|
|
|
|
if (events & Downstream::EVENT_TIMEOUT) {
|
|
|
|
status = 504;
|
2012-07-11 09:20:16 +02:00
|
|
|
} else {
|
2014-12-27 18:59:06 +01:00
|
|
|
status = 502;
|
|
|
|
}
|
|
|
|
if (error_reply(downstream, status) != 0) {
|
|
|
|
return -1;
|
2012-07-16 16:29:48 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_->signal_write();
|
|
|
|
// At this point, downstream may be deleted.
|
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::rst_stream(Downstream *downstream, uint32_t error_code) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, this) << "RST_STREAM stream_id=" << downstream->get_stream_id()
|
|
|
|
<< " with error_code=" << error_code;
|
2012-06-09 18:36:30 +02:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
int rv;
|
2013-10-25 15:50:24 +02:00
|
|
|
rv = nghttp2_submit_rst_stream(session_, NGHTTP2_FLAG_NONE,
|
|
|
|
downstream->get_stream_id(), error_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < NGHTTP2_ERR_FATAL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_rst_stream() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-06-04 16:48:31 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2012-07-27 15:11:13 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::terminate_session(uint32_t error_code) {
|
2013-10-30 16:44:23 +01:00
|
|
|
int rv;
|
2013-12-25 16:23:07 +01:00
|
|
|
rv = nghttp2_session_terminate_session(session_, error_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-10-30 16:44:23 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2013-11-04 09:53:57 +01:00
|
|
|
ssize_t downstream_data_read_callback(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id, uint8_t *buf,
|
|
|
|
size_t length, uint32_t *data_flags,
|
2013-11-04 09:53:57 +01:00
|
|
|
nghttp2_data_source *source,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data) {
|
|
|
|
auto downstream = static_cast<Downstream *>(source->ptr);
|
|
|
|
auto upstream = static_cast<Http2Upstream *>(downstream->get_upstream());
|
2014-12-27 18:59:06 +01:00
|
|
|
auto body = downstream->get_response_buf();
|
2012-06-04 16:48:31 +02:00
|
|
|
assert(body);
|
2014-04-03 11:54:15 +02:00
|
|
|
|
2015-01-17 09:37:32 +01:00
|
|
|
auto dconn = downstream->get_downstream_connection();
|
|
|
|
|
2015-01-20 17:41:17 +01:00
|
|
|
if (body->rleft() == 0 && dconn &&
|
|
|
|
downstream->get_response_state() != Downstream::MSG_COMPLETE) {
|
2015-01-17 09:37:32 +01:00
|
|
|
// Try to read more if buffer is empty. This will help small
|
|
|
|
// buffer and make priority handling a bit better.
|
|
|
|
if (upstream->downstream_read(dconn) != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
auto nread = body->remove(buf, length);
|
|
|
|
auto body_empty = body->rleft() == 0;
|
2014-11-22 09:32:59 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (body_empty &&
|
|
|
|
downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2014-11-17 16:03:52 +01:00
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream->get_upgraded()) {
|
2014-07-02 16:56:26 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_get_stream_remote_close(session, stream_id) == 0) {
|
2014-07-02 16:56:26 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_NO_ERROR);
|
|
|
|
}
|
2013-02-10 18:05:11 +01:00
|
|
|
} else {
|
|
|
|
// For tunneling, issue RST_STREAM to finish the stream.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, upstream)
|
|
|
|
<< "RST_STREAM to tunneled stream stream_id=" << stream_id;
|
2013-02-10 18:05:11 +01:00
|
|
|
}
|
2014-07-03 15:59:49 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_NO_ERROR);
|
2013-02-10 18:05:11 +01:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-05-16 14:42:30 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (body_empty) {
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->disable_upstream_wtimer();
|
2014-11-22 09:32:59 +01:00
|
|
|
} else {
|
|
|
|
downstream->reset_upstream_wtimer();
|
2014-08-09 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread > 0 && downstream->resume_read(SHRPX_NO_BUFFER, nread) != 0) {
|
2014-08-21 14:22:16 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2014-05-16 14:42:30 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == 0 && ((*data_flags) & NGHTTP2_DATA_FLAG_EOF) == 0) {
|
2014-08-21 14:22:16 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-08-21 14:22:16 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread > 0) {
|
2014-11-18 16:56:44 +01:00
|
|
|
downstream->add_response_sent_bodylen(nread);
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-08 07:29:18 +02:00
|
|
|
int Http2Upstream::error_reply(Downstream *downstream,
|
2014-11-27 15:39:04 +01:00
|
|
|
unsigned int status_code) {
|
2012-06-04 16:48:31 +02:00
|
|
|
int rv;
|
2013-08-31 17:23:07 +02:00
|
|
|
auto html = http::create_error_html(status_code);
|
2014-07-05 11:22:40 +02:00
|
|
|
downstream->set_response_http_status(status_code);
|
2014-12-27 18:59:06 +01:00
|
|
|
auto body = downstream->get_response_buf();
|
|
|
|
body->append(html.c_str(), html.size());
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
2012-07-27 15:11:13 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_data_provider data_prd;
|
2012-06-04 16:48:31 +02:00
|
|
|
data_prd.source.ptr = downstream;
|
2013-11-04 09:53:57 +01:00
|
|
|
data_prd.read_callback = downstream_data_read_callback;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-08-31 17:23:07 +02:00
|
|
|
auto content_length = util::utos(html.size());
|
2013-09-08 07:29:18 +02:00
|
|
|
auto status_code_str = util::utos(status_code);
|
2015-02-05 15:39:36 +01:00
|
|
|
auto nva =
|
|
|
|
make_array(http2::make_nv_ls(":status", status_code_str),
|
|
|
|
http2::make_nv_ll("content-type", "text/html; charset=UTF-8"),
|
|
|
|
http2::make_nv_lc("server", get_config()->server_name),
|
|
|
|
http2::make_nv_ls("content-length", content_length));
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-12-08 13:19:33 +01:00
|
|
|
rv = nghttp2_submit_response(session_, downstream->get_stream_id(),
|
|
|
|
nva.data(), nva.size(), &data_prd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < NGHTTP2_ERR_FATAL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_response() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2015-01-21 14:55:00 +01:00
|
|
|
return -1;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-07-02 16:56:26 +02:00
|
|
|
|
2012-07-27 15:11:13 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void
|
|
|
|
Http2Upstream::add_pending_downstream(std::unique_ptr<Downstream> downstream) {
|
2014-08-18 15:59:31 +02:00
|
|
|
downstream_queue_.add_pending(std::move(downstream));
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Upstream::remove_downstream(Downstream *downstream) {
|
|
|
|
if (downstream->accesslog_ready()) {
|
2014-11-18 16:56:44 +01:00
|
|
|
handler_->write_accesslog(downstream);
|
|
|
|
}
|
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
auto next_downstream =
|
|
|
|
downstream_queue_.remove_and_pop_blocked(downstream->get_stream_id());
|
2014-08-16 14:29:20 +02:00
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
if (next_downstream) {
|
|
|
|
initiate_downstream(std::move(next_downstream));
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Downstream *Http2Upstream::find_downstream(int32_t stream_id) {
|
2012-06-07 17:36:19 +02:00
|
|
|
return downstream_queue_.find(stream_id);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *Http2Upstream::get_http2_session() { return session_; }
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
// WARNING: Never call directly or indirectly nghttp2_session_send or
|
|
|
|
// nghttp2_session_recv. These calls may delete downstream.
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::on_downstream_header_complete(Downstream *downstream) {
|
2014-07-23 16:32:57 +02:00
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
if (downstream->get_non_final_response()) {
|
2014-07-23 16:32:57 +02:00
|
|
|
DLOG(INFO, downstream) << "HTTP non-final response header";
|
|
|
|
} else {
|
|
|
|
DLOG(INFO, downstream) << "HTTP response header completed";
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->http2_proxy && !get_config()->client_proxy &&
|
|
|
|
!get_config()->no_location_rewrite) {
|
2015-01-04 15:22:39 +01:00
|
|
|
downstream->rewrite_location_response_header(
|
2015-02-08 11:41:45 +01:00
|
|
|
downstream->get_request_http2_scheme());
|
2013-12-28 09:02:43 +01:00
|
|
|
}
|
2014-07-12 11:55:08 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
size_t nheader = downstream->get_response_headers().size();
|
2013-11-28 13:36:04 +01:00
|
|
|
auto nva = std::vector<nghttp2_nv>();
|
2014-08-14 15:45:21 +02:00
|
|
|
// 3 means :status and possible server and via header field.
|
|
|
|
nva.reserve(nheader + 3 + get_config()->add_response_headers.size());
|
2012-06-06 19:29:00 +02:00
|
|
|
std::string via_value;
|
2013-09-08 07:29:18 +02:00
|
|
|
auto response_status = util::utos(downstream->get_response_http_status());
|
2013-12-08 14:31:43 +01:00
|
|
|
nva.push_back(http2::make_nv_ls(":status", response_status));
|
2013-08-27 17:09:46 +02:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
http2::copy_headers_to_nva(nva, downstream->get_response_headers());
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_non_final_response()) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-07-25 17:40:06 +02:00
|
|
|
log_response_headers(downstream, nva);
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:32:57 +02:00
|
|
|
rv = nghttp2_submit_headers(session_, NGHTTP2_FLAG_NONE,
|
|
|
|
downstream->get_stream_id(), nullptr,
|
|
|
|
nva.data(), nva.size(), nullptr);
|
|
|
|
|
|
|
|
downstream->clear_response_headers();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-07-23 16:32:57 +02:00
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_headers() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->http2_proxy && !get_config()->client_proxy) {
|
2014-08-14 15:45:21 +02:00
|
|
|
nva.push_back(http2::make_nv_lc("server", get_config()->server_name));
|
|
|
|
} else {
|
2015-01-04 15:22:39 +01:00
|
|
|
auto server = downstream->get_response_header(http2::HD_SERVER);
|
|
|
|
if (server) {
|
2014-08-14 15:45:21 +02:00
|
|
|
nva.push_back(http2::make_nv_ls("server", (*server).value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto via = downstream->get_response_header(http2::HD_VIA);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->no_via) {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (via) {
|
2014-04-03 04:22:11 +02:00
|
|
|
nva.push_back(http2::make_nv_ls("via", (*via).value));
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
} else {
|
2015-01-04 15:22:39 +01:00
|
|
|
if (via) {
|
2014-04-03 04:22:11 +02:00
|
|
|
via_value = (*via).value;
|
2013-01-09 14:01:25 +01:00
|
|
|
via_value += ", ";
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
via_value += http::create_via_header_value(
|
|
|
|
downstream->get_response_major(), downstream->get_response_minor());
|
2013-12-08 14:31:43 +01:00
|
|
|
nva.push_back(http2::make_nv_ls("via", via_value));
|
2012-06-06 19:29:00 +02:00
|
|
|
}
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &p : get_config()->add_response_headers) {
|
2014-04-26 07:56:08 +02:00
|
|
|
nva.push_back(http2::make_nv(p.first, p.second));
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-07-25 17:40:06 +02:00
|
|
|
log_response_headers(downstream, nva);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-11-17 15:52:19 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->http2_upstream_dump_response_header) {
|
2013-11-17 15:52:19 +01:00
|
|
|
http2::dump_nv(get_config()->http2_upstream_dump_response_header,
|
2013-11-28 13:36:04 +01:00
|
|
|
nva.data(), nva.size());
|
2013-11-17 15:52:19 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_data_provider data_prd;
|
2012-06-04 16:48:31 +02:00
|
|
|
data_prd.source.ptr = downstream;
|
2013-11-04 09:53:57 +01:00
|
|
|
data_prd.read_callback = downstream_data_read_callback;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-07-25 16:13:27 +02:00
|
|
|
nghttp2_data_provider *data_prdptr;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->expect_response_body()) {
|
2014-07-25 16:13:27 +02:00
|
|
|
data_prdptr = &data_prd;
|
|
|
|
} else {
|
|
|
|
data_prdptr = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-08 13:19:33 +01:00
|
|
|
rv = nghttp2_submit_response(session_, downstream->get_stream_id(),
|
2014-11-27 15:39:04 +01:00
|
|
|
nva.data(), nva.size(), data_prdptr);
|
|
|
|
if (rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_response() failed";
|
2012-07-16 16:55:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-02-08 08:04:36 +01:00
|
|
|
// We need some conditions that must be fulfilled to initiate server
|
|
|
|
// push.
|
|
|
|
//
|
|
|
|
// * Server push is disabled for http2 proxy, since incoming headers
|
|
|
|
// are mixed origins. We don't know how to reliably determine the
|
|
|
|
// authority yet.
|
|
|
|
//
|
|
|
|
// * If downstream is http/2, it is likely that PUSH_PROMISE is
|
|
|
|
// coming from there, so we don't initiate PUSH_RPOMISE here.
|
|
|
|
//
|
|
|
|
// * We need 200 response code for associated resource. This is too
|
|
|
|
// restrictive, we will review this later.
|
|
|
|
//
|
|
|
|
// * We requires GET or POST for associated resource. Probably we
|
|
|
|
// don't want to push for HEAD request. Not sure other methods
|
|
|
|
// are also eligible for push.
|
2015-02-08 08:19:12 +01:00
|
|
|
if (!get_config()->no_server_push &&
|
|
|
|
get_config()->downstream_proto == PROTO_HTTP &&
|
2015-02-08 08:04:36 +01:00
|
|
|
!get_config()->http2_proxy && (downstream->get_stream_id() % 2) &&
|
2015-02-07 08:09:49 +01:00
|
|
|
downstream->get_response_header(http2::HD_LINK) &&
|
|
|
|
downstream->get_response_http_status() == 200 &&
|
|
|
|
(downstream->get_request_method() == "GET" ||
|
|
|
|
downstream->get_request_method() == "POST")) {
|
|
|
|
|
|
|
|
if (prepare_push_promise(downstream) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
// WARNING: Never call directly or indirectly nghttp2_session_send or
|
|
|
|
// nghttp2_session_recv. These calls may delete downstream.
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::on_downstream_body(Downstream *downstream,
|
2014-04-03 11:54:15 +02:00
|
|
|
const uint8_t *data, size_t len,
|
2014-11-27 15:39:04 +01:00
|
|
|
bool flush) {
|
2014-12-27 18:59:06 +01:00
|
|
|
auto body = downstream->get_response_buf();
|
|
|
|
body->append(data, len);
|
2014-04-03 11:54:15 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (flush) {
|
2014-04-03 11:54:15 +02:00
|
|
|
nghttp2_session_resume_data(session_, downstream->get_stream_id());
|
2014-08-09 11:47:45 +02:00
|
|
|
|
|
|
|
downstream->ensure_upstream_wtimer();
|
2014-04-03 11:54:15 +02:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
// WARNING: Never call directly or indirectly nghttp2_session_send or
|
|
|
|
// nghttp2_session_recv. These calls may delete downstream.
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::on_downstream_body_complete(Downstream *downstream) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DLOG(INFO, downstream) << "HTTP response completed";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2015-01-17 11:33:30 +01:00
|
|
|
|
|
|
|
if (!downstream->validate_response_bodylen()) {
|
|
|
|
rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
downstream->set_response_connection_close(true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_resume_data(session_, downstream->get_stream_id());
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->ensure_upstream_wtimer();
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool Http2Upstream::get_flow_control() const { return flow_control_; }
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Upstream::pause_read(IOCtrlReason reason) {}
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2014-08-21 14:22:16 +02:00
|
|
|
int Http2Upstream::resume_read(IOCtrlReason reason, Downstream *downstream,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t consumed) {
|
|
|
|
if (get_flow_control()) {
|
2014-08-21 14:22:16 +02:00
|
|
|
assert(downstream->get_request_datalen() >= consumed);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (consume(downstream->get_stream_id(), consumed) != 0) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return -1;
|
2013-02-08 13:46:58 +01:00
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-08-21 14:22:16 +02:00
|
|
|
downstream->dec_request_datalen(consumed);
|
2013-02-08 13:46:58 +01:00
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_->signal_write();
|
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2014-06-27 15:34:54 +02:00
|
|
|
int Http2Upstream::on_downstream_abort_request(Downstream *downstream,
|
2014-11-27 15:39:04 +01:00
|
|
|
unsigned int status_code) {
|
2014-06-27 15:34:54 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = error_reply(downstream, status_code);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-06-27 15:34:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_->signal_write();
|
|
|
|
return 0;
|
2014-06-27 15:34:54 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::consume(int32_t stream_id, size_t len) {
|
2014-07-25 14:26:03 +02:00
|
|
|
int rv;
|
2014-07-02 16:07:46 +02:00
|
|
|
|
2014-07-25 14:26:03 +02:00
|
|
|
rv = nghttp2_session_consume(session_, stream_id, len);
|
2014-07-02 16:07:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-11-08 02:51:56 +01:00
|
|
|
ULOG(WARN, this) << "nghttp2_session_consume() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2014-07-25 14:26:03 +02:00
|
|
|
return -1;
|
2014-07-02 16:07:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void
|
|
|
|
Http2Upstream::log_response_headers(Downstream *downstream,
|
|
|
|
const std::vector<nghttp2_nv> &nva) const {
|
2014-07-25 17:40:06 +02:00
|
|
|
std::stringstream ss;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &nv : nva) {
|
2014-07-25 17:40:06 +02:00
|
|
|
ss << TTY_HTTP_HD;
|
2014-11-27 15:39:04 +01:00
|
|
|
ss.write(reinterpret_cast<const char *>(nv.name), nv.namelen);
|
2014-07-25 17:40:06 +02:00
|
|
|
ss << TTY_RST << ": ";
|
2014-11-27 15:39:04 +01:00
|
|
|
ss.write(reinterpret_cast<const char *>(nv.value), nv.valuelen);
|
2014-07-25 17:40:06 +02:00
|
|
|
ss << "\n";
|
|
|
|
}
|
|
|
|
ULOG(INFO, this) << "HTTP response headers. stream_id="
|
2014-11-27 15:39:04 +01:00
|
|
|
<< downstream->get_stream_id() << "\n" << ss.str();
|
2014-07-25 17:40:06 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Upstream::on_timeout(Downstream *downstream) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-08-09 11:47:45 +02:00
|
|
|
ULOG(INFO, this) << "Stream timeout stream_id="
|
|
|
|
<< downstream->get_stream_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
rst_stream(downstream, NGHTTP2_NO_ERROR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Upstream::on_handler_delete() {
|
|
|
|
for (auto &ent : downstream_queue_.get_active_downstreams()) {
|
|
|
|
if (ent.second->accesslog_ready()) {
|
2014-11-23 09:24:23 +01:00
|
|
|
handler_->write_accesslog(ent.second.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:59:09 +01:00
|
|
|
|
2015-01-21 15:30:48 +01:00
|
|
|
int Http2Upstream::on_downstream_reset(bool no_retry) {
|
nghttpx: Check HTTP/2 downstream connection after certain idle time
Previously when requests are issued to HTTP/2 downstream connection,
but it turns out that connection is down, handlers of those requests
are deleted. In some situations, we only know connection is down when
we write something to network, so we'd like to handle this kind of
situation in more robust manner. In this change, certain seconds
passed after last network activity, we first issue PING frame to
downstream connection before issuing new HTTP request. If writing
PING frame is failed, it means connection was lost. In this case,
instead of deleting handler, pending requests are migrated to new
HTTP2/ downstream connection, so that it can continue without
affecting upstream connection.
2014-12-08 17:30:15 +01:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
for (auto &ent : downstream_queue_.get_active_downstreams()) {
|
|
|
|
auto downstream = ent.second.get();
|
|
|
|
if ((downstream->get_request_state() != Downstream::HEADER_COMPLETE &&
|
|
|
|
downstream->get_request_state() != Downstream::MSG_COMPLETE) ||
|
|
|
|
downstream->get_response_state() != Downstream::INITIAL) {
|
|
|
|
rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-21 15:30:48 +01:00
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
|
2015-02-02 17:47:04 +01:00
|
|
|
downstream->add_retry();
|
|
|
|
|
|
|
|
if (no_retry || downstream->no_more_retry()) {
|
2015-01-21 15:30:48 +01:00
|
|
|
if (on_downstream_abort_request(downstream, 503) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
nghttpx: Check HTTP/2 downstream connection after certain idle time
Previously when requests are issued to HTTP/2 downstream connection,
but it turns out that connection is down, handlers of those requests
are deleted. In some situations, we only know connection is down when
we write something to network, so we'd like to handle this kind of
situation in more robust manner. In this change, certain seconds
passed after last network activity, we first issue PING frame to
downstream connection before issuing new HTTP request. If writing
PING frame is failed, it means connection was lost. In this case,
instead of deleting handler, pending requests are migrated to new
HTTP2/ downstream connection, so that it can continue without
affecting upstream connection.
2014-12-08 17:30:15 +01:00
|
|
|
// downstream connection is clean; we can retry with new
|
|
|
|
// downstream connection.
|
|
|
|
|
|
|
|
rv = downstream->attach_downstream_connection(
|
|
|
|
handler_->get_downstream_connection());
|
|
|
|
if (rv != 0) {
|
|
|
|
rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
handler_->signal_write();
|
nghttpx: Check HTTP/2 downstream connection after certain idle time
Previously when requests are issued to HTTP/2 downstream connection,
but it turns out that connection is down, handlers of those requests
are deleted. In some situations, we only know connection is down when
we write something to network, so we'd like to handle this kind of
situation in more robust manner. In this change, certain seconds
passed after last network activity, we first issue PING frame to
downstream connection before issuing new HTTP request. If writing
PING frame is failed, it means connection was lost. In this case,
instead of deleting handler, pending requests are migrated to new
HTTP2/ downstream connection, so that it can continue without
affecting upstream connection.
2014-12-08 17:30:15 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:47:43 +01:00
|
|
|
MemchunkPool *Http2Upstream::get_mcpool() { return &mcpool_; }
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-07 08:09:49 +01:00
|
|
|
int Http2Upstream::prepare_push_promise(Downstream *downstream) {
|
|
|
|
int rv;
|
|
|
|
http_parser_url u;
|
|
|
|
memset(&u, 0, sizeof(u));
|
|
|
|
rv = http_parser_parse_url(downstream->get_request_path().c_str(),
|
|
|
|
downstream->get_request_path().size(), 0, &u);
|
|
|
|
if (rv != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char *base;
|
|
|
|
size_t baselen;
|
|
|
|
if (u.field_set & (1 << UF_PATH)) {
|
|
|
|
auto &f = u.field_data[UF_PATH];
|
|
|
|
base = downstream->get_request_path().c_str() + f.off;
|
|
|
|
baselen = f.len;
|
|
|
|
} else {
|
|
|
|
base = "/";
|
|
|
|
baselen = 1;
|
|
|
|
}
|
|
|
|
for (auto &kv : downstream->get_response_headers()) {
|
2015-02-08 06:07:01 +01:00
|
|
|
if (kv.token != http2::HD_LINK) {
|
2015-02-07 08:09:49 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (auto &link :
|
|
|
|
http2::parse_link_header(kv.value.c_str(), kv.value.size())) {
|
2015-02-08 09:29:38 +01:00
|
|
|
auto link_url = link.uri.first;
|
|
|
|
auto link_urllen = link.uri.second - link.uri.first;
|
2015-02-07 08:09:49 +01:00
|
|
|
|
|
|
|
const char *rel;
|
|
|
|
size_t rellen;
|
|
|
|
const char *relq = nullptr;
|
|
|
|
size_t relqlen = 0;
|
|
|
|
|
|
|
|
http_parser_url v;
|
|
|
|
memset(&v, 0, sizeof(v));
|
|
|
|
rv = http_parser_parse_url(link_url, link_urllen, 0, &v);
|
|
|
|
if (rv != 0) {
|
|
|
|
assert(link_urllen);
|
|
|
|
if (link_url[0] == '/') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// treat link_url as relative URI.
|
|
|
|
auto end = std::find(link_url, link_url + link_urllen, '#');
|
|
|
|
auto q = std::find(link_url, end, '?');
|
|
|
|
rel = link_url;
|
|
|
|
rellen = q - link_url;
|
|
|
|
if (q != end) {
|
|
|
|
relq = q + 1;
|
|
|
|
relqlen = end - relq;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (v.field_set & (1 << UF_HOST)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (v.field_set & (1 << UF_PATH)) {
|
|
|
|
auto &f = v.field_data[UF_PATH];
|
|
|
|
rel = link_url + f.off;
|
|
|
|
rellen = f.len;
|
|
|
|
} else {
|
|
|
|
rel = "/";
|
|
|
|
rellen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v.field_set & (1 << UF_QUERY)) {
|
|
|
|
auto &f = v.field_data[UF_QUERY];
|
|
|
|
relq = link_url + f.off;
|
|
|
|
relqlen = f.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto path = http2::path_join(base, baselen, nullptr, 0, rel, rellen, relq,
|
|
|
|
relqlen);
|
|
|
|
rv = submit_push_promise(path, downstream);
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Http2Upstream::submit_push_promise(const std::string &path,
|
|
|
|
Downstream *downstream) {
|
|
|
|
int rv;
|
|
|
|
std::vector<nghttp2_nv> nva;
|
|
|
|
nva.reserve(downstream->get_request_headers().size());
|
|
|
|
for (auto &kv : downstream->get_request_headers()) {
|
2015-02-08 06:07:01 +01:00
|
|
|
switch (kv.token) {
|
2015-02-08 06:23:22 +01:00
|
|
|
// TODO generate referer
|
|
|
|
case http2::HD__AUTHORITY:
|
|
|
|
case http2::HD__SCHEME:
|
|
|
|
case http2::HD_ACCEPT_ENCODING:
|
|
|
|
case http2::HD_ACCEPT_LANGUAGE:
|
|
|
|
case http2::HD_CACHE_CONTROL:
|
|
|
|
case http2::HD_HOST:
|
|
|
|
case http2::HD_USER_AGENT:
|
|
|
|
nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index));
|
|
|
|
break;
|
2015-02-07 08:09:49 +01:00
|
|
|
case http2::HD__METHOD:
|
|
|
|
// juse use "GET" for now
|
|
|
|
nva.push_back(http2::make_nv_lc(":method", "GET"));
|
|
|
|
continue;
|
|
|
|
case http2::HD__PATH:
|
|
|
|
nva.push_back(http2::make_nv_ls(":path", path));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_submit_push_promise(session_, NGHTTP2_FLAG_NONE,
|
|
|
|
downstream->get_stream_id(), nva.data(),
|
|
|
|
nva.size(), nullptr);
|
|
|
|
|
2015-02-08 09:21:27 +01:00
|
|
|
if (rv < 0) {
|
2015-02-07 08:09:49 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, this) << "nghttp2_submit_push_promise() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
}
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-08 09:25:21 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
std::stringstream ss;
|
|
|
|
for (auto &nv : nva) {
|
|
|
|
ss << TTY_HTTP_HD << nv.name << TTY_RST << ": " << nv.value << "\n";
|
|
|
|
}
|
|
|
|
ULOG(INFO, this) << "HTTP push request headers. promised_stream_id=" << rv
|
|
|
|
<< "\n" << ss.str();
|
|
|
|
}
|
|
|
|
|
2015-02-07 08:09:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|