11 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__NETWORK_TYPES 12 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__NETWORK_TYPES 24 #include <Balau/ThirdParty/Boost/Beast/Http/BasicFileBody.hpp> 26 #include <boost/asio/bind_executor.hpp> 27 #include <boost/asio/connect.hpp> 28 #include <boost/asio/io_context.hpp> 29 #include <boost/asio/ip/tcp.hpp> 30 #include <boost/asio/ssl/error.hpp> 31 #include <boost/asio/ssl/stream.hpp> 32 #include <boost/asio/strand.hpp> 33 #include <boost/beast/core.hpp> 34 #include <boost/beast/http.hpp> 35 #include <boost/beast/version.hpp> 36 #include <boost/beast/websocket.hpp> 37 #include <boost/config.hpp> 48 template <
typename AllocatorT>
51 return ::toString<AllocatorT>(a.to_string());
59 inline std::string toString(
const address & a) {
68 template <
typename AllocatorT>
71 return ::toString<AllocatorT>(endpoint.address().to_string(),
":", endpoint.port());
79 inline std::string toString(
const tcp::endpoint & endpoint) {
80 return ::toString(endpoint.address().to_string(),
":", endpoint.port());
86 inline void fromString(tcp::endpoint & endpoint, std::string_view value) {
89 const auto v = std::string(value);
90 static const std::regex numeric(
"[0-9]+");
92 const bool isPortOnly = std::regex_match(v, numeric);
96 ::fromString(port, value);
97 endpoint = tcp::endpoint(make_address(
"127.0.0.1"), port);
103 if (lastColon == std::string::npos) {
105 endpoint = tcp::endpoint(make_address(v), 8080);
107 auto portStr = value.substr(lastColon + 1);
111 endpoint = tcp::endpoint(make_address(v), 8080);
113 auto ip = value.substr(0, lastColon);;
115 ::fromString(port, portStr);
116 endpoint = tcp::endpoint(make_address(std::string(ip)), port);
130 template <
typename AllocatorT>
141 inline std::string toString(
const error_code & errorCode) {
142 return errorCode.message();
147 namespace beast::http {
154 template <
typename AllocatorT>
156 #ifdef BOOST_BEAST_NEW_API 169 inline std::string toString(
const status & s) {
170 #ifdef BOOST_BEAST_NEW_API 171 return std::string(obsolete_reason(s));
174 return std::string(detail::status_to_string<>((
unsigned int) u));
183 template <
typename AllocatorT>
193 inline std::string toString(
const verb & method) {
194 return std::string(to_string(method));
202 template <
typename AllocatorT>
212 inline std::string toString(
const field & f) {
213 return std::string(to_string(f));
240 using StringBody = boost::beast::http::basic_string_body<char>;
245 using FileBody = Balau::Boost::Beast::Http::basic_file_body<boost::beast::file>;
261 template<
class Body,
class Fields = boost::beast::http::fields>
262 using Request = boost::beast::http::request<Body, Fields>;
277 template<
class Body,
class Fields = boost::beast::http::fields>
278 using Response = boost::beast::http::response<Body, Fields>;
303 using Field = boost::beast::http::field;
308 using Buffer = boost::beast::flat_buffer;
313 using Error = boost::beast::http::error;
333 using Status = boost::beast::http::status;
338 namespace SSL = boost::asio::ssl;
343 using TCP = boost::asio::ip::tcp;
353 namespace WS = boost::beast::websocket;
364 return Endpoint(boost::asio::ip::make_address(address), port);
371 return Endpoint(boost::asio::ip::make_address(address), port);
377 inline std::shared_ptr<Endpoint>
newEndpoint(
const std::string & address,
unsigned short port) {
378 return std::shared_ptr<Endpoint>(
new Endpoint(boost::asio::ip::make_address(address), port));
384 inline std::shared_ptr<Endpoint>
newEndpoint(
const char * address,
unsigned short port) {
385 return std::make_shared<Endpoint>(boost::asio::ip::make_address(address), port);
390 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__NETWORK_TYPES boost::beast::http::field Field
An HTTP header field.
Definition: NetworkTypes.hpp:303
Pre-defined universal to-string functions.
Definition: NetworkTypes.hpp:39
boost::beast::http::status Status
Boost status code.
Definition: NetworkTypes.hpp:333
Definition: NetworkTypes.hpp:147
Request< CharVectorBody > CharVectorRequest
A request with a char vector body.
Definition: NetworkTypes.hpp:272
boost::beast::flat_buffer Buffer
A data buffer used in HTTP code.
Definition: NetworkTypes.hpp:308
static bool contains(const StringT< CharT, T ... > &str, const SubstrT &substring)
Does the first string contain the second string?
Definition: Strings.hpp:174
boost::beast::http::basic_string_body< char > StringBody
A message body represented by a UTF-8 string.
Definition: NetworkTypes.hpp:240
boost::beast::http::empty_body EmptyBody
A message body wth no content.
Definition: NetworkTypes.hpp:256
void fromString(tcp::endpoint &endpoint, std::string_view value)
Populate the endpoint by parsing the supplied string.
Definition: NetworkTypes.hpp:86
boost::beast::http::error Error
Boost error codes returned from HTTP algorithms and operations.
Definition: NetworkTypes.hpp:313
Balau::Boost::Beast::Http::basic_file_body< boost::beast::file > FileBody
A message body represented by a file on the filesystem.
Definition: NetworkTypes.hpp:245
boost::asio::ip::tcp TCP
The Boost SSL namespace.
Definition: NetworkTypes.hpp:343
Components and utilities working on network data transmission.
boost::beast::http::verb Method
The HTTP method (GET, HEAD, POST).
Definition: NetworkTypes.hpp:298
boost::beast::http::vector_body< char > CharVectorBody
A message body represented by a vector of 8 bit characters.
Definition: NetworkTypes.hpp:235
static constexpr auto toUnderlying(E e) noexcept -> typename std::underlying_type< E >::type
Convert the strongly typed enum to its underlying integer.
Definition: Enums.hpp:31
Response< EmptyBody > EmptyResponse
A response with an empty body.
Definition: NetworkTypes.hpp:293
boost::beast::http::request< Body, Fields > Request
The request type.
Definition: NetworkTypes.hpp:262
Endpoint makeEndpoint(const char *address, unsigned short port)
Make an endpoint on the stack.
Definition: NetworkTypes.hpp:370
Response< StringBody > StringResponse
A response with a string body.
Definition: NetworkTypes.hpp:283
boost::asio::ip::address Address
An IP4 or IP6 address.
Definition: NetworkTypes.hpp:225
boost::asio::ip::tcp::endpoint Endpoint
An IP address and port number.
Definition: NetworkTypes.hpp:230
static size_t lastIndexOf(const StringT< CharT, T ... > &str, const SubstrT &substring)
Get the character (UTF-8) / code point (UTF-32) position of the last index of the specified sub-strin...
Definition: Strings.hpp:363
Request< StringBody > StringRequest
A request with a string body.
Definition: NetworkTypes.hpp:267
Response< CharVectorBody > CharVectorResponse
A response with a char vector body.
Definition: NetworkTypes.hpp:288
std::shared_ptr< Endpoint > newEndpoint(const char *address, unsigned short port)
Make an endpoint on the heap.
Definition: NetworkTypes.hpp:384
WS::frame_type WsFrame
The Boost HTTP namespace.
Definition: NetworkTypes.hpp:358
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
boost::beast::http::response< Body, Fields > Response
The response type.
Definition: NetworkTypes.hpp:278
boost::beast::file_mode FileMode
Boost HTTP file open modes.
Definition: NetworkTypes.hpp:328
boost::system::system_error BoostSystemError
Boost system error.
Definition: NetworkTypes.hpp:318
boost::system::error_code BoostSystemErrorCode
Boost system error codes.
Definition: NetworkTypes.hpp:323
FileBody::value_type FileBodyValue
The type of the message body represented by a file on the filesystem.
Definition: NetworkTypes.hpp:251
Pre-defined universal from-string functions.