shrpx: Use raw pointer for downstream instead of deque
This commit is contained in:
parent
c78e0ca055
commit
5597ee68da
|
@ -48,6 +48,7 @@ HttpsUpstream::HttpsUpstream(ClientHandler *handler)
|
|||
: handler_(handler),
|
||||
htp_(new http_parser()),
|
||||
current_header_length_(0),
|
||||
downstream_(0),
|
||||
ioctrl_(handler->get_bev())
|
||||
{
|
||||
http_parser_init(htp_, HTTP_REQUEST);
|
||||
|
@ -57,10 +58,7 @@ HttpsUpstream::HttpsUpstream(ClientHandler *handler)
|
|||
HttpsUpstream::~HttpsUpstream()
|
||||
{
|
||||
delete htp_;
|
||||
for(std::deque<Downstream*>::iterator i = downstream_queue_.begin();
|
||||
i != downstream_queue_.end(); ++i) {
|
||||
delete *i;
|
||||
}
|
||||
delete downstream_;
|
||||
}
|
||||
|
||||
void HttpsUpstream::reset_current_header_length()
|
||||
|
@ -78,7 +76,7 @@ int htp_msg_begin(http_parser *htp)
|
|||
}
|
||||
upstream->reset_current_header_length();
|
||||
Downstream *downstream = new Downstream(upstream, 0, 0);
|
||||
upstream->add_downstream(downstream);
|
||||
upstream->attach_downstream(downstream);
|
||||
return 0;
|
||||
}
|
||||
} // namespace
|
||||
|
@ -88,7 +86,7 @@ int htp_uricb(http_parser *htp, const char *data, size_t len)
|
|||
{
|
||||
HttpsUpstream *upstream;
|
||||
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
|
||||
Downstream *downstream = upstream->get_last_downstream();
|
||||
Downstream *downstream = upstream->get_downstream();
|
||||
downstream->append_request_path(data, len);
|
||||
return 0;
|
||||
}
|
||||
|
@ -99,7 +97,7 @@ 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_last_downstream();
|
||||
Downstream *downstream = upstream->get_downstream();
|
||||
if(downstream->get_request_header_key_prev()) {
|
||||
downstream->append_last_request_header_key(data, len);
|
||||
} else {
|
||||
|
@ -114,7 +112,7 @@ 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_last_downstream();
|
||||
Downstream *downstream = upstream->get_downstream();
|
||||
if(downstream->get_request_header_key_prev()) {
|
||||
downstream->set_last_request_header_value(std::string(data, len));
|
||||
} else {
|
||||
|
@ -132,7 +130,7 @@ int htp_hdrs_completecb(http_parser *htp)
|
|||
if(ENABLE_LOG) {
|
||||
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_major(htp->http_major);
|
||||
|
@ -175,7 +173,7 @@ 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_last_downstream();
|
||||
Downstream *downstream = upstream->get_downstream();
|
||||
rv = downstream->push_upload_data_chunk
|
||||
(reinterpret_cast<const uint8_t*>(data), len);
|
||||
if(rv != 0) {
|
||||
|
@ -194,7 +192,7 @@ int htp_msg_completecb(http_parser *htp)
|
|||
}
|
||||
HttpsUpstream *upstream;
|
||||
upstream = reinterpret_cast<HttpsUpstream*>(htp->data);
|
||||
Downstream *downstream = upstream->get_last_downstream();
|
||||
Downstream *downstream = upstream->get_downstream();
|
||||
rv = downstream->end_upload_data();
|
||||
if(rv != 0) {
|
||||
return -1;
|
||||
|
@ -238,7 +236,7 @@ int HttpsUpstream::on_read()
|
|||
evbuffer_drain(input, nread);
|
||||
// Well, actually header length + some body bytes
|
||||
current_header_length_ += nread;
|
||||
Downstream *downstream = get_top_downstream();
|
||||
Downstream *downstream = get_downstream();
|
||||
http_errno htperr = HTTP_PARSER_ERRNO(htp_);
|
||||
if(htperr == HPE_PAUSED) {
|
||||
if(downstream->get_request_state() == Downstream::CONNECT_FAIL) {
|
||||
|
@ -252,8 +250,7 @@ int HttpsUpstream::on_read()
|
|||
if(downstream->get_downstream_connection() == 0) {
|
||||
// Error response already be sent
|
||||
assert(downstream->get_response_state() == Downstream::MSG_COMPLETE);
|
||||
pop_downstream();
|
||||
delete downstream;
|
||||
delete_downstream();
|
||||
} else {
|
||||
pause_read(SHRPX_MSG_BLOCK);
|
||||
}
|
||||
|
@ -296,7 +293,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr);
|
|||
|
||||
int HttpsUpstream::on_write()
|
||||
{
|
||||
Downstream *downstream = get_top_downstream();
|
||||
Downstream *downstream = get_downstream();
|
||||
if(downstream) {
|
||||
downstream->resume_read(SHRPX_NO_BUFFER);
|
||||
}
|
||||
|
@ -357,8 +354,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
|
|||
delete handler;
|
||||
return;
|
||||
} else {
|
||||
upstream->pop_downstream();
|
||||
delete downstream;
|
||||
upstream->delete_downstream();
|
||||
// Process next HTTP request
|
||||
upstream->resume_read(SHRPX_MSG_BLOCK);
|
||||
}
|
||||
|
@ -384,8 +380,7 @@ void https_downstream_readcb(bufferevent *bev, void *ptr)
|
|||
return;
|
||||
}
|
||||
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
|
||||
upstream->pop_downstream();
|
||||
delete downstream;
|
||||
upstream->delete_downstream();
|
||||
// Process next HTTP request
|
||||
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) {
|
||||
upstream->pop_downstream();
|
||||
delete downstream;
|
||||
upstream->delete_downstream();
|
||||
upstream->resume_read(SHRPX_MSG_BLOCK);
|
||||
}
|
||||
} 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) {
|
||||
upstream->pop_downstream();
|
||||
delete downstream;
|
||||
upstream->delete_downstream();
|
||||
upstream->resume_read(SHRPX_MSG_BLOCK);
|
||||
}
|
||||
}
|
||||
|
@ -500,7 +493,7 @@ int HttpsUpstream::error_reply(int status_code)
|
|||
LOG(FATAL) << "evbuffer_add() failed";
|
||||
return -1;
|
||||
}
|
||||
Downstream *downstream = get_top_downstream();
|
||||
Downstream *downstream = get_downstream();
|
||||
if(downstream) {
|
||||
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
||||
}
|
||||
|
@ -522,32 +515,21 @@ bufferevent_event_cb HttpsUpstream::get_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 0;
|
||||
} else {
|
||||
return downstream_queue_.front();
|
||||
}
|
||||
}
|
||||
|
||||
Downstream* HttpsUpstream::get_last_downstream()
|
||||
{
|
||||
if(downstream_queue_.empty()) {
|
||||
return 0;
|
||||
} else {
|
||||
return downstream_queue_.back();
|
||||
}
|
||||
return downstream_;
|
||||
}
|
||||
|
||||
int HttpsUpstream::on_downstream_header_complete(Downstream *downstream)
|
||||
|
|
|
@ -29,8 +29,6 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <deque>
|
||||
|
||||
extern "C" {
|
||||
#include "http-parser/http_parser.h"
|
||||
}
|
||||
|
@ -54,10 +52,9 @@ public:
|
|||
virtual bufferevent_data_cb get_downstream_readcb();
|
||||
virtual bufferevent_data_cb get_downstream_writecb();
|
||||
virtual bufferevent_event_cb get_downstream_eventcb();
|
||||
void add_downstream(Downstream *downstream);
|
||||
void pop_downstream();
|
||||
Downstream* get_top_downstream();
|
||||
Downstream* get_last_downstream();
|
||||
void attach_downstream(Downstream *downstream);
|
||||
void delete_downstream();
|
||||
Downstream* get_downstream() const;
|
||||
int error_reply(int status_code);
|
||||
|
||||
void pause_read(IOCtrlReason reason);
|
||||
|
@ -73,7 +70,7 @@ private:
|
|||
ClientHandler *handler_;
|
||||
http_parser *htp_;
|
||||
size_t current_header_length_;
|
||||
std::deque<Downstream*> downstream_queue_;
|
||||
Downstream *downstream_;
|
||||
IOControl ioctrl_;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue