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_client_handler.h"
|
|
|
|
|
2012-08-21 17:14:02 +02:00
|
|
|
#include <unistd.h>
|
2012-07-15 14:15:28 +02:00
|
|
|
#include <cerrno>
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
#include "shrpx_upstream.h"
|
|
|
|
#include "shrpx_spdy_upstream.h"
|
|
|
|
#include "shrpx_https_upstream.h"
|
|
|
|
#include "shrpx_config.h"
|
2012-11-18 13:23:13 +01:00
|
|
|
#include "shrpx_http_downstream_connection.h"
|
|
|
|
#include "shrpx_spdy_downstream_connection.h"
|
2012-07-17 18:08:05 +02:00
|
|
|
#include "shrpx_accesslog.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void upstream_readcb(bufferevent *bev, void *arg)
|
|
|
|
{
|
|
|
|
ClientHandler *handler = reinterpret_cast<ClientHandler*>(arg);
|
|
|
|
int rv = handler->on_read();
|
|
|
|
if(rv != 0) {
|
2012-07-16 16:03:07 +02:00
|
|
|
LOG(WARNING) << "<upstream> Read operation (application level) failure";
|
2012-06-04 16:48:31 +02:00
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void upstream_writecb(bufferevent *bev, void *arg)
|
|
|
|
{
|
|
|
|
ClientHandler *handler = reinterpret_cast<ClientHandler*>(arg);
|
2012-06-05 18:26:04 +02:00
|
|
|
// We actually depend on write low-warter mark == 0.
|
2012-06-04 16:48:31 +02:00
|
|
|
if(handler->get_should_close_after_write()) {
|
|
|
|
delete handler;
|
2012-06-04 20:11:43 +02:00
|
|
|
} else {
|
|
|
|
Upstream *upstream = handler->get_upstream();
|
2012-07-16 16:03:07 +02:00
|
|
|
int rv = upstream->on_write();
|
|
|
|
if(rv != 0) {
|
|
|
|
LOG(WARNING) << "<upstream> Write operation (application level) failure";
|
|
|
|
delete handler;
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void upstream_eventcb(bufferevent *bev, short events, void *arg)
|
|
|
|
{
|
|
|
|
ClientHandler *handler = reinterpret_cast<ClientHandler*>(arg);
|
|
|
|
bool finish = false;
|
|
|
|
if(events & BEV_EVENT_EOF) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-07 17:36:19 +02:00
|
|
|
LOG(INFO) << "Upstream EOF";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_ERROR) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-07-15 14:15:28 +02:00
|
|
|
LOG(INFO) << "Upstream network error: "
|
|
|
|
<< evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR());
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_TIMEOUT) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream time out";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
if(finish) {
|
|
|
|
delete handler;
|
|
|
|
} else {
|
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream connected. handler " << handler;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
handler->set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
|
|
|
|
handler->validate_next_proto();
|
2012-07-14 16:24:03 +02:00
|
|
|
if(ENABLE_LOG) {
|
|
|
|
if(SSL_session_reused(handler->get_ssl())) {
|
|
|
|
LOG(INFO) << "SSL/TLS session reused";
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
// At this point, input buffer is already filled with some
|
|
|
|
// bytes. The read callback is not called until new data
|
|
|
|
// come. So consume input buffer here.
|
|
|
|
handler->get_upstream()->on_read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
ClientHandler::ClientHandler(bufferevent *bev, int fd, SSL *ssl,
|
|
|
|
const char *ipaddr)
|
2012-06-04 16:48:31 +02:00
|
|
|
: bev_(bev),
|
2012-11-18 13:23:13 +01:00
|
|
|
fd_(fd),
|
|
|
|
ssl_client_ctx_(0),
|
2012-06-04 16:48:31 +02:00
|
|
|
ssl_(ssl),
|
|
|
|
upstream_(0),
|
|
|
|
ipaddr_(ipaddr),
|
|
|
|
should_close_after_write_(false)
|
|
|
|
{
|
|
|
|
bufferevent_enable(bev_, EV_READ | EV_WRITE);
|
2012-06-09 16:14:00 +02:00
|
|
|
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WARTER_MARK);
|
2012-06-04 16:48:31 +02:00
|
|
|
set_upstream_timeouts(&get_config()->upstream_read_timeout,
|
|
|
|
&get_config()->upstream_write_timeout);
|
2012-11-18 13:23:13 +01:00
|
|
|
if(ssl_) {
|
|
|
|
set_bev_cb(0, upstream_writecb, upstream_eventcb);
|
|
|
|
} else {
|
|
|
|
// For client-mode
|
|
|
|
upstream_ = new HttpsUpstream(this);
|
|
|
|
set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientHandler::~ClientHandler()
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Deleting ClientHandler " << this;
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
if(ssl_) {
|
|
|
|
SSL_shutdown(ssl_);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
bufferevent_disable(bev_, EV_READ | EV_WRITE);
|
|
|
|
bufferevent_free(bev_);
|
2012-11-18 13:23:13 +01:00
|
|
|
if(ssl_) {
|
|
|
|
SSL_free(ssl_);
|
|
|
|
}
|
|
|
|
shutdown(fd_, SHUT_WR);
|
|
|
|
close(fd_);
|
2012-06-04 16:48:31 +02:00
|
|
|
delete upstream_;
|
2012-06-09 16:14:00 +02:00
|
|
|
for(std::set<DownstreamConnection*>::iterator i = dconn_pool_.begin();
|
|
|
|
i != dconn_pool_.end(); ++i) {
|
|
|
|
delete *i;
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Deleted";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Upstream* ClientHandler::get_upstream()
|
|
|
|
{
|
|
|
|
return upstream_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent* ClientHandler::get_bev() const
|
|
|
|
{
|
|
|
|
return bev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_base* ClientHandler::get_evbase() const
|
|
|
|
{
|
|
|
|
return bufferevent_get_base(bev_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::set_bev_cb
|
|
|
|
(bufferevent_data_cb readcb, bufferevent_data_cb writecb,
|
|
|
|
bufferevent_event_cb eventcb)
|
|
|
|
{
|
|
|
|
bufferevent_setcb(bev_, readcb, writecb, eventcb, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::set_upstream_timeouts(const timeval *read_timeout,
|
|
|
|
const timeval *write_timeout)
|
|
|
|
{
|
|
|
|
bufferevent_set_timeouts(bev_, read_timeout, write_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClientHandler::validate_next_proto()
|
|
|
|
{
|
|
|
|
const unsigned char *next_proto = 0;
|
|
|
|
unsigned int next_proto_len;
|
|
|
|
SSL_get0_next_proto_negotiated(ssl_, &next_proto, &next_proto_len);
|
|
|
|
if(next_proto) {
|
|
|
|
if(ENABLE_LOG) {
|
2012-11-18 13:23:13 +01:00
|
|
|
std::string proto(next_proto, next_proto+next_proto_len);
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "Upstream negotiated next protocol: " << proto;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
uint16_t version = spdylay_npn_get_version(next_proto, next_proto_len);
|
|
|
|
if(version) {
|
|
|
|
SpdyUpstream *spdy_upstream = new SpdyUpstream(version, this);
|
|
|
|
upstream_ = spdy_upstream;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(ENABLE_LOG) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(INFO) << "No proto negotiated.";
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Use HTTP/1.1";
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
HttpsUpstream *https_upstream = new HttpsUpstream(this);
|
|
|
|
upstream_ = https_upstream;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClientHandler::on_read()
|
|
|
|
{
|
|
|
|
return upstream_->on_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClientHandler::on_event()
|
|
|
|
{
|
|
|
|
return upstream_->on_event();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& ClientHandler::get_ipaddr() const
|
|
|
|
{
|
|
|
|
return ipaddr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientHandler::get_should_close_after_write() const
|
|
|
|
{
|
|
|
|
return should_close_after_write_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::set_should_close_after_write(bool f)
|
|
|
|
{
|
|
|
|
should_close_after_write_ = f;
|
|
|
|
}
|
|
|
|
|
2012-06-09 16:14:00 +02:00
|
|
|
void ClientHandler::pool_downstream_connection(DownstreamConnection *dconn)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Pooling downstream connection " << dconn;
|
|
|
|
}
|
|
|
|
dconn_pool_.insert(dconn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientHandler::remove_downstream_connection(DownstreamConnection *dconn)
|
|
|
|
{
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Removing downstream connection " << dconn
|
|
|
|
<< " from pool";
|
|
|
|
}
|
|
|
|
dconn_pool_.erase(dconn);
|
|
|
|
}
|
|
|
|
|
|
|
|
DownstreamConnection* ClientHandler::get_downstream_connection()
|
|
|
|
{
|
|
|
|
if(dconn_pool_.empty()) {
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Downstream connection pool is empty. Create new one";
|
|
|
|
}
|
2012-11-18 13:23:13 +01:00
|
|
|
if(get_config()->client_mode) {
|
|
|
|
return new SpdyDownstreamConnection(this);
|
|
|
|
} else {
|
|
|
|
return new HttpDownstreamConnection(this);
|
|
|
|
}
|
2012-06-09 16:14:00 +02:00
|
|
|
} else {
|
|
|
|
DownstreamConnection *dconn = *dconn_pool_.begin();
|
|
|
|
dconn_pool_.erase(dconn);
|
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Reuse downstream connection " << dconn
|
|
|
|
<< " from pool";
|
|
|
|
}
|
|
|
|
return dconn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 09:20:16 +02:00
|
|
|
size_t ClientHandler::get_pending_write_length()
|
|
|
|
{
|
|
|
|
evbuffer *output = bufferevent_get_output(bev_);
|
|
|
|
return evbuffer_get_length(output);
|
|
|
|
}
|
|
|
|
|
2012-07-14 16:24:03 +02:00
|
|
|
SSL* ClientHandler::get_ssl() const
|
|
|
|
{
|
|
|
|
return ssl_;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
void ClientHandler::set_ssl_client_ctx(SSL_CTX *ssl_ctx)
|
|
|
|
{
|
|
|
|
ssl_client_ctx_ = ssl_ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX* ClientHandler::get_ssl_client_ctx() const
|
|
|
|
{
|
|
|
|
return ssl_client_ctx_;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|