icy_stream.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_ICY_STREAM_HPP
  10. #define BOOST_BEAST_HTTP_ICY_STREAM_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/asio/async_result.hpp>
  14. #include <boost/asio/buffer.hpp>
  15. #include <boost/logic/tribool.hpp>
  16. #include <type_traits>
  17. namespace boost {
  18. namespace beast {
  19. namespace http {
  20. /** Stream wrapper to process Shoutcast HTTP responses
  21. This wrapper replaces the word "ICY" in the first
  22. HTTP response received on the connection, with "HTTP/1.1".
  23. This allows the Beast parser to be used with Shoutcast
  24. servers, which send a non-standard HTTP message as the
  25. response.
  26. For asynchronous operations, the application must ensure
  27. that they are are all performed within the same implicit
  28. or explicit strand.
  29. @par Thread Safety
  30. @e Distinct @e objects: Safe.@n
  31. @e Shared @e objects: Unsafe.
  32. The application must also ensure that all asynchronous
  33. operations are performed within the same implicit or explicit strand.
  34. @par Example
  35. To use the @ref icy_stream template with an @ref tcp_stream
  36. you would write:
  37. @code
  38. http::icy_stream<tcp_stream> is(ioc);
  39. @endcode
  40. @tparam NextLayer The type representing the next layer, to which
  41. data will be read and written during operations. For synchronous
  42. operations, the type must support the <em>SyncStream</em> concept.
  43. For asynchronous operations, the type must support the
  44. <em>AsyncStream</em> concept.
  45. @note A stream object must not be moved or destroyed while there
  46. are pending asynchronous operations associated with it.
  47. @par Concepts
  48. <em>AsyncStream</em>, <em>SyncStream</em>
  49. */
  50. template<class NextLayer>
  51. class icy_stream
  52. {
  53. NextLayer stream_;
  54. char buf_[8];
  55. unsigned char n_ = 0;
  56. bool detect_ = true;
  57. struct ops;
  58. static
  59. net::const_buffer
  60. version()
  61. {
  62. return {"HTTP/1.1", 8};
  63. }
  64. public:
  65. /// The type of the next layer.
  66. using next_layer_type =
  67. typename std::remove_reference<NextLayer>::type;
  68. /// The type of the executor associated with the object.
  69. using executor_type = typename next_layer_type::executor_type;
  70. icy_stream(icy_stream&&) = default;
  71. icy_stream(icy_stream const&) = default;
  72. icy_stream& operator=(icy_stream&&) = default;
  73. icy_stream& operator=(icy_stream const&) = default;
  74. /** Destructor
  75. The treatment of pending operations will be the same as that
  76. of the next layer.
  77. */
  78. ~icy_stream() = default;
  79. /** Constructor
  80. Arguments, if any, are forwarded to the next layer's constructor.
  81. */
  82. template<class... Args>
  83. explicit
  84. icy_stream(Args&&... args);
  85. //--------------------------------------------------------------------------
  86. /** Get the executor associated with the object.
  87. This function may be used to obtain the executor object that the
  88. stream uses to dispatch handlers for asynchronous operations.
  89. @return A copy of the executor that stream will use to dispatch handlers.
  90. */
  91. executor_type
  92. get_executor() noexcept
  93. {
  94. return stream_.get_executor();
  95. }
  96. /** Get a reference to the next layer
  97. This function returns a reference to the next layer
  98. in a stack of stream layers.
  99. @return A reference to the next layer in the stack of
  100. stream layers.
  101. */
  102. next_layer_type&
  103. next_layer()
  104. {
  105. return stream_;
  106. }
  107. /** Get a reference to the next layer
  108. This function returns a reference to the next layer in a
  109. stack of stream layers.
  110. @return A reference to the next layer in the stack of
  111. stream layers.
  112. */
  113. next_layer_type const&
  114. next_layer() const
  115. {
  116. return stream_;
  117. }
  118. //--------------------------------------------------------------------------
  119. /** Read some data from the stream.
  120. This function is used to read data from the stream. The function call will
  121. block until one or more bytes of data has been read successfully, or until
  122. an error occurs.
  123. @param buffers The buffers into which the data will be read.
  124. @returns The number of bytes read.
  125. @throws system_error Thrown on failure.
  126. @note The `read_some` operation may not read all of the requested number of
  127. bytes. Consider using the function `net::read` if you need to ensure
  128. that the requested amount of data is read before the blocking operation
  129. completes.
  130. */
  131. template<class MutableBufferSequence>
  132. std::size_t
  133. read_some(MutableBufferSequence const& buffers);
  134. /** Read some data from the stream.
  135. This function is used to read data from the stream. The function call will
  136. block until one or more bytes of data has been read successfully, or until
  137. an error occurs.
  138. @param buffers The buffers into which the data will be read.
  139. @param ec Set to indicate what error occurred, if any.
  140. @returns The number of bytes read.
  141. @note The `read_some` operation may not read all of the requested number of
  142. bytes. Consider using the function `net::read` if you need to ensure
  143. that the requested amount of data is read before the blocking operation
  144. completes.
  145. */
  146. template<class MutableBufferSequence>
  147. std::size_t
  148. read_some(
  149. MutableBufferSequence const& buffers,
  150. error_code& ec);
  151. /** Start an asynchronous read.
  152. This function is used to asynchronously read one or more bytes of data from
  153. the stream. The function call always returns immediately.
  154. @param buffers The buffers into which the data will be read. Although the
  155. buffers object may be copied as necessary, ownership of the underlying
  156. buffers is retained by the caller, which must guarantee that they remain
  157. valid until the handler is called.
  158. @param handler The completion handler to invoke when the operation
  159. completes. The implementation takes ownership of the handler by
  160. performing a decay-copy. The equivalent function signature of
  161. the handler must be:
  162. @code
  163. void handler(
  164. const boost::system::error_code& error, // Result of operation.
  165. std::size_t bytes_transferred // Number of bytes read.
  166. );
  167. @endcode
  168. If the handler has an associated immediate executor,
  169. an immediate completion will be dispatched to it.
  170. Otherwise, the handler will not be invoked from within
  171. this function. Invocation of the handler will be performed
  172. by dispatching to the immediate executor. If no
  173. immediate executor is specified, this is equivalent
  174. to using `net::post`.
  175. @note The `async_read_some` operation may not read all of the requested number of
  176. bytes. Consider using the function `net::async_read` if you need
  177. to ensure that the requested amount of data is read before the asynchronous
  178. operation completes.
  179. */
  180. template<
  181. class MutableBufferSequence,
  182. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  183. net::default_completion_token_t<executor_type>
  184. >
  185. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  186. async_read_some(
  187. MutableBufferSequence const& buffers,
  188. ReadHandler&& handler =
  189. net::default_completion_token_t<executor_type>{});
  190. /** Write some data to the stream.
  191. This function is used to write data on the stream. The function call will
  192. block until one or more bytes of data has been written successfully, or
  193. until an error occurs.
  194. @param buffers The data to be written.
  195. @returns The number of bytes written.
  196. @throws system_error Thrown on failure.
  197. @note The `write_some` operation may not transmit all of the data to the
  198. peer. Consider using the function `net::write` if you need to
  199. ensure that all data is written before the blocking operation completes.
  200. */
  201. template<class ConstBufferSequence>
  202. std::size_t
  203. write_some(ConstBufferSequence const& buffers);
  204. /** Write some data to the stream.
  205. This function is used to write data on the stream. The function call will
  206. block until one or more bytes of data has been written successfully, or
  207. until an error occurs.
  208. @param buffers The data to be written.
  209. @param ec Set to indicate what error occurred, if any.
  210. @returns The number of bytes written.
  211. @note The `write_some` operation may not transmit all of the data to the
  212. peer. Consider using the function `net::write` if you need to
  213. ensure that all data is written before the blocking operation completes.
  214. */
  215. template<class ConstBufferSequence>
  216. std::size_t
  217. write_some(
  218. ConstBufferSequence const& buffers,
  219. error_code& ec);
  220. /** Start an asynchronous write.
  221. This function is used to asynchronously write one or more bytes of data to
  222. the stream. The function call always returns immediately.
  223. @param buffers The data to be written to the stream. Although the buffers
  224. object may be copied as necessary, ownership of the underlying buffers is
  225. retained by the caller, which must guarantee that they remain valid until
  226. the handler is called.
  227. @param handler The completion handler to invoke when the operation
  228. completes. The implementation takes ownership of the handler by
  229. performing a decay-copy. The equivalent function signature of
  230. the handler must be:
  231. @code
  232. void handler(
  233. error_code const& error, // Result of operation.
  234. std::size_t bytes_transferred // Number of bytes written.
  235. );
  236. @endcode
  237. If the handler has an associated immediate executor,
  238. an immediate completion will be dispatched to it.
  239. Otherwise, the handler will not be invoked from within
  240. this function. Invocation of the handler will be performed
  241. by dispatching to the immediate executor. If no
  242. immediate executor is specified, this is equivalent
  243. to using `net::post`.
  244. @note The `async_write_some` operation may not transmit all of the data to
  245. the peer. Consider using the function `net::async_write` if you need
  246. to ensure that all data is written before the asynchronous operation completes.
  247. */
  248. template<
  249. class ConstBufferSequence,
  250. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  251. net::default_completion_token_t<executor_type>
  252. >
  253. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  254. async_write_some(
  255. ConstBufferSequence const& buffers,
  256. WriteHandler&& handler =
  257. net::default_completion_token_t<executor_type>{});
  258. };
  259. } // http
  260. } // beast
  261. } // boost
  262. #include <boost/beast/_experimental/http/impl/icy_stream.hpp>
  263. #endif