summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-10 19:35:06 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-10 19:35:06 +0200
commit07f01d1ab5e68fc042356fd90fa07c199791b29c (patch)
tree89860e4e85ee49931b4193255de0a2032d94392e /http.cpp
parentda2666726e48a3dc00f05589cdf4947f22deb3c3 (diff)
Ported to Debian 10
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp119
1 files changed, 111 insertions, 8 deletions
diff --git a/http.cpp b/http.cpp
index 9a5b554..cb95b0f 100644
--- a/http.cpp
+++ b/http.cpp
@@ -1,13 +1,23 @@
#include "http.h"
+#include <boost/beast/version.hpp>
+
+// Support both boost in Debian unstable (BOOST_LATEST) and in stable (boost 1.67)
+#if BOOST_VERSION >= 107100
+#define BOOST_LATEST
+#endif
+
#include "server.h"
#include "response.h"
#include <boost/beast/version.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
-#include <boost/beast/version.hpp>
#include <boost/asio/dispatch.hpp>
+#ifndef BOOST_LATEST
+#include <boost/asio/bind_executor.hpp>
+#include <boost/asio/ip/tcp.hpp>
+#endif
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>
#include <algorithm>
@@ -38,7 +48,12 @@ 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::tcp_stream stream_;
+#else
+ tcp::socket socket_;
+ boost::asio::strand<boost::asio::io_context::executor_type> strand_;
+#endif
beast::flat_buffer buffer_;
Server& m_server;
request_type req_;
@@ -51,6 +66,7 @@ class session : public std::enable_shared_from_this<session>
res_ = sp;
// Write the response
+#ifdef BOOST_LATEST
http::async_write(
stream_,
*sp,
@@ -58,14 +74,36 @@ class session : public std::enable_shared_from_this<session>
&session::on_write,
shared_from_this(),
sp->need_eof()));
+#else
+ http::async_write(
+ socket_,
+ *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 stream
session(
+#ifdef BOOST_LATEST
tcp::socket&& socket,
+#else
+ tcp::socket socket,
+#endif
Server& server)
+#ifdef BOOST_LATEST
: stream_(std::move(socket))
+#else
+ : socket_(std::move(socket))
+ , strand_(socket_.get_executor())
+#endif
, m_server(server)
{
}
@@ -75,13 +113,15 @@ public:
run()
{
// We need to be executing within a strand to perform async operations
- // on the I/O objects in this session. Although not strictly necessary
- // for single-threaded contexts, this example code is written to be
- // thread-safe by default.
+ // on the I/O objects in this session.
+#ifdef BOOST_LATEST
net::dispatch(stream_.get_executor(),
beast::bind_front_handler(
&session::do_read,
shared_from_this()));
+#else
+ do_read();
+#endif
}
void
@@ -91,6 +131,7 @@ public:
// otherwise the operation behavior is undefined.
req_ = {};
+#ifdef BOOST_LATEST
// Set the timeout.
stream_.expires_after(std::chrono::seconds(30));
@@ -99,12 +140,29 @@ public:
beast::bind_front_handler(
&session::on_read,
shared_from_this()));
+#else
+
+ http::async_read(socket_, 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,
- std::size_t bytes_transferred)
+ std::size_t bytes_transferred
+#else
+ boost::system::error_code ec,
+ std::size_t bytes_transferred
+#endif
+ )
{
boost::ignore_unused(bytes_transferred);
@@ -121,9 +179,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);
@@ -149,8 +214,11 @@ public:
{
// Send a TCP shutdown
beast::error_code ec;
+#ifdef BOOST_LATEST
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
-
+#else
+ socket_.shutdown(tcp::socket::shutdown_send, ec);
+#endif
// At this point the connection is closed gracefully
}
};
@@ -160,8 +228,13 @@ 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
tcp::acceptor acceptor_;
+#ifndef BOOST_LATEST
+ tcp::socket socket_;
+#endif
Server& m_server;
public:
@@ -169,11 +242,20 @@ public:
net::io_context& ioc,
tcp::endpoint endpoint,
Server& server)
+#ifdef BOOST_LATEST
: ioc_(ioc)
, acceptor_(net::make_strand(ioc))
+#else
+ : acceptor_(ioc)
+ , socket_(ioc)
+#endif
, m_server(server)
{
+#ifdef BOOST_VERSION
beast::error_code ec;
+#else
+ boost::system::error_code ec;
+#endif
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -213,7 +295,11 @@ public:
void
run()
{
- do_accept();
+#ifndef BOOST_LATEST
+ if (!acceptor_.is_open())
+ return;
+#endif
+ do_accept();
}
private:
@@ -221,15 +307,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)
{
@@ -239,7 +338,11 @@ private:
{
// Create the session and run it
std::make_shared<session>(
+#ifdef BOOST_LATEST
std::move(socket),
+#else
+ std::move(socket_),
+#endif
m_server)->run();
}