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.
|
|
|
|
*/
|
|
|
|
#include "shrpx_spdy_upstream.h"
|
|
|
|
|
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"
|
|
|
|
#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"
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
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 {
|
2012-06-09 16:14:00 +02:00
|
|
|
const size_t SHRPX_SPDY_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;
|
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data);
|
|
|
|
ClientHandler *handler = upstream->get_client_handler();
|
|
|
|
bufferevent *bev = handler->get_bev();
|
|
|
|
evbuffer *output = bufferevent_get_output(bev);
|
2012-06-04 20:11:43 +02:00
|
|
|
// Check buffer length and return WOULDBLOCK if it is large enough.
|
|
|
|
if(evbuffer_get_length(output) > SHRPX_SPDY_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-07-12 17:19:03 +02:00
|
|
|
ssize_t recv_callback(nghttp2_session *session,
|
2012-06-04 16:48:31 +02:00
|
|
|
uint8_t *data, size_t len, int flags, void *user_data)
|
|
|
|
{
|
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data);
|
|
|
|
ClientHandler *handler = upstream->get_client_handler();
|
|
|
|
bufferevent *bev = handler->get_bev();
|
|
|
|
evbuffer *input = bufferevent_get_input(bev);
|
|
|
|
int nread = evbuffer_remove(input, data, len);
|
|
|
|
if(nread == -1) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-06-04 16:48:31 +02:00
|
|
|
} else if(nread == 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_WOULDBLOCK;
|
2012-06-04 16:48:31 +02:00
|
|
|
} else {
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void on_stream_close_callback
|
2013-07-12 17:19:03 +02:00
|
|
|
(nghttp2_session *session, int32_t stream_id, nghttp2_status_code status_code,
|
2012-06-04 16:48:31 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
2012-12-09 11:15:14 +01:00
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(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
|
|
|
}
|
2012-06-07 17:36:19 +02:00
|
|
|
Downstream *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
|
2012-07-11 09:20:16 +02:00
|
|
|
if(!downstream->tunnel_established() &&
|
|
|
|
!downstream->get_response_connection_close()) {
|
2012-06-09 16:14:00 +02:00
|
|
|
// Keep-alive
|
|
|
|
DownstreamConnection *dconn;
|
|
|
|
dconn = downstream->get_downstream_connection();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void on_ctrl_recv_callback
|
2013-07-12 17:19:03 +02:00
|
|
|
(nghttp2_session *session, nghttp2_frame_type type, nghttp2_frame *frame,
|
2012-06-04 16:48:31 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data);
|
|
|
|
switch(type) {
|
2013-07-12 17:19:03 +02:00
|
|
|
case NGHTTP2_SYN_STREAM: {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "Received upstream SYN_STREAM stream_id="
|
|
|
|
<< frame->syn_stream.stream_id;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-07 17:36:19 +02:00
|
|
|
Downstream *downstream;
|
2012-06-09 16:14:00 +02:00
|
|
|
downstream = new Downstream(upstream,
|
|
|
|
frame->syn_stream.stream_id,
|
|
|
|
frame->syn_stream.pri);
|
|
|
|
upstream->add_downstream(downstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->init_response_body_buf();
|
|
|
|
|
|
|
|
char **nv = frame->syn_stream.nv;
|
2012-07-11 09:20:16 +02:00
|
|
|
const char *path = 0;
|
|
|
|
const char *scheme = 0;
|
|
|
|
const char *host = 0;
|
|
|
|
const char *method = 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
for(size_t i = 0; nv[i]; i += 2) {
|
|
|
|
if(strcmp(nv[i], ":path") == 0) {
|
2012-07-11 09:20:16 +02:00
|
|
|
path = nv[i+1];
|
|
|
|
} else if(strcmp(nv[i], ":scheme") == 0) {
|
|
|
|
scheme = nv[i+1];
|
2012-06-04 16:48:31 +02:00
|
|
|
} else if(strcmp(nv[i], ":method") == 0) {
|
2012-07-11 09:20:16 +02:00
|
|
|
method = nv[i+1];
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->set_request_method(nv[i+1]);
|
2012-07-11 09:20:16 +02:00
|
|
|
} else if(strcmp(nv[i], ":host") == 0) {
|
|
|
|
host = nv[i+1];
|
2012-06-04 16:48:31 +02:00
|
|
|
} else if(nv[i][0] != ':') {
|
|
|
|
downstream->add_request_header(nv[i], nv[i+1]);
|
|
|
|
}
|
|
|
|
}
|
2012-07-11 09:20:16 +02:00
|
|
|
if(!path || !host || !method) {
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2012-07-11 09:20:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-02-08 13:46:58 +01:00
|
|
|
// SpdyDownstreamConnection examines request path to find
|
|
|
|
// scheme. We construct abs URI for spdy_bridge mode as well as
|
|
|
|
// spdy_proxy mode.
|
|
|
|
if((get_config()->spdy_proxy || get_config()->spdy_bridge) &&
|
|
|
|
scheme && path[0] == '/') {
|
2012-07-11 09:20:16 +02:00
|
|
|
std::string reqpath = scheme;
|
|
|
|
reqpath += "://";
|
|
|
|
reqpath += host;
|
|
|
|
reqpath += path;
|
|
|
|
downstream->set_request_path(reqpath);
|
|
|
|
} else {
|
|
|
|
downstream->set_request_path(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
downstream->add_request_header("host", host);
|
2012-06-04 16:48:31 +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;
|
|
|
|
for(size_t i = 0; nv[i]; i += 2) {
|
2012-12-09 13:36:02 +01:00
|
|
|
ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\n";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "HTTP request headers. stream_id="
|
|
|
|
<< downstream->get_stream_id()
|
|
|
|
<< "\n" << ss.str();
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection *dconn;
|
|
|
|
dconn = upstream->get_client_handler()->get_downstream_connection();
|
|
|
|
int rv = dconn->attach_downstream(downstream);
|
|
|
|
if(rv != 0) {
|
|
|
|
// If downstream connection fails, issue RST_STREAM.
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2012-06-09 16:14:00 +02:00
|
|
|
downstream->set_request_state(Downstream::CONNECT_FAIL);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-16 17:12:31 +02:00
|
|
|
rv = downstream->push_request_headers();
|
|
|
|
if(rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR);
|
2012-07-16 17:12:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
2013-07-12 17:19:03 +02:00
|
|
|
if(frame->syn_stream.hd.flags & NGHTTP2_CTRL_FLAG_FIN) {
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
void on_data_chunk_recv_callback(nghttp2_session *session,
|
2012-06-04 16:48:31 +02:00
|
|
|
uint8_t flags, int32_t stream_id,
|
|
|
|
const uint8_t *data, size_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data);
|
2012-06-07 17:36:19 +02:00
|
|
|
Downstream *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);
|
2012-07-16 17:12:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-06-09 18:36:30 +02:00
|
|
|
if(upstream->get_flow_control()) {
|
|
|
|
downstream->inc_recv_window_size(len);
|
|
|
|
if(downstream->get_recv_window_size() >
|
|
|
|
upstream->get_initial_window_size()) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, upstream) << "Flow control error: recv_window_size="
|
|
|
|
<< downstream->get_recv_window_size()
|
|
|
|
<< ", initial_window_size="
|
|
|
|
<< upstream->get_initial_window_size();
|
2012-06-09 18:36:30 +02:00
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
upstream->rst_stream(downstream, NGHTTP2_FLOW_CONTROL_ERROR);
|
2012-06-09 18:36:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
if(flags & NGHTTP2_DATA_FLAG_FIN) {
|
2012-06-04 16:48:31 +02:00
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-09-15 10:19:58 +02:00
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
void on_ctrl_not_send_callback(nghttp2_session *session,
|
|
|
|
nghttp2_frame_type type,
|
|
|
|
nghttp2_frame *frame,
|
2012-09-15 10:19:58 +02:00
|
|
|
int error_code, void *user_data)
|
|
|
|
{
|
2012-12-09 11:15:14 +01:00
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data);
|
|
|
|
ULOG(WARNING, upstream) << "Failed to send control frame type=" << type
|
|
|
|
<< ", error_code=" << error_code << ":"
|
2013-07-12 17:19:03 +02:00
|
|
|
<< nghttp2_strerror(error_code);
|
|
|
|
if(type == NGHTTP2_SYN_REPLY) {
|
2012-09-15 10:19:58 +02:00
|
|
|
// To avoid stream hanging around, issue RST_STREAM.
|
|
|
|
int32_t stream_id = frame->syn_reply.stream_id;
|
|
|
|
Downstream *downstream = upstream->find_downstream(stream_id);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
void on_ctrl_recv_parse_error_callback(nghttp2_session *session,
|
|
|
|
nghttp2_frame_type type,
|
2012-09-15 10:19:58 +02:00
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen, int error_code,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-12-09 11:15:14 +01:00
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(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
|
|
|
|
<< ", error_code=" << error_code << ":"
|
2013-07-12 17:19:03 +02:00
|
|
|
<< nghttp2_strerror(error_code);
|
2012-09-15 10:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
void on_unknown_ctrl_recv_callback(nghttp2_session *session,
|
2012-09-15 10:19:58 +02:00
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload, size_t payloadlen,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-12-09 11:15:14 +01:00
|
|
|
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(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
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-02-27 14:39:44 +01:00
|
|
|
namespace {
|
|
|
|
uint32_t infer_upstream_rst_stream_status_code(uint32_t downstream_status_code)
|
|
|
|
{
|
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-12 17:19:03 +02:00
|
|
|
if(downstream_status_code != NGHTTP2_REFUSED_STREAM) {
|
|
|
|
return NGHTTP2_INTERNAL_ERROR;
|
2013-02-27 14:39:44 +01:00
|
|
|
} else {
|
|
|
|
return downstream_status_code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
|
|
|
|
: handler_(handler),
|
|
|
|
session_(0)
|
|
|
|
{
|
|
|
|
//handler->set_bev_cb(spdy_readcb, 0, spdy_eventcb);
|
|
|
|
handler->set_upstream_timeouts(&get_config()->spdy_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.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;
|
2012-09-15 10:19:58 +02:00
|
|
|
callbacks.on_ctrl_not_send_callback = on_ctrl_not_send_callback;
|
|
|
|
callbacks.on_ctrl_recv_parse_error_callback =
|
|
|
|
on_ctrl_recv_parse_error_callback;
|
|
|
|
callbacks.on_unknown_ctrl_recv_callback = on_unknown_ctrl_recv_callback;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_session_server_new(&session_, version, &callbacks, this);
|
2012-06-04 16:48:31 +02:00
|
|
|
assert(rv == 0);
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
if(version == NGHTTP2_PROTO_SPDY3) {
|
2012-06-09 18:36:30 +02:00
|
|
|
int val = 1;
|
|
|
|
flow_control_ = true;
|
2012-07-26 16:18:37 +02:00
|
|
|
initial_window_size_ = 1 << get_config()->spdy_upstream_window_bits;
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_session_set_option(session_,
|
|
|
|
NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE, &val,
|
2012-06-09 18:36:30 +02:00
|
|
|
sizeof(val));
|
|
|
|
assert(rv == 0);
|
|
|
|
} else {
|
|
|
|
flow_control_ = false;
|
|
|
|
initial_window_size_ = 0;
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
// TODO Maybe call from outside?
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_settings_entry entry[2];
|
|
|
|
entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
|
2012-06-09 18:36:30 +02:00
|
|
|
entry[0].value = get_config()->spdy_max_concurrent_streams;
|
2013-07-12 17:19:03 +02:00
|
|
|
entry[0].flags = NGHTTP2_ID_FLAG_SETTINGS_NONE;
|
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;
|
2012-06-09 18:36:30 +02:00
|
|
|
entry[1].value = initial_window_size_;
|
2013-07-12 17:19:03 +02:00
|
|
|
entry[1].flags = NGHTTP2_ID_FLAG_SETTINGS_NONE;
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_submit_settings
|
|
|
|
(session_, NGHTTP2_FLAG_SETTINGS_NONE,
|
|
|
|
entry, sizeof(entry)/sizeof(nghttp2_settings_entry));
|
2012-06-04 16:48:31 +02:00
|
|
|
assert(rv == 0);
|
|
|
|
// TODO Maybe call from outside?
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
|
|
|
|
SpdyUpstream::~SpdyUpstream()
|
|
|
|
{
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_del(session_);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int SpdyUpstream::on_read()
|
|
|
|
{
|
2012-07-16 16:03:07 +02:00
|
|
|
int rv = 0;
|
2013-07-12 17:19:03 +02:00
|
|
|
if((rv = nghttp2_session_recv(session_)) < 0) {
|
|
|
|
if(rv != NGHTTP2_ERR_EOF) {
|
|
|
|
ULOG(ERROR, this) << "nghttp2_session_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
} else 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 &&
|
|
|
|
nghttp2_session_want_write(session_) == 0) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, this) << "No more read/write for this SPDY 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
|
|
|
}
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
int SpdyUpstream::on_write()
|
|
|
|
{
|
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.
|
2012-06-04 16:48:31 +02:00
|
|
|
int SpdyUpstream::send()
|
|
|
|
{
|
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 &&
|
|
|
|
nghttp2_session_want_write(session_) == 0) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
ULOG(INFO, this) << "No more read/write for this SPDY 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
|
|
|
}
|
|
|
|
|
|
|
|
int SpdyUpstream::on_event()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientHandler* SpdyUpstream::get_client_handler() const
|
|
|
|
{
|
|
|
|
return handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void spdy_downstream_readcb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
Downstream *downstream = dconn->get_downstream();
|
2012-06-04 16:48:31 +02:00
|
|
|
SpdyUpstream *upstream;
|
|
|
|
upstream = static_cast<SpdyUpstream*>(downstream->get_upstream());
|
2012-06-05 15:46:47 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::STREAM_CLOSED) {
|
2012-06-09 16:14:00 +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.
|
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-02-27 14:39:44 +01:00
|
|
|
upstream->rst_stream(downstream, infer_upstream_rst_stream_status_code
|
|
|
|
(downstream->get_response_rst_stream_status_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-02-27 14:39:44 +01:00
|
|
|
} else {
|
|
|
|
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 {
|
|
|
|
void spdy_downstream_writecb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
2012-11-21 19:13:30 +01:00
|
|
|
if(evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-09 18:36:30 +02:00
|
|
|
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
Downstream *downstream = dconn->get_downstream();
|
|
|
|
SpdyUpstream *upstream;
|
|
|
|
upstream = static_cast<SpdyUpstream*>(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 {
|
|
|
|
void spdy_downstream_eventcb(bufferevent *bev, short events, void *ptr)
|
|
|
|
{
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
Downstream *downstream = dconn->get_downstream();
|
2012-06-04 16:48:31 +02:00
|
|
|
SpdyUpstream *upstream;
|
|
|
|
upstream = static_cast<SpdyUpstream*>(downstream->get_upstream());
|
|
|
|
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
|
|
|
|
// 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);
|
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
|
|
|
}
|
|
|
|
if(downstream->tunnel_established()) {
|
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) {
|
|
|
|
// For SSL tunneling
|
2013-07-12 17:19:03 +02:00
|
|
|
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 {
|
2012-06-04 20:11:43 +02:00
|
|
|
int status;
|
|
|
|
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
|
|
|
|
|
|
|
|
int SpdyUpstream::rst_stream(Downstream *downstream, int status_code)
|
|
|
|
{
|
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="
|
|
|
|
<< downstream->get_stream_id();
|
2012-06-09 18:36:30 +02:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_submit_rst_stream(session_, downstream->get_stream_id(),
|
2012-06-04 16:48:31 +02:00
|
|
|
status_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
|
|
|
}
|
|
|
|
|
2012-06-09 18:36:30 +02:00
|
|
|
int SpdyUpstream::window_update(Downstream *downstream)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_submit_window_update(session_, downstream->get_stream_id(),
|
2012-06-09 18:36:30 +02:00
|
|
|
downstream->get_recv_window_size());
|
|
|
|
downstream->set_recv_window_size(0);
|
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
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
ssize_t spdy_data_read_callback(nghttp2_session *session,
|
2012-06-04 16:48:31 +02:00
|
|
|
int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length,
|
|
|
|
int *eof,
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_data_source *source,
|
2012-06-04 16:48:31 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
Downstream *downstream = reinterpret_cast<Downstream*>(source->ptr);
|
|
|
|
evbuffer *body = downstream->get_response_body_buf();
|
|
|
|
assert(body);
|
|
|
|
int nread = evbuffer_remove(body, buf, length);
|
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-02-10 18:05:11 +01:00
|
|
|
if(!downstream->tunnel_established()) {
|
|
|
|
*eof = 1;
|
|
|
|
} else {
|
|
|
|
// For tunneling, issue RST_STREAM to finish the stream.
|
|
|
|
SpdyUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<SpdyUpstream*>(downstream->get_upstream());
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
ULOG(INFO, upstream) << "RST_STREAM to tunneled stream stream_id="
|
|
|
|
<< stream_id;
|
|
|
|
}
|
2013-02-27 14:39:44 +01:00
|
|
|
upstream->rst_stream(downstream, infer_upstream_rst_stream_status_code
|
|
|
|
(downstream->get_response_rst_stream_status_code()));
|
2013-02-10 18:05:11 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
int SpdyUpstream::error_reply(Downstream *downstream, int status_code)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
std::string html = http::create_error_html(status_code);
|
|
|
|
downstream->init_response_body_buf();
|
|
|
|
evbuffer *body = downstream->get_response_body_buf();
|
|
|
|
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;
|
|
|
|
data_prd.read_callback = spdy_data_read_callback;
|
|
|
|
|
2013-01-16 14:43:11 +01:00
|
|
|
std::string content_length = util::utos(html.size());
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
const char *nv[] = {
|
|
|
|
":status", http::get_status_string(status_code),
|
|
|
|
":version", "http/1.1",
|
|
|
|
"content-type", "text/html; charset=UTF-8",
|
|
|
|
"server", get_config()->server_name,
|
2013-01-16 14:43:11 +01:00
|
|
|
"content-length", content_length.c_str(),
|
2012-06-04 16:48:31 +02:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_submit_response(session_, downstream->get_stream_id(), nv,
|
2012-06-04 16:48:31 +02:00
|
|
|
&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
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_data_cb SpdyUpstream::get_downstream_readcb()
|
|
|
|
{
|
|
|
|
return spdy_downstream_readcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_data_cb SpdyUpstream::get_downstream_writecb()
|
|
|
|
{
|
|
|
|
return spdy_downstream_writecb;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_event_cb SpdyUpstream::get_downstream_eventcb()
|
|
|
|
{
|
|
|
|
return spdy_downstream_eventcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpdyUpstream::add_downstream(Downstream *downstream)
|
|
|
|
{
|
|
|
|
downstream_queue_.add(downstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpdyUpstream::remove_downstream(Downstream *downstream)
|
|
|
|
{
|
|
|
|
downstream_queue_.remove(downstream);
|
|
|
|
}
|
|
|
|
|
2012-06-07 17:36:19 +02:00
|
|
|
Downstream* SpdyUpstream::find_downstream(int32_t stream_id)
|
|
|
|
{
|
|
|
|
return downstream_queue_.find(stream_id);
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session* SpdyUpstream::get_spdy_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.
|
2012-06-04 16:48:31 +02:00
|
|
|
int SpdyUpstream::on_downstream_header_complete(Downstream *downstream)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
size_t nheader = downstream->get_response_headers().size();
|
2012-06-06 19:29:00 +02:00
|
|
|
// 6 means :status, :version and possible via header field.
|
|
|
|
const char **nv = new const char*[nheader * 2 + 6 + 1];
|
2012-06-04 16:48:31 +02:00
|
|
|
size_t hdidx = 0;
|
2012-06-06 19:29:00 +02:00
|
|
|
std::string via_value;
|
|
|
|
nv[hdidx++] = ":status";
|
|
|
|
nv[hdidx++] = http::get_status_string(downstream->get_response_http_status());
|
|
|
|
nv[hdidx++] = ":version";
|
|
|
|
nv[hdidx++] = "HTTP/1.1";
|
2012-06-04 16:48:31 +02:00
|
|
|
for(Headers::const_iterator i = downstream->get_response_headers().begin();
|
|
|
|
i != downstream->get_response_headers().end(); ++i) {
|
|
|
|
if(util::strieq((*i).first.c_str(), "transfer-encoding") ||
|
|
|
|
util::strieq((*i).first.c_str(), "keep-alive") || // HTTP/1.0?
|
|
|
|
util::strieq((*i).first.c_str(), "connection") ||
|
|
|
|
util:: strieq((*i).first.c_str(), "proxy-connection")) {
|
|
|
|
// These are ignored
|
2013-01-09 14:01:25 +01:00
|
|
|
} else if(!get_config()->no_via &&
|
|
|
|
util::strieq((*i).first.c_str(), "via")) {
|
2012-06-06 19:29:00 +02:00
|
|
|
via_value = (*i).second;
|
2012-06-04 16:48:31 +02:00
|
|
|
} else {
|
|
|
|
nv[hdidx++] = (*i).first.c_str();
|
|
|
|
nv[hdidx++] = (*i).second.c_str();
|
|
|
|
}
|
|
|
|
}
|
2013-01-09 14:01:25 +01:00
|
|
|
if(!get_config()->no_via) {
|
|
|
|
if(!via_value.empty()) {
|
|
|
|
via_value += ", ";
|
|
|
|
}
|
|
|
|
via_value += http::create_via_header_value
|
|
|
|
(downstream->get_response_major(), downstream->get_response_minor());
|
|
|
|
nv[hdidx++] = "via";
|
|
|
|
nv[hdidx++] = via_value.c_str();
|
2012-06-06 19:29:00 +02:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
nv[hdidx++] = 0;
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-06-04 16:48:31 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
for(size_t i = 0; nv[i]; i += 2) {
|
2012-12-09 13:36:02 +01:00
|
|
|
ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\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-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;
|
|
|
|
data_prd.read_callback = spdy_data_read_callback;
|
|
|
|
|
2012-07-16 16:55:08 +02:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
rv = nghttp2_submit_response(session_, downstream->get_stream_id(), nv,
|
2012-07-16 16:55:08 +02:00
|
|
|
&data_prd);
|
2012-11-18 13:23:13 +01:00
|
|
|
delete [] nv;
|
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.
|
2012-06-04 16:48:31 +02:00
|
|
|
int SpdyUpstream::on_downstream_body(Downstream *downstream,
|
|
|
|
const uint8_t *data, size_t len)
|
|
|
|
{
|
|
|
|
evbuffer *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);
|
|
|
|
if(bodylen > SHRPX_SPDY_UPSTREAM_OUTPUT_UPPER_THRES) {
|
|
|
|
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.
|
2012-06-04 16:48:31 +02:00
|
|
|
int SpdyUpstream::on_downstream_body_complete(Downstream *downstream)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-09 18:36:30 +02:00
|
|
|
bool SpdyUpstream::get_flow_control() const
|
|
|
|
{
|
|
|
|
return flow_control_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t SpdyUpstream::get_initial_window_size() const
|
|
|
|
{
|
|
|
|
return initial_window_size_;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
void SpdyUpstream::pause_read(IOCtrlReason reason)
|
|
|
|
{}
|
|
|
|
|
2013-02-08 13:46:58 +01:00
|
|
|
int SpdyUpstream::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()) {
|
|
|
|
if(downstream->get_recv_window_size() >= get_initial_window_size()/2) {
|
|
|
|
window_update(downstream);
|
|
|
|
}
|
|
|
|
}
|
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
|