This commit is contained in:
Tatsuhiro Tsujikawa 2013-07-26 19:38:54 +09:00
parent 18f450fd2a
commit 41b21f7938
4 changed files with 48 additions and 48 deletions

View File

@ -75,7 +75,7 @@ SHRPX_SRCS = \
shrpx_listen_handler.cc shrpx_listen_handler.h \ shrpx_listen_handler.cc shrpx_listen_handler.h \
shrpx_client_handler.cc shrpx_client_handler.h \ shrpx_client_handler.cc shrpx_client_handler.h \
shrpx_upstream.h \ shrpx_upstream.h \
shrpx_spdy_upstream.cc shrpx_spdy_upstream.h \ shrpx_http2_upstream.cc shrpx_http2_upstream.h \
shrpx_https_upstream.cc shrpx_https_upstream.h \ shrpx_https_upstream.cc shrpx_https_upstream.h \
shrpx_downstream_queue.cc shrpx_downstream_queue.h \ shrpx_downstream_queue.cc shrpx_downstream_queue.h \
shrpx_downstream.cc shrpx_downstream.h \ shrpx_downstream.cc shrpx_downstream.h \

View File

@ -28,7 +28,7 @@
#include <cerrno> #include <cerrno>
#include "shrpx_upstream.h" #include "shrpx_upstream.h"
#include "shrpx_spdy_upstream.h" #include "shrpx_http2_upstream.h"
#include "shrpx_https_upstream.h" #include "shrpx_https_upstream.h"
#include "shrpx_config.h" #include "shrpx_config.h"
#include "shrpx_http_downstream_connection.h" #include "shrpx_http_downstream_connection.h"
@ -140,7 +140,7 @@ ClientHandler::ClientHandler(bufferevent *bev, int fd, SSL *ssl,
upstream_ = new HttpsUpstream(this); upstream_ = new HttpsUpstream(this);
} else { } else {
// no-TLS SPDY // no-TLS SPDY
upstream_ = new SpdyUpstream(this); upstream_ = new Http2Upstream(this);
} }
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb); set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
} }
@ -210,7 +210,7 @@ int ClientHandler::validate_next_proto()
CLOG(INFO, this) << "The negotiated next protocol: " << proto; CLOG(INFO, this) << "The negotiated next protocol: " << proto;
} }
if(proto == NGHTTP2_PROTO_VERSION_ID) { if(proto == NGHTTP2_PROTO_VERSION_ID) {
upstream_ = new SpdyUpstream(this); upstream_ = new Http2Upstream(this);
return 0; return 0;
} }
} else { } else {

View File

@ -22,7 +22,7 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include "shrpx_spdy_upstream.h" #include "shrpx_http2_upstream.h"
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <assert.h> #include <assert.h>
@ -51,7 +51,7 @@ ssize_t send_callback(nghttp2_session *session,
void *user_data) void *user_data)
{ {
int rv; int rv;
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data); Http2Upstream *upstream = reinterpret_cast<Http2Upstream*>(user_data);
ClientHandler *handler = upstream->get_client_handler(); ClientHandler *handler = upstream->get_client_handler();
bufferevent *bev = handler->get_bev(); bufferevent *bev = handler->get_bev();
evbuffer *output = bufferevent_get_output(bev); evbuffer *output = bufferevent_get_output(bev);
@ -74,7 +74,7 @@ namespace {
ssize_t recv_callback(nghttp2_session *session, ssize_t recv_callback(nghttp2_session *session,
uint8_t *data, size_t len, int flags, void *user_data) uint8_t *data, size_t len, int flags, void *user_data)
{ {
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data); Http2Upstream *upstream = reinterpret_cast<Http2Upstream*>(user_data);
ClientHandler *handler = upstream->get_client_handler(); ClientHandler *handler = upstream->get_client_handler();
bufferevent *bev = handler->get_bev(); bufferevent *bev = handler->get_bev();
evbuffer *input = bufferevent_get_input(bev); evbuffer *input = bufferevent_get_input(bev);
@ -94,7 +94,7 @@ void on_stream_close_callback
(nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code, (nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code,
void *user_data) void *user_data)
{ {
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data); Http2Upstream *upstream = reinterpret_cast<Http2Upstream*>(user_data);
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "Stream stream_id=" << stream_id ULOG(INFO, upstream) << "Stream stream_id=" << stream_id
<< " is being closed"; << " is being closed";
@ -139,7 +139,7 @@ namespace {
void on_frame_recv_callback void on_frame_recv_callback
(nghttp2_session *session, nghttp2_frame *frame, void *user_data) (nghttp2_session *session, nghttp2_frame *frame, void *user_data)
{ {
auto upstream = reinterpret_cast<SpdyUpstream*>(user_data); auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
switch(frame->hd.type) { switch(frame->hd.type) {
case NGHTTP2_HEADERS: { case NGHTTP2_HEADERS: {
if(frame->headers.cat != NGHTTP2_HCAT_REQUEST) { if(frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
@ -238,7 +238,7 @@ void on_data_chunk_recv_callback(nghttp2_session *session,
const uint8_t *data, size_t len, const uint8_t *data, size_t len,
void *user_data) void *user_data)
{ {
SpdyUpstream *upstream = reinterpret_cast<SpdyUpstream*>(user_data); Http2Upstream *upstream = reinterpret_cast<Http2Upstream*>(user_data);
Downstream *downstream = upstream->find_downstream(stream_id); Downstream *downstream = upstream->find_downstream(stream_id);
if(downstream) { if(downstream) {
if(downstream->push_upload_data_chunk(data, len) != 0) { if(downstream->push_upload_data_chunk(data, len) != 0) {
@ -271,7 +271,7 @@ void on_frame_not_send_callback(nghttp2_session *session,
nghttp2_frame *frame, nghttp2_frame *frame,
int lib_error_code, void *user_data) int lib_error_code, void *user_data)
{ {
auto upstream = reinterpret_cast<SpdyUpstream*>(user_data); auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
ULOG(WARNING, upstream) << "Failed to send control frame type=" ULOG(WARNING, upstream) << "Failed to send control frame type="
<< frame->hd.type << frame->hd.type
<< ", lib_error_code=" << lib_error_code << ":" << ", lib_error_code=" << lib_error_code << ":"
@ -295,7 +295,7 @@ void on_frame_recv_parse_error_callback(nghttp2_session *session,
size_t payloadlen, int lib_error_code, size_t payloadlen, int lib_error_code,
void *user_data) void *user_data)
{ {
auto upstream = reinterpret_cast<SpdyUpstream*>(user_data); auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "Failed to parse received control frame. type=" ULOG(INFO, upstream) << "Failed to parse received control frame. type="
<< type << type
@ -311,7 +311,7 @@ void on_unknown_frame_recv_callback(nghttp2_session *session,
const uint8_t *payload, size_t payloadlen, const uint8_t *payload, size_t payloadlen,
void *user_data) void *user_data)
{ {
auto upstream = reinterpret_cast<SpdyUpstream*>(user_data); auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "Received unknown control frame."; ULOG(INFO, upstream) << "Received unknown control frame.";
} }
@ -332,7 +332,7 @@ nghttp2_error_code infer_upstream_rst_stream_error_code
} }
} // namespace } // namespace
SpdyUpstream::SpdyUpstream(ClientHandler *handler) Http2Upstream::Http2Upstream(ClientHandler *handler)
: handler_(handler), : handler_(handler),
session_(nullptr) session_(nullptr)
{ {
@ -383,12 +383,12 @@ SpdyUpstream::SpdyUpstream(ClientHandler *handler)
send(); send();
} }
SpdyUpstream::~SpdyUpstream() Http2Upstream::~Http2Upstream()
{ {
nghttp2_session_del(session_); nghttp2_session_del(session_);
} }
int SpdyUpstream::on_read() int Http2Upstream::on_read()
{ {
int rv = 0; int rv = 0;
if((rv = nghttp2_session_recv(session_)) < 0) { if((rv = nghttp2_session_recv(session_)) < 0) {
@ -412,13 +412,13 @@ int SpdyUpstream::on_read()
return rv; return rv;
} }
int SpdyUpstream::on_write() int Http2Upstream::on_write()
{ {
return send(); return send();
} }
// After this function call, downstream may be deleted. // After this function call, downstream may be deleted.
int SpdyUpstream::send() int Http2Upstream::send()
{ {
int rv = 0; int rv = 0;
if((rv = nghttp2_session_send(session_)) < 0) { if((rv = nghttp2_session_send(session_)) < 0) {
@ -437,12 +437,12 @@ int SpdyUpstream::send()
return rv; return rv;
} }
int SpdyUpstream::on_event() int Http2Upstream::on_event()
{ {
return 0; return 0;
} }
ClientHandler* SpdyUpstream::get_client_handler() const ClientHandler* Http2Upstream::get_client_handler() const
{ {
return handler_; return handler_;
} }
@ -452,8 +452,8 @@ void spdy_downstream_readcb(bufferevent *bev, void *ptr)
{ {
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr); DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
Downstream *downstream = dconn->get_downstream(); Downstream *downstream = dconn->get_downstream();
SpdyUpstream *upstream; Http2Upstream *upstream;
upstream = static_cast<SpdyUpstream*>(downstream->get_upstream()); upstream = static_cast<Http2Upstream*>(downstream->get_upstream());
if(downstream->get_request_state() == Downstream::STREAM_CLOSED) { if(downstream->get_request_state() == Downstream::STREAM_CLOSED) {
// If upstream SPDY stream was closed, we just close downstream, // If upstream SPDY stream was closed, we just close downstream,
// because there is no consumer now. Downstream connection is also // because there is no consumer now. Downstream connection is also
@ -511,8 +511,8 @@ void spdy_downstream_writecb(bufferevent *bev, void *ptr)
} }
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr); DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
Downstream *downstream = dconn->get_downstream(); Downstream *downstream = dconn->get_downstream();
SpdyUpstream *upstream; Http2Upstream *upstream;
upstream = static_cast<SpdyUpstream*>(downstream->get_upstream()); upstream = static_cast<Http2Upstream*>(downstream->get_upstream());
upstream->resume_read(SHRPX_NO_BUFFER, downstream); upstream->resume_read(SHRPX_NO_BUFFER, downstream);
} }
} // namespace } // namespace
@ -522,8 +522,8 @@ void spdy_downstream_eventcb(bufferevent *bev, short events, void *ptr)
{ {
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr); DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
Downstream *downstream = dconn->get_downstream(); Downstream *downstream = dconn->get_downstream();
SpdyUpstream *upstream; Http2Upstream *upstream;
upstream = static_cast<SpdyUpstream*>(downstream->get_upstream()); upstream = static_cast<Http2Upstream*>(downstream->get_upstream());
if(events & BEV_EVENT_CONNECTED) { if(events & BEV_EVENT_CONNECTED) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
DCLOG(INFO, dconn) << "Connection established. stream_id=" DCLOG(INFO, dconn) << "Connection established. stream_id="
@ -631,7 +631,7 @@ void spdy_downstream_eventcb(bufferevent *bev, short events, void *ptr)
} }
} // namespace } // namespace
int SpdyUpstream::rst_stream(Downstream *downstream, int Http2Upstream::rst_stream(Downstream *downstream,
nghttp2_error_code error_code) nghttp2_error_code error_code)
{ {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
@ -649,7 +649,7 @@ int SpdyUpstream::rst_stream(Downstream *downstream,
return 0; return 0;
} }
int SpdyUpstream::window_update(Downstream *downstream) int Http2Upstream::window_update(Downstream *downstream)
{ {
int rv; int rv;
rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE, rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE,
@ -682,8 +682,8 @@ ssize_t spdy_data_read_callback(nghttp2_session *session,
*eof = 1; *eof = 1;
} else { } else {
// For tunneling, issue RST_STREAM to finish the stream. // For tunneling, issue RST_STREAM to finish the stream.
SpdyUpstream *upstream; Http2Upstream *upstream;
upstream = reinterpret_cast<SpdyUpstream*>(downstream->get_upstream()); upstream = reinterpret_cast<Http2Upstream*>(downstream->get_upstream());
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "RST_STREAM to tunneled stream stream_id=" ULOG(INFO, upstream) << "RST_STREAM to tunneled stream stream_id="
<< stream_id; << stream_id;
@ -699,7 +699,7 @@ ssize_t spdy_data_read_callback(nghttp2_session *session,
} }
} // namespace } // namespace
int SpdyUpstream::error_reply(Downstream *downstream, int status_code) int Http2Upstream::error_reply(Downstream *downstream, int status_code)
{ {
int rv; int rv;
std::string html = http::create_error_html(status_code); std::string html = http::create_error_html(status_code);
@ -741,44 +741,44 @@ int SpdyUpstream::error_reply(Downstream *downstream, int status_code)
return 0; return 0;
} }
bufferevent_data_cb SpdyUpstream::get_downstream_readcb() bufferevent_data_cb Http2Upstream::get_downstream_readcb()
{ {
return spdy_downstream_readcb; return spdy_downstream_readcb;
} }
bufferevent_data_cb SpdyUpstream::get_downstream_writecb() bufferevent_data_cb Http2Upstream::get_downstream_writecb()
{ {
return spdy_downstream_writecb; return spdy_downstream_writecb;
} }
bufferevent_event_cb SpdyUpstream::get_downstream_eventcb() bufferevent_event_cb Http2Upstream::get_downstream_eventcb()
{ {
return spdy_downstream_eventcb; return spdy_downstream_eventcb;
} }
void SpdyUpstream::add_downstream(Downstream *downstream) void Http2Upstream::add_downstream(Downstream *downstream)
{ {
downstream_queue_.add(downstream); downstream_queue_.add(downstream);
} }
void SpdyUpstream::remove_downstream(Downstream *downstream) void Http2Upstream::remove_downstream(Downstream *downstream)
{ {
downstream_queue_.remove(downstream); downstream_queue_.remove(downstream);
} }
Downstream* SpdyUpstream::find_downstream(int32_t stream_id) Downstream* Http2Upstream::find_downstream(int32_t stream_id)
{ {
return downstream_queue_.find(stream_id); return downstream_queue_.find(stream_id);
} }
nghttp2_session* SpdyUpstream::get_spdy_session() nghttp2_session* Http2Upstream::get_spdy_session()
{ {
return session_; return session_;
} }
// WARNING: Never call directly or indirectly nghttp2_session_send or // WARNING: Never call directly or indirectly nghttp2_session_send or
// nghttp2_session_recv. These calls may delete downstream. // nghttp2_session_recv. These calls may delete downstream.
int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) int Http2Upstream::on_downstream_header_complete(Downstream *downstream)
{ {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
DLOG(INFO, downstream) << "HTTP response header completed"; DLOG(INFO, downstream) << "HTTP response header completed";
@ -848,7 +848,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream)
// WARNING: Never call directly or indirectly nghttp2_session_send or // WARNING: Never call directly or indirectly nghttp2_session_send or
// nghttp2_session_recv. These calls may delete downstream. // nghttp2_session_recv. These calls may delete downstream.
int SpdyUpstream::on_downstream_body(Downstream *downstream, int Http2Upstream::on_downstream_body(Downstream *downstream,
const uint8_t *data, size_t len) const uint8_t *data, size_t len)
{ {
evbuffer *body = downstream->get_response_body_buf(); evbuffer *body = downstream->get_response_body_buf();
@ -869,7 +869,7 @@ int SpdyUpstream::on_downstream_body(Downstream *downstream,
// WARNING: Never call directly or indirectly nghttp2_session_send or // WARNING: Never call directly or indirectly nghttp2_session_send or
// nghttp2_session_recv. These calls may delete downstream. // nghttp2_session_recv. These calls may delete downstream.
int SpdyUpstream::on_downstream_body_complete(Downstream *downstream) int Http2Upstream::on_downstream_body_complete(Downstream *downstream)
{ {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
DLOG(INFO, downstream) << "HTTP response completed"; DLOG(INFO, downstream) << "HTTP response completed";
@ -878,20 +878,20 @@ int SpdyUpstream::on_downstream_body_complete(Downstream *downstream)
return 0; return 0;
} }
bool SpdyUpstream::get_flow_control() const bool Http2Upstream::get_flow_control() const
{ {
return flow_control_; return flow_control_;
} }
int32_t SpdyUpstream::get_initial_window_size() const int32_t Http2Upstream::get_initial_window_size() const
{ {
return initial_window_size_; return initial_window_size_;
} }
void SpdyUpstream::pause_read(IOCtrlReason reason) void Http2Upstream::pause_read(IOCtrlReason reason)
{} {}
int SpdyUpstream::resume_read(IOCtrlReason reason, Downstream *downstream) int Http2Upstream::resume_read(IOCtrlReason reason, Downstream *downstream)
{ {
if(get_flow_control()) { if(get_flow_control()) {
if(downstream->get_recv_window_size() >= get_initial_window_size()/2) { if(downstream->get_recv_window_size() >= get_initial_window_size()/2) {

View File

@ -36,10 +36,10 @@ namespace shrpx {
class ClientHandler; class ClientHandler;
class SpdyUpstream : public Upstream { class Http2Upstream : public Upstream {
public: public:
SpdyUpstream(ClientHandler *handler); Http2Upstream(ClientHandler *handler);
virtual ~SpdyUpstream(); virtual ~Http2Upstream();
virtual int on_read(); virtual int on_read();
virtual int on_write(); virtual int on_write();
virtual int on_event(); virtual int on_event();