Commit 7e217511 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Code cleanup

Mainly make nested code block to rather flat style.
parent 8c67bbe3
...@@ -272,19 +272,25 @@ void eventcb(bufferevent *bev, short events, void *ptr) ...@@ -272,19 +272,25 @@ void eventcb(bufferevent *bev, short events, void *ptr)
return; return;
} }
int fd = bufferevent_getfd(bev); auto fd = bufferevent_getfd(bev);
int val = 1; int val = 1;
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char *>(&val), sizeof(val)) == -1) { reinterpret_cast<char*>(&val), sizeof(val)) == -1) {
SSLOG(WARNING, http2session) SSLOG(WARNING, http2session)
<< "Setting option TCP_NODELAY failed: errno=" << errno; << "Setting option TCP_NODELAY failed: errno=" << errno;
} }
} else if(events & BEV_EVENT_EOF) { return;
}
if(events & BEV_EVENT_EOF) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
SSLOG(INFO, http2session) << "EOF"; SSLOG(INFO, http2session) << "EOF";
} }
http2session->disconnect(); http2session->disconnect();
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) { return;
}
if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
if(events & BEV_EVENT_ERROR) { if(events & BEV_EVENT_ERROR) {
SSLOG(INFO, http2session) << "Network error"; SSLOG(INFO, http2session) << "Network error";
...@@ -293,6 +299,7 @@ void eventcb(bufferevent *bev, short events, void *ptr) ...@@ -293,6 +299,7 @@ void eventcb(bufferevent *bev, short events, void *ptr)
} }
} }
http2session->disconnect(); http2session->disconnect();
return;
} }
} }
} // namespace } // namespace
...@@ -350,12 +357,18 @@ void proxy_eventcb(bufferevent *bev, short events, void *ptr) ...@@ -350,12 +357,18 @@ void proxy_eventcb(bufferevent *bev, short events, void *ptr)
SSLOG(ERROR, http2session) << "bufferevent_write() failed"; SSLOG(ERROR, http2session) << "bufferevent_write() failed";
http2session->disconnect(); http2session->disconnect();
} }
} else if(events & BEV_EVENT_EOF) { return;
}
if(events & BEV_EVENT_EOF) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
SSLOG(INFO, http2session) << "Proxy EOF"; SSLOG(INFO, http2session) << "Proxy EOF";
} }
http2session->disconnect(); http2session->disconnect();
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) { return;
}
if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
if(events & BEV_EVENT_ERROR) { if(events & BEV_EVENT_ERROR) {
SSLOG(INFO, http2session) << "Network error"; SSLOG(INFO, http2session) << "Network error";
...@@ -364,6 +377,7 @@ void proxy_eventcb(bufferevent *bev, short events, void *ptr) ...@@ -364,6 +377,7 @@ void proxy_eventcb(bufferevent *bev, short events, void *ptr)
} }
} }
http2session->disconnect(); http2session->disconnect();
return;
} }
} }
} // namespace } // namespace
...@@ -409,7 +423,11 @@ int Http2Session::initiate_connection() ...@@ -409,7 +423,11 @@ int Http2Session::initiate_connection()
proxy_htp_->data = this; proxy_htp_->data = this;
state_ = PROXY_CONNECTING; state_ = PROXY_CONNECTING;
} else if(state_ == DISCONNECTED || state_ == PROXY_CONNECTED) {
return 0;
}
if(state_ == DISCONNECTED || state_ == PROXY_CONNECTED) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
SSLOG(INFO, this) << "Connecting to downstream server"; SSLOG(INFO, this) << "Connecting to downstream server";
} }
...@@ -492,11 +510,12 @@ int Http2Session::initiate_connection() ...@@ -492,11 +510,12 @@ int Http2Session::initiate_connection()
if(state_ != CONNECTED) { if(state_ != CONNECTED) {
state_ = CONNECTING; state_ = CONNECTING;
} }
} else {
// Unreachable return 0;
DIE();
} }
return 0;
// Unreachable
DIE();
} }
void Http2Session::unwrap_free_bev() void Http2Session::unwrap_free_bev()
...@@ -517,10 +536,13 @@ int htp_hdrs_completecb(http_parser *htp) ...@@ -517,10 +536,13 @@ int htp_hdrs_completecb(http_parser *htp)
SSLOG(INFO, http2session) << "Tunneling success"; SSLOG(INFO, http2session) << "Tunneling success";
} }
http2session->set_state(Http2Session::PROXY_CONNECTED); http2session->set_state(Http2Session::PROXY_CONNECTED);
} else {
SSLOG(WARNING, http2session) << "Tunneling failed"; return 0;
http2session->set_state(Http2Session::PROXY_FAILED);
} }
SSLOG(WARNING, http2session) << "Tunneling failed";
http2session->set_state(Http2Session::PROXY_FAILED);
return 0; return 0;
} }
} // namespace } // namespace
......
This diff is collapsed.
...@@ -193,19 +193,23 @@ int htp_hdrs_completecb(http_parser *htp) ...@@ -193,19 +193,23 @@ int htp_hdrs_completecb(http_parser *htp)
} }
rv = dconn->attach_downstream(downstream); rv = dconn->attach_downstream(downstream);
if(rv != 0) { if(rv != 0) {
downstream->set_request_state(Downstream::CONNECT_FAIL); downstream->set_request_state(Downstream::CONNECT_FAIL);
downstream->set_downstream_connection(0); downstream->set_downstream_connection(nullptr);
delete dconn; delete dconn;
return -1; return -1;
} else {
rv = downstream->push_request_headers();
if(rv != 0) {
return -1;
}
downstream->set_request_state(Downstream::HEADER_COMPLETE);
return 0;
} }
rv = downstream->push_request_headers();
if(rv != 0) {
return -1;
}
downstream->set_request_state(Downstream::HEADER_COMPLETE);
return 0;
} }
} // namespace } // namespace
...@@ -431,9 +435,9 @@ int HttpsUpstream::resume_read(IOCtrlReason reason, Downstream *downstream) ...@@ -431,9 +435,9 @@ int HttpsUpstream::resume_read(IOCtrlReason reason, Downstream *downstream)
// are not notified by readcb until new data arrive. // are not notified by readcb until new data arrive.
http_parser_pause(&htp_, 0); http_parser_pause(&htp_, 0);
return on_read(); return on_read();
} else {
return 0;
} }
return 0;
} }
namespace { namespace {
...@@ -443,80 +447,109 @@ void https_downstream_readcb(bufferevent *bev, void *ptr) ...@@ -443,80 +447,109 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
auto downstream = dconn->get_downstream(); auto downstream = dconn->get_downstream();
auto upstream = static_cast<HttpsUpstream*>(downstream->get_upstream()); auto upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
int rv; int rv;
rv = downstream->on_read(); rv = downstream->on_read();
if(downstream->get_response_state() == Downstream::MSG_RESET) { if(downstream->get_response_state() == Downstream::MSG_RESET) {
delete upstream->get_client_handler(); delete upstream->get_client_handler();
} else if(rv == 0) {
auto handler = upstream->get_client_handler(); return;
}
if(downstream->get_response_state() == Downstream::MSG_COMPLETE) {
if(downstream->get_response_connection_close()) { if(rv != 0) {
// Connection close
downstream->set_downstream_connection(0);
delete dconn;
dconn = 0;
} else {
// Keep-alive
dconn->detach_downstream(downstream);
}
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
if(handler->get_should_close_after_write() &&
handler->get_outbuf_length() == 0) {
// If all upstream response body has already written out to
// the peer, we cannot use writecb for ClientHandler. In
// this case, we just delete handler here.
delete handler;
return;
} else {
upstream->delete_downstream();
// Process next HTTP request
if(upstream->resume_read(SHRPX_MSG_BLOCK, 0) == -1) {
return;
}
}
} else if(downstream->get_upgraded()) {
// This path is effectively only taken for HTTP2 downstream
// because only HTTP2 downstream sets response_state to
// MSG_COMPLETE and this function. For HTTP downstream, EOF
// from tunnel connection is handled on
// https_downstream_eventcb.
//
// Tunneled connection always indicates connection close.
if(handler->get_outbuf_length() == 0) {
// For tunneled connection, if there is no pending data,
// delete handler because on_write will not be called.
delete handler;
} else {
if(LOG_ENABLED(INFO)) {
DLOG(INFO, downstream) << "Tunneled connection has pending data";
}
}
}
} else {
if(handler->get_outbuf_length() >= OUTBUF_MAX_THRES) {
downstream->pause_read(SHRPX_NO_BUFFER);
}
}
} else {
if(downstream->get_response_state() == Downstream::HEADER_COMPLETE) { if(downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
// We already sent HTTP response headers to upstream // We already sent HTTP response headers to upstream
// client. Just close the upstream connection. // client. Just close the upstream connection.
delete upstream->get_client_handler(); delete upstream->get_client_handler();
} else {
// We did not sent any HTTP response, so sent error return;
// response. Cannot reuse downstream connection in this case. }
if(upstream->error_reply(502) != 0) {
delete upstream->get_client_handler(); // We did not sent any HTTP response, so sent error
// response. Cannot reuse downstream connection in this case.
if(upstream->error_reply(502) != 0) {
delete upstream->get_client_handler();
return;
}
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
upstream->delete_downstream();
// Process next HTTP request
if(upstream->resume_read(SHRPX_MSG_BLOCK, 0) == -1) {
return; return;
} }
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
upstream->delete_downstream();
// Process next HTTP request
if(upstream->resume_read(SHRPX_MSG_BLOCK, 0) == -1) {
return;
}
}
} }
return;
}
auto handler = upstream->get_client_handler();
if(downstream->get_response_state() != Downstream::MSG_COMPLETE) {
if(handler->get_outbuf_length() >= OUTBUF_MAX_THRES) {
downstream->pause_read(SHRPX_NO_BUFFER);
}
return;
}
if(downstream->get_response_connection_close()) {
// Connection close
downstream->set_downstream_connection(nullptr);
delete dconn;
dconn = nullptr;
} else {
// Keep-alive
dconn->detach_downstream(downstream);
}
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
if(handler->get_should_close_after_write() &&
handler->get_outbuf_length() == 0) {
// If all upstream response body has already written out to
// the peer, we cannot use writecb for ClientHandler. In
// this case, we just delete handler here.
delete handler;
return;
}
upstream->delete_downstream();
// Process next HTTP request
if(upstream->resume_read(SHRPX_MSG_BLOCK, 0) == -1) {
return;
}
return;
}
if(downstream->get_upgraded()) {
// This path is effectively only taken for HTTP2 downstream
// because only HTTP2 downstream sets response_state to
// MSG_COMPLETE and this function. For HTTP downstream, EOF
// from tunnel connection is handled on
// https_downstream_eventcb.
//
// Tunneled connection always indicates connection close.
if(handler->get_outbuf_length() == 0) {
// For tunneled connection, if there is no pending data,
// delete handler because on_write will not be called.
delete handler;
return;
}
if(LOG_ENABLED(INFO)) {
DLOG(INFO, downstream) << "Tunneled connection has pending data";
}
return;
} }
} }
} // namespace } // namespace
...@@ -545,7 +578,11 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr) ...@@ -545,7 +578,11 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
DCLOG(INFO, dconn) << "Connection established"; DCLOG(INFO, dconn) << "Connection established";
} }
} else if(events & BEV_EVENT_EOF) {
return;
}
if(events & BEV_EVENT_EOF) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
DCLOG(INFO, dconn) << "EOF"; DCLOG(INFO, dconn) << "EOF";
} }
...@@ -567,9 +604,7 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr) ...@@ -567,9 +604,7 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
delete handler; delete handler;
return; return;
} }
} else if(downstream->get_response_state() == Downstream::MSG_COMPLETE) { } else if(downstream->get_response_state() != Downstream::MSG_COMPLETE) {
// Nothing to do
} else {
// error // error
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
DCLOG(INFO, dconn) << "Treated as error"; DCLOG(INFO, dconn) << "Treated as error";
...@@ -585,7 +620,11 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr) ...@@ -585,7 +620,11 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
return; return;
} }
} }
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
return;
}
if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
if(events & BEV_EVENT_ERROR) { if(events & BEV_EVENT_ERROR) {
DCLOG(INFO, dconn) << "Network error"; DCLOG(INFO, dconn) << "Network error";
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment