shrpx_https_upstream.cc: Code cleanup

This commit is contained in:
Tatsuhiro Tsujikawa 2013-09-24 23:39:36 +09:00
parent a0326b3f2b
commit dc0af2e074
1 changed files with 43 additions and 54 deletions

View File

@ -51,7 +51,7 @@ const size_t SHRPX_HTTPS_MAX_HEADER_LENGTH = 64*1024;
HttpsUpstream::HttpsUpstream(ClientHandler *handler)
: handler_(handler),
current_header_length_(0),
downstream_(0),
downstream_(nullptr),
ioctrl_(handler->get_bev())
{
http_parser_init(&htp_, HTTP_REQUEST);
@ -71,13 +71,12 @@ void HttpsUpstream::reset_current_header_length()
namespace {
int htp_msg_begin(http_parser *htp)
{
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "HTTP request started";
}
upstream->reset_current_header_length();
Downstream *downstream = new Downstream(upstream, 0, 0);
auto downstream = new Downstream(upstream, 0, 0);
upstream->attach_downstream(downstream);
return 0;
}
@ -86,9 +85,8 @@ int htp_msg_begin(http_parser *htp)
namespace {
int htp_uricb(http_parser *htp, const char *data, size_t len)
{
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_downstream();
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto downstream = upstream->get_downstream();
downstream->append_request_path(data, len);
return 0;
}
@ -97,9 +95,8 @@ int htp_uricb(http_parser *htp, const char *data, size_t len)
namespace {
int htp_hdr_keycb(http_parser *htp, const char *data, size_t len)
{
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_downstream();
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto downstream = upstream->get_downstream();
if(downstream->get_request_header_key_prev()) {
downstream->append_last_request_header_key(data, len);
} else {
@ -112,9 +109,8 @@ int htp_hdr_keycb(http_parser *htp, const char *data, size_t len)
namespace {
int htp_hdr_valcb(http_parser *htp, const char *data, size_t len)
{
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_downstream();
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto downstream = upstream->get_downstream();
if(downstream->get_request_header_key_prev()) {
downstream->set_last_request_header_value(std::string(data, len));
} else {
@ -128,12 +124,11 @@ namespace {
int htp_hdrs_completecb(http_parser *htp)
{
int rv;
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "HTTP request headers completed";
}
Downstream *downstream = upstream->get_downstream();
auto downstream = upstream->get_downstream();
downstream->set_request_method(http_method_str((enum http_method)htp->method));
downstream->set_request_major(htp->http_major);
@ -148,7 +143,7 @@ int htp_hdrs_completecb(http_parser *htp)
<< downstream->get_request_path() << " "
<< "HTTP/" << downstream->get_request_major() << "."
<< downstream->get_request_minor() << "\n";
const Headers& headers = downstream->get_request_headers();
const auto& headers = downstream->get_request_headers();
for(size_t i = 0; i < headers.size(); ++i) {
ss << TTY_HTTP_HD << headers[i].first << TTY_RST << ": "
<< headers[i].second << "\n";
@ -160,7 +155,7 @@ int htp_hdrs_completecb(http_parser *htp)
downstream->get_request_method() != "CONNECT") {
// Make sure that request path is an absolute URI.
http_parser_url u;
const char *url = downstream->get_request_path().c_str();
auto url = downstream->get_request_path().c_str();
memset(&u, 0, sizeof(u));
rv = http_parser_parse_url(url,
downstream->get_request_path().size(),
@ -171,8 +166,7 @@ int htp_hdrs_completecb(http_parser *htp)
}
}
DownstreamConnection *dconn;
dconn = upstream->get_client_handler()->get_downstream_connection();
auto dconn = upstream->get_client_handler()->get_downstream_connection();
if(downstream->get_expect_100_continue()) {
static const char reply_100[] = "HTTP/1.1 100 Continue\r\n\r\n";
@ -205,9 +199,8 @@ namespace {
int htp_bodycb(http_parser *htp, const char *data, size_t len)
{
int rv;
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_downstream();
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto downstream = upstream->get_downstream();
rv = downstream->push_upload_data_chunk
(reinterpret_cast<const uint8_t*>(data), len);
if(rv != 0) {
@ -221,12 +214,11 @@ namespace {
int htp_msg_completecb(http_parser *htp)
{
int rv;
HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
auto upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
if(LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "HTTP request completed";
}
Downstream *downstream = upstream->get_downstream();
auto downstream = upstream->get_downstream();
downstream->set_request_state(Downstream::MSG_COMPLETE);
rv = downstream->end_upload_data();
if(rv != 0) {
@ -256,10 +248,10 @@ http_parser_settings htp_hooks = {
// one http request is fully received.
int HttpsUpstream::on_read()
{
bufferevent *bev = handler_->get_bev();
evbuffer *input = bufferevent_get_input(bev);
auto bev = handler_->get_bev();
auto input = bufferevent_get_input(bev);
size_t inputlen = evbuffer_get_length(input);
unsigned char *mem = evbuffer_pullup(input, -1);
auto mem = evbuffer_pullup(input, -1);
if(inputlen == 0) {
return 0;
@ -293,7 +285,7 @@ int HttpsUpstream::on_read()
// execution
downstream = get_downstream();
auto handler = get_client_handler();
http_errno htperr = HTTP_PARSER_ERRNO(&htp_);
auto htperr = HTTP_PARSER_ERRNO(&htp_);
if(htperr == HPE_PAUSED) {
if(downstream->get_request_state() == Downstream::CONNECT_FAIL) {
handler->set_should_close_after_write(true);
@ -358,7 +350,7 @@ int HttpsUpstream::on_read()
int HttpsUpstream::on_write()
{
int rv = 0;
Downstream *downstream = get_downstream();
auto downstream = get_downstream();
if(downstream) {
rv = downstream->resume_read(SHRPX_NO_BUFFER);
}
@ -395,10 +387,9 @@ int HttpsUpstream::resume_read(IOCtrlReason reason, Downstream *downstream)
namespace {
void https_downstream_readcb(bufferevent *bev, void *ptr)
{
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
Downstream *downstream = dconn->get_downstream();
HttpsUpstream *upstream;
upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
auto downstream = dconn->get_downstream();
auto upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
int rv;
rv = downstream->on_read();
if(downstream->get_response_state() == Downstream::MSG_RESET) {
@ -414,7 +405,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
// Keep-alive
dconn->detach_downstream(downstream);
}
ClientHandler *handler = upstream->get_client_handler();
auto handler = upstream->get_client_handler();
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
if(handler->get_should_close_after_write() &&
handler->get_pending_write_length() == 0) {
@ -449,8 +440,8 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
}
}
} else {
ClientHandler *handler = upstream->get_client_handler();
bufferevent *bev = handler->get_bev();
auto handler = upstream->get_client_handler();
auto bev = handler->get_bev();
size_t outputlen = evbuffer_get_length(bufferevent_get_output(bev));
if(outputlen > SHRPX_HTTPS_UPSTREAM_OUTPUT_UPPER_THRES) {
downstream->pause_read(SHRPX_NO_BUFFER);
@ -486,10 +477,9 @@ void https_downstream_writecb(bufferevent *bev, void *ptr)
if(evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
return;
}
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
Downstream *downstream = dconn->get_downstream();
HttpsUpstream *upstream;
upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
auto downstream = dconn->get_downstream();
auto upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
// May return -1
upstream->resume_read(SHRPX_NO_BUFFER, downstream);
}
@ -498,10 +488,9 @@ void https_downstream_writecb(bufferevent *bev, void *ptr)
namespace {
void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
{
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
Downstream *downstream = dconn->get_downstream();
HttpsUpstream *upstream;
upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
auto dconn = reinterpret_cast<DownstreamConnection*>(ptr);
auto downstream = dconn->get_downstream();
auto upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
if(events & BEV_EVENT_CONNECTED) {
if(LOG_ENABLED(INFO)) {
DCLOG(INFO, dconn) << "Connection established";
@ -519,7 +508,7 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
upstream->on_downstream_body_complete(downstream);
downstream->set_response_state(Downstream::MSG_COMPLETE);
ClientHandler *handler = upstream->get_client_handler();
auto handler = upstream->get_client_handler();
if(handler->get_should_close_after_write() &&
handler->get_pending_write_length() == 0) {
// If all upstream response body has already written out to
@ -578,7 +567,7 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
int HttpsUpstream::error_reply(int status_code)
{
std::string html = http::create_error_html(status_code);
auto html = http::create_error_html(status_code);
std::string header;
header.reserve(512);
header += "HTTP/1.1 ";
@ -592,13 +581,13 @@ int HttpsUpstream::error_reply(int status_code)
header += "Connection: close\r\n";
}
header += "\r\n";
evbuffer *output = bufferevent_get_output(handler_->get_bev());
auto output = bufferevent_get_output(handler_->get_bev());
if(evbuffer_add(output, header.c_str(), header.size()) != 0 ||
evbuffer_add(output, html.c_str(), html.size()) != 0) {
ULOG(FATAL, this) << "evbuffer_add() failed";
return -1;
}
Downstream *downstream = get_downstream();
auto downstream = get_downstream();
if(downstream) {
downstream->set_response_state(Downstream::MSG_COMPLETE);
}
@ -708,7 +697,7 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream)
}
ULOG(INFO, this) << "HTTP response headers\n" << hdrp;
}
evbuffer *output = bufferevent_get_output(handler_->get_bev());
auto output = bufferevent_get_output(handler_->get_bev());
if(evbuffer_add(output, hdrs.c_str(), hdrs.size()) != 0) {
ULOG(FATAL, this) << "evbuffer_add() failed";
return -1;
@ -727,7 +716,7 @@ int HttpsUpstream::on_downstream_body(Downstream *downstream,
if(len == 0) {
return 0;
}
evbuffer *output = bufferevent_get_output(handler_->get_bev());
auto output = bufferevent_get_output(handler_->get_bev());
if(downstream->get_chunked_response()) {
char chunk_size_hex[16];
rv = snprintf(chunk_size_hex, sizeof(chunk_size_hex), "%X\r\n",
@ -753,7 +742,7 @@ int HttpsUpstream::on_downstream_body(Downstream *downstream,
int HttpsUpstream::on_downstream_body_complete(Downstream *downstream)
{
if(downstream->get_chunked_response()) {
evbuffer *output = bufferevent_get_output(handler_->get_bev());
auto output = bufferevent_get_output(handler_->get_bev());
if(evbuffer_add(output, "0\r\n\r\n", 5) != 0) {
ULOG(FATAL, this) << "evbuffer_add() failed";
return -1;
@ -764,7 +753,7 @@ int HttpsUpstream::on_downstream_body_complete(Downstream *downstream)
}
if(downstream->get_request_connection_close() ||
downstream->get_response_connection_close()) {
ClientHandler *handler = get_client_handler();
auto handler = get_client_handler();
handler->set_should_close_after_write(true);
}
return 0;