2013-07-26 13:12:55 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-26 13:12:55 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_spdy_upstream.h"
|
|
|
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
|
|
|
#include "shrpx_client_handler.h"
|
|
|
|
#include "shrpx_downstream.h"
|
|
|
|
#include "shrpx_downstream_connection.h"
|
|
|
|
#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"
|
2013-07-26 13:12:55 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
const size_t OUTBUF_MAX_THRES = 16 * 1024;
|
|
|
|
const size_t INBUF_MAX_THRES = 16 * 1024;
|
2013-07-26 13:12:55 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t send_callback(spdylay_session *session, const uint8_t *data, size_t len,
|
|
|
|
int flags, void *user_data) {
|
2013-07-26 13:12:55 +02:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
2013-12-20 15:36:24 +01:00
|
|
|
auto handler = upstream->get_client_handler();
|
2014-03-03 13:18:24 +01:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
// Check buffer length and return WOULDBLOCK if it is large enough.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (handler->get_outbuf_length() + upstream->sendbuf.get_buflen() >=
|
|
|
|
OUTBUF_MAX_THRES) {
|
2013-07-26 13:12:55 +02:00
|
|
|
return SPDYLAY_ERR_WOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
2014-03-04 16:23:33 +01:00
|
|
|
rv = upstream->sendbuf.add(data, len);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-04 16:23:33 +01:00
|
|
|
ULOG(FATAL, upstream) << "evbuffer_add() failed";
|
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-03-03 13:18:24 +01:00
|
|
|
return len;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t recv_callback(spdylay_session *session, uint8_t *data, size_t len,
|
|
|
|
int flags, void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
2013-12-20 15:36:24 +01:00
|
|
|
auto handler = upstream->get_client_handler();
|
|
|
|
auto bev = handler->get_bev();
|
|
|
|
auto input = bufferevent_get_input(bev);
|
2013-07-26 13:12:55 +02:00
|
|
|
int nread = evbuffer_remove(input, data, len);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == -1) {
|
2013-07-26 13:12:55 +02:00
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (nread == 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
return SPDYLAY_ERR_WOULDBLOCK;
|
|
|
|
} else {
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_stream_close_callback(spdylay_session *session, int32_t stream_id,
|
|
|
|
spdylay_status_code status_code,
|
|
|
|
void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(INFO, upstream) << "Stream stream_id=" << stream_id
|
|
|
|
<< " is being closed";
|
|
|
|
}
|
2013-12-20 15:36:24 +01:00
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!downstream) {
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-21 14:22:16 +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
|
|
|
// downstrea was deleted
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstrea was deleted
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
2013-07-26 13:12:55 +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
|
|
|
// downstrea 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-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
|
|
|
|
spdylay_frame *frame, void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
|
|
|
switch (type) {
|
2013-07-26 13:12:55 +02:00
|
|
|
case SPDYLAY_SYN_STREAM: {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(INFO, upstream) << "Received upstream SYN_STREAM stream_id="
|
|
|
|
<< frame->syn_stream.stream_id;
|
|
|
|
}
|
2014-08-18 15:59:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
auto downstream = upstream->add_pending_downstream(
|
|
|
|
frame->syn_stream.stream_id, frame->syn_stream.pri);
|
2014-08-18 15:59:31 +02:00
|
|
|
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->init_upstream_timer();
|
|
|
|
downstream->reset_upstream_rtimer();
|
2013-07-26 13:12:55 +02:00
|
|
|
downstream->init_response_body_buf();
|
|
|
|
|
2013-10-25 14:50:56 +02:00
|
|
|
auto nv = frame->syn_stream.nv;
|
|
|
|
const char *path = nullptr;
|
|
|
|
const char *scheme = nullptr;
|
|
|
|
const char *host = nullptr;
|
|
|
|
const char *method = nullptr;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size_t i = 0; nv[i]; i += 2) {
|
|
|
|
if (strcmp(nv[i], ":path") == 0) {
|
|
|
|
path = nv[i + 1];
|
|
|
|
} else if (strcmp(nv[i], ":scheme") == 0) {
|
|
|
|
scheme = nv[i + 1];
|
|
|
|
} else if (strcmp(nv[i], ":method") == 0) {
|
|
|
|
method = nv[i + 1];
|
|
|
|
} else if (strcmp(nv[i], ":host") == 0) {
|
|
|
|
host = nv[i + 1];
|
|
|
|
} else if (nv[i][0] != ':') {
|
|
|
|
downstream->add_request_header(nv[i], nv[i + 1]);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
downstream->normalize_request_headers();
|
|
|
|
|
2013-10-25 14:50:56 +02:00
|
|
|
bool is_connect = method && strcmp("CONNECT", method) == 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!path || !host || !method || http2::lws(host) || http2::lws(path) ||
|
|
|
|
http2::lws(method) ||
|
|
|
|
(!is_connect && (!scheme || http2::lws(scheme)))) {
|
2013-07-26 13:12:55 +02:00
|
|
|
upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 14:50:56 +02:00
|
|
|
|
|
|
|
downstream->set_request_method(method);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (is_connect) {
|
2013-10-25 14:50:56 +02:00
|
|
|
downstream->set_request_http2_authority(path);
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
2013-10-25 14:50:56 +02:00
|
|
|
downstream->set_request_http2_scheme(scheme);
|
|
|
|
downstream->set_request_http2_authority(host);
|
2013-07-26 13:12:55 +02:00
|
|
|
downstream->set_request_path(path);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
downstream->set_request_start_time(
|
|
|
|
std::chrono::high_resolution_clock::now());
|
2014-11-19 17:53:30 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN)) {
|
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();
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
std::stringstream ss;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size_t i = 0; nv[i]; i += 2) {
|
|
|
|
ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i + 1] << "\n";
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
ULOG(INFO, upstream) << "HTTP request headers. stream_id="
|
2014-11-27 15:39:04 +01:00
|
|
|
<< downstream->get_stream_id() << "\n" << ss.str();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->disable_upstream_rtimer();
|
2013-07-26 13:12:55 +02:00
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
2014-08-16 14:29:20 +02:00
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
upstream->start_downstream(downstream);
|
2014-08-16 14:29:20 +02:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-04 17:07:00 +01:00
|
|
|
void SpdyUpstream::start_downstream(Downstream *downstream) {
|
|
|
|
auto next_downstream =
|
|
|
|
downstream_queue_.pop_pending(downstream->get_stream_id());
|
|
|
|
assert(next_downstream);
|
2014-08-16 14:29:20 +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 SpdyUpstream::initiate_downstream(std::unique_ptr<Downstream> downstream) {
|
|
|
|
int rv = downstream->attach_downstream_connection(
|
|
|
|
handler_->get_downstream_connection());
|
|
|
|
if (rv != 0) {
|
2014-08-16 14:29:20 +02:00
|
|
|
// If downstream connection fails, issue RST_STREAM.
|
2014-08-18 15:59:31 +02:00
|
|
|
rst_stream(downstream.get(), SPDYLAY_INTERNAL_ERROR);
|
2014-08-16 14:29:20 +02:00
|
|
|
downstream->set_request_state(Downstream::CONNECT_FAIL);
|
2014-08-18 15:59:31 +02:00
|
|
|
|
|
|
|
downstream_queue_.add_failure(std::move(downstream));
|
|
|
|
|
2014-08-16 14:29:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
rv = downstream->push_request_headers();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-08-18 15:59:31 +02:00
|
|
|
rst_stream(downstream.get(), SPDYLAY_INTERNAL_ERROR);
|
|
|
|
|
|
|
|
downstream_queue_.add_failure(std::move(downstream));
|
2014-08-16 14:29:20 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
downstream_queue_.add_active(std::move(downstream));
|
2014-08-16 14:29:20 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id, const uint8_t *data,
|
|
|
|
size_t len, void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
2013-12-20 15:36:24 +01: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-08-21 14:22:16 +02:00
|
|
|
upstream->consume(stream_id, len);
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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, SPDYLAY_INTERNAL_ERROR);
|
2014-08-21 14:22:16 +02:00
|
|
|
|
|
|
|
upstream->consume(stream_id, len);
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!upstream->get_flow_control()) {
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If connection-level window control is not enabled (e.g,
|
|
|
|
// spdy/3), spdylay_session_get_recv_data_length() is always
|
|
|
|
// returns 0.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (spdylay_session_get_recv_data_length(session) >
|
|
|
|
std::max(SPDYLAY_INITIAL_WINDOW_SIZE,
|
|
|
|
1 << get_config()->http2_upstream_connection_window_bits)) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-06-01 16:44:32 +02:00
|
|
|
ULOG(INFO, upstream)
|
2014-11-27 15:39:04 +01:00
|
|
|
<< "Flow control error on connection: "
|
|
|
|
<< "recv_window_size="
|
|
|
|
<< spdylay_session_get_recv_data_length(session) << ", window_size="
|
|
|
|
<< (1 << get_config()->http2_upstream_connection_window_bits);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (spdylay_session_get_stream_recv_data_length(session, stream_id) >
|
|
|
|
std::max(SPDYLAY_INITIAL_WINDOW_SIZE,
|
|
|
|
1 << get_config()->http2_upstream_window_bits)) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, upstream) << "Flow control error: recv_window_size="
|
|
|
|
<< spdylay_session_get_stream_recv_data_length(
|
|
|
|
session, stream_id)
|
|
|
|
<< ", initial_window_size="
|
|
|
|
<< (1 << get_config()->http2_upstream_window_bits);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
upstream->rst_stream(downstream, SPDYLAY_FLOW_CONTROL_ERROR);
|
|
|
|
return;
|
2013-10-29 16:00:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void on_data_recv_callback(spdylay_session *session, uint8_t flags,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id, int32_t length, void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
2013-10-29 16:00:58 +01:00
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream && (flags & SPDYLAY_DATA_FLAG_FIN)) {
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->disable_upstream_rtimer();
|
2013-10-29 16:00:58 +01:00
|
|
|
downstream->end_upload_data();
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void on_ctrl_not_send_callback(spdylay_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
int error_code, void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
2014-11-08 02:51:56 +01:00
|
|
|
ULOG(WARN, upstream) << "Failed to send control frame type=" << type
|
|
|
|
<< ", error_code=" << error_code << ":"
|
|
|
|
<< spdylay_strerror(error_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (type == SPDYLAY_SYN_REPLY) {
|
2013-07-26 13:12:55 +02:00
|
|
|
// To avoid stream hanging around, issue RST_STREAM.
|
2013-12-20 15:36:24 +01:00
|
|
|
auto stream_id = frame->syn_reply.stream_id;
|
|
|
|
auto downstream = upstream->find_downstream(stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream) {
|
2013-07-26 13:12:55 +02:00
|
|
|
upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void on_ctrl_recv_parse_error_callback(spdylay_session *session,
|
|
|
|
spdylay_frame_type type,
|
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen, int error_code,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(INFO, upstream) << "Failed to parse received control frame. type="
|
2014-11-27 15:39:04 +01:00
|
|
|
<< type << ", error_code=" << error_code << ":"
|
2013-07-26 13:12:55 +02:00
|
|
|
<< spdylay_strerror(error_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void on_unknown_ctrl_recv_callback(spdylay_session *session,
|
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload, size_t payloadlen,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data) {
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(user_data);
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(INFO, upstream) << "Received unknown control frame.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-03-30 12:09:21 +02:00
|
|
|
// Infer upstream RST_STREAM status code from downstream HTTP/2
|
2013-07-26 13:12:55 +02:00
|
|
|
// error code.
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t infer_upstream_rst_stream_status_code(uint32_t downstream_error_code) {
|
2013-07-26 13:12:55 +02:00
|
|
|
// Only propagate *_REFUSED_STREAM so that upstream client can
|
|
|
|
// resend request.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream_error_code == NGHTTP2_REFUSED_STREAM) {
|
2013-07-26 13:12:55 +02:00
|
|
|
return SPDYLAY_REFUSED_STREAM;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
|
2014-12-04 17:07:00 +01:00
|
|
|
: downstream_queue_(get_config()->http2_proxy
|
|
|
|
? get_config()->downstream_connections_per_host
|
|
|
|
: 0),
|
|
|
|
handler_(handler), session_(nullptr) {
|
2014-11-27 15:39:04 +01:00
|
|
|
// handler->set_bev_cb(spdy_readcb, 0, spdy_eventcb);
|
2014-09-18 16:03:36 +02:00
|
|
|
reset_timeouts();
|
2013-07-26 13:12:55 +02:00
|
|
|
|
|
|
|
spdylay_session_callbacks callbacks;
|
|
|
|
memset(&callbacks, 0, sizeof(callbacks));
|
|
|
|
callbacks.send_callback = send_callback;
|
|
|
|
callbacks.recv_callback = recv_callback;
|
|
|
|
callbacks.on_stream_close_callback = on_stream_close_callback;
|
|
|
|
callbacks.on_ctrl_recv_callback = on_ctrl_recv_callback;
|
|
|
|
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-07-26 13:12:55 +02:00
|
|
|
callbacks.on_ctrl_not_send_callback = on_ctrl_not_send_callback;
|
|
|
|
callbacks.on_ctrl_recv_parse_error_callback =
|
2014-11-27 15:39:04 +01:00
|
|
|
on_ctrl_recv_parse_error_callback;
|
2013-07-26 13:12:55 +02:00
|
|
|
callbacks.on_unknown_ctrl_recv_callback = on_unknown_ctrl_recv_callback;
|
|
|
|
|
|
|
|
int rv;
|
|
|
|
rv = spdylay_session_server_new(&session_, version, &callbacks, this);
|
|
|
|
assert(rv == 0);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (version >= SPDYLAY_PROTO_SPDY3) {
|
2013-07-26 13:12:55 +02:00
|
|
|
int val = 1;
|
|
|
|
flow_control_ = true;
|
2013-11-20 16:15:17 +01:00
|
|
|
initial_window_size_ = 1 << get_config()->http2_upstream_window_bits;
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = spdylay_session_set_option(
|
|
|
|
session_, SPDYLAY_OPT_NO_AUTO_WINDOW_UPDATE2, &val, sizeof(val));
|
2013-07-26 13:12:55 +02:00
|
|
|
assert(rv == 0);
|
|
|
|
} else {
|
|
|
|
flow_control_ = false;
|
|
|
|
initial_window_size_ = 0;
|
|
|
|
}
|
|
|
|
// TODO Maybe call from outside?
|
|
|
|
spdylay_settings_entry entry[2];
|
|
|
|
entry[0].settings_id = SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS;
|
2013-11-04 10:14:05 +01:00
|
|
|
entry[0].value = get_config()->http2_max_concurrent_streams;
|
2013-07-26 13:12:55 +02:00
|
|
|
entry[0].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
|
|
|
|
|
|
|
|
entry[1].settings_id = SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
|
|
entry[1].value = initial_window_size_;
|
|
|
|
entry[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, entry,
|
|
|
|
util::array_size(entry));
|
2013-07-26 13:12:55 +02:00
|
|
|
assert(rv == 0);
|
2013-11-20 16:15:17 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (version >= SPDYLAY_PROTO_SPDY3_1 &&
|
|
|
|
get_config()->http2_upstream_connection_window_bits > 16) {
|
|
|
|
int32_t delta = (1 << get_config()->http2_upstream_connection_window_bits) -
|
|
|
|
SPDYLAY_INITIAL_WINDOW_SIZE;
|
2013-11-20 16:15:17 +01:00
|
|
|
rv = spdylay_submit_window_update(session_, 0, delta);
|
|
|
|
assert(rv == 0);
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
// TODO Maybe call from outside?
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
SpdyUpstream::~SpdyUpstream() { spdylay_session_del(session_); }
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::on_read() {
|
2013-07-26 13:12:55 +02:00
|
|
|
int rv = 0;
|
2014-03-03 13:18:24 +01:00
|
|
|
|
|
|
|
rv = spdylay_session_recv(session_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
|
|
|
if (rv != SPDYLAY_ERR_EOF) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(ERROR, this) << "spdylay_session_recv() returned error: "
|
|
|
|
<< spdylay_strerror(rv);
|
|
|
|
}
|
2014-03-03 13:18:24 +01:00
|
|
|
return rv;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-03-03 13:18:24 +01:00
|
|
|
return send();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::on_write() { return send(); }
|
2013-07-26 13:12:55 +02:00
|
|
|
|
|
|
|
// After this function call, downstream may be deleted.
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::send() {
|
2013-07-26 13:12:55 +02:00
|
|
|
int rv = 0;
|
2014-03-15 08:10:42 +01:00
|
|
|
uint8_t buf[16384];
|
2014-03-03 13:18:24 +01:00
|
|
|
|
2014-11-05 16:56:07 +01:00
|
|
|
sendbuf.reset(bufferevent_get_output(handler_->get_bev()), buf, sizeof(buf),
|
|
|
|
handler_->get_write_limit());
|
2014-03-03 13:18:24 +01:00
|
|
|
|
|
|
|
rv = spdylay_session_send(session_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(ERROR, this) << "spdylay_session_send() returned error: "
|
|
|
|
<< spdylay_strerror(rv);
|
2014-03-03 13:18:24 +01:00
|
|
|
return rv;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-03-04 16:23:33 +01:00
|
|
|
|
|
|
|
rv = sendbuf.flush();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-03 13:18:24 +01:00
|
|
|
ULOG(FATAL, this) << "evbuffer_add() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-05 16:56:07 +01:00
|
|
|
handler_->update_warmup_writelen(sendbuf.get_writelen());
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (spdylay_session_want_read(session_) == 0 &&
|
|
|
|
spdylay_session_want_write(session_) == 0 &&
|
|
|
|
handler_->get_outbuf_length() == 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-03-03 13:18:24 +01:00
|
|
|
ULOG(INFO, this) << "No more read/write for this SPDY session";
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-03-03 13:18:24 +01:00
|
|
|
return -1;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-03-03 13:18:24 +01:00
|
|
|
return 0;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::on_event() { return 0; }
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ClientHandler *SpdyUpstream::get_client_handler() const { return handler_; }
|
2013-07-26 13:12:55 +02:00
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void spdy_downstream_readcb(bufferevent *bev, void *ptr) {
|
|
|
|
auto dconn = static_cast<DownstreamConnection *>(ptr);
|
2013-12-20 15:36:24 +01:00
|
|
|
auto downstream = dconn->get_downstream();
|
2014-11-27 15:39:04 +01:00
|
|
|
auto upstream = static_cast<SpdyUpstream *>(downstream->get_upstream());
|
|
|
|
if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
2013-07-26 13:12:55 +02:00
|
|
|
// If upstream SPDY stream was closed, we just close downstream,
|
|
|
|
// because there is no consumer now. Downstream connection is also
|
|
|
|
// closed in this case.
|
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstrea was deleted
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::MSG_RESET) {
|
2013-07-26 13:12:55 +02: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-11-27 15:39:04 +01:00
|
|
|
upstream->rst_stream(downstream,
|
|
|
|
infer_upstream_rst_stream_status_code(
|
|
|
|
downstream->get_response_rst_stream_error_code()));
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream->pop_downstream_connection();
|
2014-06-01 16:44:32 +02:00
|
|
|
dconn = nullptr;
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
2014-06-01 16:44:32 +02:00
|
|
|
auto rv = downstream->on_read();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DCLOG(INFO, dconn) << "HTTP parser failure";
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2013-07-26 13:12:55 +02:00
|
|
|
upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (downstream->get_response_state() != Downstream::MSG_COMPLETE) {
|
2013-08-20 17:56:08 +02:00
|
|
|
// If response was completed, then don't issue RST_STREAM
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->error_reply(downstream, 502) != 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
// Clearly, we have to close downstream connection on http parser
|
|
|
|
// failure.
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream->pop_downstream_connection();
|
2014-06-01 16:44:32 +02:00
|
|
|
dconn = nullptr;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->send() != 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// At this point, downstream may be deleted.
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void spdy_downstream_writecb(bufferevent *bev, void *ptr) {
|
|
|
|
if (evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
auto dconn = static_cast<DownstreamConnection *>(ptr);
|
2014-08-21 14:22:16 +02:00
|
|
|
dconn->on_write();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void spdy_downstream_eventcb(bufferevent *bev, short events, void *ptr) {
|
|
|
|
auto dconn = static_cast<DownstreamConnection *>(ptr);
|
2013-12-20 15:36:24 +01:00
|
|
|
auto downstream = dconn->get_downstream();
|
2014-11-27 15:39:04 +01:00
|
|
|
auto upstream = static_cast<SpdyUpstream *>(downstream->get_upstream());
|
2014-06-01 16:44:32 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (events & BEV_EVENT_CONNECTED) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DCLOG(INFO, dconn) << "Connection established. stream_id="
|
|
|
|
<< downstream->get_stream_id();
|
|
|
|
}
|
|
|
|
int fd = bufferevent_getfd(bev);
|
|
|
|
int val = 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&val),
|
|
|
|
sizeof(val)) == -1) {
|
2014-11-08 02:51:56 +01:00
|
|
|
DCLOG(WARN, dconn) << "Setting option TCP_NODELAY failed: errno="
|
|
|
|
<< errno;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (events & BEV_EVENT_EOF) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DCLOG(INFO, dconn) << "EOF. stream_id=" << downstream->get_stream_id();
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
2013-07-26 13:12:55 +02:00
|
|
|
// If stream was closed already, we don't need to send reply at
|
|
|
|
// the first place. We can delete downstream.
|
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstrea was deleted
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
// Delete downstream connection. If we don't delete it here, it
|
|
|
|
// will be pooled in on_stream_close_callback.
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream->pop_downstream_connection();
|
2014-06-01 16:44:32 +02:00
|
|
|
dconn = nullptr;
|
|
|
|
// downstream wil be deleted in on_stream_close_callback.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2014-06-01 16:44:32 +02:00
|
|
|
// Server may indicate the end of the request by EOF
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-06-01 16:44:32 +02:00
|
|
|
ULOG(INFO, upstream) << "Downstream body was ended by EOF";
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
|
|
|
|
// For tunneled connection, MSG_COMPLETE signals
|
|
|
|
// spdy_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.
|
|
|
|
upstream->on_downstream_body_complete(downstream);
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (downstream->get_response_state() != Downstream::MSG_COMPLETE) {
|
2014-06-01 16:44:32 +02:00
|
|
|
// If stream was not closed, then we set MSG_COMPLETE and let
|
|
|
|
// on_stream_close_callback delete downstream.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->error_reply(downstream, 502) != 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->send() != 0) {
|
2014-06-01 16:44:32 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// At this point, downstream may be deleted.
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
if (events & BEV_EVENT_ERROR) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DCLOG(INFO, dconn) << "Downstream network error: "
|
2014-11-27 15:39:04 +01:00
|
|
|
<< evutil_socket_error_to_string(
|
|
|
|
EVUTIL_SOCKET_ERROR());
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
|
|
|
DCLOG(INFO, dconn) << "Timeout";
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_upgraded()) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DCLOG(INFO, dconn) << "Note: this is tunnel connection";
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
2013-07-26 13:12:55 +02:00
|
|
|
upstream->remove_downstream(downstream);
|
2014-08-18 15:59:31 +02:00
|
|
|
// downstrea was deleted
|
|
|
|
|
2014-06-01 16:44:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete downstream connection. If we don't delete it here, it
|
|
|
|
// will be pooled in on_stream_close_callback.
|
2014-08-18 17:16:51 +02:00
|
|
|
downstream->pop_downstream_connection();
|
2014-06-01 16:44:32 +02:00
|
|
|
dconn = nullptr;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2014-06-01 16:44:32 +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.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_upgraded()) {
|
2014-06-01 16:44:32 +02:00
|
|
|
upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
}
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2014-06-01 16:44:32 +02:00
|
|
|
upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
2014-06-01 16:44:32 +02:00
|
|
|
unsigned int status;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (events & BEV_EVENT_TIMEOUT) {
|
2014-06-01 16:44:32 +02:00
|
|
|
status = 504;
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
2014-06-01 16:44:32 +02:00
|
|
|
status = 502;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->error_reply(downstream, status) != 0) {
|
2014-06-01 16:44:32 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-01 16:44:32 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (upstream->send() != 0) {
|
2014-06-01 16:44:32 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// At this point, downstream may be deleted.
|
|
|
|
return;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::rst_stream(Downstream *downstream, int status_code) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, this) << "RST_STREAM stream_id=" << downstream->get_stream_id();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
int rv;
|
|
|
|
rv = spdylay_submit_rst_stream(session_, downstream->get_stream_id(),
|
|
|
|
status_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < SPDYLAY_ERR_FATAL) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(FATAL, this) << "spdylay_submit_rst_stream() failed: "
|
|
|
|
<< spdylay_strerror(rv);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t spdy_data_read_callback(spdylay_session *session, int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length, int *eof,
|
|
|
|
spdylay_data_source *source, void *user_data) {
|
|
|
|
auto downstream = static_cast<Downstream *>(source->ptr);
|
|
|
|
auto upstream = static_cast<SpdyUpstream *>(downstream->get_upstream());
|
2013-12-20 15:36:24 +01:00
|
|
|
auto body = downstream->get_response_body_buf();
|
2014-11-06 13:14:14 +01:00
|
|
|
auto handler = upstream->get_client_handler();
|
2013-07-26 13:12:55 +02:00
|
|
|
assert(body);
|
2014-11-06 13:14:14 +01:00
|
|
|
|
|
|
|
auto limit = handler->get_write_limit();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (limit != -1) {
|
2014-11-06 13:14:14 +01:00
|
|
|
// 9 is HTTP/2 frame header length. Make DATA frame also under
|
|
|
|
// certain limit, so that application layer can flush at DATA
|
|
|
|
// frame boundary, instead of buffering large frame.
|
|
|
|
assert(limit > 9);
|
|
|
|
length = std::min(length, static_cast<size_t>(limit - 9));
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
int nread = evbuffer_remove(body, buf, length);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == -1) {
|
2013-12-20 15:28:54 +01:00
|
|
|
ULOG(FATAL, upstream) << "evbuffer_remove() failed";
|
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == 0 &&
|
|
|
|
downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
if (!downstream->get_upgraded()) {
|
2013-07-26 13:12:55 +02:00
|
|
|
*eof = 1;
|
|
|
|
} 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-07-26 13:12:55 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
upstream->rst_stream(
|
|
|
|
downstream, infer_upstream_rst_stream_status_code(
|
|
|
|
downstream->get_response_rst_stream_error_code()));
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-16 14:42:30 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (evbuffer_get_length(body) > 0) {
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->reset_upstream_wtimer();
|
|
|
|
} else {
|
|
|
|
downstream->disable_upstream_wtimer();
|
|
|
|
}
|
|
|
|
|
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 SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2014-05-16 14:42:30 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == 0 && *eof != 1) {
|
2014-08-21 14:22:16 +02:00
|
|
|
return SPDYLAY_ERR_DEFERRED;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-05-16 14:42:30 +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);
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::error_reply(Downstream *downstream,
|
|
|
|
unsigned int status_code) {
|
2013-07-26 13:12:55 +02:00
|
|
|
int rv;
|
2013-12-20 15:36:24 +01:00
|
|
|
auto html = http::create_error_html(status_code);
|
2014-07-05 11:22:40 +02:00
|
|
|
downstream->set_response_http_status(status_code);
|
2013-07-26 13:12:55 +02:00
|
|
|
downstream->init_response_body_buf();
|
2013-12-20 15:36:24 +01:00
|
|
|
auto body = downstream->get_response_body_buf();
|
2013-07-26 13:12:55 +02:00
|
|
|
rv = evbuffer_add(body, html.c_str(), html.size());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == -1) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(FATAL, this) << "evbuffer_add() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
|
|
|
|
spdylay_data_provider data_prd;
|
|
|
|
data_prd.source.ptr = downstream;
|
|
|
|
data_prd.read_callback = spdy_data_read_callback;
|
|
|
|
|
|
|
|
std::string content_length = util::utos(html.size());
|
2013-10-02 16:29:44 +02:00
|
|
|
std::string status_string = http2::get_status_string(status_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
const char *nv[] = {":status", status_string.c_str(),
|
|
|
|
":version", "http/1.1",
|
|
|
|
"content-type", "text/html; charset=UTF-8",
|
|
|
|
"server", get_config()->server_name,
|
|
|
|
"content-length", content_length.c_str(),
|
|
|
|
nullptr};
|
2013-07-26 13:12:55 +02:00
|
|
|
|
|
|
|
rv = spdylay_submit_response(session_, downstream->get_stream_id(), nv,
|
|
|
|
&data_prd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < SPDYLAY_ERR_FATAL) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(FATAL, this) << "spdylay_submit_response() failed: "
|
|
|
|
<< spdylay_strerror(rv);
|
|
|
|
DIE();
|
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bufferevent_data_cb SpdyUpstream::get_downstream_readcb() {
|
2013-07-26 13:12:55 +02:00
|
|
|
return spdy_downstream_readcb;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bufferevent_data_cb SpdyUpstream::get_downstream_writecb() {
|
2013-07-26 13:12:55 +02:00
|
|
|
return spdy_downstream_writecb;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bufferevent_event_cb SpdyUpstream::get_downstream_eventcb() {
|
2013-07-26 13:12:55 +02:00
|
|
|
return spdy_downstream_eventcb;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Downstream *SpdyUpstream::add_pending_downstream(int32_t stream_id,
|
|
|
|
int32_t priority) {
|
2014-08-18 15:59:31 +02:00
|
|
|
auto downstream = util::make_unique<Downstream>(this, stream_id, priority);
|
|
|
|
auto res = downstream.get();
|
|
|
|
|
|
|
|
downstream_queue_.add_pending(std::move(downstream));
|
|
|
|
|
|
|
|
return res;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdyUpstream::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));
|
|
|
|
}
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Downstream *SpdyUpstream::find_downstream(int32_t stream_id) {
|
2013-07-26 13:12:55 +02:00
|
|
|
return downstream_queue_.find(stream_id);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
spdylay_session *SpdyUpstream::get_http2_session() { return session_; }
|
2013-07-26 13:12:55 +02:00
|
|
|
|
|
|
|
// WARNING: Never call directly or indirectly spdylay_session_send or
|
|
|
|
// spdylay_session_recv. These calls may delete downstream.
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) {
|
|
|
|
if (downstream->get_non_final_response()) {
|
2014-07-23 16:32:57 +02:00
|
|
|
// SPDY does not support non-final response. We could send it
|
|
|
|
// with HEADERS and final response in SYN_REPLY, but it is not
|
|
|
|
// official way.
|
|
|
|
downstream->clear_response_headers();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DLOG(INFO, downstream) << "HTTP response header completed";
|
|
|
|
}
|
2013-12-21 09:49:31 +01:00
|
|
|
downstream->normalize_response_headers();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->http2_proxy && !get_config()->client_proxy &&
|
|
|
|
!get_config()->no_location_rewrite) {
|
|
|
|
downstream->rewrite_norm_location_response_header(
|
|
|
|
get_client_handler()->get_upstream_scheme(), get_config()->port);
|
2013-12-28 09:02:43 +01:00
|
|
|
}
|
2013-07-26 13:12:55 +02:00
|
|
|
size_t nheader = downstream->get_response_headers().size();
|
2014-08-14 15:45:21 +02:00
|
|
|
// 8 means server, :status, :version and possible via header field.
|
2014-11-27 15:39:04 +01:00
|
|
|
auto nv = util::make_unique<const char *[]>(
|
|
|
|
nheader * 2 + 8 + get_config()->add_response_headers.size() * 2 + 1);
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
size_t hdidx = 0;
|
|
|
|
std::string via_value;
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string status_string =
|
|
|
|
http2::get_status_string(downstream->get_response_http_status());
|
2013-10-02 16:29:44 +02:00
|
|
|
nv[hdidx++] = ":status";
|
|
|
|
nv[hdidx++] = status_string.c_str();
|
2013-07-26 13:12:55 +02:00
|
|
|
nv[hdidx++] = ":version";
|
|
|
|
nv[hdidx++] = "HTTP/1.1";
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &hd : downstream->get_response_headers()) {
|
|
|
|
if (hd.name.empty() || hd.name.c_str()[0] == ':' ||
|
|
|
|
util::strieq(hd.name.c_str(), "transfer-encoding") ||
|
|
|
|
util::strieq(hd.name.c_str(), "keep-alive") || // HTTP/1.0?
|
|
|
|
util::strieq(hd.name.c_str(), "connection") ||
|
|
|
|
util::strieq(hd.name.c_str(), "proxy-connection")) {
|
2013-07-26 13:12:55 +02:00
|
|
|
// These are ignored
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (!get_config()->no_via && util::strieq(hd.name.c_str(), "via")) {
|
2014-04-03 04:22:11 +02:00
|
|
|
via_value = hd.value;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (!get_config()->http2_proxy && !get_config()->client_proxy &&
|
|
|
|
util::strieq(hd.name.c_str(), "server")) {
|
2014-08-14 15:45:21 +02:00
|
|
|
// Rewrite server header field later
|
2013-07-26 13:12:55 +02:00
|
|
|
} else {
|
2014-04-03 04:22:11 +02:00
|
|
|
nv[hdidx++] = hd.name.c_str();
|
|
|
|
nv[hdidx++] = hd.value.c_str();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-14 15:45:21 +02:00
|
|
|
|
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
|
|
|
nv[hdidx++] = "server";
|
|
|
|
nv[hdidx++] = get_config()->server_name;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->no_via) {
|
|
|
|
if (!via_value.empty()) {
|
2013-07-26 13:12:55 +02: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-07-26 13:12:55 +02:00
|
|
|
nv[hdidx++] = "via";
|
|
|
|
nv[hdidx++] = via_value.c_str();
|
|
|
|
}
|
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
|
|
|
nv[hdidx++] = p.first.c_str();
|
|
|
|
nv[hdidx++] = p.second.c_str();
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
nv[hdidx++] = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
std::stringstream ss;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size_t i = 0; nv[i]; i += 2) {
|
|
|
|
ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i + 1] << "\n";
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
ULOG(INFO, this) << "HTTP response headers. stream_id="
|
2014-11-27 15:39:04 +01:00
|
|
|
<< downstream->get_stream_id() << "\n" << ss.str();
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
|
|
|
spdylay_data_provider data_prd;
|
|
|
|
data_prd.source.ptr = downstream;
|
|
|
|
data_prd.read_callback = spdy_data_read_callback;
|
|
|
|
|
|
|
|
int rv;
|
2013-09-06 18:52:46 +02:00
|
|
|
rv = spdylay_submit_response(session_, downstream->get_stream_id(), nv.get(),
|
2013-07-26 13:12:55 +02:00
|
|
|
&data_prd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(FATAL, this) << "spdylay_submit_response() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Never call directly or indirectly spdylay_session_send or
|
|
|
|
// spdylay_session_recv. These calls may delete downstream.
|
|
|
|
int SpdyUpstream::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) {
|
2013-12-20 15:36:24 +01:00
|
|
|
auto body = downstream->get_response_body_buf();
|
2013-07-26 13:12:55 +02:00
|
|
|
int rv = evbuffer_add(body, data, len);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-26 13:12:55 +02:00
|
|
|
ULOG(FATAL, this) << "evbuffer_add() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
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
|
|
|
spdylay_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
|
|
|
}
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (evbuffer_get_length(body) >= INBUF_MAX_THRES) {
|
|
|
|
if (!flush) {
|
2014-04-03 11:54:15 +02:00
|
|
|
spdylay_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
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
downstream->pause_read(SHRPX_NO_BUFFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Never call directly or indirectly spdylay_session_send or
|
|
|
|
// spdylay_session_recv. These calls may delete downstream.
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::on_downstream_body_complete(Downstream *downstream) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2013-07-26 13:12:55 +02:00
|
|
|
DLOG(INFO, downstream) << "HTTP response completed";
|
|
|
|
}
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
spdylay_session_resume_data(session_, downstream->get_stream_id());
|
2014-08-09 11:47:45 +02:00
|
|
|
downstream->ensure_upstream_wtimer();
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool SpdyUpstream::get_flow_control() const { return flow_control_; }
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdyUpstream::pause_read(IOCtrlReason reason) {}
|
2013-07-26 13:12:55 +02:00
|
|
|
|
2014-08-21 14:22:16 +02:00
|
|
|
int SpdyUpstream::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-08-21 14:22:16 +02:00
|
|
|
return -1;
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-08-21 14:22:16 +02:00
|
|
|
|
|
|
|
downstream->dec_request_datalen(consumed);
|
2013-07-26 13:12:55 +02:00
|
|
|
}
|
2014-08-21 14:22:16 +02:00
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
return send();
|
|
|
|
}
|
|
|
|
|
2014-06-27 15:34:54 +02:00
|
|
|
int SpdyUpstream::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return send();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::consume(int32_t stream_id, size_t len) {
|
2014-08-21 14:22:16 +02:00
|
|
|
int rv;
|
2014-07-02 16:07:46 +02:00
|
|
|
|
2014-08-21 14:22:16 +02:00
|
|
|
rv = spdylay_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) << "spdylay_session_consume() returned error: "
|
|
|
|
<< spdylay_strerror(rv);
|
2014-08-21 14:22:16 +02:00
|
|
|
return -1;
|
2014-07-02 16:07:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int SpdyUpstream::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, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdyUpstream::reset_timeouts() {
|
2014-09-18 16:03:36 +02:00
|
|
|
handler_->set_upstream_timeouts(&get_config()->http2_upstream_read_timeout,
|
|
|
|
&get_config()->upstream_write_timeout);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdyUpstream::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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 SpdyUpstream::on_downstream_reset() {
|
|
|
|
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, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// downstream connection is clean; we can retry with new
|
|
|
|
// downstream connection.
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
|
|
|
|
rv = downstream->attach_downstream_connection(
|
|
|
|
handler_->get_downstream_connection());
|
|
|
|
if (rv != 0) {
|
|
|
|
rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
downstream->pop_downstream_connection();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = send();
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
} // namespace shrpx
|