summaryrefslogtreecommitdiff
path: root/crow
diff options
context:
space:
mode:
authorEd Tanous <ed.tanous@intel.com>2019-03-14 02:23:37 +0300
committerEd Tanous <ed.tanous@intel.com>2019-03-29 23:41:09 +0300
commite278c18fc22faa2e5099f3f43a813942144c7560 (patch)
tree49071083c913f58f8fc5873a55d1aa3b2ed10576 /crow
parentd265304eda0053cd45d21aa7a269cc2212fd8d24 (diff)
downloadbmcweb-e278c18fc22faa2e5099f3f43a813942144c7560.tar.xz
update ASIO interfaces
This commit does 2 things. 1. Upgrades and prepares bmcweb for boost 1.70. 2. Allows us to compile with BOOST_AIO_NO_DEPRECATED Tested: Compiled against 1.69 and 1.70. All changes should be no-op. Change-Id: I557ecd840fe2b88c0fa01978a1b666b40ccccca4 Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Diffstat (limited to 'crow')
-rw-r--r--crow/include/crow/http_connection.h73
-rw-r--r--crow/include/crow/http_server.h69
-rw-r--r--crow/include/crow/websocket.h2
3 files changed, 97 insertions, 47 deletions
diff --git a/crow/include/crow/http_connection.h b/crow/include/crow/http_connection.h
index c02f4ec1e3..ba2bcdd98c 100644
--- a/crow/include/crow/http_connection.h
+++ b/crow/include/crow/http_connection.h
@@ -8,7 +8,11 @@
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast/core/flat_static_buffer.hpp>
+#if BOOST_VERSION >= 107000
+#include <boost/beast/ssl/ssl_stream.hpp>
+#else
#include <boost/beast/experimental/core/ssl_stream.hpp>
+#endif
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <chrono>
@@ -323,30 +327,22 @@ class Connection
}
}
- std::string epName;
- boost::system::error_code ec;
- tcp::endpoint ep = adaptor.lowest_layer().remote_endpoint(ec);
- if (!ec)
- {
- epName = boost::lexical_cast<std::string>(ep);
- }
-
- BMCWEB_LOG_INFO << "Request: " << epName << " " << this << " HTTP/"
- << req->version() / 10 << "." << req->version() % 10
- << ' ' << req->methodString() << " " << req->target();
+ BMCWEB_LOG_INFO << "Request: "
+ << " " << this << " HTTP/" << req->version() / 10 << "."
+ << req->version() % 10 << ' ' << req->methodString()
+ << " " << req->target();
needToCallAfterHandlers = false;
if (!isInvalidRequest)
{
res.completeRequestHandler = [] {};
- res.isAliveHelper = [this]() -> bool {
- return adaptor.lowest_layer().is_open();
- };
+ res.isAliveHelper = [this]() -> bool { return isAlive(); };
ctx = detail::Context<Middlewares...>();
- req->middlewareContext = (void*)&ctx;
- req->ioService = &adaptor.get_executor().context();
+ req->middlewareContext = static_cast<void*>(&ctx);
+ req->ioService = static_cast<decltype(req->ioService)>(
+ &adaptor.get_executor().context());
detail::middlewareCallHelper<
0, decltype(ctx), decltype(*middlewares), Middlewares...>(
*middlewares, *req, res, ctx);
@@ -378,6 +374,35 @@ class Connection
}
}
+ bool isAlive()
+ {
+
+ if constexpr (std::is_same_v<Adaptor,
+ boost::beast::ssl_stream<
+ boost::asio::ip::tcp::socket>>)
+ {
+ return adaptor.next_layer().is_open();
+ }
+ else
+ {
+ return adaptor.is_open();
+ }
+ }
+ void close()
+ {
+
+ if constexpr (std::is_same_v<Adaptor,
+ boost::beast::ssl_stream<
+ boost::asio::ip::tcp::socket>>)
+ {
+ adaptor.next_layer().close();
+ }
+ else
+ {
+ adaptor.close();
+ }
+ }
+
void completeRequest()
{
BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' '
@@ -397,7 +422,7 @@ class Connection
// auto self = this->shared_from_this();
res.completeRequestHandler = res.completeRequestHandler = [] {};
- if (!adaptor.lowest_layer().is_open())
+ if (!isAlive())
{
// BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " <<
// isReading
@@ -456,7 +481,7 @@ class Connection
{
// if the adaptor isn't open anymore, and wasn't handed to a
// websocket, treat as an error
- if (!adaptor.lowest_layer().is_open() && !req->isUpgrade())
+ if (!isAlive() && !req->isUpgrade())
{
errorWhileReading = true;
}
@@ -465,7 +490,7 @@ class Connection
if (errorWhileReading)
{
cancelDeadlineTimer();
- adaptor.lowest_layer().close();
+ close();
BMCWEB_LOG_DEBUG << this << " from read(1)";
checkDestroy();
return;
@@ -505,7 +530,7 @@ class Connection
}
else
{
- if (!adaptor.lowest_layer().is_open())
+ if (!isAlive())
{
errorWhileReading = true;
}
@@ -513,7 +538,7 @@ class Connection
if (errorWhileReading)
{
cancelDeadlineTimer();
- adaptor.lowest_layer().close();
+ close();
BMCWEB_LOG_DEBUG << this << " from read(1)";
checkDestroy();
return;
@@ -545,7 +570,7 @@ class Connection
}
if (!res.keepAlive())
{
- adaptor.lowest_layer().close();
+ close();
BMCWEB_LOG_DEBUG << this << " from write(1)";
checkDestroy();
return;
@@ -587,11 +612,11 @@ class Connection
cancelDeadlineTimer();
timerCancelKey = timerQueue.add([this] {
- if (!adaptor.lowest_layer().is_open())
+ if (!isAlive())
{
return;
}
- adaptor.lowest_layer().close();
+ close();
});
BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' '
<< timerCancelKey;
diff --git a/crow/include/crow/http_server.h b/crow/include/crow/http_server.h
index 75b49a463f..31b8ec2d04 100644
--- a/crow/include/crow/http_server.h
+++ b/crow/include/crow/http_server.h
@@ -2,10 +2,16 @@
#include <atomic>
#include <boost/asio/deadline_timer.hpp>
+#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/ssl/context.hpp>
+#if BOOST_VERSION >= 107000
+#include <boost/beast/ssl/ssl_stream.hpp>
+#else
#include <boost/beast/experimental/core/ssl_stream.hpp>
+#endif
+
#include <boost/date_time/posix_time/posix_time.hpp>
#include <chrono>
#include <cstdint>
@@ -47,9 +53,8 @@ class Server
std::make_shared<boost::asio::io_context>()) :
Server(handler,
std::make_unique<tcp::acceptor>(
- *io,
- tcp::endpoint(
- boost::asio::ip::address::from_string(bindaddr), port)),
+ *io, tcp::endpoint(boost::asio::ip::make_address(bindaddr),
+ port)),
middlewares, adaptor_ctx, io)
{
}
@@ -168,30 +173,50 @@ class Server
boost::asio::ip::tcp::socket>>::value)
{
adaptorTemp = Adaptor(*ioService, *adaptorCtx);
+ Connection<Adaptor, Handler, Middlewares...>* p =
+ new Connection<Adaptor, Handler, Middlewares...>(
+ *ioService, handler, serverName, middlewares,
+ getCachedDateStr, timerQueue,
+ std::move(adaptorTemp.value()));
+
+ acceptor->async_accept(p->socket().next_layer(),
+ [this, p](boost::system::error_code ec) {
+ if (!ec)
+ {
+ boost::asio::post(
+ *this->ioService,
+ [p] { p->start(); });
+ }
+ else
+ {
+ delete p;
+ }
+ doAccept();
+ });
}
else
{
adaptorTemp = Adaptor(*ioService);
+ Connection<Adaptor, Handler, Middlewares...>* p =
+ new Connection<Adaptor, Handler, Middlewares...>(
+ *ioService, handler, serverName, middlewares,
+ getCachedDateStr, timerQueue,
+ std::move(adaptorTemp.value()));
+
+ acceptor->async_accept(
+ p->socket(), [this, p](boost::system::error_code ec) {
+ if (!ec)
+ {
+ boost::asio::post(*this->ioService,
+ [p] { p->start(); });
+ }
+ else
+ {
+ delete p;
+ }
+ doAccept();
+ });
}
-
- Connection<Adaptor, Handler, Middlewares...>* p =
- new Connection<Adaptor, Handler, Middlewares...>(
- *ioService, handler, serverName, middlewares, getCachedDateStr,
- timerQueue, std::move(adaptorTemp.value()));
-
- acceptor->async_accept(p->socket().lowest_layer(),
- [this, p](boost::system::error_code ec) {
- if (!ec)
- {
- this->ioService->post(
- [p] { p->start(); });
- }
- else
- {
- delete p;
- }
- doAccept();
- });
}
private:
diff --git a/crow/include/crow/websocket.h b/crow/include/crow/websocket.h
index 6901eb108f..778a29707b 100644
--- a/crow/include/crow/websocket.h
+++ b/crow/include/crow/websocket.h
@@ -66,7 +66,7 @@ template <typename Adaptor> class ConnectionImpl : public Connection
boost::asio::io_context& get_io_context() override
{
- return ws.get_executor().context();
+ return (boost::asio::io_context&)ws.get_executor().context();
}
void start()