2014-09-21 11:28:06 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 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.
|
|
|
|
*/
|
|
|
|
// We wrote this code based on the original code which has the
|
|
|
|
// following license:
|
|
|
|
//
|
|
|
|
// connection.hpp
|
|
|
|
// ~~~~~~~~~~~~~~
|
|
|
|
//
|
|
|
|
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
2015-03-05 13:42:48 +01:00
|
|
|
#ifndef ASIO_SERVER_CONNECTION_H
|
|
|
|
#define ASIO_SERVER_CONNECTION_H
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/array.hpp>
|
|
|
|
|
2015-03-03 18:43:13 +01:00
|
|
|
#include <nghttp2/asio_http2_server.h>
|
|
|
|
|
2015-03-05 13:42:48 +01:00
|
|
|
#include "asio_server_http2_handler.h"
|
2015-03-04 17:52:12 +01:00
|
|
|
#include "asio_server_serve_mux.h"
|
2014-09-21 11:28:06 +02:00
|
|
|
#include "util.h"
|
2015-06-21 07:32:47 +02:00
|
|
|
#include "template.h"
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
|
|
|
namespace asio_http2 {
|
|
|
|
|
|
|
|
namespace server {
|
|
|
|
|
|
|
|
/// Represents a single connection from a client.
|
2014-11-27 15:39:04 +01:00
|
|
|
template <typename socket_type>
|
|
|
|
class connection : public std::enable_shared_from_this<connection<socket_type>>,
|
|
|
|
private boost::noncopyable {
|
2014-09-21 11:28:06 +02:00
|
|
|
public:
|
|
|
|
/// Construct a connection with the given io_service.
|
2014-11-27 15:39:04 +01:00
|
|
|
template <typename... SocketArgs>
|
2015-12-21 16:33:12 +01:00
|
|
|
explicit connection(
|
|
|
|
serve_mux &mux,
|
|
|
|
const boost::posix_time::time_duration &tls_handshake_timeout,
|
|
|
|
const boost::posix_time::time_duration &read_timeout,
|
|
|
|
SocketArgs &&... args)
|
2016-01-27 13:14:07 +01:00
|
|
|
: socket_(std::forward<SocketArgs>(args)...),
|
|
|
|
mux_(mux),
|
2015-12-21 16:33:12 +01:00
|
|
|
deadline_(socket_.get_io_service()),
|
|
|
|
tls_handshake_timeout_(tls_handshake_timeout),
|
2016-01-27 13:14:07 +01:00
|
|
|
read_timeout_(read_timeout),
|
|
|
|
writing_(false),
|
|
|
|
stopped_(false) {}
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
/// Start the first asynchronous operation for the connection.
|
2014-11-27 15:39:04 +01:00
|
|
|
void start() {
|
2016-03-04 14:43:36 +01:00
|
|
|
boost::system::error_code ec;
|
|
|
|
|
2015-12-16 18:38:21 +01:00
|
|
|
handler_ = std::make_shared<http2_handler>(
|
2016-03-04 14:43:36 +01:00
|
|
|
socket_.get_io_service(), socket_.lowest_layer().remote_endpoint(ec),
|
2015-12-16 18:38:21 +01:00
|
|
|
[this]() { do_write(); }, mux_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (handler_->start() != 0) {
|
2015-12-21 16:33:12 +01:00
|
|
|
stop();
|
2014-09-21 11:28:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
socket_type &socket() { return socket_; }
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
void start_tls_handshake_deadline() {
|
|
|
|
deadline_.expires_from_now(tls_handshake_timeout_);
|
|
|
|
deadline_.async_wait(
|
|
|
|
std::bind(&connection::handle_deadline, this->shared_from_this()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_read_deadline() {
|
|
|
|
deadline_.expires_from_now(read_timeout_);
|
|
|
|
deadline_.async_wait(
|
|
|
|
std::bind(&connection::handle_deadline, this->shared_from_this()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_deadline() {
|
|
|
|
if (stopped_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deadline_.expires_at() <=
|
|
|
|
boost::asio::deadline_timer::traits_type::now()) {
|
|
|
|
stop();
|
|
|
|
deadline_.expires_at(boost::posix_time::pos_infin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
deadline_.async_wait(
|
|
|
|
std::bind(&connection::handle_deadline, this->shared_from_this()));
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void do_read() {
|
2014-09-21 11:28:06 +02:00
|
|
|
auto self = this->shared_from_this();
|
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
deadline_.expires_from_now(read_timeout_);
|
|
|
|
|
2015-11-12 16:53:29 +01:00
|
|
|
socket_.async_read_some(
|
|
|
|
boost::asio::buffer(buffer_),
|
|
|
|
[this, self](const boost::system::error_code &e,
|
|
|
|
std::size_t bytes_transferred) {
|
2015-12-21 16:33:12 +01:00
|
|
|
if (e) {
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-12 16:53:29 +01:00
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
if (handler_->on_read(buffer_, bytes_transferred) != 0) {
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-12 16:53:29 +01:00
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
do_write();
|
2015-11-12 16:53:29 +01:00
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
if (!writing_ && handler_->should_stop()) {
|
|
|
|
stop();
|
|
|
|
return;
|
2015-11-12 16:53:29 +01:00
|
|
|
}
|
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
do_read();
|
|
|
|
|
2015-11-12 16:53:29 +01:00
|
|
|
// If an error occurs then no new asynchronous operations are
|
|
|
|
// started. This means that all shared_ptr references to the
|
|
|
|
// connection object will disappear and the object will be
|
|
|
|
// destroyed automatically after this handler returns. The
|
|
|
|
// connection class's destructor closes the socket.
|
|
|
|
});
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void do_write() {
|
2014-09-21 11:28:06 +02:00
|
|
|
auto self = this->shared_from_this();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (writing_) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rv;
|
|
|
|
std::size_t nwrite;
|
|
|
|
|
|
|
|
rv = handler_->on_write(outbuf_, nwrite);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2015-12-21 16:33:12 +01:00
|
|
|
stop();
|
2014-09-21 11:28:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nwrite == 0) {
|
2015-12-21 16:33:12 +01:00
|
|
|
if (handler_->should_stop()) {
|
|
|
|
stop();
|
|
|
|
}
|
2014-09-21 11:28:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
writing_ = true;
|
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
// Reset read deadline here, because normally client is sending
|
|
|
|
// something, it does not expect timeout while doing it.
|
|
|
|
deadline_.expires_from_now(read_timeout_);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
boost::asio::async_write(
|
|
|
|
socket_, boost::asio::buffer(outbuf_, nwrite),
|
|
|
|
[this, self](const boost::system::error_code &e, std::size_t) {
|
2015-12-21 16:33:12 +01:00
|
|
|
if (e) {
|
|
|
|
stop();
|
|
|
|
return;
|
2014-11-27 15:39:04 +01:00
|
|
|
}
|
2015-12-21 16:33:12 +01:00
|
|
|
|
|
|
|
writing_ = false;
|
|
|
|
|
|
|
|
do_write();
|
2014-11-27 15:39:04 +01:00
|
|
|
});
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// No new asynchronous operations are started. This means that all
|
|
|
|
// shared_ptr references to the connection object will disappear and
|
|
|
|
// the object will be destroyed automatically after this handler
|
|
|
|
// returns. The connection class's destructor closes the socket.
|
|
|
|
}
|
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
void stop() {
|
|
|
|
if (stopped_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopped_ = true;
|
|
|
|
boost::system::error_code ignored_ec;
|
|
|
|
socket_.lowest_layer().close(ignored_ec);
|
|
|
|
deadline_.cancel();
|
|
|
|
}
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
private:
|
|
|
|
socket_type socket_;
|
|
|
|
|
2015-03-04 17:52:12 +01:00
|
|
|
serve_mux &mux_;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
std::shared_ptr<http2_handler> handler_;
|
|
|
|
|
|
|
|
/// Buffer for incoming data.
|
2015-06-21 07:32:47 +02:00
|
|
|
boost::array<uint8_t, 8_k> buffer_;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
boost::array<uint8_t, 64_k> outbuf_;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
boost::asio::deadline_timer deadline_;
|
|
|
|
boost::posix_time::time_duration tls_handshake_timeout_;
|
|
|
|
boost::posix_time::time_duration read_timeout_;
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
bool writing_;
|
2015-12-21 16:33:12 +01:00
|
|
|
bool stopped_;
|
2014-09-21 11:28:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace server
|
|
|
|
|
|
|
|
} // namespace asio_http2
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
2015-03-05 13:42:48 +01:00
|
|
|
#endif // ASIO_SERVER_CONNECTION_H
|