nghttpx: Rename Spdy{Session,DownstreamConnection} to Http2{*}

This commit is contained in:
Tatsuhiro Tsujikawa 2013-11-04 17:53:57 +09:00
parent a483105e11
commit f3d71aa5bb
18 changed files with 313 additions and 303 deletions

View File

@ -74,8 +74,8 @@ NGHTTPX_SRCS = \
shrpx_downstream.cc shrpx_downstream.h \
shrpx_downstream_connection.cc shrpx_downstream_connection.h \
shrpx_http_downstream_connection.cc shrpx_http_downstream_connection.h \
shrpx_spdy_downstream_connection.cc shrpx_spdy_downstream_connection.h \
shrpx_spdy_session.cc shrpx_spdy_session.h \
shrpx_http2_downstream_connection.cc shrpx_http2_downstream_connection.h \
shrpx_http2_session.cc shrpx_http2_session.h \
shrpx_log.cc shrpx_log.h \
shrpx_http.cc shrpx_http.h \
shrpx_io_control.cc shrpx_io_control.h \

View File

@ -294,7 +294,7 @@ int event_loop()
if(get_config()->num_worker > 1) {
listener_handler->create_worker_thread(get_config()->num_worker);
} else if(get_config()->downstream_proto == PROTO_SPDY) {
listener_handler->create_spdy_session();
listener_handler->create_http2_session();
}
if(LOG_ENABLED(INFO)) {

View File

@ -32,7 +32,7 @@
#include "shrpx_https_upstream.h"
#include "shrpx_config.h"
#include "shrpx_http_downstream_connection.h"
#include "shrpx_spdy_downstream_connection.h"
#include "shrpx_http2_downstream_connection.h"
#include "shrpx_accesslog.h"
#ifdef HAVE_SPDYLAY
#include "shrpx_spdy_upstream.h"
@ -218,7 +218,7 @@ ClientHandler::ClientHandler(bufferevent *bev, int fd, SSL *ssl,
ssl_(ssl),
ipaddr_(ipaddr),
should_close_after_write_(false),
spdy_(nullptr),
http2session_(nullptr),
left_connhd_len_(NGHTTP2_CLIENT_CONNECTION_HEADER_LEN)
{
bufferevent_set_rate_limit(bev_, get_config()->rate_limit_cfg);
@ -376,8 +376,8 @@ DownstreamConnection* ClientHandler::get_downstream_connection()
CLOG(INFO, this) << "Downstream connection pool is empty."
<< " Create new one";
}
if(spdy_) {
return new SpdyDownstreamConnection(this);
if(http2session_) {
return new Http2DownstreamConnection(this);
} else {
return new HttpDownstreamConnection(this);
}
@ -403,14 +403,14 @@ SSL* ClientHandler::get_ssl() const
return ssl_;
}
void ClientHandler::set_spdy_session(SpdySession *spdy)
void ClientHandler::set_http2_session(Http2Session *http2session)
{
spdy_ = spdy;
http2session_ = http2session;
}
SpdySession* ClientHandler::get_spdy_session() const
Http2Session* ClientHandler::get_http2_session() const
{
return spdy_;
return http2session_;
}
size_t ClientHandler::get_left_connhd_len() const

View File

@ -37,7 +37,7 @@ namespace shrpx {
class Upstream;
class DownstreamConnection;
class SpdySession;
class Http2Session;
class HttpsUpstream;
class ClientHandler {
@ -63,8 +63,8 @@ public:
DownstreamConnection* get_downstream_connection();
size_t get_pending_write_length();
SSL* get_ssl() const;
void set_spdy_session(SpdySession *spdy);
SpdySession* get_spdy_session() const;
void set_http2_session(Http2Session *http2session);
Http2Session* get_http2_session() const;
size_t get_left_connhd_len() const;
void set_left_connhd_len(size_t left);
// Call this function when HTTP/2.0 connection header is received at
@ -83,9 +83,9 @@ private:
std::string ipaddr_;
bool should_close_after_write_;
std::set<DownstreamConnection*> dconn_pool_;
// Shared SPDY session for each thread. NULL if backend is not
// Shared HTTP2 session for each thread. NULL if backend is not
// SPDY. Not deleted by this object.
SpdySession *spdy_;
Http2Session *http2session_;
// The number of bytes of HTTP/2.0 client connection header to read
size_t left_connhd_len_;
};

View File

@ -22,7 +22,7 @@
* 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_downstream_connection.h"
#include "shrpx_http2_downstream_connection.h"
#include <unistd.h>
@ -38,7 +38,7 @@
#include "shrpx_config.h"
#include "shrpx_error.h"
#include "shrpx_http.h"
#include "shrpx_spdy_session.h"
#include "shrpx_http2_session.h"
#include "http2.h"
#include "util.h"
@ -46,15 +46,15 @@ using namespace nghttp2;
namespace shrpx {
SpdyDownstreamConnection::SpdyDownstreamConnection
Http2DownstreamConnection::Http2DownstreamConnection
(ClientHandler *client_handler)
: DownstreamConnection(client_handler),
spdy_(client_handler->get_spdy_session()),
http2session_(client_handler->get_http2_session()),
request_body_buf_(0),
sd_(0)
{}
SpdyDownstreamConnection::~SpdyDownstreamConnection()
Http2DownstreamConnection::~Http2DownstreamConnection()
{
if(LOG_ENABLED(INFO)) {
DCLOG(INFO, this) << "Deleting";
@ -64,10 +64,10 @@ SpdyDownstreamConnection::~SpdyDownstreamConnection()
}
if(downstream_) {
if(submit_rst_stream(downstream_) == 0) {
spdy_->notify();
http2session_->notify();
}
}
spdy_->remove_downstream_connection(this);
http2session_->remove_downstream_connection(this);
// Downstream and DownstreamConnection may be deleted
// asynchronously.
if(downstream_) {
@ -78,7 +78,7 @@ SpdyDownstreamConnection::~SpdyDownstreamConnection()
}
}
int SpdyDownstreamConnection::init_request_body_buf()
int Http2DownstreamConnection::init_request_body_buf()
{
int rv;
if(request_body_buf_) {
@ -97,7 +97,7 @@ int SpdyDownstreamConnection::init_request_body_buf()
return 0;
}
int SpdyDownstreamConnection::attach_downstream(Downstream *downstream)
int Http2DownstreamConnection::attach_downstream(Downstream *downstream)
{
if(LOG_ENABLED(INFO)) {
DCLOG(INFO, this) << "Attaching to DOWNSTREAM:" << downstream;
@ -105,22 +105,22 @@ int SpdyDownstreamConnection::attach_downstream(Downstream *downstream)
if(init_request_body_buf() == -1) {
return -1;
}
spdy_->add_downstream_connection(this);
if(spdy_->get_state() == SpdySession::DISCONNECTED) {
spdy_->notify();
http2session_->add_downstream_connection(this);
if(http2session_->get_state() == Http2Session::DISCONNECTED) {
http2session_->notify();
}
downstream->set_downstream_connection(this);
downstream_ = downstream;
return 0;
}
void SpdyDownstreamConnection::detach_downstream(Downstream *downstream)
void Http2DownstreamConnection::detach_downstream(Downstream *downstream)
{
if(LOG_ENABLED(INFO)) {
DCLOG(INFO, this) << "Detaching from DOWNSTREAM:" << downstream;
}
if(submit_rst_stream(downstream) == 0) {
spdy_->notify();
http2session_->notify();
}
downstream->set_downstream_connection(0);
downstream_ = 0;
@ -128,10 +128,10 @@ void SpdyDownstreamConnection::detach_downstream(Downstream *downstream)
client_handler_->pool_downstream_connection(this);
}
int SpdyDownstreamConnection::submit_rst_stream(Downstream *downstream)
int Http2DownstreamConnection::submit_rst_stream(Downstream *downstream)
{
int rv = -1;
if(spdy_->get_state() == SpdySession::CONNECTED &&
if(http2session_->get_state() == Http2Session::CONNECTED &&
downstream->get_downstream_stream_id() != -1) {
switch(downstream->get_response_state()) {
case Downstream::MSG_RESET:
@ -143,36 +143,35 @@ int SpdyDownstreamConnection::submit_rst_stream(Downstream *downstream)
<< downstream << ", stream_id="
<< downstream->get_downstream_stream_id();
}
rv = spdy_->submit_rst_stream(downstream->get_downstream_stream_id(),
NGHTTP2_INTERNAL_ERROR);
rv = http2session_->submit_rst_stream
(downstream->get_downstream_stream_id(),
NGHTTP2_INTERNAL_ERROR);
}
}
return rv;
}
namespace {
ssize_t spdy_data_read_callback(nghttp2_session *session,
int32_t stream_id,
uint8_t *buf, size_t length,
int *eof,
nghttp2_data_source *source,
void *user_data)
ssize_t http2_data_read_callback(nghttp2_session *session,
int32_t stream_id,
uint8_t *buf, size_t length,
int *eof,
nghttp2_data_source *source,
void *user_data)
{
StreamData *sd;
sd = reinterpret_cast<StreamData*>
auto sd = reinterpret_cast<StreamData*>
(nghttp2_session_get_stream_user_data(session, stream_id));
if(!sd || !sd->dconn) {
return NGHTTP2_ERR_DEFERRED;
}
SpdyDownstreamConnection *dconn;
dconn = reinterpret_cast<SpdyDownstreamConnection*>(source->ptr);
Downstream *downstream = dconn->get_downstream();
auto dconn = reinterpret_cast<Http2DownstreamConnection*>(source->ptr);
auto downstream = dconn->get_downstream();
if(!downstream) {
// In this case, RST_STREAM should have been issued. But depending
// on the priority, DATA frame may come first.
return NGHTTP2_ERR_DEFERRED;
}
evbuffer *body = dconn->get_request_body_buf();
auto body = dconn->get_request_body_buf();
int nread = 0;
for(;;) {
nread = evbuffer_remove(body, buf, length);
@ -216,12 +215,13 @@ ssize_t spdy_data_read_callback(nghttp2_session *session,
}
} // namespace
int SpdyDownstreamConnection::push_request_headers()
int Http2DownstreamConnection::push_request_headers()
{
int rv;
if(spdy_->get_state() != SpdySession::CONNECTED) {
// The SPDY session to the backend has not been established. This
// function will be called again just after it is established.
if(http2session_->get_state() != Http2Session::CONNECTED) {
// The HTTP2 session to the backend has not been established.
// This function will be called again just after it is
// established.
return 0;
}
if(!downstream_) {
@ -288,7 +288,7 @@ int SpdyDownstreamConnection::push_request_headers()
}
nv.push_back(":scheme");
if(scheme.empty()) {
// The default scheme is http. For SPDY upstream, the path must
// The default scheme is http. For HTTP2 upstream, the path must
// be absolute URI, so scheme should be provided.
nv.push_back("http");
} else {
@ -391,20 +391,20 @@ int SpdyDownstreamConnection::push_request_headers()
// Request-body is expected.
nghttp2_data_provider data_prd;
data_prd.source.ptr = this;
data_prd.read_callback = spdy_data_read_callback;
rv = spdy_->submit_request(this, 0, nv.data(), &data_prd);
data_prd.read_callback = http2_data_read_callback;
rv = http2session_->submit_request(this, 0, nv.data(), &data_prd);
} else {
rv = spdy_->submit_request(this, 0, nv.data(), nullptr);
rv = http2session_->submit_request(this, 0, nv.data(), nullptr);
}
if(rv != 0) {
DCLOG(FATAL, this) << "nghttp2_submit_request() failed";
return -1;
}
spdy_->notify();
http2session_->notify();
return 0;
}
int SpdyDownstreamConnection::push_upload_data_chunk(const uint8_t *data,
int Http2DownstreamConnection::push_upload_data_chunk(const uint8_t *data,
size_t datalen)
{
int rv = evbuffer_add(request_body_buf_, data, datalen);
@ -413,49 +413,49 @@ int SpdyDownstreamConnection::push_upload_data_chunk(const uint8_t *data,
return -1;
}
if(downstream_->get_downstream_stream_id() != -1) {
rv = spdy_->resume_data(this);
rv = http2session_->resume_data(this);
if(rv != 0) {
return -1;
}
spdy_->notify();
http2session_->notify();
}
return 0;
}
int SpdyDownstreamConnection::end_upload_data()
int Http2DownstreamConnection::end_upload_data()
{
int rv;
if(downstream_->get_downstream_stream_id() != -1) {
rv = spdy_->resume_data(this);
rv = http2session_->resume_data(this);
if(rv != 0) {
return -1;
}
spdy_->notify();
http2session_->notify();
}
return 0;
}
int SpdyDownstreamConnection::resume_read(IOCtrlReason reason)
int Http2DownstreamConnection::resume_read(IOCtrlReason reason)
{
int rv1 = 0, rv2 = 0;
if(spdy_->get_state() == SpdySession::CONNECTED &&
spdy_->get_flow_control()) {
if(http2session_->get_state() == Http2Session::CONNECTED &&
http2session_->get_flow_control()) {
int32_t window_size_increment;
window_size_increment = http2::determine_window_update_transmission
(spdy_->get_session(), 0);
(http2session_->get_session(), 0);
if(window_size_increment != -1) {
rv1 = spdy_->submit_window_update(nullptr, window_size_increment);
rv1 = http2session_->submit_window_update(nullptr, window_size_increment);
if(rv1 == 0) {
spdy_->notify();
http2session_->notify();
}
}
if(downstream_ && downstream_->get_downstream_stream_id() != -1) {
window_size_increment = http2::determine_window_update_transmission
(spdy_->get_session(), downstream_->get_downstream_stream_id());
(http2session_->get_session(), downstream_->get_downstream_stream_id());
if(window_size_increment != -1) {
rv2 = spdy_->submit_window_update(this, window_size_increment);
rv2 = http2session_->submit_window_update(this, window_size_increment);
if(rv2 == 0) {
spdy_->notify();
http2session_->notify();
}
}
}
@ -463,22 +463,22 @@ int SpdyDownstreamConnection::resume_read(IOCtrlReason reason)
return (rv1 == 0 && rv2 == 0) ? 0 : -1;
}
int SpdyDownstreamConnection::on_read()
int Http2DownstreamConnection::on_read()
{
return 0;
}
int SpdyDownstreamConnection::on_write()
int Http2DownstreamConnection::on_write()
{
return 0;
}
evbuffer* SpdyDownstreamConnection::get_request_body_buf() const
evbuffer* Http2DownstreamConnection::get_request_body_buf() const
{
return request_body_buf_;
}
void SpdyDownstreamConnection::attach_stream_data(StreamData *sd)
void Http2DownstreamConnection::attach_stream_data(StreamData *sd)
{
// It is possible sd->dconn is not NULL. sd is detached when
// on_stream_close_callback. Before that, after MSG_COMPLETE is set
@ -490,7 +490,7 @@ void SpdyDownstreamConnection::attach_stream_data(StreamData *sd)
sd_->dconn = this;
}
StreamData* SpdyDownstreamConnection::detach_stream_data()
StreamData* Http2DownstreamConnection::detach_stream_data()
{
if(sd_) {
StreamData *sd = sd_;
@ -502,7 +502,7 @@ StreamData* SpdyDownstreamConnection::detach_stream_data()
}
}
bool SpdyDownstreamConnection::get_output_buffer_full()
bool Http2DownstreamConnection::get_output_buffer_full()
{
if(request_body_buf_) {
return

View File

@ -22,8 +22,8 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SHRPX_SPDY_DOWNSTREAM_CONNECTION_H
#define SHRPX_SPDY_DOWNSTREAM_CONNECTION_H
#ifndef SHRPX_HTTP2_DOWNSTREAM_CONNECTION_H
#define SHRPX_HTTP2_DOWNSTREAM_CONNECTION_H
#include "shrpx.h"
@ -36,12 +36,12 @@
namespace shrpx {
struct StreamData;
class SpdySession;
class Http2Session;
class SpdyDownstreamConnection : public DownstreamConnection {
class Http2DownstreamConnection : public DownstreamConnection {
public:
SpdyDownstreamConnection(ClientHandler *client_handler);
virtual ~SpdyDownstreamConnection();
Http2DownstreamConnection(ClientHandler *client_handler);
virtual ~Http2DownstreamConnection();
virtual int attach_downstream(Downstream *downstream);
virtual void detach_downstream(Downstream *downstream);
@ -70,11 +70,11 @@ public:
int submit_rst_stream(Downstream *downstream);
private:
SpdySession *spdy_;
Http2Session *http2session_;
evbuffer *request_body_buf_;
StreamData *sd_;
};
} // namespace shrpx
#endif // SHRPX_SPDY_DOWNSTREAM_CONNECTION_H
#endif // SHRPX_HTTP2_DOWNSTREAM_CONNECTION_H

View File

@ -22,7 +22,7 @@
* 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_session.h"
#include "shrpx_http2_session.h"
#include <netinet/tcp.h>
#include <unistd.h>
@ -36,7 +36,7 @@
#include "shrpx_downstream.h"
#include "shrpx_config.h"
#include "shrpx_error.h"
#include "shrpx_spdy_downstream_connection.h"
#include "shrpx_http2_downstream_connection.h"
#include "shrpx_client_handler.h"
#include "shrpx_ssl.h"
#include "shrpx_http.h"
@ -48,7 +48,7 @@ using namespace nghttp2;
namespace shrpx {
SpdySession::SpdySession(event_base *evbase, SSL_CTX *ssl_ctx)
Http2Session::Http2Session(event_base *evbase, SSL_CTX *ssl_ctx)
: evbase_(evbase),
ssl_ctx_(ssl_ctx),
ssl_(nullptr),
@ -63,12 +63,12 @@ SpdySession::SpdySession(event_base *evbase, SSL_CTX *ssl_ctx)
settings_timerev_(nullptr)
{}
SpdySession::~SpdySession()
Http2Session::~Http2Session()
{
disconnect();
}
int SpdySession::disconnect()
int Http2Session::disconnect()
{
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, this) << "Disconnecting";
@ -121,12 +121,12 @@ int SpdySession::disconnect()
state_ = DISCONNECTED;
// Delete all client handler associated to Downstream. When deleting
// SpdyDownstreamConnection, it calls this object's
// Http2DownstreamConnection, it calls this object's
// remove_downstream_connection(). The multiple
// SpdyDownstreamConnection objects belong to the same ClientHandler
// Http2DownstreamConnection objects belong to the same ClientHandler
// object. So first dump ClientHandler objects and delete them once
// and for all.
std::vector<SpdyDownstreamConnection*> vec(dconns_.begin(), dconns_.end());
std::vector<Http2DownstreamConnection*> vec(dconns_.begin(), dconns_.end());
std::set<ClientHandler*> handlers;
for(size_t i = 0; i < vec.size(); ++i) {
handlers.insert(vec[i]->get_client_handler());
@ -147,20 +147,21 @@ namespace {
void notify_readcb(bufferevent *bev, void *arg)
{
int rv;
auto spdy = reinterpret_cast<SpdySession*>(arg);
spdy->clear_notify();
switch(spdy->get_state()) {
case SpdySession::DISCONNECTED:
rv = spdy->initiate_connection();
auto http2session = reinterpret_cast<Http2Session*>(arg);
http2session->clear_notify();
switch(http2session->get_state()) {
case Http2Session::DISCONNECTED:
rv = http2session->initiate_connection();
if(rv != 0) {
SSLOG(FATAL, spdy) << "Could not initiate notification connection";
SSLOG(FATAL, http2session)
<< "Could not initiate notification connection";
DIE();
}
break;
case SpdySession::CONNECTED:
rv = spdy->send();
case Http2Session::CONNECTED:
rv = http2session->send();
if(rv != 0) {
spdy->disconnect();
http2session->disconnect();
}
break;
}
@ -170,21 +171,21 @@ void notify_readcb(bufferevent *bev, void *arg)
namespace {
void notify_eventcb(bufferevent *bev, short events, void *arg)
{
auto spdy = reinterpret_cast<SpdySession*>(arg);
auto http2session = reinterpret_cast<Http2Session*>(arg);
// TODO should DIE()?
if(events & BEV_EVENT_EOF) {
SSLOG(ERROR, spdy) << "Notification connection lost: EOF";
SSLOG(ERROR, http2session) << "Notification connection lost: EOF";
}
if(events & BEV_EVENT_TIMEOUT) {
SSLOG(ERROR, spdy) << "Notification connection lost: timeout";
SSLOG(ERROR, http2session) << "Notification connection lost: timeout";
}
if(events & BEV_EVENT_ERROR) {
SSLOG(ERROR, spdy) << "Notification connection lost: network error";
SSLOG(ERROR, http2session) << "Notification connection lost: network error";
}
}
} // namespace
int SpdySession::init_notification()
int Http2Session::init_notification()
{
int rv;
int sockpair[2];
@ -220,10 +221,10 @@ namespace {
void readcb(bufferevent *bev, void *ptr)
{
int rv;
auto spdy = reinterpret_cast<SpdySession*>(ptr);
rv = spdy->on_read();
auto http2session = reinterpret_cast<Http2Session*>(ptr);
rv = http2session->on_read();
if(rv != 0) {
spdy->disconnect();
http2session->disconnect();
}
}
} // namespace
@ -235,10 +236,10 @@ void writecb(bufferevent *bev, void *ptr)
return;
}
int rv;
auto spdy = reinterpret_cast<SpdySession*>(ptr);
rv = spdy->on_write();
auto http2session = reinterpret_cast<Http2Session*>(ptr);
rv = http2session->on_write();
if(rv != 0) {
spdy->disconnect();
http2session->disconnect();
}
}
} // namespace
@ -246,39 +247,39 @@ void writecb(bufferevent *bev, void *ptr)
namespace {
void eventcb(bufferevent *bev, short events, void *ptr)
{
auto spdy = reinterpret_cast<SpdySession*>(ptr);
auto http2session = reinterpret_cast<Http2Session*>(ptr);
if(events & BEV_EVENT_CONNECTED) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Connection established";
SSLOG(INFO, http2session) << "Connection established";
}
spdy->set_state(SpdySession::CONNECTED);
http2session->set_state(Http2Session::CONNECTED);
if((!get_config()->downstream_no_tls &&
!get_config()->insecure && spdy->check_cert() != 0) ||
spdy->on_connect() != 0) {
spdy->disconnect();
!get_config()->insecure && http2session->check_cert() != 0) ||
http2session->on_connect() != 0) {
http2session->disconnect();
return;
}
int fd = bufferevent_getfd(bev);
int val = 1;
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char *>(&val), sizeof(val)) == -1) {
SSLOG(WARNING, spdy) << "Setting option TCP_NODELAY failed: errno="
<< errno;
SSLOG(WARNING, http2session)
<< "Setting option TCP_NODELAY failed: errno=" << errno;
}
} else if(events & BEV_EVENT_EOF) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "EOF";
SSLOG(INFO, http2session) << "EOF";
}
spdy->disconnect();
http2session->disconnect();
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
if(LOG_ENABLED(INFO)) {
if(events & BEV_EVENT_ERROR) {
SSLOG(INFO, spdy) << "Network error";
SSLOG(INFO, http2session) << "Network error";
} else {
SSLOG(INFO, spdy) << "Timeout";
SSLOG(INFO, http2session) << "Timeout";
}
}
spdy->disconnect();
http2session->disconnect();
}
}
} // namespace
@ -286,24 +287,24 @@ void eventcb(bufferevent *bev, short events, void *ptr)
namespace {
void proxy_readcb(bufferevent *bev, void *ptr)
{
auto spdy = reinterpret_cast<SpdySession*>(ptr);
if(spdy->on_read_proxy() == 0) {
switch(spdy->get_state()) {
case SpdySession::PROXY_CONNECTED:
auto http2session = reinterpret_cast<Http2Session*>(ptr);
if(http2session->on_read_proxy() == 0) {
switch(http2session->get_state()) {
case Http2Session::PROXY_CONNECTED:
// The current bufferevent is no longer necessary, so delete it
// here. But we keep fd_ inside it.
spdy->unwrap_free_bev();
http2session->unwrap_free_bev();
// Initiate SSL/TLS handshake through established tunnel.
if(spdy->initiate_connection() != 0) {
spdy->disconnect();
if(http2session->initiate_connection() != 0) {
http2session->disconnect();
}
break;
case SpdySession::PROXY_FAILED:
spdy->disconnect();
case Http2Session::PROXY_FAILED:
http2session->disconnect();
break;
}
} else {
spdy->disconnect();
http2session->disconnect();
}
}
} // namespace
@ -311,10 +312,10 @@ void proxy_readcb(bufferevent *bev, void *ptr)
namespace {
void proxy_eventcb(bufferevent *bev, short events, void *ptr)
{
auto spdy = reinterpret_cast<SpdySession*>(ptr);
auto http2session = reinterpret_cast<Http2Session*>(ptr);
if(events & BEV_EVENT_CONNECTED) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Connected to the proxy";
SSLOG(INFO, http2session) << "Connected to the proxy";
}
std::string req = "CONNECT ";
req += get_config()->downstream_hostport;
@ -330,36 +331,36 @@ void proxy_eventcb(bufferevent *bev, short events, void *ptr)
}
req += "\r\n";
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "HTTP proxy request headers\n" << req;
SSLOG(INFO, http2session) << "HTTP proxy request headers\n" << req;
}
if(bufferevent_write(bev, req.c_str(), req.size()) != 0) {
SSLOG(ERROR, spdy) << "bufferevent_write() failed";
spdy->disconnect();
SSLOG(ERROR, http2session) << "bufferevent_write() failed";
http2session->disconnect();
}
} else if(events & BEV_EVENT_EOF) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Proxy EOF";
SSLOG(INFO, http2session) << "Proxy EOF";
}
spdy->disconnect();
http2session->disconnect();
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
if(LOG_ENABLED(INFO)) {
if(events & BEV_EVENT_ERROR) {
SSLOG(INFO, spdy) << "Network error";
SSLOG(INFO, http2session) << "Network error";
} else {
SSLOG(INFO, spdy) << "Timeout";
SSLOG(INFO, http2session) << "Timeout";
}
}
spdy->disconnect();
http2session->disconnect();
}
}
} // namespace
int SpdySession::check_cert()
int Http2Session::check_cert()
{
return ssl::check_cert(ssl_);
}
int SpdySession::initiate_connection()
int Http2Session::initiate_connection()
{
int rv = 0;
if(get_config()->downstream_http_proxy_host && state_ == DISCONNECTED) {
@ -475,7 +476,7 @@ int SpdySession::initiate_connection()
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WARTER_MARK);
bufferevent_enable(bev_, EV_READ);
bufferevent_setcb(bev_, readcb, writecb, eventcb, this);
// Set timeout for SPDY session
// Set timeout for HTTP2 session
bufferevent_set_timeouts(bev_, &get_config()->downstream_read_timeout,
&get_config()->downstream_write_timeout);
@ -490,7 +491,7 @@ int SpdySession::initiate_connection()
return 0;
}
void SpdySession::unwrap_free_bev()
void Http2Session::unwrap_free_bev()
{
assert(fd_ == -1);
fd_ = bufferevent_getfd(bev_);
@ -501,16 +502,16 @@ void SpdySession::unwrap_free_bev()
namespace {
int htp_hdrs_completecb(http_parser *htp)
{
auto spdy = reinterpret_cast<SpdySession*>(htp->data);
auto http2session = reinterpret_cast<Http2Session*>(htp->data);
// We just check status code here
if(htp->status_code == 200) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Tunneling success";
SSLOG(INFO, http2session) << "Tunneling success";
}
spdy->set_state(SpdySession::PROXY_CONNECTED);
http2session->set_state(Http2Session::PROXY_CONNECTED);
} else {
SSLOG(WARNING, spdy) << "Tunneling failed";
spdy->set_state(SpdySession::PROXY_FAILED);
SSLOG(WARNING, http2session) << "Tunneling failed";
http2session->set_state(Http2Session::PROXY_FAILED);
}
return 0;
}
@ -529,7 +530,7 @@ http_parser_settings htp_hooks = {
};
} // namespace
int SpdySession::on_read_proxy()
int Http2Session::on_read_proxy()
{
auto input = bufferevent_get_input(bev_);
auto mem = evbuffer_pullup(input, -1);
@ -547,18 +548,19 @@ int SpdySession::on_read_proxy()
}
}
void SpdySession::add_downstream_connection(SpdyDownstreamConnection *dconn)
void Http2Session::add_downstream_connection(Http2DownstreamConnection *dconn)
{
dconns_.insert(dconn);
}
void SpdySession::remove_downstream_connection(SpdyDownstreamConnection *dconn)
void Http2Session::remove_downstream_connection
(Http2DownstreamConnection *dconn)
{
dconns_.erase(dconn);
dconn->detach_stream_data();
}
void SpdySession::remove_stream_data(StreamData *sd)
void Http2Session::remove_stream_data(StreamData *sd)
{
streams_.erase(sd);
if(sd->dconn) {
@ -567,7 +569,7 @@ void SpdySession::remove_stream_data(StreamData *sd)
delete sd;
}
int SpdySession::submit_request(SpdyDownstreamConnection *dconn,
int Http2Session::submit_request(Http2DownstreamConnection *dconn,
uint8_t pri, const char **nv,
const nghttp2_data_provider *data_prd)
{
@ -585,7 +587,7 @@ int SpdySession::submit_request(SpdyDownstreamConnection *dconn,
return 0;
}
int SpdySession::submit_rst_stream(int32_t stream_id,
int Http2Session::submit_rst_stream(int32_t stream_id,
nghttp2_error_code error_code)
{
assert(state_ == CONNECTED);
@ -605,7 +607,7 @@ int SpdySession::submit_rst_stream(int32_t stream_id,
return 0;
}
int SpdySession::submit_window_update(SpdyDownstreamConnection *dconn,
int Http2Session::submit_window_update(Http2DownstreamConnection *dconn,
int32_t amount)
{
assert(state_ == CONNECTED);
@ -626,22 +628,22 @@ int SpdySession::submit_window_update(SpdyDownstreamConnection *dconn,
return 0;
}
int32_t SpdySession::get_initial_window_size() const
int32_t Http2Session::get_initial_window_size() const
{
return (1 << get_config()->spdy_downstream_window_bits) - 1;
}
nghttp2_session* SpdySession::get_session() const
nghttp2_session* Http2Session::get_session() const
{
return session_;
}
bool SpdySession::get_flow_control() const
bool Http2Session::get_flow_control() const
{
return flow_control_;
}
int SpdySession::resume_data(SpdyDownstreamConnection *dconn)
int Http2Session::resume_data(Http2DownstreamConnection *dconn)
{
assert(state_ == CONNECTED);
auto downstream = dconn->get_downstream();
@ -659,12 +661,12 @@ int SpdySession::resume_data(SpdyDownstreamConnection *dconn)
}
namespace {
void call_downstream_readcb(SpdySession *spdy, Downstream *downstream)
void call_downstream_readcb(Http2Session *http2session, Downstream *downstream)
{
auto upstream = downstream->get_upstream();
if(upstream) {
(upstream->get_downstream_readcb())
(spdy->get_bev(),
(http2session->get_bev(),
downstream->get_downstream_connection());
}
}
@ -676,8 +678,8 @@ ssize_t send_callback(nghttp2_session *session,
void *user_data)
{
int rv;
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto bev = spdy->get_bev();
auto http2session = reinterpret_cast<Http2Session*>(user_data);
auto bev = http2session->get_bev();
auto output = bufferevent_get_output(bev);
// Check buffer length and return WOULDBLOCK if it is large enough.
if(evbuffer_get_length(output) > Downstream::OUTPUT_UPPER_THRES) {
@ -686,7 +688,7 @@ ssize_t send_callback(nghttp2_session *session,
rv = evbuffer_add(output, data, len);
if(rv == -1) {
SSLOG(FATAL, spdy) << "evbuffer_add() failed";
SSLOG(FATAL, http2session) << "evbuffer_add() failed";
return NGHTTP2_ERR_CALLBACK_FAILURE;
} else {
return len;
@ -698,8 +700,8 @@ namespace {
ssize_t recv_callback(nghttp2_session *session,
uint8_t *data, size_t len, int flags, void *user_data)
{
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto bev = spdy->get_bev();
auto http2session = reinterpret_cast<Http2Session*>(user_data);
auto bev = http2session->get_bev();
auto input = bufferevent_get_input(bev);
int nread = evbuffer_remove(input, data, len);
if(nread == -1) {
@ -718,10 +720,10 @@ int on_stream_close_callback
void *user_data)
{
int rv;
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Stream stream_id=" << stream_id
<< " is being closed";
SSLOG(INFO, http2session) << "Stream stream_id=" << stream_id
<< " is being closed";
}
auto sd = reinterpret_cast<StreamData*>
(nghttp2_session_get_stream_user_data(session, stream_id));
@ -744,12 +746,12 @@ int on_stream_close_callback
} else {
downstream->set_response_state(Downstream::MSG_RESET);
}
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
// dconn may be deleted
}
}
// The life time of StreamData ends here
spdy->remove_stream_data(sd);
http2session->remove_stream_data(sd);
return 0;
}
} // namespace
@ -757,19 +759,19 @@ int on_stream_close_callback
namespace {
void settings_timeout_cb(evutil_socket_t fd, short what, void *arg)
{
auto spdy = reinterpret_cast<SpdySession*>(arg);
SSLOG(INFO, spdy) << "SETTINGS timeout";
if(spdy->fail_session(NGHTTP2_SETTINGS_TIMEOUT) != 0) {
spdy->disconnect();
auto http2session = reinterpret_cast<Http2Session*>(arg);
SSLOG(INFO, http2session) << "SETTINGS timeout";
if(http2session->fail_session(NGHTTP2_SETTINGS_TIMEOUT) != 0) {
http2session->disconnect();
return;
}
if(spdy->send() != 0) {
spdy->disconnect();
if(http2session->send() != 0) {
http2session->disconnect();
}
}
} // namespace
int SpdySession::start_settings_timer()
int Http2Session::start_settings_timer()
{
int rv;
// We submit SETTINGS only once
@ -789,7 +791,7 @@ int SpdySession::start_settings_timer()
return 0;
}
void SpdySession::stop_settings_timer()
void Http2Session::stop_settings_timer()
{
if(settings_timerev_ == nullptr) {
return;
@ -803,7 +805,7 @@ int on_frame_recv_callback
(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
{
int rv;
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
switch(frame->hd.type) {
case NGHTTP2_HEADERS: {
if(frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
@ -812,13 +814,15 @@ int on_frame_recv_callback
auto sd = reinterpret_cast<StreamData*>
(nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
if(!sd || !sd->dconn) {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_INTERNAL_ERROR);
http2session->submit_rst_stream(frame->hd.stream_id,
NGHTTP2_INTERNAL_ERROR);
break;
}
auto downstream = sd->dconn->get_downstream();
if(!downstream ||
downstream->get_downstream_stream_id() != frame->hd.stream_id) {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_INTERNAL_ERROR);
http2session->submit_rst_stream(frame->hd.stream_id,
NGHTTP2_INTERNAL_ERROR);
break;
}
auto nva = frame->headers.nva;
@ -826,9 +830,10 @@ int on_frame_recv_callback
// Assuming that nva is sorted by name.
if(!http2::check_http2_headers(nva, nvlen)) {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
http2session->submit_rst_stream(frame->hd.stream_id,
NGHTTP2_PROTOCOL_ERROR);
downstream->set_response_state(Downstream::MSG_RESET);
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
return 0;
}
@ -841,9 +846,10 @@ int on_frame_recv_callback
auto status = http2::get_unique_header(nva, nvlen, ":status");
if(!status || http2::value_lws(status)) {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
http2session->submit_rst_stream(frame->hd.stream_id,
NGHTTP2_PROTOCOL_ERROR);
downstream->set_response_state(Downstream::MSG_RESET);
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
return 0;
}
downstream->set_response_http_status
@ -869,7 +875,7 @@ int on_frame_recv_callback
downstream->set_response_connection_close(true);
} else {
// Otherwise, use chunked encoding to keep upstream
// connection open. In SPDY, we are supporsed not to
// connection open. In HTTP2, we are supporsed not to
// receive transfer-encoding.
downstream->add_response_header("transfer-encoding", "chunked");
}
@ -885,9 +891,9 @@ int on_frame_recv_callback
ss.write(reinterpret_cast<char*>(nva[i].value), nva[i].valuelen);
ss << "\n";
}
SSLOG(INFO, spdy) << "HTTP response headers. stream_id="
<< frame->hd.stream_id
<< "\n" << ss.str();
SSLOG(INFO, http2session) << "HTTP response headers. stream_id="
<< frame->hd.stream_id
<< "\n" << ss.str();
}
auto upstream = downstream->get_upstream();
@ -903,8 +909,8 @@ int on_frame_recv_callback
}
downstream->set_request_state(Downstream::HEADER_COMPLETE);
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "HTTP upgrade success. stream_id="
<< frame->hd.stream_id;
SSLOG(INFO, http2session) << "HTTP upgrade success. stream_id="
<< frame->hd.stream_id;
}
} else if(downstream->get_request_method() == "CONNECT") {
// If request is CONNECT, terminate request body to avoid for
@ -913,10 +919,11 @@ int on_frame_recv_callback
}
rv = upstream->on_downstream_header_complete(downstream);
if(rv != 0) {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
http2session->submit_rst_stream(frame->hd.stream_id,
NGHTTP2_PROTOCOL_ERROR);
downstream->set_response_state(Downstream::MSG_RESET);
}
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
break;
}
case NGHTTP2_RST_STREAM: {
@ -932,9 +939,9 @@ int on_frame_recv_callback
// upstream *after* whole response body is sent. We just set
// MSG_COMPLETE here. Upstream will take care of that.
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "RST_STREAM against tunneled stream "
<< "stream_id="
<< frame->hd.stream_id;
SSLOG(INFO, http2session) << "RST_STREAM against tunneled stream "
<< "stream_id="
<< frame->hd.stream_id;
}
downstream->get_upstream()->on_downstream_body_complete(downstream);
downstream->set_response_state(Downstream::MSG_COMPLETE);
@ -945,7 +952,7 @@ int on_frame_recv_callback
}
downstream->set_response_rst_stream_error_code
(frame->rst_stream.error_code);
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
}
}
break;
@ -954,15 +961,17 @@ int on_frame_recv_callback
if((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
break;
}
spdy->stop_settings_timer();
http2session->stop_settings_timer();
break;
case NGHTTP2_PUSH_PROMISE:
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Received downstream PUSH_PROMISE stream_id="
<< frame->hd.stream_id;
SSLOG(INFO, http2session)
<< "Received downstream PUSH_PROMISE stream_id="
<< frame->hd.stream_id;
}
// We just respond with RST_STREAM.
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_REFUSED_STREAM);
http2session->submit_rst_stream(frame->hd.stream_id,
NGHTTP2_REFUSED_STREAM);
break;
default:
break;
@ -978,26 +987,26 @@ int on_data_chunk_recv_callback(nghttp2_session *session,
void *user_data)
{
int rv;
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
auto sd = reinterpret_cast<StreamData*>
(nghttp2_session_get_stream_user_data(session, stream_id));
if(!sd || !sd->dconn) {
spdy->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
http2session->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
return 0;
}
auto downstream = sd->dconn->get_downstream();
if(!downstream || downstream->get_downstream_stream_id() != stream_id) {
spdy->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
http2session->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
return 0;
}
auto upstream = downstream->get_upstream();
rv = upstream->on_downstream_body(downstream, data, len);
if(rv != 0) {
spdy->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
http2session->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
downstream->set_response_state(Downstream::MSG_RESET);
}
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
return 0;
}
} // namespace
@ -1007,20 +1016,20 @@ int before_frame_send_callback(nghttp2_session *session,
const nghttp2_frame *frame,
void *user_data)
{
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
if(frame->hd.type == NGHTTP2_HEADERS &&
frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
auto sd = reinterpret_cast<StreamData*>
(nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
if(!sd || !sd->dconn) {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_CANCEL);
http2session->submit_rst_stream(frame->hd.stream_id, NGHTTP2_CANCEL);
return 0;
}
auto downstream = sd->dconn->get_downstream();
if(downstream) {
downstream->set_downstream_stream_id(frame->hd.stream_id);
} else {
spdy->submit_rst_stream(frame->hd.stream_id, NGHTTP2_CANCEL);
http2session->submit_rst_stream(frame->hd.stream_id, NGHTTP2_CANCEL);
}
}
return 0;
@ -1031,10 +1040,10 @@ namespace {
int on_frame_send_callback(nghttp2_session* session,
const nghttp2_frame *frame, void *user_data)
{
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
if(frame->hd.type == NGHTTP2_SETTINGS &&
(frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
if(spdy->start_settings_timer() != 0) {
if(http2session->start_settings_timer() != 0) {
return NGHTTP2_ERR_CALLBACK_FAILURE;
}
}
@ -1047,11 +1056,11 @@ int on_frame_not_send_callback(nghttp2_session *session,
const nghttp2_frame *frame,
int lib_error_code, void *user_data)
{
auto spdy = reinterpret_cast<SpdySession*>(user_data);
SSLOG(WARNING, spdy) << "Failed to send control frame type="
<< frame->hd.type << ", "
<< "lib_error_code=" << lib_error_code << ":"
<< nghttp2_strerror(lib_error_code);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
SSLOG(WARNING, http2session) << "Failed to send control frame type="
<< frame->hd.type << ", "
<< "lib_error_code=" << lib_error_code << ":"
<< nghttp2_strerror(lib_error_code);
if(frame->hd.type == NGHTTP2_HEADERS &&
frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
// To avoid stream hanging around, flag Downstream::MSG_RESET and
@ -1068,9 +1077,9 @@ int on_frame_not_send_callback(nghttp2_session *session,
return 0;
}
downstream->set_response_state(Downstream::MSG_RESET);
call_downstream_readcb(spdy, downstream);
call_downstream_readcb(http2session, downstream);
}
spdy->remove_stream_data(sd);
http2session->remove_stream_data(sd);
}
return 0;
}
@ -1084,12 +1093,13 @@ int on_frame_recv_parse_error_callback(nghttp2_session *session,
size_t payloadlen, int lib_error_code,
void *user_data)
{
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Failed to parse received control frame. type="
<< type
<< ", lib_error_code=" << lib_error_code << ":"
<< nghttp2_strerror(lib_error_code);
SSLOG(INFO, http2session)
<< "Failed to parse received control frame. type="
<< type
<< ", lib_error_code=" << lib_error_code << ":"
<< nghttp2_strerror(lib_error_code);
}
return 0;
}
@ -1101,15 +1111,15 @@ int on_unknown_frame_recv_callback(nghttp2_session *session,
const uint8_t *payload, size_t payloadlen,
void *user_data)
{
auto spdy = reinterpret_cast<SpdySession*>(user_data);
auto http2session = reinterpret_cast<Http2Session*>(user_data);
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, spdy) << "Received unknown control frame";
SSLOG(INFO, http2session) << "Received unknown control frame";
}
return 0;
}
} // namespace
int SpdySession::on_connect()
int Http2Session::on_connect()
{
int rv;
const unsigned char *next_proto = nullptr;
@ -1186,7 +1196,7 @@ int SpdySession::on_connect()
return 0;
}
int SpdySession::on_read()
int Http2Session::on_read()
{
int rv = 0;
if((rv = nghttp2_session_recv(session_)) < 0) {
@ -1211,12 +1221,12 @@ int SpdySession::on_read()
return rv;
}
int SpdySession::on_write()
int Http2Session::on_write()
{
return send();
}
int SpdySession::send()
int Http2Session::send()
{
int rv = 0;
if((rv = nghttp2_session_send(session_)) < 0) {
@ -1236,14 +1246,14 @@ int SpdySession::send()
return rv;
}
void SpdySession::clear_notify()
void Http2Session::clear_notify()
{
auto input = bufferevent_get_output(rdbev_);
evbuffer_drain(input, evbuffer_get_length(input));
notified_ = false;
}
void SpdySession::notify()
void Http2Session::notify()
{
if(!notified_) {
bufferevent_write(wrbev_, "1", 1);
@ -1251,22 +1261,22 @@ void SpdySession::notify()
}
}
bufferevent* SpdySession::get_bev() const
bufferevent* Http2Session::get_bev() const
{
return bev_;
}
int SpdySession::get_state() const
int Http2Session::get_state() const
{
return state_;
}
void SpdySession::set_state(int state)
void Http2Session::set_state(int state)
{
state_ = state;
}
int SpdySession::fail_session(nghttp2_error_code error_code)
int Http2Session::fail_session(nghttp2_error_code error_code)
{
int rv;
rv = nghttp2_session_fail_session(session_, error_code);

View File

@ -22,8 +22,8 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SHRPX_SPDY_SESSION_H
#define SHRPX_SPDY_SESSION_H
#ifndef SHRPX_HTTP2_SESSION_H
#define SHRPX_HTTP2_SESSION_H
#include "shrpx.h"
@ -41,16 +41,16 @@
namespace shrpx {
class SpdyDownstreamConnection;
class Http2DownstreamConnection;
struct StreamData {
SpdyDownstreamConnection *dconn;
Http2DownstreamConnection *dconn;
};
class SpdySession {
class Http2Session {
public:
SpdySession(event_base *evbase, SSL_CTX *ssl_ctx);
~SpdySession();
Http2Session(event_base *evbase, SSL_CTX *ssl_ctx);
~Http2Session();
int init_notification();
@ -59,12 +59,12 @@ public:
int disconnect();
int initiate_connection();
void add_downstream_connection(SpdyDownstreamConnection *dconn);
void remove_downstream_connection(SpdyDownstreamConnection *dconn);
void add_downstream_connection(Http2DownstreamConnection *dconn);
void remove_downstream_connection(Http2DownstreamConnection *dconn);
void remove_stream_data(StreamData *sd);
int submit_request(SpdyDownstreamConnection *dconn,
int submit_request(Http2DownstreamConnection *dconn,
uint8_t pri, const char **nv,
const nghttp2_data_provider *data_prd);
@ -72,7 +72,7 @@ public:
// To send WINDOW_UPDATE for a connection, specify nullptr to
// |dconn|.
int submit_window_update(SpdyDownstreamConnection *dconn, int32_t amount);
int submit_window_update(Http2DownstreamConnection *dconn, int32_t amount);
int fail_session(nghttp2_error_code error_code);
@ -82,7 +82,7 @@ public:
bool get_flow_control() const;
int resume_data(SpdyDownstreamConnection *dconn);
int resume_data(Http2DownstreamConnection *dconn);
int on_connect();
@ -130,7 +130,7 @@ private:
int fd_;
nghttp2_session *session_;
bufferevent *bev_;
std::set<SpdyDownstreamConnection*> dconns_;
std::set<Http2DownstreamConnection*> dconns_;
std::set<StreamData*> streams_;
int state_;
bool notified_;
@ -144,4 +144,4 @@ private:
} // namespace shrpx
#endif // SHRPX_SPDY_SESSION_H
#endif // SHRPX_HTTP2_SESSION_H

View File

@ -467,7 +467,6 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
session_(nullptr),
settings_timerev_(nullptr)
{
//handler->set_bev_cb(spdy_readcb, 0, spdy_eventcb);
handler->set_upstream_timeouts(&get_config()->spdy_upstream_read_timeout,
&get_config()->upstream_write_timeout);
@ -585,7 +584,7 @@ ClientHandler* Http2Upstream::get_client_handler() const
}
namespace {
void spdy_downstream_readcb(bufferevent *bev, void *ptr)
void downstream_readcb(bufferevent *bev, void *ptr)
{
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
auto downstream = dconn->get_downstream();
@ -641,7 +640,7 @@ void spdy_downstream_readcb(bufferevent *bev, void *ptr)
} // namespace
namespace {
void spdy_downstream_writecb(bufferevent *bev, void *ptr)
void downstream_writecb(bufferevent *bev, void *ptr)
{
if(evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
return;
@ -654,7 +653,7 @@ void spdy_downstream_writecb(bufferevent *bev, void *ptr)
} // namespace
namespace {
void spdy_downstream_eventcb(bufferevent *bev, short events, void *ptr)
void downstream_eventcb(bufferevent *bev, short events, void *ptr)
{
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
auto downstream = dconn->get_downstream();
@ -695,9 +694,9 @@ void spdy_downstream_eventcb(bufferevent *bev, short events, void *ptr)
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.
// downstream_data_read_callback to send RST_STREAM after
// pending response body is sent. This is needed to ensure
// that RST_STREAM is sent after all pending data are sent.
upstream->on_downstream_body_complete(downstream);
} else if(downstream->get_response_state() != Downstream::MSG_COMPLETE) {
// If stream was not closed, then we set MSG_COMPLETE and let
@ -817,12 +816,12 @@ int Http2Upstream::fail_session(nghttp2_error_code error_code)
}
namespace {
ssize_t spdy_data_read_callback(nghttp2_session *session,
int32_t stream_id,
uint8_t *buf, size_t length,
int *eof,
nghttp2_data_source *source,
void *user_data)
ssize_t downstream_data_read_callback(nghttp2_session *session,
int32_t stream_id,
uint8_t *buf, size_t length,
int *eof,
nghttp2_data_source *source,
void *user_data)
{
auto downstream = reinterpret_cast<Downstream*>(source->ptr);
auto body = downstream->get_response_body_buf();
@ -867,7 +866,7 @@ int Http2Upstream::error_reply(Downstream *downstream,
nghttp2_data_provider data_prd;
data_prd.source.ptr = downstream;
data_prd.read_callback = spdy_data_read_callback;
data_prd.read_callback = downstream_data_read_callback;
auto content_length = util::utos(html.size());
auto status_code_str = util::utos(status_code);
@ -895,17 +894,17 @@ int Http2Upstream::error_reply(Downstream *downstream,
bufferevent_data_cb Http2Upstream::get_downstream_readcb()
{
return spdy_downstream_readcb;
return downstream_readcb;
}
bufferevent_data_cb Http2Upstream::get_downstream_writecb()
{
return spdy_downstream_writecb;
return downstream_writecb;
}
bufferevent_event_cb Http2Upstream::get_downstream_eventcb()
{
return spdy_downstream_eventcb;
return downstream_eventcb;
}
void Http2Upstream::add_downstream(Downstream *downstream)
@ -923,7 +922,7 @@ Downstream* Http2Upstream::find_downstream(int32_t stream_id)
return downstream_queue_.find(stream_id);
}
nghttp2_session* Http2Upstream::get_spdy_session()
nghttp2_session* Http2Upstream::get_http2_session()
{
return session_;
}
@ -975,7 +974,7 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream)
}
nghttp2_data_provider data_prd;
data_prd.source.ptr = downstream;
data_prd.read_callback = spdy_data_read_callback;
data_prd.read_callback = downstream_data_read_callback;
int rv;
rv = nghttp2_submit_response(session_, downstream->get_stream_id(),

View File

@ -55,7 +55,7 @@ public:
void remove_downstream(Downstream *downstream);
Downstream* find_downstream(int32_t stream_id);
nghttp2_session* get_spdy_session();
nghttp2_session* get_http2_session();
int rst_stream(Downstream *downstream, nghttp2_error_code error_code);
// To send WINDOW_UPDATE for a connection, specify nullptr to

View File

@ -31,7 +31,7 @@
#include "shrpx_client_handler.h"
#include "shrpx_downstream.h"
#include "shrpx_downstream_connection.h"
#include "shrpx_spdy_downstream_connection.h"
#include "shrpx_http2_downstream_connection.h"
#include "shrpx_http.h"
#include "shrpx_config.h"
#include "shrpx_error.h"

View File

@ -37,7 +37,7 @@
#include "shrpx_ssl.h"
#include "shrpx_worker.h"
#include "shrpx_config.h"
#include "shrpx_spdy_session.h"
#include "shrpx_http2_session.h"
namespace shrpx {
@ -49,7 +49,7 @@ ListenHandler::ListenHandler(event_base *evbase, SSL_CTX *sv_ssl_ctx,
worker_round_robin_cnt_(0),
workers_(nullptr),
num_worker_(0),
spdy_(nullptr)
http2session_(nullptr)
{}
ListenHandler::~ListenHandler()
@ -110,7 +110,7 @@ int ListenHandler::accept_connection(evutil_socket_t fd,
LLOG(ERROR, this) << "ClientHandler creation failed";
return 0;
}
client->set_spdy_session(spdy_);
client->set_http2_session(http2session_);
} else {
size_t idx = worker_round_robin_cnt_ % num_worker_;
++worker_round_robin_cnt_;
@ -133,11 +133,11 @@ event_base* ListenHandler::get_evbase() const
return evbase_;
}
int ListenHandler::create_spdy_session()
int ListenHandler::create_http2_session()
{
int rv;
spdy_ = new SpdySession(evbase_, cl_ssl_ctx_);
rv = spdy_->init_notification();
http2session_ = new Http2Session(evbase_, cl_ssl_ctx_);
rv = http2session_->init_notification();
return rv;
}

View File

@ -43,7 +43,7 @@ struct WorkerInfo {
bufferevent *bev;
};
class SpdySession;
class Http2Session;
class ListenHandler {
public:
@ -52,7 +52,7 @@ public:
int accept_connection(evutil_socket_t fd, sockaddr *addr, int addrlen);
void create_worker_thread(size_t num);
event_base* get_evbase() const;
int create_spdy_session();
int create_http2_session();
private:
event_base *evbase_;
// The frontend server SSL_CTX
@ -62,9 +62,9 @@ private:
unsigned int worker_round_robin_cnt_;
WorkerInfo *workers_;
size_t num_worker_;
// Shared backend SPDY session. NULL if multi-threaded. In
// Shared backend HTTP2 session. NULL if multi-threaded. In
// multi-threaded case, see shrpx_worker.cc.
SpdySession *spdy_;
Http2Session *http2session_;
};
} // namespace shrpx

View File

@ -800,7 +800,7 @@ Downstream* SpdyUpstream::find_downstream(int32_t stream_id)
return downstream_queue_.find(stream_id);
}
spdylay_session* SpdyUpstream::get_spdy_session()
spdylay_session* SpdyUpstream::get_http2_session()
{
return session_;
}

View File

@ -52,7 +52,7 @@ public:
void remove_downstream(Downstream *downstream);
Downstream* find_downstream(int32_t stream_id);
spdylay_session* get_spdy_session();
spdylay_session* get_http2_session();
int rst_stream(Downstream *downstream, int status_code);
int window_update(Downstream *downstream);

View File

@ -29,13 +29,14 @@
#include "shrpx_ssl.h"
#include "shrpx_log.h"
#include "shrpx_client_handler.h"
#include "shrpx_spdy_session.h"
#include "shrpx_http2_session.h"
namespace shrpx {
ThreadEventReceiver::ThreadEventReceiver(SSL_CTX *ssl_ctx, SpdySession *spdy)
ThreadEventReceiver::ThreadEventReceiver(SSL_CTX *ssl_ctx,
Http2Session *http2session)
: ssl_ctx_(ssl_ctx),
spdy_(spdy)
http2session_(http2session)
{}
ThreadEventReceiver::~ThreadEventReceiver()
@ -63,7 +64,7 @@ void ThreadEventReceiver::on_read(bufferevent *bev)
&wev.client_addr.sa,
wev.client_addrlen);
if(client_handler) {
client_handler->set_spdy_session(spdy_);
client_handler->set_http2_session(http2session_);
if(LOG_ENABLED(INFO)) {
TLOG(INFO, this) << "CLIENT_HANDLER:" << client_handler << " created";
}

View File

@ -35,7 +35,7 @@
namespace shrpx {
class SpdySession;
class Http2Session;
struct WorkerEvent {
evutil_socket_t client_fd;
@ -45,14 +45,14 @@ struct WorkerEvent {
class ThreadEventReceiver {
public:
ThreadEventReceiver(SSL_CTX *ssl_ctx, SpdySession *spdy);
ThreadEventReceiver(SSL_CTX *ssl_ctx, Http2Session *http2session);
~ThreadEventReceiver();
void on_read(bufferevent *bev);
private:
SSL_CTX *ssl_ctx_;
// Shared SPDY session for each thread. NULL if not client mode. Not
// deleted by this object.
SpdySession *spdy_;
// Shared HTTP2 session for each thread. NULL if not client
// mode. Not deleted by this object.
Http2Session *http2session_;
};
} // namespace shrpx

View File

@ -35,7 +35,7 @@
#include "shrpx_ssl.h"
#include "shrpx_thread_event_receiver.h"
#include "shrpx_log.h"
#include "shrpx_spdy_session.h"
#include "shrpx_http2_session.h"
#include "util.h"
using namespace nghttp2;
@ -89,15 +89,15 @@ void Worker::run()
LOG(ERROR) << "bufferevent_socket_new() failed";
return;
}
std::unique_ptr<SpdySession> spdy;
std::unique_ptr<Http2Session> http2session;
if(get_config()->downstream_proto == PROTO_SPDY) {
spdy = util::make_unique<SpdySession>(evbase.get(), cl_ssl_ctx_);
if(spdy->init_notification() == -1) {
http2session = util::make_unique<Http2Session>(evbase.get(), cl_ssl_ctx_);
if(http2session->init_notification() == -1) {
DIE();
}
}
auto receiver = util::make_unique<ThreadEventReceiver>(sv_ssl_ctx_,
spdy.get());
http2session.get());
bufferevent_enable(bev.get(), EV_READ);
bufferevent_setcb(bev.get(), readcb, nullptr, eventcb, receiver.get());