2012-06-04 16:48:31 +02:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_https_upstream.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "shrpx_client_handler.h"
|
|
|
|
#include "shrpx_downstream.h"
|
2012-06-09 16:14:00 +02:00
|
|
|
#include "shrpx_downstream_connection.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "shrpx_http.h"
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_error.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using namespace spdylay;
|
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
namespace {
|
2012-06-09 16:14:00 +02:00
|
|
|
const size_t SHRPX_HTTPS_UPSTREAM_OUTPUT_UPPER_THRES = 64*1024;
|
2012-06-05 15:13:22 +02:00
|
|
|
const size_t SHRPX_HTTPS_MAX_HEADER_LENGTH = 64*1024;
|
2012-06-04 20:11:43 +02:00
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
HttpsUpstream::HttpsUpstream(ClientHandler *handler)
|
|
|
|
: handler_(handler),
|
2012-06-04 20:11:43 +02:00
|
|
|
htp_(htparser_new()),
|
2012-06-05 15:13:22 +02:00
|
|
|
current_header_length_(0),
|
2012-06-04 20:11:43 +02:00
|
|
|
ioctrl_(handler->get_bev())
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
htparser_init(htp_, htp_type_request);
|
|
|
|
htparser_set_userdata(htp_, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpsUpstream::~HttpsUpstream()
|
|
|
|
{
|
|
|
|
free(htp_);
|
|
|
|
for(std::deque<Downstream*>::iterator i = downstream_queue_.begin();
|
|
|
|
i != downstream_queue_.end(); ++i) {
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-05 15:13:22 +02:00
|
|
|
void HttpsUpstream::reset_current_header_length()
|
|
|
|
{
|
|
|
|
current_header_length_ = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
|
|
|
int htp_msg_begin(htparser *htp)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream https request start";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
2012-06-05 15:13:22 +02:00
|
|
|
upstream->reset_current_header_length();
|
2012-06-09 16:14:00 +02:00
|
|
|
Downstream *downstream = new Downstream(upstream, 0, 0);
|
|
|
|
upstream->add_downstream(downstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_methodcb(htparser *htp, const char *data, size_t len)
|
|
|
|
{
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
downstream->set_request_method(std::string(data, len));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_uricb(htparser *htp, const char *data, size_t len)
|
|
|
|
{
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
downstream->set_request_path(std::string(data, len));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdrs_begincb(htparser *htp)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream https request headers start";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
|
|
|
|
int version = htparser_get_major(htp)*100 + htparser_get_minor(htp);
|
|
|
|
if(version < 101) {
|
|
|
|
downstream->set_request_connection_close(true);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdr_keycb(htparser *htp, const char *data, size_t len)
|
|
|
|
{
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
downstream->add_request_header(std::string(data, len), "");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdr_valcb(htparser *htp, const char *data, size_t len)
|
|
|
|
{
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
downstream->set_last_request_header_value(std::string(data, len));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_hdrs_completecb(htparser *htp)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream https request headers complete";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
|
2012-06-06 17:43:18 +02:00
|
|
|
downstream->set_request_major(htparser_get_major(htp));
|
|
|
|
downstream->set_request_minor(htparser_get_minor(htp));
|
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection *dconn;
|
|
|
|
dconn = upstream->get_client_handler()->get_downstream_connection();
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
int rv = dconn->attach_downstream(downstream);
|
|
|
|
if(rv != 0) {
|
|
|
|
downstream->set_request_state(Downstream::CONNECT_FAIL);
|
|
|
|
downstream->set_downstream_connection(0);
|
|
|
|
delete dconn;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
downstream->push_request_headers();
|
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
|
|
|
return 0;
|
2012-06-05 19:23:07 +02:00
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_bodycb(htparser *htp, const char *data, size_t len)
|
|
|
|
{
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
downstream->push_upload_data_chunk(reinterpret_cast<const uint8_t*>(data),
|
|
|
|
len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int htp_msg_completecb(htparser *htp)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream https request complete";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = reinterpret_cast<HttpsUpstream*>(htparser_get_userdata(htp));
|
|
|
|
Downstream *downstream = upstream->get_last_downstream();
|
|
|
|
downstream->end_upload_data();
|
|
|
|
downstream->set_request_state(Downstream::MSG_COMPLETE);
|
|
|
|
// Stop further processing to complete this request
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
htparse_hooks htp_hooks = {
|
|
|
|
htp_msg_begin, /*htparse_hook on_msg_begin;*/
|
|
|
|
htp_methodcb, /*htparse_data_hook method;*/
|
|
|
|
0, /* htparse_data_hook scheme;*/
|
|
|
|
0, /* htparse_data_hook host; */
|
|
|
|
0, /* htparse_data_hook port; */
|
|
|
|
0, /* htparse_data_hook path; */
|
|
|
|
0, /* htparse_data_hook args; */
|
|
|
|
htp_uricb, /* htparse_data_hook uri; */
|
|
|
|
htp_hdrs_begincb, /* htparse_hook on_hdrs_begin; */
|
|
|
|
htp_hdr_keycb, /* htparse_data_hook hdr_key; */
|
|
|
|
htp_hdr_valcb, /* htparse_data_hook hdr_val; */
|
|
|
|
htp_hdrs_completecb, /* htparse_hook on_hdrs_complete; */
|
|
|
|
0, /*htparse_hook on_new_chunk;*/
|
|
|
|
0, /*htparse_hook on_chunk_complete;*/
|
|
|
|
0, /*htparse_hook on_chunks_complete;*/
|
|
|
|
htp_bodycb, /* htparse_data_hook body; */
|
|
|
|
htp_msg_completecb /* htparse_hook on_msg_complete;*/
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// on_read() does not consume all available data in input buffer if
|
|
|
|
// one http request is fully received.
|
|
|
|
int HttpsUpstream::on_read()
|
|
|
|
{
|
|
|
|
bufferevent *bev = handler_->get_bev();
|
|
|
|
evbuffer *input = bufferevent_get_input(bev);
|
|
|
|
unsigned char *mem = evbuffer_pullup(input, -1);
|
|
|
|
int nread = htparser_run(htp_, &htp_hooks,
|
|
|
|
reinterpret_cast<const char*>(mem),
|
|
|
|
evbuffer_get_length(input));
|
|
|
|
evbuffer_drain(input, nread);
|
2012-06-05 15:13:22 +02:00
|
|
|
// Well, actually header length + some body bytes
|
|
|
|
current_header_length_ += nread;
|
2012-06-04 16:48:31 +02:00
|
|
|
htpparse_error htperr = htparser_get_error(htp_);
|
2012-06-09 17:30:44 +02:00
|
|
|
Downstream *downstream = get_top_downstream();
|
2012-06-04 16:48:31 +02:00
|
|
|
if(htperr == htparse_error_user) {
|
2012-06-09 16:14:00 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::CONNECT_FAIL) {
|
2012-06-05 19:23:07 +02:00
|
|
|
get_client_handler()->set_should_close_after_write(true);
|
|
|
|
error_reply(503);
|
2012-06-09 16:14:00 +02:00
|
|
|
// Downstream gets deleted after response body is read.
|
|
|
|
} else {
|
|
|
|
assert(downstream->get_request_state() == Downstream::MSG_COMPLETE);
|
|
|
|
if(downstream->get_downstream_connection() == 0) {
|
|
|
|
// Error response already be sent
|
|
|
|
assert(downstream->get_response_state() == Downstream::MSG_COMPLETE);
|
|
|
|
pop_downstream();
|
|
|
|
delete downstream;
|
|
|
|
} else {
|
|
|
|
pause_read(SHRPX_MSG_BLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(htperr == htparse_error_none) {
|
2012-06-09 17:30:44 +02:00
|
|
|
// downstream can be NULL here.
|
|
|
|
if(downstream && downstream->get_request_state() == Downstream::INITIAL &&
|
|
|
|
current_header_length_ > SHRPX_HTTPS_MAX_HEADER_LENGTH) {
|
2012-06-05 15:13:22 +02:00
|
|
|
LOG(WARNING) << "Request Header too long:" << current_header_length_
|
|
|
|
<< " bytes";
|
|
|
|
get_client_handler()->set_should_close_after_write(true);
|
|
|
|
error_reply(400);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-09 16:14:00 +02:00
|
|
|
} else {
|
2012-06-04 16:48:31 +02:00
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream http parse failure: "
|
2012-06-04 16:48:31 +02:00
|
|
|
<< htparser_get_strerror(htp_);
|
|
|
|
}
|
2012-06-05 15:13:22 +02:00
|
|
|
get_client_handler()->set_should_close_after_write(true);
|
|
|
|
error_reply(400);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
namespace {
|
|
|
|
void https_downstream_readcb(bufferevent *bev, void *ptr);
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
int HttpsUpstream::on_write()
|
|
|
|
{
|
|
|
|
Downstream *downstream = get_top_downstream();
|
|
|
|
if(downstream) {
|
|
|
|
downstream->resume_read(SHRPX_NO_BUFFER);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
int HttpsUpstream::on_event()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientHandler* HttpsUpstream::get_client_handler() const
|
|
|
|
{
|
|
|
|
return handler_;
|
|
|
|
}
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
void HttpsUpstream::pause_read(IOCtrlReason reason)
|
|
|
|
{
|
|
|
|
ioctrl_.pause_read(reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpsUpstream::resume_read(IOCtrlReason reason)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-06-04 20:11:43 +02:00
|
|
|
if(ioctrl_.resume_read(reason)) {
|
|
|
|
// Process remaining data in input buffer here because these bytes
|
|
|
|
// are not notified by readcb until new data arrive.
|
|
|
|
on_read();
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void https_downstream_readcb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
Downstream *downstream = dconn->get_downstream();
|
2012-06-04 16:48:31 +02:00
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
|
|
|
|
int rv = downstream->parse_http_response();
|
|
|
|
if(rv == 0) {
|
|
|
|
if(downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
2012-06-07 16:42:11 +02:00
|
|
|
if(downstream->get_response_connection_close()) {
|
2012-06-09 16:14:00 +02:00
|
|
|
// 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) {
|
2012-06-07 16:42:11 +02:00
|
|
|
upstream->pop_downstream();
|
|
|
|
delete downstream;
|
2012-06-09 16:14:00 +02:00
|
|
|
// Process next HTTP request
|
|
|
|
upstream->resume_read(SHRPX_MSG_BLOCK);
|
2012-06-07 16:42:11 +02:00
|
|
|
}
|
2012-06-04 20:11:43 +02:00
|
|
|
} else {
|
|
|
|
ClientHandler *handler = upstream->get_client_handler();
|
|
|
|
bufferevent *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);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
2012-06-09 16:14:00 +02:00
|
|
|
// We already sent HTTP response headers to upstream
|
|
|
|
// client. Just close the upstream connection.
|
2012-06-04 16:48:31 +02:00
|
|
|
delete upstream->get_client_handler();
|
|
|
|
} else {
|
2012-06-09 16:14:00 +02:00
|
|
|
// We did not sent any HTTP response, so sent error
|
|
|
|
// response. Cannot reuse downstream connection in this case.
|
2012-06-05 15:13:22 +02:00
|
|
|
upstream->error_reply(502);
|
2012-06-09 16:14:00 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
upstream->pop_downstream();
|
|
|
|
delete downstream;
|
|
|
|
// Process next HTTP request
|
|
|
|
upstream->resume_read(SHRPX_MSG_BLOCK);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void https_downstream_writecb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void https_downstream_eventcb(bufferevent *bev, short events, void *ptr)
|
|
|
|
{
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection *dconn = reinterpret_cast<DownstreamConnection*>(ptr);
|
|
|
|
Downstream *downstream = dconn->get_downstream();
|
2012-06-04 16:48:31 +02:00
|
|
|
HttpsUpstream *upstream;
|
|
|
|
upstream = static_cast<HttpsUpstream*>(downstream->get_upstream());
|
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Downstream connection established. downstream "
|
|
|
|
<< downstream;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-07 16:42:11 +02:00
|
|
|
} else if(events & BEV_EVENT_EOF) {
|
2012-06-04 16:48:31 +02:00
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Downstream EOF. stream_id="
|
2012-06-04 16:48:31 +02:00
|
|
|
<< downstream->get_stream_id();
|
|
|
|
}
|
2012-06-07 17:39:55 +02:00
|
|
|
if(downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
|
|
|
// Server may indicate the end of the request by EOF
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Assuming downstream content-length is 0 byte";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-07 17:39:55 +02:00
|
|
|
upstream->on_downstream_body_complete(downstream);
|
|
|
|
//downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
} else if(downstream->get_response_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
// Nothing to do
|
|
|
|
} else {
|
|
|
|
// error
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Treated as downstream error";
|
|
|
|
}
|
|
|
|
upstream->error_reply(502);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-09 16:14:00 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
upstream->pop_downstream();
|
|
|
|
delete downstream;
|
|
|
|
upstream->resume_read(SHRPX_MSG_BLOCK);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Downstream error/timeout. " << downstream;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
if(downstream->get_response_state() == Downstream::INITIAL) {
|
|
|
|
int status;
|
|
|
|
if(events & BEV_EVENT_TIMEOUT) {
|
|
|
|
status = 504;
|
|
|
|
} else {
|
|
|
|
status = 502;
|
|
|
|
}
|
2012-06-05 15:13:22 +02:00
|
|
|
upstream->error_reply(status);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-09 16:14:00 +02:00
|
|
|
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
|
|
|
|
upstream->pop_downstream();
|
|
|
|
delete downstream;
|
|
|
|
upstream->resume_read(SHRPX_MSG_BLOCK);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-05 15:13:22 +02:00
|
|
|
void HttpsUpstream::error_reply(int status_code)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
std::string html = http::create_error_html(status_code);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "HTTP/1.1 " << http::get_status_string(status_code) << "\r\n"
|
|
|
|
<< "Server: " << get_config()->server_name << "\r\n"
|
|
|
|
<< "Content-Length: " << html.size() << "\r\n"
|
2012-06-05 15:13:22 +02:00
|
|
|
<< "Content-Type: " << "text/html; charset=UTF-8\r\n";
|
|
|
|
if(get_client_handler()->get_should_close_after_write()) {
|
|
|
|
ss << "Connection: close\r\n";
|
|
|
|
}
|
|
|
|
ss << "\r\n";
|
2012-06-04 16:48:31 +02:00
|
|
|
std::string header = ss.str();
|
|
|
|
evbuffer *output = bufferevent_get_output(handler_->get_bev());
|
|
|
|
evbuffer_add(output, header.c_str(), header.size());
|
|
|
|
evbuffer_add(output, html.c_str(), html.size());
|
2012-06-09 16:14:00 +02:00
|
|
|
Downstream *downstream = get_top_downstream();
|
|
|
|
if(downstream) {
|
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_data_cb HttpsUpstream::get_downstream_readcb()
|
|
|
|
{
|
|
|
|
return https_downstream_readcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_data_cb HttpsUpstream::get_downstream_writecb()
|
|
|
|
{
|
|
|
|
return https_downstream_writecb;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_event_cb HttpsUpstream::get_downstream_eventcb()
|
|
|
|
{
|
|
|
|
return https_downstream_eventcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpsUpstream::add_downstream(Downstream *downstream)
|
|
|
|
{
|
|
|
|
downstream_queue_.push_back(downstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpsUpstream::pop_downstream()
|
|
|
|
{
|
|
|
|
downstream_queue_.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
Downstream* HttpsUpstream::get_top_downstream()
|
|
|
|
{
|
2012-06-04 20:11:43 +02:00
|
|
|
if(downstream_queue_.empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return downstream_queue_.front();
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Downstream* HttpsUpstream::get_last_downstream()
|
|
|
|
{
|
2012-06-04 20:11:43 +02:00
|
|
|
if(downstream_queue_.empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return downstream_queue_.back();
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int HttpsUpstream::on_downstream_header_complete(Downstream *downstream)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Downstream on_downstream_header_complete";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-06 17:43:18 +02:00
|
|
|
std::string via_value;
|
2012-06-04 16:48:31 +02:00
|
|
|
std::string hdrs = "HTTP/1.1 ";
|
|
|
|
hdrs += http::get_status_string(downstream->get_response_http_status());
|
|
|
|
hdrs += "\r\n";
|
|
|
|
for(Headers::const_iterator i = downstream->get_response_headers().begin();
|
|
|
|
i != downstream->get_response_headers().end(); ++i) {
|
|
|
|
if(util::strieq((*i).first.c_str(), "keep-alive") || // HTTP/1.0?
|
|
|
|
util::strieq((*i).first.c_str(), "connection") ||
|
|
|
|
util:: strieq((*i).first.c_str(), "proxy-connection")) {
|
|
|
|
// These are ignored
|
2012-06-06 17:43:18 +02:00
|
|
|
} else if(util::strieq((*i).first.c_str(), "via")) {
|
|
|
|
via_value = (*i).second;
|
2012-06-04 16:48:31 +02:00
|
|
|
} else {
|
2012-06-06 17:43:18 +02:00
|
|
|
hdrs += (*i).first;
|
|
|
|
hdrs += ": ";
|
|
|
|
hdrs += (*i).second;
|
2012-06-04 16:48:31 +02:00
|
|
|
hdrs += "\r\n";
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 15:13:22 +02:00
|
|
|
if(get_client_handler()->get_should_close_after_write()) {
|
2012-06-04 16:48:31 +02:00
|
|
|
hdrs += "Connection: close\r\n";
|
2012-06-07 15:40:42 +02:00
|
|
|
} else if(downstream->get_request_major() == 1 &&
|
|
|
|
downstream->get_request_minor() == 0) {
|
|
|
|
hdrs += "Connection: Keep-Alive\r\n";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-06 17:43:18 +02:00
|
|
|
hdrs += "Via: ";
|
|
|
|
hdrs += via_value;
|
|
|
|
if(!via_value.empty()) {
|
|
|
|
hdrs += ", ";
|
|
|
|
}
|
|
|
|
hdrs += http::create_via_header_value
|
|
|
|
(downstream->get_response_major(), downstream->get_response_minor());
|
|
|
|
hdrs += "\r\n";
|
2012-06-04 16:48:31 +02:00
|
|
|
hdrs += "\r\n";
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream https response headers\n" << hdrs;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
evbuffer *output = bufferevent_get_output(handler_->get_bev());
|
|
|
|
evbuffer_add(output, hdrs.c_str(), hdrs.size());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpsUpstream::on_downstream_body(Downstream *downstream,
|
|
|
|
const uint8_t *data, size_t len)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
evbuffer *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",
|
|
|
|
static_cast<unsigned int>(len));
|
|
|
|
evbuffer_add(output, chunk_size_hex, rv);
|
|
|
|
}
|
|
|
|
evbuffer_add(output, data, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpsUpstream::on_downstream_body_complete(Downstream *downstream)
|
|
|
|
{
|
|
|
|
if(downstream->get_chunked_response()) {
|
|
|
|
evbuffer *output = bufferevent_get_output(handler_->get_bev());
|
|
|
|
evbuffer_add(output, "0\r\n\r\n", 5);
|
|
|
|
}
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Downstream on_downstream_body_complete";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
if(downstream->get_request_connection_close()) {
|
|
|
|
ClientHandler *handler = downstream->get_upstream()->get_client_handler();
|
|
|
|
handler->set_should_close_after_write(true);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|