2012-11-20 17:29:39 +01:00
|
|
|
/*
|
2013-07-12 17:19:03 +02:00
|
|
|
* nghttp2 - HTTP/2.0 C Library
|
2012-11-20 17:29:39 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-11-04 09:53:57 +01:00
|
|
|
#include "shrpx_http2_session.h"
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-01-10 16:11:41 +01:00
|
|
|
#include <netinet/tcp.h>
|
2012-11-20 17:29:39 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include <event2/bufferevent_ssl.h>
|
|
|
|
|
|
|
|
#include "shrpx_upstream.h"
|
|
|
|
#include "shrpx_downstream.h"
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_error.h"
|
2013-11-04 09:53:57 +01:00
|
|
|
#include "shrpx_http2_downstream_connection.h"
|
2012-11-20 17:29:39 +01:00
|
|
|
#include "shrpx_client_handler.h"
|
2012-11-22 13:46:15 +01:00
|
|
|
#include "shrpx_ssl.h"
|
2013-08-25 18:25:31 +02:00
|
|
|
#include "shrpx_http.h"
|
2013-08-27 19:47:22 +02:00
|
|
|
#include "http2.h"
|
2012-11-20 17:29:39 +01:00
|
|
|
#include "util.h"
|
2013-02-09 08:42:01 +01:00
|
|
|
#include "base64.h"
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
using namespace nghttp2;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
Http2Session::Http2Session(event_base *evbase, SSL_CTX *ssl_ctx)
|
2012-11-20 17:29:39 +01:00
|
|
|
: evbase_(evbase),
|
|
|
|
ssl_ctx_(ssl_ctx),
|
2013-07-26 12:33:25 +02:00
|
|
|
ssl_(nullptr),
|
|
|
|
session_(nullptr),
|
|
|
|
bev_(nullptr),
|
|
|
|
wrbev_(nullptr),
|
|
|
|
rdbev_(nullptr),
|
2013-12-06 15:17:38 +01:00
|
|
|
settings_timerev_(nullptr),
|
|
|
|
fd_(-1),
|
|
|
|
state_(DISCONNECTED),
|
|
|
|
notified_(false),
|
|
|
|
flow_control_(false)
|
2012-11-20 17:29:39 +01:00
|
|
|
{}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
Http2Session::~Http2Session()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::disconnect()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
SSLOG(INFO, this) << "Disconnecting";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_del(session_);
|
2013-09-24 14:34:04 +02:00
|
|
|
session_ = nullptr;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-10-31 13:45:17 +01:00
|
|
|
if(settings_timerev_) {
|
|
|
|
event_free(settings_timerev_);
|
|
|
|
settings_timerev_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
if(ssl_) {
|
2014-01-08 15:32:47 +01:00
|
|
|
SSL_set_shutdown(ssl_, SSL_RECEIVED_SHUTDOWN);
|
2012-11-20 17:29:39 +01:00
|
|
|
SSL_shutdown(ssl_);
|
|
|
|
}
|
|
|
|
if(bev_) {
|
2013-02-22 11:26:41 +01:00
|
|
|
int fd = bufferevent_getfd(bev_);
|
2012-11-20 17:29:39 +01:00
|
|
|
bufferevent_disable(bev_, EV_READ | EV_WRITE);
|
|
|
|
bufferevent_free(bev_);
|
2013-09-24 14:34:04 +02:00
|
|
|
bev_ = nullptr;
|
2013-02-22 13:54:07 +01:00
|
|
|
if(fd != -1) {
|
|
|
|
if(fd_ == -1) {
|
|
|
|
fd_ = fd;
|
|
|
|
} else if(fd != fd_) {
|
|
|
|
SSLOG(WARNING, this) << "fd in bev_ != fd_";
|
|
|
|
shutdown(fd, SHUT_WR);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
if(ssl_) {
|
|
|
|
SSL_free(ssl_);
|
|
|
|
}
|
2013-09-24 14:34:04 +02:00
|
|
|
ssl_ = nullptr;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
if(fd_ != -1) {
|
2013-02-22 13:54:07 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
SSLOG(INFO, this) << "Closing fd=" << fd_;
|
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
shutdown(fd_, SHUT_WR);
|
|
|
|
close(fd_);
|
|
|
|
fd_ = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(proxy_htp_) {
|
2013-09-23 17:19:53 +02:00
|
|
|
proxy_htp_.reset();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
notified_ = false;
|
|
|
|
state_ = DISCONNECTED;
|
|
|
|
|
|
|
|
// Delete all client handler associated to Downstream. When deleting
|
2013-11-04 09:53:57 +01:00
|
|
|
// Http2DownstreamConnection, it calls this object's
|
2013-02-08 13:46:58 +01:00
|
|
|
// remove_downstream_connection(). The multiple
|
2013-11-04 09:53:57 +01:00
|
|
|
// Http2DownstreamConnection objects belong to the same ClientHandler
|
2013-02-08 13:46:58 +01:00
|
|
|
// object. So first dump ClientHandler objects and delete them once
|
|
|
|
// and for all.
|
2013-11-04 09:53:57 +01:00
|
|
|
std::vector<Http2DownstreamConnection*> vec(dconns_.begin(), dconns_.end());
|
2013-02-08 13:46:58 +01:00
|
|
|
std::set<ClientHandler*> handlers;
|
2012-11-20 17:29:39 +01:00
|
|
|
for(size_t i = 0; i < vec.size(); ++i) {
|
2013-02-08 13:46:58 +01:00
|
|
|
handlers.insert(vec[i]->get_client_handler());
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-09-24 14:34:04 +02:00
|
|
|
for(auto& h : handlers) {
|
|
|
|
delete h;
|
2013-02-08 13:46:58 +01:00
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
dconns_.clear();
|
2013-09-24 14:34:04 +02:00
|
|
|
for(auto& s : streams_) {
|
|
|
|
delete s;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
streams_.clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void notify_readcb(bufferevent *bev, void *arg)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(arg);
|
|
|
|
http2session->clear_notify();
|
|
|
|
switch(http2session->get_state()) {
|
|
|
|
case Http2Session::DISCONNECTED:
|
|
|
|
rv = http2session->initiate_connection();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(FATAL, http2session)
|
2013-12-20 14:44:30 +01:00
|
|
|
<< "Could not initiate backend connection";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
break;
|
2013-11-04 09:53:57 +01:00
|
|
|
case Http2Session::CONNECTED:
|
|
|
|
rv = http2session->send();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void notify_eventcb(bufferevent *bev, short events, void *arg)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(arg);
|
2012-11-20 17:29:39 +01:00
|
|
|
// TODO should DIE()?
|
|
|
|
if(events & BEV_EVENT_EOF) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(ERROR, http2session) << "Notification connection lost: EOF";
|
2012-12-09 11:15:14 +01:00
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_TIMEOUT) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(ERROR, http2session) << "Notification connection lost: timeout";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_ERROR) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(ERROR, http2session) << "Notification connection lost: network error";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::init_notification()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int sockpair[2];
|
|
|
|
rv = socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair);
|
|
|
|
if(rv == -1) {
|
2013-02-25 14:43:44 +01:00
|
|
|
SSLOG(FATAL, this) << "socketpair() failed: errno=" << errno;
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wrbev_ = bufferevent_socket_new(evbase_, sockpair[0],
|
|
|
|
BEV_OPT_CLOSE_ON_FREE|
|
|
|
|
BEV_OPT_DEFER_CALLBACKS);
|
|
|
|
if(!wrbev_) {
|
2012-12-09 11:15:14 +01:00
|
|
|
SSLOG(FATAL, this) << "bufferevent_socket_new() failed";
|
2012-11-20 17:29:39 +01:00
|
|
|
for(int i = 0; i < 2; ++i) {
|
|
|
|
close(sockpair[i]);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rdbev_ = bufferevent_socket_new(evbase_, sockpair[1],
|
|
|
|
BEV_OPT_CLOSE_ON_FREE|
|
|
|
|
BEV_OPT_DEFER_CALLBACKS);
|
|
|
|
if(!rdbev_) {
|
2012-12-09 11:15:14 +01:00
|
|
|
SSLOG(FATAL, this) << "bufferevent_socket_new() failed";
|
2012-11-20 17:29:39 +01:00
|
|
|
close(sockpair[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bufferevent_enable(rdbev_, EV_READ);
|
2013-09-24 14:34:04 +02:00
|
|
|
bufferevent_setcb(rdbev_, notify_readcb, nullptr, notify_eventcb, this);
|
2012-11-20 17:29:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void readcb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(ptr);
|
|
|
|
rv = http2session->on_read();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void writecb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
2012-11-21 19:13:30 +01:00
|
|
|
if(evbuffer_get_length(bufferevent_get_output(bev)) > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(ptr);
|
|
|
|
rv = http2session->on_write();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void eventcb(bufferevent *bev, short events, void *ptr)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(ptr);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Connection established";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->set_state(Http2Session::CONNECTED);
|
2013-08-03 12:01:57 +02:00
|
|
|
if((!get_config()->downstream_no_tls &&
|
2013-11-04 09:53:57 +01:00
|
|
|
!get_config()->insecure && http2session->check_cert() != 0) ||
|
|
|
|
http2session->on_connect() != 0) {
|
|
|
|
http2session->disconnect();
|
2012-11-20 17:29:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-01-10 16:11:41 +01:00
|
|
|
int fd = bufferevent_getfd(bev);
|
|
|
|
int val = 1;
|
2013-01-25 14:00:33 +01:00
|
|
|
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
reinterpret_cast<char *>(&val), sizeof(val)) == -1) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(WARNING, http2session)
|
|
|
|
<< "Setting option TCP_NODELAY failed: errno=" << errno;
|
2013-01-25 14:00:33 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
} else if(events & BEV_EVENT_EOF) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "EOF";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2012-11-20 17:29:39 +01:00
|
|
|
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-09 11:15:14 +01:00
|
|
|
if(events & BEV_EVENT_ERROR) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Network error";
|
2012-12-09 11:15:14 +01:00
|
|
|
} else {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Timeout";
|
2012-12-09 11:15:14 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
namespace {
|
|
|
|
void proxy_readcb(bufferevent *bev, void *ptr)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(ptr);
|
|
|
|
if(http2session->on_read_proxy() == 0) {
|
|
|
|
switch(http2session->get_state()) {
|
|
|
|
case Http2Session::PROXY_CONNECTED:
|
2013-02-09 08:42:01 +01:00
|
|
|
// The current bufferevent is no longer necessary, so delete it
|
2013-02-22 11:26:41 +01:00
|
|
|
// here. But we keep fd_ inside it.
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->unwrap_free_bev();
|
2013-02-09 08:42:01 +01:00
|
|
|
// Initiate SSL/TLS handshake through established tunnel.
|
2013-11-04 09:53:57 +01:00
|
|
|
if(http2session->initiate_connection() != 0) {
|
|
|
|
http2session->disconnect();
|
2013-02-22 13:54:07 +01:00
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
break;
|
2013-11-04 09:53:57 +01:00
|
|
|
case Http2Session::PROXY_FAILED:
|
|
|
|
http2session->disconnect();
|
2013-02-09 08:42:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void proxy_eventcb(bufferevent *bev, short events, void *ptr)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(ptr);
|
2013-02-09 08:42:01 +01:00
|
|
|
if(events & BEV_EVENT_CONNECTED) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Connected to the proxy";
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
std::string req = "CONNECT ";
|
|
|
|
req += get_config()->downstream_hostport;
|
|
|
|
req += " HTTP/1.1\r\nHost: ";
|
|
|
|
req += get_config()->downstream_host;
|
|
|
|
req += "\r\n";
|
|
|
|
if(get_config()->downstream_http_proxy_userinfo) {
|
|
|
|
req += "Proxy-Authorization: Basic ";
|
|
|
|
size_t len = strlen(get_config()->downstream_http_proxy_userinfo);
|
|
|
|
req += base64::encode(get_config()->downstream_http_proxy_userinfo,
|
|
|
|
get_config()->downstream_http_proxy_userinfo+len);
|
|
|
|
req += "\r\n";
|
|
|
|
}
|
|
|
|
req += "\r\n";
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "HTTP proxy request headers\n" << req;
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
if(bufferevent_write(bev, req.c_str(), req.size()) != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(ERROR, http2session) << "bufferevent_write() failed";
|
|
|
|
http2session->disconnect();
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
} else if(events & BEV_EVENT_EOF) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Proxy EOF";
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2013-02-09 08:42:01 +01:00
|
|
|
} else if(events & (BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
if(events & BEV_EVENT_ERROR) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Network error";
|
2013-02-09 08:42:01 +01:00
|
|
|
} else {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Timeout";
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::check_cert()
|
2012-11-22 13:46:15 +01:00
|
|
|
{
|
|
|
|
return ssl::check_cert(ssl_);
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::initiate_connection()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-02-22 13:54:07 +01:00
|
|
|
int rv = 0;
|
2013-02-09 08:42:01 +01:00
|
|
|
if(get_config()->downstream_http_proxy_host && state_ == DISCONNECTED) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
SSLOG(INFO, this) << "Connecting to the proxy "
|
|
|
|
<< get_config()->downstream_http_proxy_host << ":"
|
|
|
|
<< get_config()->downstream_http_proxy_port;
|
|
|
|
}
|
|
|
|
bev_ = bufferevent_socket_new(evbase_, -1, BEV_OPT_DEFER_CALLBACKS);
|
2013-09-24 16:17:53 +02:00
|
|
|
if(!bev_) {
|
|
|
|
SSLOG(ERROR, this) << "bufferevent_socket_new() failed";
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
bufferevent_enable(bev_, EV_READ);
|
|
|
|
bufferevent_set_timeouts(bev_, &get_config()->downstream_read_timeout,
|
|
|
|
&get_config()->downstream_write_timeout);
|
|
|
|
|
|
|
|
// No need to set writecb because we write the request when
|
|
|
|
// connected at once.
|
2013-09-24 14:34:04 +02:00
|
|
|
bufferevent_setcb(bev_, proxy_readcb, nullptr, proxy_eventcb, this);
|
2013-02-09 08:42:01 +01:00
|
|
|
rv = bufferevent_socket_connect
|
|
|
|
(bev_,
|
|
|
|
const_cast<sockaddr*>(&get_config()->downstream_http_proxy_addr.sa),
|
|
|
|
get_config()->downstream_http_proxy_addrlen);
|
|
|
|
if(rv != 0) {
|
|
|
|
SSLOG(ERROR, this) << "Failed to connect to the proxy "
|
|
|
|
<< get_config()->downstream_http_proxy_host << ":"
|
|
|
|
<< get_config()->downstream_http_proxy_port;
|
|
|
|
bufferevent_free(bev_);
|
2013-09-24 14:34:04 +02:00
|
|
|
bev_ = nullptr;
|
2013-02-09 08:42:01 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2013-09-23 17:19:53 +02:00
|
|
|
proxy_htp_ = util::make_unique<http_parser>();
|
|
|
|
http_parser_init(proxy_htp_.get(), HTTP_RESPONSE);
|
2013-02-09 08:42:01 +01:00
|
|
|
proxy_htp_->data = this;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
state_ = PROXY_CONNECTING;
|
|
|
|
} else if(state_ == DISCONNECTED || state_ == PROXY_CONNECTED) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
SSLOG(INFO, this) << "Connecting to downstream server";
|
|
|
|
}
|
2013-02-22 13:54:07 +01:00
|
|
|
if(ssl_ctx_) {
|
|
|
|
// We are establishing TLS connection.
|
|
|
|
ssl_ = SSL_new(ssl_ctx_);
|
|
|
|
if(!ssl_) {
|
|
|
|
SSLOG(ERROR, this) << "SSL_new() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-22 13:51:11 +01:00
|
|
|
|
2013-09-24 14:34:04 +02:00
|
|
|
const char *sni_name = nullptr;
|
2013-03-29 14:06:33 +01:00
|
|
|
if ( get_config()->backend_tls_sni_name ) {
|
|
|
|
sni_name = get_config()->backend_tls_sni_name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sni_name = get_config()->downstream_host;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ssl::numeric_host(sni_name)) {
|
2013-02-22 13:54:07 +01:00
|
|
|
// TLS extensions: SNI. There is no documentation about the return
|
|
|
|
// code for this function (actually this is macro wrapping SSL_ctrl
|
|
|
|
// at the time of this writing).
|
2013-03-29 14:06:33 +01:00
|
|
|
SSL_set_tlsext_host_name(ssl_, sni_name);
|
2013-02-22 13:54:07 +01:00
|
|
|
}
|
|
|
|
// If state_ == PROXY_CONNECTED, we has connected to the proxy
|
|
|
|
// using fd_ and tunnel has been established.
|
|
|
|
bev_ = bufferevent_openssl_socket_new(evbase_, fd_, ssl_,
|
|
|
|
BUFFEREVENT_SSL_CONNECTING,
|
|
|
|
BEV_OPT_DEFER_CALLBACKS);
|
2013-09-24 16:17:53 +02:00
|
|
|
if(!bev_) {
|
|
|
|
SSLOG(ERROR, this) << "bufferevent_socket_new() failed";
|
|
|
|
SSL_free(ssl_);
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2013-02-22 13:54:07 +01:00
|
|
|
rv = bufferevent_socket_connect
|
|
|
|
(bev_,
|
|
|
|
// TODO maybe not thread-safe?
|
|
|
|
const_cast<sockaddr*>(&get_config()->downstream_addr.sa),
|
|
|
|
get_config()->downstream_addrlen);
|
|
|
|
} else if(state_ == DISCONNECTED) {
|
|
|
|
// Without TLS and proxy.
|
|
|
|
bev_ = bufferevent_socket_new(evbase_, -1, BEV_OPT_DEFER_CALLBACKS);
|
2013-09-24 16:17:53 +02:00
|
|
|
if(!bev_) {
|
|
|
|
SSLOG(ERROR, this) << "bufferevent_socket_new() failed";
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2013-02-22 13:54:07 +01:00
|
|
|
rv = bufferevent_socket_connect
|
|
|
|
(bev_,
|
|
|
|
const_cast<sockaddr*>(&get_config()->downstream_addr.sa),
|
|
|
|
get_config()->downstream_addrlen);
|
|
|
|
} else {
|
|
|
|
assert(state_ == PROXY_CONNECTED);
|
|
|
|
// Without TLS but with proxy.
|
|
|
|
bev_ = bufferevent_socket_new(evbase_, fd_, BEV_OPT_DEFER_CALLBACKS);
|
2013-09-24 16:17:53 +02:00
|
|
|
if(!bev_) {
|
|
|
|
SSLOG(ERROR, this) << "bufferevent_socket_new() failed";
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
2013-02-22 13:54:07 +01:00
|
|
|
// Connection already established.
|
|
|
|
eventcb(bev_, BEV_EVENT_CONNECTED, this);
|
|
|
|
// eventcb() has no return value. Check state_ to whether it was
|
|
|
|
// failed or not.
|
|
|
|
if(state_ == DISCONNECTED) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
|
|
|
if(rv != 0) {
|
|
|
|
bufferevent_free(bev_);
|
2013-09-24 14:34:04 +02:00
|
|
|
bev_ = nullptr;
|
2013-02-09 08:42:01 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WARTER_MARK);
|
|
|
|
bufferevent_enable(bev_, EV_READ);
|
|
|
|
bufferevent_setcb(bev_, readcb, writecb, eventcb, this);
|
2013-11-04 09:53:57 +01:00
|
|
|
// Set timeout for HTTP2 session
|
2013-11-04 09:22:52 +01:00
|
|
|
bufferevent_set_timeouts(bev_, &get_config()->downstream_read_timeout,
|
|
|
|
&get_config()->downstream_write_timeout);
|
2013-02-09 08:42:01 +01:00
|
|
|
|
2013-02-22 13:54:07 +01:00
|
|
|
// We have been already connected when no TLS and proxy is used.
|
|
|
|
if(state_ != CONNECTED) {
|
|
|
|
state_ = CONNECTING;
|
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
} else {
|
|
|
|
// Unreachable
|
|
|
|
DIE();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::unwrap_free_bev()
|
2013-02-09 08:42:01 +01:00
|
|
|
{
|
2013-02-22 11:26:41 +01:00
|
|
|
assert(fd_ == -1);
|
|
|
|
fd_ = bufferevent_getfd(bev_);
|
2013-02-09 08:42:01 +01:00
|
|
|
bufferevent_free(bev_);
|
2013-09-24 14:34:04 +02:00
|
|
|
bev_ = nullptr;
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
namespace {
|
|
|
|
int htp_hdrs_completecb(http_parser *htp)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(htp->data);
|
2013-02-09 08:42:01 +01:00
|
|
|
// We just check status code here
|
|
|
|
if(htp->status_code == 200) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Tunneling success";
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->set_state(Http2Session::PROXY_CONNECTED);
|
2013-02-09 08:42:01 +01:00
|
|
|
} else {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(WARNING, http2session) << "Tunneling failed";
|
|
|
|
http2session->set_state(Http2Session::PROXY_FAILED);
|
2013-02-09 08:42:01 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
http_parser_settings htp_hooks = {
|
2013-09-24 14:34:04 +02:00
|
|
|
nullptr, /*http_cb on_message_begin;*/
|
|
|
|
nullptr, /*http_data_cb on_url;*/
|
|
|
|
nullptr, /*http_cb on_status_complete */
|
|
|
|
nullptr, /*http_data_cb on_header_field;*/
|
|
|
|
nullptr, /*http_data_cb on_header_value;*/
|
2013-02-09 08:42:01 +01:00
|
|
|
htp_hdrs_completecb, /*http_cb on_headers_complete;*/
|
2013-09-24 14:34:04 +02:00
|
|
|
nullptr, /*http_data_cb on_body;*/
|
|
|
|
nullptr /*http_cb on_message_complete;*/
|
2013-02-09 08:42:01 +01:00
|
|
|
};
|
|
|
|
} // namespace
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::on_read_proxy()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-09-24 14:34:04 +02:00
|
|
|
auto input = bufferevent_get_input(bev_);
|
|
|
|
auto mem = evbuffer_pullup(input, -1);
|
2013-02-09 08:42:01 +01:00
|
|
|
|
2013-09-23 17:19:53 +02:00
|
|
|
size_t nread = http_parser_execute(proxy_htp_.get(), &htp_hooks,
|
2013-02-09 08:42:01 +01:00
|
|
|
reinterpret_cast<const char*>(mem),
|
|
|
|
evbuffer_get_length(input));
|
|
|
|
|
2013-12-20 15:28:54 +01:00
|
|
|
if(evbuffer_drain(input, nread) != 0) {
|
|
|
|
SSLOG(FATAL, this) << "evbuffer_drain() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-24 14:34:04 +02:00
|
|
|
auto htperr = HTTP_PARSER_ERRNO(proxy_htp_.get());
|
2013-02-09 08:42:01 +01:00
|
|
|
if(htperr == HPE_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::add_downstream_connection(Http2DownstreamConnection *dconn)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
dconns_.insert(dconn);
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::remove_downstream_connection
|
|
|
|
(Http2DownstreamConnection *dconn)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
dconns_.erase(dconn);
|
|
|
|
dconn->detach_stream_data();
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::remove_stream_data(StreamData *sd)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
streams_.erase(sd);
|
|
|
|
if(sd->dconn) {
|
|
|
|
sd->dconn->detach_stream_data();
|
|
|
|
}
|
|
|
|
delete sd;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::submit_request(Http2DownstreamConnection *dconn,
|
2013-11-28 13:36:04 +01:00
|
|
|
uint8_t pri,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
|
|
|
const nghttp2_data_provider *data_prd)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
assert(state_ == CONNECTED);
|
2013-09-24 14:34:04 +02:00
|
|
|
auto sd = util::make_unique<StreamData>();
|
2013-12-08 13:19:33 +01:00
|
|
|
int rv = nghttp2_submit_request(session_, pri, nva, nvlen,
|
|
|
|
data_prd, sd.get());
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv == 0) {
|
2013-09-24 14:34:04 +02:00
|
|
|
dconn->attach_stream_data(sd.get());
|
|
|
|
streams_.insert(sd.release());
|
2012-11-20 17:29:39 +01:00
|
|
|
} else {
|
2013-07-12 17:19:03 +02:00
|
|
|
SSLOG(FATAL, this) << "nghttp2_submit_request() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::submit_rst_stream(int32_t stream_id,
|
2013-07-26 12:33:25 +02:00
|
|
|
nghttp2_error_code error_code)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
assert(state_ == CONNECTED);
|
2013-09-29 17:13:04 +02:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
SSLOG(INFO, this) << "RST_STREAM stream_id="
|
|
|
|
<< stream_id
|
|
|
|
<< " with error_code="
|
|
|
|
<< error_code;
|
|
|
|
}
|
2013-10-25 15:50:24 +02:00
|
|
|
int rv = nghttp2_submit_rst_stream(session_, NGHTTP2_FLAG_NONE,
|
|
|
|
stream_id, error_code);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
SSLOG(FATAL, this) << "nghttp2_submit_rst_stream() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::submit_window_update(Http2DownstreamConnection *dconn,
|
2012-11-21 15:47:48 +01:00
|
|
|
int32_t amount)
|
|
|
|
{
|
|
|
|
assert(state_ == CONNECTED);
|
|
|
|
int rv;
|
2013-10-29 16:51:01 +01:00
|
|
|
int32_t stream_id;
|
|
|
|
if(dconn) {
|
|
|
|
stream_id = dconn->get_downstream()->get_downstream_stream_id();
|
|
|
|
} else {
|
|
|
|
stream_id = 0;
|
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE,
|
|
|
|
stream_id, amount);
|
2013-07-12 17:19:03 +02:00
|
|
|
if(rv < NGHTTP2_ERR_FATAL) {
|
|
|
|
SSLOG(FATAL, this) << "nghttp2_submit_window_update() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-21 15:47:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
nghttp2_session* Http2Session::get_session() const
|
2013-10-29 16:07:35 +01:00
|
|
|
{
|
2013-10-29 16:51:01 +01:00
|
|
|
return session_;
|
2013-10-29 16:07:35 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
bool Http2Session::get_flow_control() const
|
2012-11-21 15:47:48 +01:00
|
|
|
{
|
|
|
|
return flow_control_;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::resume_data(Http2DownstreamConnection *dconn)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
assert(state_ == CONNECTED);
|
2013-09-24 14:34:04 +02:00
|
|
|
auto downstream = dconn->get_downstream();
|
2013-07-12 17:19:03 +02:00
|
|
|
int rv = nghttp2_session_resume_data(session_,
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->get_downstream_stream_id());
|
|
|
|
switch(rv) {
|
|
|
|
case 0:
|
2013-07-12 17:19:03 +02:00
|
|
|
case NGHTTP2_ERR_INVALID_ARGUMENT:
|
2012-11-20 17:29:39 +01:00
|
|
|
return 0;
|
|
|
|
default:
|
2013-07-12 17:19:03 +02:00
|
|
|
SSLOG(FATAL, this) << "nghttp2_resume_session() failed: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2013-11-04 09:53:57 +01:00
|
|
|
void call_downstream_readcb(Http2Session *http2session, Downstream *downstream)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-09-24 14:34:04 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(upstream) {
|
|
|
|
(upstream->get_downstream_readcb())
|
2013-11-04 09:53:57 +01:00
|
|
|
(http2session->get_bev(),
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->get_downstream_connection());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
ssize_t send_callback(nghttp2_session *session,
|
2012-11-20 17:29:39 +01:00
|
|
|
const uint8_t *data, size_t len, int flags,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
|
|
|
auto bev = http2session->get_bev();
|
2013-09-24 14:34:04 +02:00
|
|
|
auto output = bufferevent_get_output(bev);
|
2012-11-20 17:29:39 +01:00
|
|
|
// Check buffer length and return WOULDBLOCK if it is large enough.
|
|
|
|
if(evbuffer_get_length(output) > Downstream::OUTPUT_UPPER_THRES) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_WOULDBLOCK;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = evbuffer_add(output, data, len);
|
|
|
|
if(rv == -1) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(FATAL, http2session) << "evbuffer_add() failed";
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-11-20 17:29:39 +01:00
|
|
|
} else {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-07-12 17:19:03 +02:00
|
|
|
ssize_t recv_callback(nghttp2_session *session,
|
2012-11-20 17:29:39 +01:00
|
|
|
uint8_t *data, size_t len, int flags, void *user_data)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
|
|
|
auto bev = http2session->get_bev();
|
2013-09-24 14:34:04 +02:00
|
|
|
auto input = bufferevent_get_input(bev);
|
2012-11-20 17:29:39 +01:00
|
|
|
int nread = evbuffer_remove(input, data, len);
|
|
|
|
if(nread == -1) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-11-20 17:29:39 +01:00
|
|
|
} else if(nread == 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_WOULDBLOCK;
|
2012-11-20 17:29:39 +01:00
|
|
|
} else {
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 15:58:05 +02:00
|
|
|
int on_stream_close_callback
|
2013-07-26 12:33:25 +02:00
|
|
|
(nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code,
|
2012-11-20 17:29:39 +01:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Stream stream_id=" << stream_id
|
|
|
|
<< " is being closed";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
2013-07-12 17:19:03 +02:00
|
|
|
(nghttp2_session_get_stream_user_data(session, stream_id));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(sd == 0) {
|
|
|
|
// We might get this close callback when pushed streams are
|
|
|
|
// closed.
|
2013-08-29 15:58:05 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto dconn = sd->dconn;
|
2012-11-20 17:29:39 +01:00
|
|
|
if(dconn) {
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = dconn->get_downstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(downstream && downstream->get_downstream_stream_id() == stream_id) {
|
2013-07-26 12:33:25 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
|
|
|
if(error_code == NGHTTP2_NO_ERROR) {
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
rv = upstream->on_downstream_body_complete(downstream);
|
|
|
|
if(rv != 0) {
|
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2012-11-20 17:29:39 +01:00
|
|
|
// dconn may be deleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The life time of StreamData ends here
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->remove_stream_data(sd);
|
2013-08-29 15:58:05 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-10-31 13:45:17 +01:00
|
|
|
namespace {
|
|
|
|
void settings_timeout_cb(evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(arg);
|
|
|
|
SSLOG(INFO, http2session) << "SETTINGS timeout";
|
2013-12-25 16:23:07 +01:00
|
|
|
if(http2session->terminate_session(NGHTTP2_SETTINGS_TIMEOUT) != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->disconnect();
|
2013-10-31 13:45:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
if(http2session->send() != 0) {
|
|
|
|
http2session->disconnect();
|
2013-10-31 13:45:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::start_settings_timer()
|
2013-10-31 13:45:17 +01:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
// We submit SETTINGS only once
|
|
|
|
if(settings_timerev_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
settings_timerev_ = evtimer_new(evbase_, settings_timeout_cb, this);
|
|
|
|
if(settings_timerev_ == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// SETTINGS ACK timeout is 10 seconds for now
|
|
|
|
timeval settings_timeout = { 10, 0 };
|
|
|
|
rv = evtimer_add(settings_timerev_, &settings_timeout);
|
|
|
|
if(rv == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::stop_settings_timer()
|
2013-10-31 13:45:17 +01:00
|
|
|
{
|
|
|
|
if(settings_timerev_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event_free(settings_timerev_);
|
|
|
|
settings_timerev_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
2013-08-29 14:03:39 +02:00
|
|
|
int on_frame_recv_callback
|
2013-09-03 14:24:14 +02:00
|
|
|
(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-07-26 12:33:25 +02:00
|
|
|
switch(frame->hd.type) {
|
|
|
|
case NGHTTP2_HEADERS: {
|
2014-01-09 15:47:21 +01:00
|
|
|
if(frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
// server sends request HEADERS
|
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id,
|
|
|
|
NGHTTP2_REFUSED_STREAM);
|
|
|
|
break;
|
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
if(frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
|
|
|
|
break;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
|
|
|
(nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!sd || !sd->dconn) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
2012-11-20 17:29:39 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = sd->dconn->get_downstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!downstream ||
|
2013-07-26 12:33:25 +02:00
|
|
|
downstream->get_downstream_stream_id() != frame->hd.stream_id) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
2012-11-20 17:29:39 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-11-13 15:56:02 +01:00
|
|
|
// nva is no longer sorted
|
|
|
|
auto nva = http2::sort_nva(frame->headers.nva, frame->headers.nvlen);
|
2013-08-27 17:09:46 +02:00
|
|
|
|
2013-11-13 15:56:02 +01:00
|
|
|
if(!http2::check_http2_headers(nva)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
2013-08-27 17:09:46 +02:00
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
2013-12-05 13:54:36 +01:00
|
|
|
for(auto& nv : nva) {
|
|
|
|
if(nv.namelen > 0 && nv.name[0] != ':') {
|
|
|
|
downstream->add_response_header(http2::name_to_str(&nv),
|
|
|
|
http2::value_to_str(&nv));
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 14:47:53 +02:00
|
|
|
|
2013-11-13 15:56:02 +01:00
|
|
|
auto status = http2::get_unique_header(nva, ":status");
|
2013-08-27 19:47:22 +02:00
|
|
|
if(!status || http2::value_lws(status)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-08-27 17:09:46 +02:00
|
|
|
downstream->set_response_http_status
|
2013-08-27 19:47:22 +02:00
|
|
|
(strtoul(http2::value_to_str(status).c_str(), nullptr, 10));
|
2013-08-27 17:09:46 +02:00
|
|
|
|
|
|
|
// Just assume it is HTTP/1.1. But we really consider to say 2.0
|
|
|
|
// here.
|
|
|
|
downstream->set_response_major(1);
|
|
|
|
downstream->set_response_minor(1);
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-11-13 15:56:02 +01:00
|
|
|
auto content_length = http2::get_header(nva, "content-length");
|
2013-08-27 17:09:46 +02:00
|
|
|
if(!content_length && downstream->get_request_method() != "HEAD" &&
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->get_request_method() != "CONNECT") {
|
|
|
|
unsigned int status;
|
|
|
|
status = downstream->get_response_http_status();
|
|
|
|
if(!((100 <= status && status <= 199) || status == 204 ||
|
|
|
|
status == 304)) {
|
2013-01-27 08:20:14 +01:00
|
|
|
// Here we have response body but Content-Length is not known
|
|
|
|
// in advance.
|
|
|
|
if(downstream->get_request_major() <= 0 ||
|
|
|
|
downstream->get_request_minor() <= 0) {
|
|
|
|
// We simply close connection for pre-HTTP/1.1 in this case.
|
|
|
|
downstream->set_response_connection_close(true);
|
|
|
|
} else {
|
|
|
|
// Otherwise, use chunked encoding to keep upstream
|
2013-11-04 09:53:57 +01:00
|
|
|
// connection open. In HTTP2, we are supporsed not to
|
2013-01-27 08:20:14 +01:00
|
|
|
// receive transfer-encoding.
|
|
|
|
downstream->add_response_header("transfer-encoding", "chunked");
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-11-20 17:29:39 +01:00
|
|
|
std::stringstream ss;
|
2013-12-05 13:54:36 +01:00
|
|
|
for(auto& nv : nva) {
|
2013-07-26 12:33:25 +02:00
|
|
|
ss << TTY_HTTP_HD;
|
2013-12-05 13:54:36 +01:00
|
|
|
ss.write(reinterpret_cast<char*>(nv.name), nv.namelen);
|
2013-07-26 12:33:25 +02:00
|
|
|
ss << TTY_RST << ": ";
|
2013-12-05 13:54:36 +01:00
|
|
|
ss.write(reinterpret_cast<char*>(nv.value), nv.valuelen);
|
2013-07-26 12:33:25 +02:00
|
|
|
ss << "\n";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "HTTP response headers. stream_id="
|
|
|
|
<< frame->hd.stream_id
|
|
|
|
<< "\n" << ss.str();
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::HEADER_COMPLETE);
|
2013-07-31 14:48:37 +02:00
|
|
|
downstream->check_upgrade_fulfilled();
|
|
|
|
if(downstream->get_upgraded()) {
|
2013-02-11 09:20:52 +01:00
|
|
|
downstream->set_response_connection_close(true);
|
2013-07-31 14:48:37 +02:00
|
|
|
// On upgrade sucess, both ends can send data
|
2013-07-31 15:14:25 +02:00
|
|
|
if(upstream->resume_read(SHRPX_MSG_BLOCK, downstream) != 0) {
|
|
|
|
// If resume_read fails, just drop connection. Not ideal.
|
|
|
|
delete upstream->get_client_handler();
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2013-07-31 15:14:25 +02:00
|
|
|
}
|
2013-07-31 14:48:37 +02:00
|
|
|
downstream->set_request_state(Downstream::HEADER_COMPLETE);
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "HTTP upgrade success. stream_id="
|
|
|
|
<< frame->hd.stream_id;
|
2013-07-31 14:48:37 +02:00
|
|
|
}
|
2013-08-23 17:28:50 +02:00
|
|
|
} else if(downstream->get_request_method() == "CONNECT") {
|
|
|
|
// If request is CONNECT, terminate request body to avoid for
|
|
|
|
// stream to stall.
|
|
|
|
downstream->end_upload_data();
|
2013-02-11 09:20:52 +01:00
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
rv = upstream->on_downstream_header_complete(downstream);
|
|
|
|
if(rv != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2012-11-20 17:29:39 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
case NGHTTP2_RST_STREAM: {
|
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
|
|
|
(nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
|
|
|
if(sd && sd->dconn) {
|
|
|
|
auto downstream = sd->dconn->get_downstream();
|
|
|
|
if(downstream &&
|
|
|
|
downstream->get_downstream_stream_id() == frame->hd.stream_id) {
|
2013-07-31 14:48:37 +02:00
|
|
|
if(downstream->get_upgraded() &&
|
2013-07-26 12:33:25 +02:00
|
|
|
downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
|
|
|
|
// For tunneled connection, we has to submit RST_STREAM to
|
|
|
|
// upstream *after* whole response body is sent. We just set
|
|
|
|
// MSG_COMPLETE here. Upstream will take care of that.
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "RST_STREAM against tunneled stream "
|
|
|
|
<< "stream_id="
|
|
|
|
<< frame->hd.stream_id;
|
2013-07-26 12:33:25 +02:00
|
|
|
}
|
|
|
|
downstream->get_upstream()->on_downstream_body_complete(downstream);
|
|
|
|
downstream->set_response_state(Downstream::MSG_COMPLETE);
|
|
|
|
} else {
|
|
|
|
// If we got RST_STREAM, just flag MSG_RESET to indicate
|
|
|
|
// upstream connection must be terminated.
|
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
|
|
|
}
|
|
|
|
downstream->set_response_rst_stream_error_code
|
|
|
|
(frame->rst_stream.error_code);
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2013-07-26 12:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-10-31 13:45:17 +01:00
|
|
|
case NGHTTP2_SETTINGS:
|
|
|
|
if((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->stop_settings_timer();
|
2013-10-31 13:45:17 +01:00
|
|
|
break;
|
2013-07-26 12:33:25 +02:00
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session)
|
2014-01-09 15:47:21 +01:00
|
|
|
<< "Received downstream PUSH_PROMISE stream_id=" << frame->hd.stream_id
|
|
|
|
<< ", promised_stream_id=" << frame->push_promise.promised_stream_id;
|
2013-07-26 12:33:25 +02:00
|
|
|
}
|
|
|
|
// We just respond with RST_STREAM.
|
2014-01-09 15:47:21 +01:00
|
|
|
http2session->submit_rst_stream(frame->push_promise.promised_stream_id,
|
2013-11-04 09:53:57 +01:00
|
|
|
NGHTTP2_REFUSED_STREAM);
|
2013-07-26 12:33:25 +02:00
|
|
|
break;
|
2012-11-20 17:29:39 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 14:18:40 +02:00
|
|
|
int on_data_chunk_recv_callback(nghttp2_session *session,
|
|
|
|
uint8_t flags, int32_t stream_id,
|
|
|
|
const uint8_t *data, size_t len,
|
|
|
|
void *user_data)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
int rv;
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-07-26 12:33:25 +02:00
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
2013-07-12 17:19:03 +02:00
|
|
|
(nghttp2_session_get_stream_user_data(session, stream_id));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!sd || !sd->dconn) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = sd->dconn->get_downstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!downstream || downstream->get_downstream_stream_id() != stream_id) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2012-11-21 15:47:48 +01:00
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
auto upstream = downstream->get_upstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
rv = upstream->on_downstream_body(downstream, data, len);
|
|
|
|
if(rv != 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(stream_id, NGHTTP2_INTERNAL_ERROR);
|
2012-11-20 17:29:39 +01:00
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 14:45:10 +02:00
|
|
|
int before_frame_send_callback(nghttp2_session *session,
|
2013-09-03 14:24:14 +02:00
|
|
|
const nghttp2_frame *frame,
|
2013-08-29 14:45:10 +02:00
|
|
|
void *user_data)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-07-26 12:33:25 +02:00
|
|
|
if(frame->hd.type == NGHTTP2_HEADERS &&
|
|
|
|
frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
|
|
|
(nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!sd || !sd->dconn) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id, NGHTTP2_CANCEL);
|
2013-08-29 14:45:10 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = sd->dconn->get_downstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(downstream) {
|
2013-07-26 12:33:25 +02:00
|
|
|
downstream->set_downstream_stream_id(frame->hd.stream_id);
|
2012-11-20 17:29:39 +01:00
|
|
|
} else {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->submit_rst_stream(frame->hd.stream_id, NGHTTP2_CANCEL);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 14:45:10 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-10-31 13:45:17 +01:00
|
|
|
namespace {
|
|
|
|
int on_frame_send_callback(nghttp2_session* session,
|
|
|
|
const nghttp2_frame *frame, void *user_data)
|
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-10-31 13:45:17 +01:00
|
|
|
if(frame->hd.type == NGHTTP2_SETTINGS &&
|
|
|
|
(frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
|
2013-11-04 09:53:57 +01:00
|
|
|
if(http2session->start_settings_timer() != 0) {
|
2013-10-31 13:45:17 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
namespace {
|
2013-08-29 14:51:58 +02:00
|
|
|
int on_frame_not_send_callback(nghttp2_session *session,
|
2013-09-03 14:24:14 +02:00
|
|
|
const nghttp2_frame *frame,
|
|
|
|
int lib_error_code, void *user_data)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
|
|
|
SSLOG(WARNING, http2session) << "Failed to send control frame type="
|
|
|
|
<< frame->hd.type << ", "
|
|
|
|
<< "lib_error_code=" << lib_error_code << ":"
|
|
|
|
<< nghttp2_strerror(lib_error_code);
|
2013-07-26 12:33:25 +02:00
|
|
|
if(frame->hd.type == NGHTTP2_HEADERS &&
|
|
|
|
frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
2012-11-20 17:29:39 +01:00
|
|
|
// To avoid stream hanging around, flag Downstream::MSG_RESET and
|
|
|
|
// terminate the upstream and downstream connections.
|
2013-07-26 12:33:25 +02:00
|
|
|
auto sd = reinterpret_cast<StreamData*>
|
|
|
|
(nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!sd) {
|
2013-08-29 14:51:58 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
if(sd->dconn) {
|
2013-07-26 12:33:25 +02:00
|
|
|
auto downstream = sd->dconn->get_downstream();
|
2012-11-20 17:29:39 +01:00
|
|
|
if(!downstream ||
|
2013-07-26 12:33:25 +02:00
|
|
|
downstream->get_downstream_stream_id() != frame->hd.stream_id) {
|
2013-08-29 14:51:58 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
downstream->set_response_state(Downstream::MSG_RESET);
|
2013-11-04 09:53:57 +01:00
|
|
|
call_downstream_readcb(http2session, downstream);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session->remove_stream_data(sd);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-08-29 14:51:58 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 16:07:07 +02:00
|
|
|
int on_frame_recv_parse_error_callback(nghttp2_session *session,
|
|
|
|
nghttp2_frame_type type,
|
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen, int lib_error_code,
|
|
|
|
void *user_data)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session)
|
|
|
|
<< "Failed to parse received control frame. type="
|
|
|
|
<< type
|
|
|
|
<< ", lib_error_code=" << lib_error_code << ":"
|
|
|
|
<< nghttp2_strerror(lib_error_code);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-08-29 16:07:07 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2013-08-29 16:10:18 +02:00
|
|
|
int on_unknown_frame_recv_callback(nghttp2_session *session,
|
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload, size_t payloadlen,
|
|
|
|
void *user_data)
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-11-04 09:53:57 +01:00
|
|
|
auto http2session = reinterpret_cast<Http2Session*>(user_data);
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-11-04 09:53:57 +01:00
|
|
|
SSLOG(INFO, http2session) << "Received unknown control frame";
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-08-29 16:10:18 +02:00
|
|
|
return 0;
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::on_connect()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
int rv;
|
2013-02-22 13:54:07 +01:00
|
|
|
if(ssl_ctx_) {
|
2014-01-01 15:54:28 +01:00
|
|
|
const unsigned char *next_proto = nullptr;
|
|
|
|
unsigned int next_proto_len;
|
2013-02-22 13:54:07 +01:00
|
|
|
SSL_get0_next_proto_negotiated(ssl_, &next_proto, &next_proto_len);
|
2014-01-01 15:54:28 +01:00
|
|
|
for(int i = 0; i < 2; ++i) {
|
|
|
|
if(next_proto) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
std::string proto(next_proto, next_proto+next_proto_len);
|
|
|
|
SSLOG(INFO, this) << "Negotiated next protocol: " << proto;
|
|
|
|
}
|
|
|
|
if(next_proto_len != NGHTTP2_PROTO_VERSION_ID_LEN ||
|
|
|
|
memcmp(NGHTTP2_PROTO_VERSION_ID, next_proto,
|
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
SSL_get0_alpn_selected(ssl_, &next_proto, &next_proto_len);
|
|
|
|
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
|
|
|
|
break;
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2013-02-22 13:54:07 +01:00
|
|
|
}
|
2014-01-01 15:54:28 +01:00
|
|
|
if(!next_proto) {
|
2013-02-22 13:54:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_session_callbacks callbacks;
|
2012-11-20 17:29:39 +01:00
|
|
|
memset(&callbacks, 0, sizeof(callbacks));
|
|
|
|
callbacks.send_callback = send_callback;
|
|
|
|
callbacks.recv_callback = recv_callback;
|
|
|
|
callbacks.on_stream_close_callback = on_stream_close_callback;
|
2013-07-26 12:33:25 +02:00
|
|
|
callbacks.on_frame_recv_callback = on_frame_recv_callback;
|
2012-11-20 17:29:39 +01:00
|
|
|
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
|
2013-07-26 12:33:25 +02:00
|
|
|
callbacks.before_frame_send_callback = before_frame_send_callback;
|
2013-10-31 13:45:17 +01:00
|
|
|
callbacks.on_frame_send_callback = on_frame_send_callback;
|
2013-07-26 12:33:25 +02:00
|
|
|
callbacks.on_frame_not_send_callback = on_frame_not_send_callback;
|
|
|
|
callbacks.on_frame_recv_parse_error_callback =
|
|
|
|
on_frame_recv_parse_error_callback;
|
|
|
|
callbacks.on_unknown_frame_recv_callback = on_unknown_frame_recv_callback;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2013-11-07 16:12:39 +01:00
|
|
|
nghttp2_opt_set opt_set;
|
|
|
|
opt_set.no_auto_stream_window_update = 1;
|
|
|
|
opt_set.no_auto_connection_window_update = 1;
|
|
|
|
uint32_t opt_set_mask =
|
|
|
|
NGHTTP2_OPT_NO_AUTO_STREAM_WINDOW_UPDATE |
|
|
|
|
NGHTTP2_OPT_NO_AUTO_CONNECTION_WINDOW_UPDATE;
|
|
|
|
rv = nghttp2_session_client_new2(&session_, &callbacks, this,
|
|
|
|
opt_set_mask, &opt_set);
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
flow_control_ = true;
|
2012-11-21 15:47:48 +01:00
|
|
|
|
2013-11-02 08:59:59 +01:00
|
|
|
nghttp2_settings_entry entry[3];
|
|
|
|
entry[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
|
|
|
|
entry[0].value = 0;
|
|
|
|
entry[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
|
2013-11-04 10:14:05 +01:00
|
|
|
entry[1].value = get_config()->http2_max_concurrent_streams;
|
2013-11-02 08:59:59 +01:00
|
|
|
|
|
|
|
entry[2].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
2013-11-12 03:08:43 +01:00
|
|
|
entry[2].value = (1 << get_config()->http2_downstream_window_bits) - 1;
|
2012-11-21 15:47:48 +01:00
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry,
|
|
|
|
sizeof(entry)/sizeof(nghttp2_settings_entry));
|
2012-11-20 17:29:39 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-26 12:33:25 +02:00
|
|
|
|
2013-11-20 16:15:17 +01:00
|
|
|
if(get_config()->http2_downstream_connection_window_bits > 16) {
|
|
|
|
int32_t delta =
|
|
|
|
(1 << get_config()->http2_downstream_connection_window_bits) - 1
|
|
|
|
- NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
|
|
|
|
rv = nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE, 0, delta);
|
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:28:54 +01:00
|
|
|
rv = bufferevent_write(bev_, NGHTTP2_CLIENT_CONNECTION_HEADER,
|
|
|
|
NGHTTP2_CLIENT_CONNECTION_HEADER_LEN);
|
|
|
|
if(rv != 0) {
|
|
|
|
SSLOG(FATAL, this) << "bufferevent_write() failed";
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-26 14:35:14 +02:00
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
rv = send();
|
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// submit pending request
|
2013-07-26 12:33:25 +02:00
|
|
|
for(auto dconn : dconns_) {
|
|
|
|
if(dconn->push_request_headers() != 0) {
|
2012-11-20 17:29:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::on_read()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
int rv = 0;
|
2013-07-12 17:19:03 +02:00
|
|
|
if((rv = nghttp2_session_recv(session_)) < 0) {
|
|
|
|
if(rv != NGHTTP2_ERR_EOF) {
|
|
|
|
SSLOG(ERROR, this) << "nghttp2_session_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
} else if((rv = nghttp2_session_send(session_)) < 0) {
|
|
|
|
SSLOG(ERROR, this) << "nghttp2_session_send() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2012-12-16 17:10:45 +01:00
|
|
|
if(rv == 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
if(nghttp2_session_want_read(session_) == 0 &&
|
2013-08-01 13:31:29 +02:00
|
|
|
nghttp2_session_want_write(session_) == 0 &&
|
|
|
|
evbuffer_get_length(bufferevent_get_output(bev_)) == 0) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-16 17:10:45 +01:00
|
|
|
SSLOG(INFO, this) << "No more read/write for this session";
|
|
|
|
}
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::on_write()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
return send();
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::send()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
int rv = 0;
|
2013-07-12 17:19:03 +02:00
|
|
|
if((rv = nghttp2_session_send(session_)) < 0) {
|
|
|
|
SSLOG(ERROR, this) << "nghttp2_session_send() returned error: "
|
|
|
|
<< nghttp2_strerror(rv);
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2012-12-16 17:10:45 +01:00
|
|
|
if(rv == 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
if(nghttp2_session_want_read(session_) == 0 &&
|
2013-08-01 13:31:29 +02:00
|
|
|
nghttp2_session_want_write(session_) == 0 &&
|
|
|
|
evbuffer_get_length(bufferevent_get_output(bev_)) == 0) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-12-16 17:10:45 +01:00
|
|
|
SSLOG(INFO, this) << "No more read/write for this session";
|
|
|
|
}
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::clear_notify()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
2013-09-24 14:34:04 +02:00
|
|
|
auto input = bufferevent_get_output(rdbev_);
|
2013-12-20 15:28:54 +01:00
|
|
|
if(evbuffer_drain(input, evbuffer_get_length(input)) != 0) {
|
|
|
|
SSLOG(FATAL, this) << "evbuffer_drain() failed";
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
notified_ = false;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::notify()
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
if(!notified_) {
|
2013-12-20 15:28:54 +01:00
|
|
|
if(bufferevent_write(wrbev_, "1", 1) != 0) {
|
|
|
|
SSLOG(FATAL, this) << "bufferevent_write failed";
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
notified_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
bufferevent* Http2Session::get_bev() const
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
return bev_;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
int Http2Session::get_state() const
|
2012-11-20 17:29:39 +01:00
|
|
|
{
|
|
|
|
return state_;
|
|
|
|
}
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
void Http2Session::set_state(int state)
|
2013-02-09 08:42:01 +01:00
|
|
|
{
|
|
|
|
state_ = state;
|
|
|
|
}
|
|
|
|
|
2013-12-25 16:23:07 +01:00
|
|
|
int Http2Session::terminate_session(nghttp2_error_code error_code)
|
2013-10-31 13:45:17 +01:00
|
|
|
{
|
|
|
|
int rv;
|
2013-12-25 16:23:07 +01:00
|
|
|
rv = nghttp2_session_terminate_session(session_, error_code);
|
2013-10-31 13:45:17 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
} // namespace shrpx
|