2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2013-07-12 17:19:03 +02:00
|
|
|
* nghttp2 - HTTP/2.0 C Library
|
2012-06-04 16:48:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
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"
|
2012-07-17 18:08:05 +02:00
|
|
|
#include "shrpx_accesslog.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"
|
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 {
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
namespace {
|
2013-11-04 10:22:29 +01:00
|
|
|
const size_t SHRPX_HTTP2_UPSTREAM_OUTPUT_UPPER_THRES = 64*1024;
|
2012-06-04 20:11:43 +02:00
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
ssize_t send_callback(nghttp2_session *session,
|
2012-06-04 16:48:31 +02:00
|
|
|
const uint8_t *data, size_t len, int flags,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-08-31 17:23:07 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
auto bev = handler->get_bev();
|
|
|
|
auto output = bufferevent_get_output(bev);
|
2012-06-04 20:11:43 +02:00
|
|
|
// Check buffer length and return WOULDBLOCK if it is large enough.
|
2013-11-04 10:22:29 +01:00
|
|
|
if(evbuffer_get_length(output) > SHRPX_HTTP2_UPSTREAM_OUTPUT_UPPER_THRES) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_WOULDBLOCK;
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
rv = evbuffer_add(output, data, len);
|
|
|
|
if(rv == -1) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(FATAL, upstream) << "evbuffer_add() failed";
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-06-04 16:48:31 +02:00
|
|
|
} else {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 15:58:05 +02:00
|
|
|
int on_stream_close_callback
|
2013-07-26 12:33:25 +02:00
|
|
|
(nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code,
|
2012-06-04 16:48:31 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
2013-08-31 17:23:07 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
2013-01-21 14:42:49 +01:00
|
|
|
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
|
|
|
}
|
2013-08-31 17:23:07 +02:00
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2012-06-04 16:48:31 +02:00
|
|
|
if(downstream) {
|
2012-06-05 19:23:07 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::CONNECT_FAIL) {
|
2012-06-07 17:36:19 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
delete downstream;
|
2012-06-05 19:23:07 +02:00
|
|
|
} else {
|
|
|
|
downstream->set_request_state(Downstream::STREAM_CLOSED);
|
|
|
|
if(downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2012-06-09 16:14:00 +02:00
|
|
|
// At this point, downstream response was read
|
2013-07-31 14:48:37 +02:00
|
|
|
if(!downstream->get_upgraded() &&
|
2012-07-11 09:20:16 +02:00
|
|
|
!downstream->get_response_connection_close()) {
|
2012-06-09 16:14:00 +02:00
|
|
|
// Keep-alive
|
2013-08-31 17:23:07 +02:00
|
|
|
auto dconn = downstream->get_downstream_connection();
|
2012-06-09 16:14:00 +02:00
|
|
|
if(dconn) {
|
|
|
|
dconn->detach_downstream(downstream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
upstream->remove_downstream(downstream);
|
2012-06-12 14:56:41 +02:00
|
|
|
delete downstream;
|
2012-06-06 14:39:55 +02:00
|
|
|
} else {
|
2012-06-12 14:56:41 +02:00
|
|
|
// At this point, downstream read may be paused.
|
2012-07-16 17:12:31 +02:00
|
|
|
|
|
|
|
// If shrpx_downstream::push_request_headers() failed, the
|
|
|
|
// error is handled here.
|
2012-06-12 14:56:41 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
|
|
|
delete downstream;
|
2012-06-12 15:43:28 +02:00
|
|
|
// How to test this case? Request sufficient large download
|
|
|
|
// and make client send RST_STREAM after it gets first DATA
|
|
|
|
// frame chunk.
|
2012-06-05 19:23:07 +02:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 15:58:05 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-08-03 11:51:01 +02:00
|
|
|
int Http2Upstream::upgrade_upstream(HttpsUpstream *http)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
std::string settings_payload;
|
|
|
|
auto downstream = http->get_downstream();
|
|
|
|
for(auto& hd : downstream->get_request_headers()) {
|
|
|
|
if(util::strieq(hd.first.c_str(), "http2-settings")) {
|
|
|
|
auto val = hd.second;
|
|
|
|
util::to_base64(val);
|
|
|
|
settings_payload = base64::decode(std::begin(val), std::end(val));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = nghttp2_session_upgrade
|
|
|
|
(session_,
|
|
|
|
reinterpret_cast<const uint8_t*>(settings_payload.c_str()),
|
|
|
|
settings_payload.size(),
|
|
|
|
nullptr);
|
|
|
|
if(rv != 0) {
|
|
|
|
ULOG(WARNING, this) << "nghttp2_session_upgrade() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-26 14:46:35 +02:00
|
|
|
pre_upstream_.reset(http);
|
2013-08-03 11:51:01 +02:00
|
|
|
http->pop_downstream();
|
|
|
|
downstream->reset_upstream(this);
|
|
|
|
add_downstream(downstream);
|
|
|
|
downstream->init_response_body_buf();
|
|
|
|
downstream->set_stream_id(1);
|
|
|
|
downstream->set_priority(0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-30 16:44:23 +01:00
|
|
|
namespace {
|
|
|
|
void settings_timeout_cb(evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(arg);
|
|
|
|
ULOG(INFO, upstream) << "SETTINGS timeout";
|
2013-12-25 16:23:07 +01:00
|
|
|
if(upstream->terminate_session(NGHTTP2_SETTINGS_TIMEOUT) != 0) {
|
2013-10-30 16:44:23 +01:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(upstream->send() != 0) {
|
|
|
|
delete upstream->get_client_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int Http2Upstream::start_settings_timer()
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
// We submit SETTINGS only once
|
|
|
|
if(settings_timerev_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
settings_timerev_ = evtimer_new(handler_->get_evbase(), settings_timeout_cb,
|
|
|
|
this);
|
|
|
|
if(settings_timerev_ == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// SETTINGS ACK timeout is 10 seconds for now
|
|
|
|
timeval settings_timeout = { 10, 0 };
|
|
|
|
rv = evtimer_add(settings_timerev_, &settings_timeout);
|
|
|
|
if(rv == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Http2Upstream::stop_settings_timer()
|
|
|
|
{
|
|
|
|
if(settings_timerev_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event_free(settings_timerev_);
|
|
|
|
settings_timerev_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
namespace {
|
|
|
|
int on_header_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame,
|
|
|
|
const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if(frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
|
|
|
if(!downstream) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-16 18:16:53 +01:00
|
|
|
if(!http2::check_nv(name, namelen, value, valuelen)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
downstream->split_add_request_header(name, namelen, value, valuelen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int on_end_headers_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame,
|
|
|
|
nghttp2_error_code error_code,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if(error_code != NGHTTP2_NO_ERROR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int rv;
|
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
|
|
|
if(!downstream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
downstream->normalize_request_headers();
|
|
|
|
auto& nva = downstream->get_request_headers();
|
|
|
|
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
std::stringstream ss;
|
|
|
|
for(auto& nv : nva) {
|
|
|
|
ss << TTY_HTTP_HD << nv.first << TTY_RST << ": " << nv.second << "\n";
|
|
|
|
}
|
|
|
|
ULOG(INFO, upstream) << "HTTP request headers. stream_id="
|
|
|
|
<< downstream->get_stream_id()
|
|
|
|
<< "\n" << ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(get_config()->http2_upstream_dump_request_header) {
|
|
|
|
http2::dump_nv(get_config()->http2_upstream_dump_request_header, nva);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!http2::check_http2_headers(nva)) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto host = http2::get_unique_header(nva, "host");
|
|
|
|
auto authority = http2::get_unique_header(nva, ":authority");
|
|
|
|
auto path = http2::get_unique_header(nva, ":path");
|
|
|
|
auto method = http2::get_unique_header(nva, ":method");
|
|
|
|
auto scheme = http2::get_unique_header(nva, ":scheme");
|
|
|
|
bool is_connect = method && "CONNECT" == method->second;
|
|
|
|
bool having_host = http2::non_empty_value(host);
|
|
|
|
bool having_authority = http2::non_empty_value(authority);
|
|
|
|
|
|
|
|
if(is_connect) {
|
|
|
|
// Here we strictly require :authority header field.
|
|
|
|
if(scheme || path || !having_authority) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For proxy, :authority is required. Otherwise, we can accept
|
|
|
|
// :authority or host for methods.
|
|
|
|
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)) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!is_connect &&
|
|
|
|
(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
|
|
|
|
auto content_length = http2::get_header(nva, "content-length");
|
|
|
|
if(!content_length || http2::value_lws(content_length)) {
|
|
|
|
// If content-length is missing,
|
|
|
|
// Downstream::push_upload_data_chunk will fail and
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
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));
|
|
|
|
|
|
|
|
downstream->check_upgrade_request();
|
|
|
|
|
|
|
|
auto dconn = upstream->get_client_handler()->get_downstream_connection();
|
|
|
|
rv = dconn->attach_downstream(downstream);
|
|
|
|
if(rv != 0) {
|
|
|
|
// If downstream connection fails, issue RST_STREAM.
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
downstream->set_request_state(Downstream::CONNECT_FAIL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rv = downstream->push_request_headers();
|
|
|
|
if(rv != 0) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
|
|
|
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2013-08-29 14:03:39 +02:00
|
|
|
int on_frame_recv_callback
|
2013-09-03 14:24:14 +02:00
|
|
|
(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2014-01-09 15:47:21 +01:00
|
|
|
int rv;
|
2013-07-26 12:38:54 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
2013-07-26 12:33:25 +02:00
|
|
|
switch(frame->hd.type) {
|
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
if(frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
break;
|
|
|
|
}
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-07-26 12:33:25 +02:00
|
|
|
ULOG(INFO, upstream) << "Received upstream request HEADERS stream_id="
|
|
|
|
<< frame->hd.stream_id;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = new Downstream(upstream,
|
|
|
|
frame->hd.stream_id,
|
|
|
|
frame->headers.pri);
|
2012-06-09 16:14:00 +02:00
|
|
|
upstream->add_downstream(downstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->init_response_body_buf();
|
|
|
|
break;
|
|
|
|
}
|
2014-01-18 08:12:03 +01:00
|
|
|
case NGHTTP2_PRIORITY: {
|
|
|
|
auto downstream = upstream->find_downstream(frame->hd.stream_id);
|
|
|
|
if(!downstream) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = downstream->change_priority(frame->priority.pri);
|
|
|
|
if(rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-10-30 16:44:23 +01:00
|
|
|
case NGHTTP2_SETTINGS:
|
|
|
|
if((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
upstream->stop_settings_timer();
|
|
|
|
break;
|
2014-01-09 15:47:21 +01:00
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
|
|
|
rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
|
|
|
|
frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_REFUSED_STREAM);
|
|
|
|
if(rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
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 {
|
2013-08-29 14:18:40 +02: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)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-08-31 17:23:07 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2012-06-04 16:48:31 +02:00
|
|
|
if(downstream) {
|
2012-07-16 17:12:31 +02:00
|
|
|
if(downstream->push_upload_data_chunk(data, len) != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2012-07-16 17:12:31 +02:00
|
|
|
}
|
2013-10-29 16:00:58 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int on_data_recv_callback(nghttp2_session *session,
|
|
|
|
uint16_t length, uint8_t flags, int32_t stream_id,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
|
|
|
if(downstream && (flags & NGHTTP2_FLAG_END_STREAM)) {
|
|
|
|
downstream->end_upload_data();
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-10-30 16:44:23 +01:00
|
|
|
namespace {
|
|
|
|
int on_frame_send_callback(nghttp2_session* session,
|
|
|
|
const nghttp2_frame *frame, void *user_data)
|
|
|
|
{
|
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
|
|
|
if(frame->hd.type == NGHTTP2_SETTINGS &&
|
|
|
|
(frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
|
|
|
if(upstream->start_settings_timer() != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2013-09-03 14:24:14 +02:00
|
|
|
const nghttp2_frame *frame,
|
2013-08-29 14:51:58 +02:00
|
|
|
int lib_error_code, void *user_data)
|
2012-09-15 10:19:58 +02:00
|
|
|
{
|
2013-07-26 12:38:54 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
2013-07-26 12:33:25 +02:00
|
|
|
ULOG(WARNING, upstream) << "Failed to send control frame type="
|
2013-08-01 13:33:04 +02:00
|
|
|
<< static_cast<uint32_t>(frame->hd.type)
|
2013-07-26 12:33:25 +02:00
|
|
|
<< ", lib_error_code=" << lib_error_code << ":"
|
|
|
|
<< nghttp2_strerror(lib_error_code);
|
|
|
|
if(frame->hd.type == NGHTTP2_HEADERS &&
|
|
|
|
frame->headers.cat == NGHTTP2_HCAT_RESPONSE) {
|
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);
|
2012-09-15 10:19:58 +02: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
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 16:07:07 +02:00
|
|
|
int on_frame_recv_parse_error_callback(nghttp2_session *session,
|
|
|
|
nghttp2_frame_type type,
|
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen, int lib_error_code,
|
|
|
|
void *user_data)
|
2012-09-15 10:19:58 +02:00
|
|
|
{
|
2013-07-26 12:38:54 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "Failed to parse received control frame. type="
|
|
|
|
<< type
|
2013-07-26 12:33:25 +02:00
|
|
|
<< ", error_code=" << lib_error_code << ":"
|
|
|
|
<< nghttp2_strerror(lib_error_code);
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
2013-08-29 16:07:07 +02:00
|
|
|
return 0;
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 16:10:18 +02:00
|
|
|
int on_unknown_frame_recv_callback(nghttp2_session *session,
|
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload, size_t payloadlen,
|
|
|
|
void *user_data)
|
2012-09-15 10:19:58 +02:00
|
|
|
{
|
2013-07-26 12:38:54 +02:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "Received unknown control frame.";
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
2013-08-29 16:10:18 +02:00
|
|
|
return 0;
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-02-27 14:39:44 +01:00
|
|
|
namespace {
|
2013-07-26 12:33:25 +02:00
|
|
|
nghttp2_error_code infer_upstream_rst_stream_error_code
|
|
|
|
(nghttp2_error_code downstream_error_code)
|
2013-02-27 14:39:44 +01:00
|
|
|
{
|
2013-07-12 17:19:03 +02:00
|
|
|
// Only propagate NGHTTP2_REFUSED_STREAM so that upstream client
|
2013-02-27 14:39:44 +01:00
|
|
|
// can resend request.
|
2013-07-26 12:33:25 +02:00
|
|
|
if(downstream_error_code != NGHTTP2_REFUSED_STREAM) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_INTERNAL_ERROR;
|
2013-02-27 14:39:44 +01:00
|
|
|
} else {
|
2013-07-26 12:33:25 +02:00
|
|
|
return downstream_error_code;
|
2013-02-27 14:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
Http2Upstream::Http2Upstream(ClientHandler *handler)
|
2012-06-04 16:48:31 +02:00
|
|
|
: handler_(handler),
|
2013-10-30 16:44:23 +01:00
|
|
|
session_(nullptr),
|
|
|
|
settings_timerev_(nullptr)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-11-04 10:14:05 +01:00
|
|
|
handler->set_upstream_timeouts(&get_config()->http2_upstream_read_timeout,
|
2012-07-17 17:13:11 +02:00
|
|
|
&get_config()->upstream_write_timeout);
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_callbacks callbacks;
|
2012-06-04 16:48:31 +02:00
|
|
|
memset(&callbacks, 0, sizeof(callbacks));
|
|
|
|
callbacks.send_callback = send_callback;
|
|
|
|
callbacks.on_stream_close_callback = on_stream_close_callback;
|
2013-07-26 12:33:25 +02:00
|
|
|
callbacks.on_frame_recv_callback = on_frame_recv_callback;
|
2012-06-04 16:48:31 +02:00
|
|
|
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
|
2013-10-29 16:00:58 +01:00
|
|
|
callbacks.on_data_recv_callback = on_data_recv_callback;
|
2013-10-30 16:44:23 +01:00
|
|
|
callbacks.on_frame_send_callback = on_frame_send_callback;
|
2013-07-26 12:33:25 +02:00
|
|
|
callbacks.on_frame_not_send_callback = on_frame_not_send_callback;
|
|
|
|
callbacks.on_frame_recv_parse_error_callback =
|
|
|
|
on_frame_recv_parse_error_callback;
|
|
|
|
callbacks.on_unknown_frame_recv_callback = on_unknown_frame_recv_callback;
|
2014-01-16 15:41:13 +01:00
|
|
|
callbacks.on_header_callback = on_header_callback;
|
|
|
|
callbacks.on_end_headers_callback = on_end_headers_callback;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-11-07 16:12:39 +01:00
|
|
|
nghttp2_opt_set opt_set;
|
|
|
|
opt_set.no_auto_stream_window_update = 1;
|
|
|
|
opt_set.no_auto_connection_window_update = 1;
|
|
|
|
uint32_t opt_set_mask =
|
|
|
|
NGHTTP2_OPT_NO_AUTO_STREAM_WINDOW_UPDATE |
|
|
|
|
NGHTTP2_OPT_NO_AUTO_CONNECTION_WINDOW_UPDATE;
|
2012-06-04 16:48:31 +02:00
|
|
|
int rv;
|
2013-11-07 16:12:39 +01:00
|
|
|
rv = nghttp2_session_server_new2(&session_, &callbacks, this,
|
|
|
|
opt_set_mask, &opt_set);
|
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?
|
2014-01-09 15:47:21 +01:00
|
|
|
nghttp2_settings_entry entry[3];
|
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
|
|
|
|
2014-01-09 15:47:21 +01:00
|
|
|
entry[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
|
|
|
|
entry[2].value = 0;
|
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE,
|
|
|
|
entry,
|
|
|
|
sizeof(entry)/sizeof(nghttp2_settings_entry));
|
2012-06-04 16:48:31 +02:00
|
|
|
assert(rv == 0);
|
2013-11-20 16:15:17 +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;
|
|
|
|
rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE, 0, delta);
|
|
|
|
assert(rv == 0);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
Http2Upstream::~Http2Upstream()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_del(session_);
|
2013-10-30 16:44:23 +01:00
|
|
|
if(settings_timerev_) {
|
|
|
|
event_free(settings_timerev_);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::on_read()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2014-01-18 08:50:52 +01:00
|
|
|
ssize_t rv = 0;
|
|
|
|
auto bev = handler_->get_bev();
|
|
|
|
auto input = bufferevent_get_input(bev);
|
|
|
|
auto inputlen = evbuffer_get_length(input);
|
|
|
|
auto mem = evbuffer_pullup(input, -1);
|
|
|
|
|
|
|
|
rv = nghttp2_session_mem_recv(session_, mem, inputlen);
|
|
|
|
if(rv < 0) {
|
|
|
|
ULOG(ERROR, this) << "nghttp2_session_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
evbuffer_drain(input, rv);
|
|
|
|
rv = nghttp2_session_send(session_);
|
|
|
|
if(rv < 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
ULOG(ERROR, this) << "nghttp2_session_send() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2014-01-18 08:50:52 +01:00
|
|
|
return -1;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-01-18 08:50:52 +01:00
|
|
|
if(nghttp2_session_want_read(session_) == 0 &&
|
|
|
|
nghttp2_session_want_write(session_) == 0 &&
|
|
|
|
evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, this) << "No more read/write for this HTTP2 session";
|
2012-07-18 18:59:55 +02:00
|
|
|
}
|
2014-01-18 08:50:52 +01:00
|
|
|
rv = -1;
|
2012-07-18 18:59:55 +02:00
|
|
|
}
|
2014-01-18 08:50:52 +01:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::on_write()
|
2012-06-04 20:11:43 +02:00
|
|
|
{
|
2012-07-16 16:03:07 +02:00
|
|
|
return send();
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 14:56:41 +02:00
|
|
|
// After this function call, downstream may be deleted.
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::send()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-07-16 16:03:07 +02:00
|
|
|
int rv = 0;
|
2013-07-12 17:19:03 +02:00
|
|
|
if((rv = nghttp2_session_send(session_)) < 0) {
|
|
|
|
ULOG(ERROR, this) << "nghttp2_session_send() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-07-18 18:59:55 +02:00
|
|
|
if(rv == 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
if(nghttp2_session_want_read(session_) == 0 &&
|
2013-08-01 13:31:29 +02:00
|
|
|
nghttp2_session_want_write(session_) == 0 &&
|
|
|
|
evbuffer_get_length(bufferevent_get_output(handler_->get_bev())) == 0) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 10:22:29 +01:00
|
|
|
ULOG(INFO, this) << "No more read/write for this HTTP2 session";
|
2012-07-18 18:59:55 +02:00
|
|
|
}
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 16:03:07 +02:00
|
|
|
return rv;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::on_event()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
ClientHandler* Http2Upstream::get_client_handler() const
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
return handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2013-11-04 09:53:57 +01:00
|
|
|
void downstream_readcb(bufferevent *bev, void *ptr)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-08-31 17:23:07 +02:00
|
|
|
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = static_cast<Http2Upstream*>(downstream->get_upstream());
|
2012-06-05 15:46:47 +02: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.
|
2012-06-05 15:46:47 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
|
|
|
delete downstream;
|
|
|
|
return;
|
|
|
|
}
|
2013-02-09 15:20:29 +01:00
|
|
|
|
|
|
|
if(downstream->get_response_state() == Downstream::MSG_RESET) {
|
|
|
|
// 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.
|
2013-07-26 12:33:25 +02:00
|
|
|
upstream->rst_stream(downstream, infer_upstream_rst_stream_error_code
|
|
|
|
(downstream->get_response_rst_stream_error_code()));
|
2013-02-09 15:20:29 +01:00
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
delete dconn;
|
2013-02-27 14:39:44 +01:00
|
|
|
dconn = 0;
|
|
|
|
} else {
|
|
|
|
int rv = downstream->on_read();
|
|
|
|
if(rv != 0) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
DCLOG(INFO, dconn) << "HTTP parser failure";
|
|
|
|
}
|
|
|
|
if(downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2013-08-20 17:56:08 +02:00
|
|
|
} else if(downstream->get_response_state() != Downstream::MSG_COMPLETE) {
|
|
|
|
// If response was completed, then don't issue RST_STREAM
|
2013-02-27 14:39:44 +01:00
|
|
|
if(upstream->error_reply(downstream, 502) != 0) {
|
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
2012-07-16 16:29:48 +02:00
|
|
|
}
|
2013-02-27 14:39:44 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
// Clearly, we have to close downstream connection on http parser
|
|
|
|
// failure.
|
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
delete dconn;
|
|
|
|
dconn = 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-16 16:29:48 +02:00
|
|
|
if(upstream->send() != 0) {
|
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
2012-06-12 14:56:41 +02:00
|
|
|
// At this point, downstream may be deleted.
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-11-04 09:53:57 +01:00
|
|
|
void downstream_writecb(bufferevent *bev, void *ptr)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-11-21 19:13:30 +01:00
|
|
|
if(evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-31 17:23:07 +02:00
|
|
|
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = static_cast<Http2Upstream*>(downstream->get_upstream());
|
2013-02-08 13:46:58 +01:00
|
|
|
upstream->resume_read(SHRPX_NO_BUFFER, downstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-11-04 09:53:57 +01:00
|
|
|
void downstream_eventcb(bufferevent *bev, short events, void *ptr)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-08-31 17:23:07 +02:00
|
|
|
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
auto downstream = dconn->get_downstream();
|
|
|
|
auto upstream = static_cast<Http2Upstream*>(downstream->get_upstream());
|
2012-06-04 16:48:31 +02:00
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Connection established. stream_id="
|
|
|
|
<< downstream->get_stream_id();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-01-10 16:11:41 +01:00
|
|
|
int fd = bufferevent_getfd(bev);
|
|
|
|
int val = 1;
|
2013-01-25 14:00:33 +01:00
|
|
|
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
reinterpret_cast<char *>(&val), sizeof(val)) == -1) {
|
2013-02-25 14:43:44 +01:00
|
|
|
DCLOG(WARNING, dconn) << "Setting option TCP_NODELAY failed: errno="
|
|
|
|
<< errno;
|
2013-01-25 14:00:33 +01:00
|
|
|
}
|
2012-06-07 17:36:19 +02:00
|
|
|
} else if(events & BEV_EVENT_EOF) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "EOF. stream_id=" << downstream->get_stream_id();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
2012-06-09 16:14:00 +02:00
|
|
|
// If stream was closed already, we don't need to send reply at
|
2012-06-04 20:11:43 +02:00
|
|
|
// the first place. We can delete downstream.
|
2012-06-04 16:48:31 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
|
|
|
delete downstream;
|
2012-06-04 20:11:43 +02:00
|
|
|
} else {
|
2012-06-09 16:14:00 +02:00
|
|
|
// Delete downstream connection. If we don't delete it here, it
|
|
|
|
// will be pooled in on_stream_close_callback.
|
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
delete dconn;
|
|
|
|
dconn = 0;
|
2012-06-04 20:11:43 +02:00
|
|
|
// 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
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "Downstream body was ended by EOF";
|
2012-06-04 20:11:43 +02:00
|
|
|
}
|
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
2013-02-10 18:05:11 +01:00
|
|
|
|
|
|
|
// For tunneled connection, MSG_COMPLETE signals
|
2013-11-04 09:53:57 +01:00
|
|
|
// 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.
|
2013-02-10 18:05:11 +01:00
|
|
|
upstream->on_downstream_body_complete(downstream);
|
2013-03-01 12:43:35 +01:00
|
|
|
} else if(downstream->get_response_state() != Downstream::MSG_COMPLETE) {
|
2012-06-04 20:11:43 +02:00
|
|
|
// If stream was not closed, then we set MSG_COMPLETE and let
|
|
|
|
// on_stream_close_callback delete downstream.
|
2012-07-16 16:29:48 +02:00
|
|
|
if(upstream->error_reply(downstream, 502) != 0) {
|
2013-02-28 16:07:00 +01:00
|
|
|
delete upstream->get_client_handler();
|
2012-07-16 16:29:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
2012-07-16 16:29:48 +02:00
|
|
|
if(upstream->send() != 0) {
|
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
2012-07-15 14:15:28 +02:00
|
|
|
// At this point, downstream may be deleted.
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-07-15 14:15:28 +02:00
|
|
|
if(events & BEV_EVENT_ERROR) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Downstream network error: "
|
|
|
|
<< evutil_socket_error_to_string
|
|
|
|
(EVUTIL_SOCKET_ERROR());
|
2012-07-15 14:15:28 +02:00
|
|
|
} else {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Timeout";
|
2012-07-15 14:15:28 +02:00
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
if(downstream->get_upgraded()) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DCLOG(INFO, dconn) << "Note: this is tunnel connection";
|
2012-07-15 14:15:28 +02:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
|
|
|
upstream->remove_downstream(downstream);
|
|
|
|
delete downstream;
|
|
|
|
} else {
|
2012-06-09 16:14:00 +02:00
|
|
|
// Delete downstream connection. If we don't delete it here, it
|
|
|
|
// will be pooled in on_stream_close_callback.
|
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
delete dconn;
|
|
|
|
dconn = 0;
|
2012-07-11 09:20:16 +02:00
|
|
|
if(downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2013-08-20 17:56:08 +02:00
|
|
|
// 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()) {
|
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
}
|
2012-07-11 09:20:16 +02:00
|
|
|
} else {
|
2012-06-04 20:11:43 +02:00
|
|
|
if(downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2012-06-04 16:48:31 +02:00
|
|
|
} else {
|
2013-09-08 07:29:18 +02:00
|
|
|
unsigned int status;
|
2012-06-04 20:11:43 +02:00
|
|
|
if(events & BEV_EVENT_TIMEOUT) {
|
|
|
|
status = 504;
|
|
|
|
} else {
|
|
|
|
status = 502;
|
|
|
|
}
|
2012-07-16 16:29:48 +02:00
|
|
|
if(upstream->error_reply(downstream, status) != 0) {
|
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-07-16 16:29:48 +02:00
|
|
|
if(upstream->send() != 0) {
|
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
2012-07-15 14:15:28 +02:00
|
|
|
// At this point, downstream may be deleted.
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::rst_stream(Downstream *downstream,
|
2013-08-27 19:47:22 +02:00
|
|
|
nghttp2_error_code error_code)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, this) << "RST_STREAM stream_id="
|
2013-09-29 17:13:04 +02:00
|
|
|
<< 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);
|
2013-07-12 17:19:03 +02:00
|
|
|
if(rv < NGHTTP2_ERR_FATAL) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-29 16:07:35 +01:00
|
|
|
int Http2Upstream::window_update(Downstream *downstream,
|
|
|
|
int32_t window_size_increment)
|
2012-06-09 18:36:30 +02:00
|
|
|
{
|
|
|
|
int rv;
|
2013-07-26 12:33:25 +02:00
|
|
|
rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE,
|
2013-10-29 16:51:01 +01:00
|
|
|
downstream ?
|
|
|
|
downstream->get_stream_id() : 0,
|
2013-10-29 16:07:35 +01:00
|
|
|
window_size_increment);
|
2013-07-12 17:19:03 +02:00
|
|
|
if(rv < NGHTTP2_ERR_FATAL) {
|
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_window_update() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-06-09 18:36:30 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2012-07-27 15:11:13 +02:00
|
|
|
return 0;
|
2012-06-09 18:36:30 +02:00
|
|
|
}
|
|
|
|
|
2013-12-25 16:23:07 +01:00
|
|
|
int Http2Upstream::terminate_session(nghttp2_error_code 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);
|
2013-10-30 16:44:23 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
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,
|
|
|
|
int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length,
|
|
|
|
int *eof,
|
|
|
|
nghttp2_data_source *source,
|
|
|
|
void *user_data)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-08-31 17:23:07 +02:00
|
|
|
auto downstream = reinterpret_cast<Downstream*>(source->ptr);
|
2013-12-20 15:28:54 +01:00
|
|
|
auto upstream = reinterpret_cast<Http2Upstream*>(downstream->get_upstream());
|
2013-08-31 17:23:07 +02:00
|
|
|
auto body = downstream->get_response_body_buf();
|
2012-06-04 16:48:31 +02:00
|
|
|
assert(body);
|
|
|
|
int nread = evbuffer_remove(body, buf, length);
|
2013-12-20 15:28:54 +01:00
|
|
|
if(nread == -1) {
|
|
|
|
ULOG(FATAL, upstream) << "evbuffer_remove() failed";
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2013-02-10 18:05:11 +01:00
|
|
|
if(nread == 0 &&
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2013-07-31 14:48:37 +02:00
|
|
|
if(!downstream->get_upgraded()) {
|
2013-02-10 18:05:11 +01:00
|
|
|
*eof = 1;
|
|
|
|
} else {
|
|
|
|
// For tunneling, issue RST_STREAM to finish the stream.
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, upstream) << "RST_STREAM to tunneled stream stream_id="
|
|
|
|
<< stream_id;
|
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
upstream->rst_stream(downstream, infer_upstream_rst_stream_error_code
|
|
|
|
(downstream->get_response_rst_stream_error_code()));
|
2013-02-10 18:05:11 +01:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2014-01-18 10:39:25 +01:00
|
|
|
// Send WINDOW_UPDATE before buffer is empty to avoid delay because
|
|
|
|
// of RTT.
|
|
|
|
if(*eof != 1 &&
|
|
|
|
evbuffer_get_length(body) < SHRPX_HTTP2_UPSTREAM_OUTPUT_UPPER_THRES &&
|
|
|
|
downstream->resume_read(SHRPX_NO_BUFFER) != 0) {
|
|
|
|
DLOG(WARNING, downstream) << "Sending WINDOW_UPDATE failed";
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
if(nread == 0 && *eof != 1) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
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,
|
|
|
|
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);
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->init_response_body_buf();
|
2013-08-31 17:23:07 +02:00
|
|
|
auto body = downstream->get_response_body_buf();
|
2012-06-04 16:48:31 +02:00
|
|
|
rv = evbuffer_add(body, html.c_str(), html.size());
|
|
|
|
if(rv == -1) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(FATAL, this) << "evbuffer_add() failed";
|
2012-07-16 16:29:48 +02:00
|
|
|
return -1;
|
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);
|
2013-12-08 13:19:33 +01:00
|
|
|
auto nva = std::vector<nghttp2_nv>{
|
2013-12-08 14:31:43 +01:00
|
|
|
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);
|
2013-07-12 17:19:03 +02:00
|
|
|
if(rv < NGHTTP2_ERR_FATAL) {
|
|
|
|
ULOG(FATAL, this) << "nghttp2_submit_response() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-06-04 16:48:31 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2012-12-09 15:19:48 +01:00
|
|
|
if(get_config()->accesslog) {
|
|
|
|
upstream_response(get_client_handler()->get_ipaddr(),
|
|
|
|
status_code, downstream);
|
|
|
|
}
|
2012-07-27 15:11:13 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
bufferevent_data_cb Http2Upstream::get_downstream_readcb()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
return downstream_readcb;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
bufferevent_data_cb Http2Upstream::get_downstream_writecb()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
return downstream_writecb;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
bufferevent_event_cb Http2Upstream::get_downstream_eventcb()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
return downstream_eventcb;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
void Http2Upstream::add_downstream(Downstream *downstream)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
downstream_queue_.add(downstream);
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
void Http2Upstream::remove_downstream(Downstream *downstream)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
downstream_queue_.remove(downstream);
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
Downstream* Http2Upstream::find_downstream(int32_t stream_id)
|
2012-06-07 17:36:19 +02:00
|
|
|
{
|
|
|
|
return downstream_queue_.find(stream_id);
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
nghttp2_session* Http2Upstream::get_http2_session()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
return session_;
|
|
|
|
}
|
|
|
|
|
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_header_complete(Downstream *downstream)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
DLOG(INFO, downstream) << "HTTP response header completed";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
downstream->normalize_response_headers();
|
2013-12-28 09:02:43 +01:00
|
|
|
if(!get_config()->http2_proxy && !get_config()->client_proxy) {
|
|
|
|
downstream->rewrite_norm_location_response_header
|
|
|
|
(get_client_handler()->get_upstream_scheme(), get_config()->port);
|
|
|
|
}
|
2013-12-06 16:32:14 +01:00
|
|
|
downstream->concat_norm_response_headers();
|
2013-08-27 17:09:46 +02:00
|
|
|
auto end_headers = std::end(downstream->get_response_headers());
|
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>();
|
|
|
|
// 2 means :status and possible via header field.
|
|
|
|
nva.reserve(nheader + 2);
|
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
|
|
|
|
2013-11-28 13:36:04 +01:00
|
|
|
http2::copy_norm_headers_to_nva(nva, downstream->get_response_headers());
|
2013-08-27 17:09:46 +02:00
|
|
|
auto via = downstream->get_norm_response_header("via");
|
|
|
|
if(get_config()->no_via) {
|
|
|
|
if(via != end_headers) {
|
2013-12-08 14:31:43 +01:00
|
|
|
nva.push_back(http2::make_nv_ls("via", (*via).second));
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
} else {
|
|
|
|
if(via != end_headers) {
|
|
|
|
via_value = (*via).second;
|
2013-01-09 14:01:25 +01:00
|
|
|
via_value += ", ";
|
|
|
|
}
|
|
|
|
via_value += http::create_via_header_value
|
|
|
|
(downstream->get_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
|
|
|
}
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-06-04 16:48:31 +02:00
|
|
|
std::stringstream ss;
|
2013-11-28 13:36:04 +01:00
|
|
|
for(auto& nv : nva) {
|
|
|
|
ss << TTY_HTTP_HD;
|
|
|
|
ss.write(reinterpret_cast<const char*>(nv.name), nv.namelen);
|
|
|
|
ss << TTY_RST << ": ";
|
|
|
|
ss.write(reinterpret_cast<const char*>(nv.value), nv.valuelen);
|
|
|
|
ss << "\n";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, this) << "HTTP response headers. stream_id="
|
|
|
|
<< downstream->get_stream_id() << "\n"
|
|
|
|
<< ss.str();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-11-17 15:52:19 +01:00
|
|
|
|
|
|
|
if(get_config()->http2_upstream_dump_response_header) {
|
|
|
|
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
|
|
|
|
2012-07-16 16:55:08 +02:00
|
|
|
int rv;
|
2013-12-08 13:19:33 +01:00
|
|
|
rv = nghttp2_submit_response(session_, downstream->get_stream_id(),
|
|
|
|
nva.data(), nva.size(), &data_prd);
|
2012-07-16 16:55:08 +02:00
|
|
|
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;
|
|
|
|
}
|
2012-12-09 15:19:48 +01:00
|
|
|
if(get_config()->accesslog) {
|
|
|
|
upstream_response(get_client_handler()->get_ipaddr(),
|
|
|
|
downstream->get_response_http_status(),
|
|
|
|
downstream);
|
|
|
|
}
|
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,
|
2013-08-27 19:47:22 +02:00
|
|
|
const uint8_t *data, size_t len)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-08-31 17:23:07 +02:00
|
|
|
auto body = downstream->get_response_body_buf();
|
2012-07-16 16:03:07 +02:00
|
|
|
int rv = evbuffer_add(body, data, len);
|
|
|
|
if(rv != 0) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(FATAL, this) << "evbuffer_add() failed";
|
2012-07-16 16:03:07 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_resume_data(session_, downstream->get_stream_id());
|
2012-06-04 20:11:43 +02:00
|
|
|
|
|
|
|
size_t bodylen = evbuffer_get_length(body);
|
2013-11-04 10:22:29 +01:00
|
|
|
if(bodylen > SHRPX_HTTP2_UPSTREAM_OUTPUT_UPPER_THRES) {
|
2012-06-04 20:11:43 +02:00
|
|
|
downstream->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
}
|
|
|
|
|
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_complete(Downstream *downstream)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2013-01-21 14:42:49 +01:00
|
|
|
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
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_resume_data(session_, downstream->get_stream_id());
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
bool Http2Upstream::get_flow_control() const
|
2012-06-09 18:36:30 +02:00
|
|
|
{
|
|
|
|
return flow_control_;
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
void Http2Upstream::pause_read(IOCtrlReason reason)
|
2012-11-18 13:23:13 +01:00
|
|
|
{}
|
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
int Http2Upstream::resume_read(IOCtrlReason reason, Downstream *downstream)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-02-08 13:46:58 +01:00
|
|
|
if(get_flow_control()) {
|
2013-10-29 16:51:01 +01:00
|
|
|
int32_t window_size_increment;
|
|
|
|
window_size_increment = http2::determine_window_update_transmission
|
|
|
|
(session_, 0);
|
|
|
|
if(window_size_increment != -1) {
|
|
|
|
window_update(nullptr, window_size_increment);
|
|
|
|
}
|
|
|
|
window_size_increment = http2::determine_window_update_transmission
|
2013-10-29 16:07:35 +01:00
|
|
|
(session_, downstream->get_stream_id());
|
2013-10-29 16:51:01 +01:00
|
|
|
if(window_size_increment != -1) {
|
|
|
|
window_update(downstream, window_size_increment);
|
2013-02-08 13:46:58 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-27 14:55:44 +01:00
|
|
|
return send();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|