icy_stream.hpp 11 KB

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