diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-10 19:35:06 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-10 19:35:06 +0200 |
commit | 07f01d1ab5e68fc042356fd90fa07c199791b29c (patch) | |
tree | 89860e4e85ee49931b4193255de0a2032d94392e /https.cpp | |
parent | da2666726e48a3dc00f05589cdf4947f22deb3c3 (diff) |
Ported to Debian 10
Diffstat (limited to 'https.cpp')
-rw-r--r-- | https.cpp | 145 |
1 files changed, 137 insertions, 8 deletions
@@ -10,10 +10,15 @@ #include <boost/asio/buffer.hpp> #include <boost/beast/core.hpp> #include <boost/beast/http.hpp> -#include <boost/beast/version.hpp> -#include <boost/beast/ssl.hpp> #include <boost/asio/dispatch.hpp> #include <boost/asio/ssl/context.hpp> +#ifdef BOOST_LATEST +#include <boost/beast/ssl.hpp> +#else +#include <boost/asio/ip/tcp.hpp> +#include <boost/asio/ssl/stream.hpp> +#include <boost/asio/bind_executor.hpp> +#endif #include <boost/asio/strand.hpp> #include <boost/config.hpp> @@ -39,8 +44,15 @@ namespace { // Report a failure void -fail(beast::error_code ec, char const* what) +fail( +#ifdef BOOST_LATEST + beast::error_code ec, +#else + boost::system::error_code ec, +#endif + char const* what) { +#ifdef BOOST_LATEST // ssl::error::stream_truncated, also known as an SSL "short read", // indicates the peer closed the connection without performing the // required closing handshake (for example, Google does this to @@ -60,6 +72,7 @@ fail(beast::error_code ec, char const* what) if(ec == net::ssl::error::stream_truncated) return; +#endif std::cerr << what << ": " << ec.message() << "\n"; } @@ -67,7 +80,13 @@ fail(beast::error_code ec, char const* what) // Handles an HTTP server connection class session : public std::enable_shared_from_this<session> { +#ifdef BOOST_LATEST beast::ssl_stream<beast::tcp_stream> stream_; +#else + tcp::socket socket_; + ssl::stream<tcp::socket&> stream_; + boost::asio::strand<boost::asio::io_context::executor_type> strand_; +#endif beast::flat_buffer buffer_; Server& m_server; http::request<http::string_body> req_; @@ -80,6 +99,7 @@ class session : public std::enable_shared_from_this<session> res_ = sp; // Write the response +#ifdef BOOST_LATEST http::async_write( stream_, *sp, @@ -87,15 +107,38 @@ class session : public std::enable_shared_from_this<session> &session::on_write, shared_from_this(), sp->need_eof())); +#else + http::async_write( + stream_, + *sp, + boost::asio::bind_executor( + strand_, + std::bind( + &session::on_write, + shared_from_this(), + std::placeholders::_1, + std::placeholders::_2, + sp->need_eof()))); +#endif } public: // Take ownership of the socket explicit session( +#ifdef BOOST_LATEST tcp::socket&& socket, +#else + tcp::socket socket, +#endif ssl::context& ctx, Server& server) +#ifdef BOOST_LATEST : stream_(std::move(socket), ctx) +#else + : socket_(std::move(socket)) + , stream_(socket_, ctx) + , strand_(socket_.get_executor()) +#endif , m_server(server) { } @@ -104,6 +147,7 @@ public: void run() { +#ifdef BOOST_LATEST // We need to be executing within a strand to perform async operations // on the I/O objects in this session. net::dispatch( @@ -111,8 +155,19 @@ public: beast::bind_front_handler( &session::on_run, shared_from_this())); +#else + stream_.async_handshake( + ssl::stream_base::server, + boost::asio::bind_executor( + strand_, + std::bind( + &session::on_handshake, + shared_from_this(), + std::placeholders::_1))); +#endif } +#ifdef BOOST_LATEST void on_run() { @@ -127,9 +182,16 @@ public: &session::on_handshake, shared_from_this())); } +#endif void - on_handshake(beast::error_code ec) + on_handshake( +#ifdef BOOST_LATEST + beast::error_code ec +#else + boost::system::error_code ec +#endif + ) { if(ec) return fail(ec, "handshake"); @@ -144,6 +206,7 @@ public: // otherwise the operation behavior is undefined. req_ = {}; +#ifdef BOOST_LATEST // Set the timeout. beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30)); @@ -152,11 +215,25 @@ public: beast::bind_front_handler( &session::on_read, shared_from_this())); +#else + http::async_read(stream_, buffer_, req_, + boost::asio::bind_executor( + strand_, + std::bind( + &session::on_read, + shared_from_this(), + std::placeholders::_1, + std::placeholders::_2))); +#endif } void on_read( +#ifdef BOOST_LATEST beast::error_code ec, +#else + boost::system::error_code ec, +#endif std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); @@ -174,9 +251,16 @@ public: void on_write( +#ifdef BOOST_LATEST bool close, beast::error_code ec, - std::size_t bytes_transferred) + std::size_t bytes_transferred +#else + boost::system::error_code ec, + std::size_t bytes_transferred, + bool close +#endif + ) { boost::ignore_unused(bytes_transferred); @@ -200,6 +284,7 @@ public: void do_close() { +#ifdef BOOST_LATEST // Set the timeout. beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30)); @@ -208,6 +293,15 @@ public: beast::bind_front_handler( &session::on_shutdown, shared_from_this())); +#else + stream_.async_shutdown( + boost::asio::bind_executor( + strand_, + std::bind( + &session::on_shutdown, + shared_from_this(), + std::placeholders::_1))); +#endif } void @@ -225,9 +319,14 @@ public: // Accepts incoming connections and launches the sessions class listener : public std::enable_shared_from_this<listener> { +#ifdef BOOST_LATEST net::io_context& ioc_; +#endif ssl::context& ctx_; tcp::acceptor acceptor_; +#ifndef BOOST_LATEST + tcp::socket socket_; +#endif ::Server& m_server; public: @@ -235,13 +334,22 @@ public: net::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint, - Server& server) - : ioc_(ioc) - , ctx_(ctx) + Server& server) : +#ifdef BOOST_LATEST + ioc_(ioc), +#endif + ctx_(ctx) , acceptor_(ioc) +#ifndef BOOST_LATEST + , socket_(ioc) +#endif , m_server(server) { +#ifdef BOOST_LATEST beast::error_code ec; +#else + boost::system::error_code ec; +#endif // Open the acceptor acceptor_.open(endpoint.protocol(), ec); @@ -281,6 +389,10 @@ public: void run() { +#ifndef BOOST_LATEST + if(! acceptor_.is_open()) + return; +#endif do_accept(); } @@ -289,15 +401,28 @@ private: do_accept() { // The new connection gets its own strand +#ifdef BOOST_LATEST acceptor_.async_accept( net::make_strand(ioc_), beast::bind_front_handler( &listener::on_accept, shared_from_this())); +#else + acceptor_.async_accept( + socket_, + std::bind( + &listener::on_accept, + shared_from_this(), + std::placeholders::_1)); +#endif } void +#ifdef BOOST_LATEST on_accept(beast::error_code ec, tcp::socket socket) +#else + on_accept(boost::system::error_code ec) +#endif { if(ec) { @@ -307,7 +432,11 @@ private: { // Create the session and run it std::make_shared<session>( +#ifdef BOOST_LATEST std::move(socket), +#else + std::move(socket_), +#endif ctx_, m_server)->run(); } |