NetworkTypes.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
11 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__NETWORK_TYPES
12 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__NETWORK_TYPES
13 
19 
20 #include <Balau/Type/ToString.hpp>
22 #include <Balau/Util/Enums.hpp>
23 
24 #include <Balau/ThirdParty/Boost/Beast/Http/BasicFileBody.hpp>
25 
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>
38 
39 namespace boost {
40 
41 namespace asio::ip {
42 
48 template <typename AllocatorT>
49 inline Balau::U8String<AllocatorT> toString(const address & a) {
50  // Currently not possible to avoid std::allocator.
51  return ::toString<AllocatorT>(a.to_string());
52 }
53 
59 inline std::string toString(const address & a) {
60  return a.to_string();
61 }
62 
68 template <typename AllocatorT>
69 inline Balau::U8String<AllocatorT> toString(const tcp::endpoint & endpoint) {
70  // Currently not possible to avoid std::allocator.
71  return ::toString<AllocatorT>(endpoint.address().to_string(), ":", endpoint.port());
72 }
73 
79 inline std::string toString(const tcp::endpoint & endpoint) {
80  return ::toString(endpoint.address().to_string(), ":", endpoint.port());
81 }
82 
86 inline void fromString(tcp::endpoint & endpoint, std::string_view value) {
87  // TODO improve implementation (ipv4 / ipv6).
88 
89  const auto v = std::string(value);
90  static const std::regex numeric("[0-9]+");
91 
92  const bool isPortOnly = std::regex_match(v, numeric);
93 
94  if (isPortOnly) {
95  unsigned short port;
96  ::fromString(port, value);
97  endpoint = tcp::endpoint(make_address("127.0.0.1"), port);
98  return;
99  }
100 
101  auto lastColon = Balau::Util::Strings::lastIndexOf(value, ":");
102 
103  if (lastColon == std::string::npos) {
104  // No port.
105  endpoint = tcp::endpoint(make_address(v), 8080);
106  } else {
107  auto portStr = value.substr(lastColon + 1);
108 
109  if (Balau::Util::Strings::contains(portStr, "]")) {
110  // No port.
111  endpoint = tcp::endpoint(make_address(v), 8080);
112  } else {
113  auto ip = value.substr(0, lastColon);;
114  unsigned short port;
115  ::fromString(port, portStr);
116  endpoint = tcp::endpoint(make_address(std::string(ip)), port);
117  }
118  }
119 }
120 
121 } // namespace asio::ip
122 
123 namespace system {
124 
130 template <typename AllocatorT>
131 inline Balau::U8String<AllocatorT> toString(const error_code & errorCode) {
132  // Currently not possible to avoid std::allocator.
133  return Balau::U8String<AllocatorT>(errorCode.message());
134 }
135 
141 inline std::string toString(const error_code & errorCode) {
142  return errorCode.message();
143 }
144 
145 } // namespace system
146 
147 namespace beast::http {
148 
154 template <typename AllocatorT>
155 inline Balau::U8String<AllocatorT> toString(const status & s) {
156  #ifdef BOOST_BEAST_NEW_API
157  return Balau::U8String<AllocatorT>(obsolete_reason(s));
158  #else
160  return Balau::U8String<AllocatorT>(detail::status_to_string<>((unsigned int) u));
161  #endif
162 }
163 
169 inline std::string toString(const status & s) {
170  #ifdef BOOST_BEAST_NEW_API
171  return std::string(obsolete_reason(s));
172  #else
174  return std::string(detail::status_to_string<>((unsigned int) u));
175  #endif
176 }
177 
183 template <typename AllocatorT>
184 inline Balau::U8String<AllocatorT> toString(const verb & method) {
185  return Balau::U8String<AllocatorT>(to_string(method));
186 }
187 
193 inline std::string toString(const verb & method) {
194  return std::string(to_string(method));
195 }
196 
202 template <typename AllocatorT>
203 inline Balau::U8String<AllocatorT> toString(const field & f) {
204  return Balau::U8String<AllocatorT>(to_string(f));
205 }
206 
212 inline std::string toString(const field & f) {
213  return std::string(to_string(f));
214 }
215 
216 } // namespace beast::http
217 
218 } // namespace boost
219 
220 namespace Balau::Network {
221 
225 using Address = boost::asio::ip::address;
226 
230 using Endpoint = boost::asio::ip::tcp::endpoint;
231 
235 using CharVectorBody = boost::beast::http::vector_body<char>;
236 
240 using StringBody = boost::beast::http::basic_string_body<char>;
241 
245 using FileBody = Balau::Boost::Beast::Http::basic_file_body<boost::beast::file>;
246 //using FileBody = boost::beast::http::file_body;
247 
251 using FileBodyValue = FileBody::value_type;
252 
256 using EmptyBody = boost::beast::http::empty_body;
257 
261 template<class Body, class Fields = boost::beast::http::fields>
262 using Request = boost::beast::http::request<Body, Fields>;
263 
268 
273 
277 template<class Body, class Fields = boost::beast::http::fields>
278 using Response = boost::beast::http::response<Body, Fields>;
279 
284 
289 
294 
298 using Method = boost::beast::http::verb;
299 
303 using Field = boost::beast::http::field;
304 
308 using Buffer = boost::beast::flat_buffer;
309 
313 using Error = boost::beast::http::error;
314 
318 using BoostSystemError = boost::system::system_error;
319 
323 using BoostSystemErrorCode = boost::system::error_code;
324 
328 using FileMode = boost::beast::file_mode;
329 
333 using Status = boost::beast::http::status;
334 
338 namespace SSL = boost::asio::ssl;
339 
343 using TCP = boost::asio::ip::tcp;
344 
348 namespace HTTP = boost::beast::http;
349 
353 namespace WS = boost::beast::websocket;
354 
358 using WsFrame = WS::frame_type;
359 
363 inline Endpoint makeEndpoint(const std::string & address, unsigned short port) {
364  return Endpoint(boost::asio::ip::make_address(address), port);
365 }
366 
370 inline Endpoint makeEndpoint(const char * address, unsigned short port) {
371  return Endpoint(boost::asio::ip::make_address(address), port);
372 }
373 
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));
379 }
380 
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);
386 }
387 
388 } // namespace Balau::Network
389 
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
Utilities for enums.
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.