icy_stream.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. Regardless of whether the asynchronous operation completes
  169. immediately or not, the handler will not be invoked from within
  170. this function. Invocation of the handler will be performed in a
  171. manner equivalent to using `net::post`.
  172. @note The `async_read_some` operation may not read all of the requested number of
  173. bytes. Consider using the function `net::async_read` if you need
  174. to ensure that the requested amount of data is read before the asynchronous
  175. operation completes.
  176. */
  177. template<
  178. class MutableBufferSequence,
  179. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  180. net::default_completion_token_t<executor_type>
  181. >
  182. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  183. async_read_some(
  184. MutableBufferSequence const& buffers,
  185. ReadHandler&& handler =
  186. net::default_completion_token_t<executor_type>{});
  187. /** Write some data to the stream.
  188. This function is used to write data on the stream. The function call will
  189. block until one or more bytes of data has been written successfully, or
  190. until an error occurs.
  191. @param buffers The data to be written.
  192. @returns The number of bytes written.
  193. @throws system_error Thrown on failure.
  194. @note The `write_some` operation may not transmit all of the data to the
  195. peer. Consider using the function `net::write` if you need to
  196. ensure that all data is written before the blocking operation completes.
  197. */
  198. template<class ConstBufferSequence>
  199. std::size_t
  200. write_some(ConstBufferSequence const& buffers);
  201. /** Write some data to the stream.
  202. This function is used to write data on the stream. The function call will
  203. block until one or more bytes of data has been written successfully, or
  204. until an error occurs.
  205. @param buffers The data to be written.
  206. @param ec Set to indicate what error occurred, if any.
  207. @returns The number of bytes written.
  208. @note The `write_some` operation may not transmit all of the data to the
  209. peer. Consider using the function `net::write` if you need to
  210. ensure that all data is written before the blocking operation completes.
  211. */
  212. template<class ConstBufferSequence>
  213. std::size_t
  214. write_some(
  215. ConstBufferSequence const& buffers,
  216. error_code& ec);
  217. /** Start an asynchronous write.
  218. This function is used to asynchronously write one or more bytes of data to
  219. the stream. The function call always returns immediately.
  220. @param buffers The data to be written to the stream. Although the buffers
  221. object may be copied as necessary, ownership of the underlying buffers is
  222. retained by the caller, which must guarantee that they remain valid until
  223. the handler is called.
  224. @param handler The completion handler to invoke when the operation
  225. completes. The implementation takes ownership of the handler by
  226. performing a decay-copy. The equivalent function signature of
  227. the handler must be:
  228. @code
  229. void handler(
  230. error_code const& error, // Result of operation.
  231. std::size_t bytes_transferred // Number of bytes written.
  232. );
  233. @endcode
  234. Regardless of whether the asynchronous operation completes
  235. immediately or not, the handler will not be invoked from within
  236. this function. Invocation of the handler will be performed in a
  237. manner equivalent to using `net::post`.
  238. @note The `async_write_some` operation may not transmit all of the data to
  239. the peer. Consider using the function `net::async_write` if you need
  240. to ensure that all data is written before the asynchronous operation completes.
  241. */
  242. template<
  243. class ConstBufferSequence,
  244. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  245. net::default_completion_token_t<executor_type>
  246. >
  247. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  248. async_write_some(
  249. ConstBufferSequence const& buffers,
  250. WriteHandler&& handler =
  251. net::default_completion_token_t<executor_type>{});
  252. };
  253. } // http
  254. } // beast
  255. } // boost
  256. #include <boost/beast/_experimental/http/impl/icy_stream.hpp>
  257. #endif