Template Class base_socket

Class Documentation

template<typename Stream>
class stream_client::http::base_socket

HTTP stream layer.

This class wraps usage of boost::beast::http::request_serializer and boost::beast::http::response_parser over Stream. If you want HTTPS use stream_client::ssl::tcp_client type for underlying stream.

Note

Not thread-safe, multi-thread rw access may mess up your data stream and/or timeout handling.

tparam Stream

Type of underlying stream to use.

Public Types

using allocator_type = std::allocator<char>
using next_layer_type = typename std::remove_reference<Stream>::type
using protocol_type = typename next_layer_type::protocol_type
using endpoint_type = typename next_layer_type::endpoint_type
using clock_type = typename next_layer_type::clock_type
using time_duration_type = typename next_layer_type::time_duration_type
using time_point_type = typename next_layer_type::time_point_type

Public Functions

template<class Arg1, class ...ArgN, class = typename std::enable_if<!std::is_same<typename std::decay<Arg1>::type, base_socket<Stream>>::value>::type>
inline base_socket(Arg1 &&arg1, ArgN&&... argn)

Parametrized constructor.

Constructs basic HTTTP stream. This operation blocks until stream is constructed. Passed arguments forwarded to Stream constructor.

Template Parameters
  • Arg1 – Type of the first argument.

  • ...ArgN – Types of next arguments.

Parameters
  • [in] arg1 – First argument to pass to Stream constructor.

  • [in] ...argn – Next arguments to pass to Stream constructor.

Throws

boost::system::system_error – Thrown on failure.

base_socket(const base_socket<Stream> &other) = delete

Copy constructor is not permitted.

base_socket<Stream> &operator=(const base_socket<Stream> &other) = delete

Copy assignment is not permitted.

base_socket(base_socket<Stream> &&other) = default

Move constructor.

base_socket<Stream> &operator=(base_socket<Stream> &&other) = default

Move assignment.

virtual ~base_socket() = default

Destructor.

template<typename Body, typename Fields>
boost::optional<boost::beast::http::response<Body, Fields>> perform(const boost::beast::http::request<Body, Fields> &request, boost::system::error_code &ec, const time_point_type &deadline)

Perform HTTP request.

This function is used to send a request and wait&receive a response until specified deadline is met.

Template Parameters
  • Body – Type of the body used in the request.

  • Fields – Type of the fields used in the request.

Parameters
  • [in] request – HTTP request to send.

  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

Received response wrapped in boost::optional or boost::none if an error occurred.

template<typename Body, typename Fields>
inline boost::optional<boost::beast::http::response<Body, Fields>> perform(const boost::beast::http::request<Body, Fields> &request, boost::system::error_code &ec, const time_duration_type &timeout)

Perform HTTP request.

This function is used to send a request and wait&receive a response within specified time.

Template Parameters
  • Body – Type of the body used in the request.

  • Fields – Type of the fields used in the request.

Parameters
  • [in] request – HTTP request to send.

  • [in] timeout – Expiration duration.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

Received response wrapped in boost::optional or boost::none if an error occurred.

template<typename Body, typename Fields>
inline boost::optional<boost::beast::http::response<Body, Fields>> perform(const boost::beast::http::request<Body, Fields> &request, boost::system::error_code &ec)

Perform HTTP request.

This function is used to send a request and wait&receive a response. Timeout for this operation is defined by stream’s I/O default timeout.

Template Parameters
  • Body – Type of the body used in the request.

  • Fields – Type of the fields used in the request.

Parameters
  • [in] request – HTTP request to send.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

Received response wrapped in boost::optional or boost::none if an error occurred.

template<typename Body, typename Fields>
inline boost::beast::http::response<Body, Fields> perform(const boost::beast::http::request<Body, Fields> &request)

Perform HTTP request.

This function is used to send a request and wait&receive a response. Timeout for this operation is defined by stream’s I/O default timeout.

Template Parameters
  • Body – Type of the body used in the request.

  • Fields – Type of the fields used in the request.

Parameters

[in] request – HTTP request to send.

Throws

boost::system::system_error – Thrown on failure.

Returns

Received response.

inline const next_layer_type &next_layer() const

Get a const reference to the underlying stream of type Socket.

Returns

This function returns a const reference to the underlying stream.

inline next_layer_type &next_layer()

Get a reference to the underlying stream of type Socket.

Returns

This function returns a reference to the underlying stream.

inline bool is_open() const

Determine whether the underlying stream is open.

Public Static Attributes

static const size_t kHeaderLimit = 1 << 20

Headers length limit for internal parser.

static const size_t kBodyLimit = 10 << 20

Body length limit for internal parser.

Protected Functions

template<typename Body, typename Fields>
void send_request(const boost::beast::http::request<Body, Fields> &request, boost::system::error_code &ec, const time_point_type &deadline)

Send HTTP request.

This function is used to send a request within defined deadline. Implemented inside as multiple calls of write_some() on the stream.

Template Parameters
  • Body – Type of the body used in the request.

  • Fields – Type of the fields used in the request.

Parameters
  • [in] request – HTTP request to send.

  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

Received response wrapped in boost::optional or boost::none if an error occurred.

template<typename Parser, typename DynamicBuffer>
void recv_response(Parser &response_parser, DynamicBuffer &buffer, boost::system::error_code &ec, const time_point_type &deadline)

Receive HTTP response.

This function is used to fill response_parser with received response within defined deadline. Implemented inside as multiple calls of read_some() on the stream.

Template Parameters
  • Parser – Type of response parse.

  • DynamicBuffer – Type of the buffer used to store read data.

Parameters
  • [in] response_parser – HTTP request parser to use.

  • [in] buffer – Buffer for incoming data.

  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

template<typename Body, typename Fields>
boost::optional<boost::beast::http::response<Body, Fields>> recv_response(boost::system::error_code &ec, const time_point_type &deadline)

Receive HTTP response using internal parser.

Parser uses intenal allocator for fast operations, but it’s limited with kHeaderLimit and kBodyLimit.

Template Parameters
  • Parser – Type of response parse.

  • DynamicBuffer – Type of the buffer used to store read data.

Parameters
  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

Received response wrapped in boost::optional or boost::none if an error occurred.