forked from boostorg/beast
net is a namespace alias for boost::asio:
The namespace alias beast::net replaces boost::asio in all code and documentation.
This commit is contained in:
@@ -12,8 +12,8 @@
|
||||
|
||||
#include <boost/beast.hpp>
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Return a reasonable mime type based on the extension of a file.
|
||||
boost::beast::string_view
|
||||
mime_type(boost::beast::string_view path)
|
||||
beast::string_view
|
||||
mime_type(beast::string_view path)
|
||||
{
|
||||
using boost::beast::iequals;
|
||||
using beast::iequals;
|
||||
auto const ext = [&path]
|
||||
{
|
||||
auto const pos = path.rfind(".");
|
||||
if(pos == boost::beast::string_view::npos)
|
||||
return boost::beast::string_view{};
|
||||
if(pos == beast::string_view::npos)
|
||||
return beast::string_view{};
|
||||
return path.substr(pos);
|
||||
}();
|
||||
if(iequals(ext, ".htm")) return "text/html";
|
||||
@@ -56,8 +56,8 @@ mime_type(boost::beast::string_view path)
|
||||
// The returned path is normalized for the platform.
|
||||
std::string
|
||||
path_cat(
|
||||
boost::beast::string_view base,
|
||||
boost::beast::string_view path)
|
||||
beast::string_view base,
|
||||
beast::string_view path)
|
||||
{
|
||||
if(base.empty())
|
||||
return path.to_string();
|
||||
@@ -88,13 +88,13 @@ template<
|
||||
class Send>
|
||||
void
|
||||
handle_request(
|
||||
boost::beast::string_view doc_root,
|
||||
beast::string_view doc_root,
|
||||
http::request<Body, http::basic_fields<Allocator>>&& req,
|
||||
Send&& send)
|
||||
{
|
||||
// Returns a bad request response
|
||||
auto const bad_request =
|
||||
[&req](boost::beast::string_view why)
|
||||
[&req](beast::string_view why)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::bad_request, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -107,7 +107,7 @@ handle_request(
|
||||
|
||||
// Returns a not found response
|
||||
auto const not_found =
|
||||
[&req](boost::beast::string_view target)
|
||||
[&req](beast::string_view target)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::not_found, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -120,7 +120,7 @@ handle_request(
|
||||
|
||||
// Returns a server error response
|
||||
auto const server_error =
|
||||
[&req](boost::beast::string_view what)
|
||||
[&req](beast::string_view what)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::internal_server_error, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -139,7 +139,7 @@ handle_request(
|
||||
// Request path must be absolute and not contain "..".
|
||||
if( req.target().empty() ||
|
||||
req.target()[0] != '/' ||
|
||||
req.target().find("..") != boost::beast::string_view::npos)
|
||||
req.target().find("..") != beast::string_view::npos)
|
||||
return send(bad_request("Illegal request-target"));
|
||||
|
||||
// Build the path to the requested file
|
||||
@@ -148,9 +148,9 @@ handle_request(
|
||||
path.append("index.html");
|
||||
|
||||
// Attempt to open the file
|
||||
boost::beast::error_code ec;
|
||||
beast::error_code ec;
|
||||
http::file_body::value_type body;
|
||||
body.open(path.c_str(), boost::beast::file_mode::scan, ec);
|
||||
body.open(path.c_str(), beast::file_mode::scan, ec);
|
||||
|
||||
// Handle the case where the file doesn't exist
|
||||
if(ec == boost::system::errc::no_such_file_or_directory)
|
||||
@@ -213,7 +213,7 @@ run()
|
||||
// Report a failure
|
||||
void
|
||||
http_session::
|
||||
fail(error_code ec, char const* what)
|
||||
fail(beast::error_code ec, char const* what)
|
||||
{
|
||||
// Don't report on canceled operations
|
||||
if(ec == net::error::operation_aborted)
|
||||
@@ -239,7 +239,7 @@ operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
*sp,
|
||||
[self, sp](error_code ec, std::size_t bytes)
|
||||
[self, sp](beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
});
|
||||
@@ -247,7 +247,7 @@ operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
|
||||
void
|
||||
http_session::
|
||||
on_read(error_code ec, std::size_t)
|
||||
on_read(beast::error_code ec, std::size_t)
|
||||
{
|
||||
// This means they closed the connection
|
||||
if(ec == http::error::end_of_stream)
|
||||
@@ -291,7 +291,7 @@ on_read(error_code ec, std::size_t)
|
||||
// Write the response
|
||||
http::async_write(this->socket_, *sp,
|
||||
[self = shared_from_this(), sp](
|
||||
error_code ec, std::size_t bytes)
|
||||
beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
});
|
||||
@@ -300,7 +300,7 @@ on_read(error_code ec, std::size_t)
|
||||
auto self = shared_from_this();
|
||||
http::async_write(this->socket_, *sp,
|
||||
[self, sp](
|
||||
error_code ec, std::size_t bytes)
|
||||
beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
});
|
||||
@@ -321,7 +321,7 @@ on_read(error_code ec, std::size_t)
|
||||
|
||||
void
|
||||
http_session::
|
||||
on_write(error_code ec, std::size_t, bool close)
|
||||
on_write(beast::error_code ec, std::size_t, bool close)
|
||||
{
|
||||
// Handle the error, if any
|
||||
if(ec)
|
||||
|
||||
@@ -40,10 +40,9 @@ class http_session : public std::enable_shared_from_this<http_session>
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg) const;
|
||||
};
|
||||
|
||||
void fail(error_code ec, char const* what);
|
||||
void on_read(error_code ec, std::size_t);
|
||||
void on_write(
|
||||
error_code ec, std::size_t, bool close);
|
||||
void fail(beast::error_code ec, char const* what);
|
||||
void on_read(beast::error_code ec, std::size_t);
|
||||
void on_write(beast::error_code ec, std::size_t, bool close);
|
||||
|
||||
public:
|
||||
http_session(
|
||||
|
||||
@@ -20,7 +20,7 @@ listener(
|
||||
, socket_(ioc)
|
||||
, state_(state)
|
||||
{
|
||||
error_code ec;
|
||||
beast::error_code ec;
|
||||
|
||||
// Open the acceptor
|
||||
acceptor_.open(endpoint.protocol(), ec);
|
||||
@@ -72,7 +72,7 @@ run()
|
||||
// Report a failure
|
||||
void
|
||||
listener::
|
||||
fail(error_code ec, char const* what)
|
||||
fail(beast::error_code ec, char const* what)
|
||||
{
|
||||
// Don't report on canceled operations
|
||||
if(ec == net::error::operation_aborted)
|
||||
@@ -83,7 +83,7 @@ fail(error_code ec, char const* what)
|
||||
// Handle a connection
|
||||
void
|
||||
listener::
|
||||
on_accept(error_code ec)
|
||||
on_accept(beast::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "accept");
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#ifndef CPPCON2018_LISTENER_HPP
|
||||
#define CPPCON2018_LISTENER_HPP
|
||||
|
||||
#include "beast.hpp"
|
||||
#include "net.hpp"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -24,8 +25,8 @@ class listener : public std::enable_shared_from_this<listener>
|
||||
tcp::socket socket_;
|
||||
std::shared_ptr<shared_state> state_;
|
||||
|
||||
void fail(error_code ec, char const* what);
|
||||
void on_accept(error_code ec);
|
||||
void fail(beast::error_code ec, char const* what);
|
||||
void on_accept(beast::error_code ec);
|
||||
|
||||
public:
|
||||
listener(
|
||||
|
||||
@@ -12,8 +12,7 @@
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
namespace net = boost::asio; // namespace asio
|
||||
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
using error_code = boost::system::error_code; // from <boost/system/error_code.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -27,7 +27,7 @@ websocket_session::
|
||||
|
||||
void
|
||||
websocket_session::
|
||||
fail(error_code ec, char const* what)
|
||||
fail(beast::error_code ec, char const* what)
|
||||
{
|
||||
// Don't report these
|
||||
if( ec == net::error::operation_aborted ||
|
||||
@@ -39,7 +39,7 @@ fail(error_code ec, char const* what)
|
||||
|
||||
void
|
||||
websocket_session::
|
||||
on_accept(error_code ec)
|
||||
on_accept(beast::error_code ec)
|
||||
{
|
||||
// Handle the error, if any
|
||||
if(ec)
|
||||
@@ -60,7 +60,7 @@ on_accept(error_code ec)
|
||||
|
||||
void
|
||||
websocket_session::
|
||||
on_read(error_code ec, std::size_t)
|
||||
on_read(beast::error_code ec, std::size_t)
|
||||
{
|
||||
// Handle the error, if any
|
||||
if(ec)
|
||||
@@ -105,7 +105,7 @@ send(std::shared_ptr<std::string const> const& ss)
|
||||
|
||||
void
|
||||
websocket_session::
|
||||
on_write(error_code ec, std::size_t)
|
||||
on_write(beast::error_code ec, std::size_t)
|
||||
{
|
||||
// Handle the error, if any
|
||||
if(ec)
|
||||
|
||||
@@ -31,10 +31,10 @@ class websocket_session : public std::enable_shared_from_this<websocket_session>
|
||||
std::shared_ptr<shared_state> state_;
|
||||
std::vector<std::shared_ptr<std::string const>> queue_;
|
||||
|
||||
void fail(error_code ec, char const* what);
|
||||
void on_accept(error_code ec);
|
||||
void on_read(error_code ec, std::size_t bytes_transferred);
|
||||
void on_write(error_code ec, std::size_t bytes_transferred);
|
||||
void fail(beast::error_code ec, char const* what);
|
||||
void on_accept(beast::error_code ec);
|
||||
void on_read(beast::error_code ec, std::size_t bytes_transferred);
|
||||
void on_write(beast::error_code ec, std::size_t bytes_transferred);
|
||||
|
||||
public:
|
||||
websocket_session(
|
||||
|
||||
Reference in New Issue
Block a user