shrpx: Use raw pointer for downstream instead of deque

This commit is contained in:
Tatsuhiro Tsujikawa 2012-09-20 22:28:40 +09:00
parent c78e0ca055
commit 5597ee68da
2 changed files with 29 additions and 50 deletions

View File

@ -48,6 +48,7 @@ HttpsUpstream::HttpsUpstream(ClientHandler *handler)
: handler_(handler), : handler_(handler),
htp_(new http_parser()), htp_(new http_parser()),
current_header_length_(0), current_header_length_(0),
downstream_(0),
ioctrl_(handler->get_bev()) ioctrl_(handler->get_bev())
{ {
http_parser_init(htp_, HTTP_REQUEST); http_parser_init(htp_, HTTP_REQUEST);
@ -57,10 +58,7 @@ HttpsUpstream::HttpsUpstream(ClientHandler *handler)
HttpsUpstream::~HttpsUpstream() HttpsUpstream::~HttpsUpstream()
{ {
delete htp_; delete htp_;
for(std::deque<Downstream*>::iterator i = downstream_queue_.begin(); delete downstream_;
i != downstream_queue_.end(); ++i) {
delete *i;
}
} }
void HttpsUpstream::reset_current_header_length() void HttpsUpstream::reset_current_header_length()
@ -78,7 +76,7 @@ int htp_msg_begin(http_parser *htp)
} }
upstream->reset_current_header_length(); upstream->reset_current_header_length();
Downstream *downstream = new Downstream(upstream, 0, 0); Downstream *downstream = new Downstream(upstream, 0, 0);
upstream->add_downstream(downstream); upstream->attach_downstream(downstream);
return 0; return 0;
} }
} // namespace } // namespace
@ -88,7 +86,7 @@ int htp_uricb(http_parser *htp, const char *data, size_t len)
{ {
HttpsUpstream *upstream; HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data); upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_last_downstream(); Downstream *downstream = upstream->get_downstream();
downstream->append_request_path(data, len); downstream->append_request_path(data, len);
return 0; return 0;
} }
@ -99,7 +97,7 @@ int htp_hdr_keycb(http_parser *htp, const char *data, size_t len)
{ {
HttpsUpstream *upstream; HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data); upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_last_downstream(); Downstream *downstream = upstream->get_downstream();
if(downstream->get_request_header_key_prev()) { if(downstream->get_request_header_key_prev()) {
downstream->append_last_request_header_key(data, len); downstream->append_last_request_header_key(data, len);
} else { } else {
@ -114,7 +112,7 @@ int htp_hdr_valcb(http_parser *htp, const char *data, size_t len)
{ {
HttpsUpstream *upstream; HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data); upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_last_downstream(); Downstream *downstream = upstream->get_downstream();
if(downstream->get_request_header_key_prev()) { if(downstream->get_request_header_key_prev()) {
downstream->set_last_request_header_value(std::string(data, len)); downstream->set_last_request_header_value(std::string(data, len));
} else { } else {
@ -132,7 +130,7 @@ int htp_hdrs_completecb(http_parser *htp)
if(ENABLE_LOG) { if(ENABLE_LOG) {
LOG(INFO) << "Upstream https request headers complete " << upstream; LOG(INFO) << "Upstream https request headers complete " << upstream;
} }
Downstream *downstream = upstream->get_last_downstream(); Downstream *downstream = upstream->get_downstream();
downstream->set_request_method(http_method_str((enum http_method)htp->method)); downstream->set_request_method(http_method_str((enum http_method)htp->method));
downstream->set_request_major(htp->http_major); downstream->set_request_major(htp->http_major);
@ -175,7 +173,7 @@ int htp_bodycb(http_parser *htp, const char *data, size_t len)
int rv; int rv;
HttpsUpstream *upstream; HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data); upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_last_downstream(); Downstream *downstream = upstream->get_downstream();
rv = downstream->push_upload_data_chunk rv = downstream->push_upload_data_chunk
(reinterpret_cast<const uint8_t*>(data), len); (reinterpret_cast<const uint8_t*>(data), len);
if(rv != 0) { if(rv != 0) {
@ -194,7 +192,7 @@ int htp_msg_completecb(http_parser *htp)
} }
HttpsUpstream *upstream; HttpsUpstream *upstream;
upstream = reinterpret_cast<HttpsUpstream*>(htp->data); upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
Downstream *downstream = upstream->get_last_downstream(); Downstream *downstream = upstream->get_downstream();
rv = downstream->end_upload_data(); rv = downstream->end_upload_data();
if(rv != 0) { if(rv != 0) {
return -1; return -1;
@ -238,7 +236,7 @@ int HttpsUpstream::on_read()
evbuffer_drain(input, nread); evbuffer_drain(input, nread);
// Well, actually header length + some body bytes // Well, actually header length + some body bytes
current_header_length_ += nread; current_header_length_ += nread;
Downstream *downstream = get_top_downstream(); Downstream *downstream = get_downstream();
http_errno htperr = HTTP_PARSER_ERRNO(htp_); http_errno htperr = HTTP_PARSER_ERRNO(htp_);
if(htperr == HPE_PAUSED) { if(htperr == HPE_PAUSED) {
if(downstream->get_request_state() == Downstream::CONNECT_FAIL) { if(downstream->get_request_state() == Downstream::CONNECT_FAIL) {
@ -252,8 +250,7 @@ int HttpsUpstream::on_read()
if(downstream->get_downstream_connection() == 0) { if(downstream->get_downstream_connection() == 0) {
// Error response already be sent // Error response already be sent
assert(downstream->get_response_state() == Downstream::MSG_COMPLETE); assert(downstream->get_response_state() == Downstream::MSG_COMPLETE);
pop_downstream(); delete_downstream();
delete downstream;
} else { } else {
pause_read(SHRPX_MSG_BLOCK); pause_read(SHRPX_MSG_BLOCK);
} }
@ -296,7 +293,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr);
int HttpsUpstream::on_write() int HttpsUpstream::on_write()
{ {
Downstream *downstream = get_top_downstream(); Downstream *downstream = get_downstream();
if(downstream) { if(downstream) {
downstream->resume_read(SHRPX_NO_BUFFER); downstream->resume_read(SHRPX_NO_BUFFER);
} }
@ -357,8 +354,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
delete handler; delete handler;
return; return;
} else { } else {
upstream->pop_downstream(); upstream->delete_downstream();
delete downstream;
// Process next HTTP request // Process next HTTP request
upstream->resume_read(SHRPX_MSG_BLOCK); upstream->resume_read(SHRPX_MSG_BLOCK);
} }
@ -384,8 +380,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
return; return;
} }
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) { if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
upstream->pop_downstream(); upstream->delete_downstream();
delete downstream;
// Process next HTTP request // Process next HTTP request
upstream->resume_read(SHRPX_MSG_BLOCK); upstream->resume_read(SHRPX_MSG_BLOCK);
} }
@ -452,8 +447,7 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
} }
} }
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) { if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
upstream->pop_downstream(); upstream->delete_downstream();
delete downstream;
upstream->resume_read(SHRPX_MSG_BLOCK); upstream->resume_read(SHRPX_MSG_BLOCK);
} }
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) { } else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
@ -473,8 +467,7 @@ void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
} }
} }
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) { if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
upstream->pop_downstream(); upstream->delete_downstream();
delete downstream;
upstream->resume_read(SHRPX_MSG_BLOCK); upstream->resume_read(SHRPX_MSG_BLOCK);
} }
} }
@ -500,7 +493,7 @@ int HttpsUpstream::error_reply(int status_code)
LOG(FATAL) << "evbuffer_add() failed"; LOG(FATAL) << "evbuffer_add() failed";
return -1; return -1;
} }
Downstream *downstream = get_top_downstream(); Downstream *downstream = get_downstream();
if(downstream) { if(downstream) {
downstream->set_response_state(Downstream::MSG_COMPLETE); downstream->set_response_state(Downstream::MSG_COMPLETE);
} }
@ -522,32 +515,21 @@ bufferevent_event_cb HttpsUpstream::get_downstream_eventcb()
return https_downstream_eventcb; return https_downstream_eventcb;
} }
void HttpsUpstream::add_downstream(Downstream *downstream) void HttpsUpstream::attach_downstream(Downstream *downstream)
{ {
downstream_queue_.push_back(downstream); assert(!downstream_);
downstream_ = downstream;
} }
void HttpsUpstream::pop_downstream() void HttpsUpstream::delete_downstream()
{ {
downstream_queue_.pop_front(); delete downstream_;
downstream_ = 0;
} }
Downstream* HttpsUpstream::get_top_downstream() Downstream* HttpsUpstream::get_downstream() const
{ {
if(downstream_queue_.empty()) { return downstream_;
return 0;
} else {
return downstream_queue_.front();
}
}
Downstream* HttpsUpstream::get_last_downstream()
{
if(downstream_queue_.empty()) {
return 0;
} else {
return downstream_queue_.back();
}
} }
int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) int HttpsUpstream::on_downstream_header_complete(Downstream *downstream)

View File

@ -29,8 +29,6 @@
#include <stdint.h> #include <stdint.h>
#include <deque>
extern "C" { extern "C" {
#include "http-parser/http_parser.h" #include "http-parser/http_parser.h"
} }
@ -54,10 +52,9 @@ public:
virtual bufferevent_data_cb get_downstream_readcb(); virtual bufferevent_data_cb get_downstream_readcb();
virtual bufferevent_data_cb get_downstream_writecb(); virtual bufferevent_data_cb get_downstream_writecb();
virtual bufferevent_event_cb get_downstream_eventcb(); virtual bufferevent_event_cb get_downstream_eventcb();
void add_downstream(Downstream *downstream); void attach_downstream(Downstream *downstream);
void pop_downstream(); void delete_downstream();
Downstream* get_top_downstream(); Downstream* get_downstream() const;
Downstream* get_last_downstream();
int error_reply(int status_code); int error_reply(int status_code);
void pause_read(IOCtrlReason reason); void pause_read(IOCtrlReason reason);
@ -73,7 +70,7 @@ private:
ClientHandler *handler_; ClientHandler *handler_;
http_parser *htp_; http_parser *htp_;
size_t current_header_length_; size_t current_header_length_;
std::deque<Downstream*> downstream_queue_; Downstream *downstream_;
IOControl ioctrl_; IOControl ioctrl_;
}; };