| 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 | |
| 10 | #ifndef BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP |
| 11 | #define BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP |
| 12 | |
| 13 | #include <boost/beast/core/buffer_traits.hpp> |
| 14 | #include <boost/beast/websocket/rfc6455.hpp> |
| 15 | #include <boost/beast/websocket/teardown.hpp> |
| 16 | #include <boost/beast/websocket/detail/hybi13.hpp> |
| 17 | #include <boost/beast/websocket/detail/mask.hpp> |
| 18 | #include <boost/beast/websocket/impl/stream_impl.hpp> |
| 19 | #include <boost/beast/version.hpp> |
| 20 | #include <boost/beast/http/read.hpp> |
| 21 | #include <boost/beast/http/write.hpp> |
| 22 | #include <boost/beast/http/rfc7230.hpp> |
| 23 | #include <boost/beast/core/buffers_cat.hpp> |
| 24 | #include <boost/beast/core/buffers_prefix.hpp> |
| 25 | #include <boost/beast/core/buffers_suffix.hpp> |
| 26 | #include <boost/beast/core/flat_static_buffer.hpp> |
| 27 | #include <boost/beast/core/detail/clamp.hpp> |
| 28 | #include <boost/asio/steady_timer.hpp> |
| 29 | #include <boost/assert.hpp> |
| 30 | #include <boost/make_shared.hpp> |
| 31 | #include <boost/throw_exception.hpp> |
| 32 | #include <algorithm> |
| 33 | #include <chrono> |
| 34 | #include <memory> |
| 35 | #include <stdexcept> |
| 36 | #include <utility> |
| 37 | |
| 38 | namespace boost { |
| 39 | namespace beast { |
| 40 | namespace websocket { |
| 41 | |
| 42 | template<class NextLayer, bool deflateSupported> |
| 43 | stream<NextLayer, deflateSupported>:: |
| 44 | ~stream() |
| 45 | { |
| 46 | if(impl_) |
| 47 | impl_->remove(); |
| 48 | } |
| 49 | |
| 50 | template<class NextLayer, bool deflateSupported> |
| 51 | template<class... Args> |
| 52 | stream<NextLayer, deflateSupported>:: |
| 53 | stream(Args&&... args) |
| 54 | : impl_(boost::make_shared<impl_type>( |
| 55 | std::forward<Args>(args)...)) |
| 56 | { |
| 57 | BOOST_ASSERT(impl_->rd_buf.max_size() >= |
| 58 | max_control_frame_size); |
| 59 | } |
| 60 | |
| 61 | template<class NextLayer, bool deflateSupported> |
| 62 | template<class Other> |
| 63 | stream<NextLayer, deflateSupported>:: |
| 64 | stream(stream<Other> && other) |
| 65 | : impl_(boost::make_shared<impl_type>(std::move(other.next_layer()))) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | |
| 70 | template<class NextLayer, bool deflateSupported> |
| 71 | auto |
| 72 | stream<NextLayer, deflateSupported>:: |
| 73 | get_executor() noexcept -> |
| 74 | executor_type |
| 75 | { |
| 76 | return impl_->stream().get_executor(); |
| 77 | } |
| 78 | |
| 79 | template<class NextLayer, bool deflateSupported> |
| 80 | auto |
| 81 | stream<NextLayer, deflateSupported>:: |
| 82 | next_layer() noexcept -> |
| 83 | next_layer_type& |
| 84 | { |
| 85 | return impl_->stream(); |
| 86 | } |
| 87 | |
| 88 | template<class NextLayer, bool deflateSupported> |
| 89 | auto |
| 90 | stream<NextLayer, deflateSupported>:: |
| 91 | next_layer() const noexcept -> |
| 92 | next_layer_type const& |
| 93 | { |
| 94 | return impl_->stream(); |
| 95 | } |
| 96 | |
| 97 | template<class NextLayer, bool deflateSupported> |
| 98 | bool |
| 99 | stream<NextLayer, deflateSupported>:: |
| 100 | is_open() const noexcept |
| 101 | { |
| 102 | return impl_->status_ == status::open; |
| 103 | } |
| 104 | |
| 105 | template<class NextLayer, bool deflateSupported> |
| 106 | bool |
| 107 | stream<NextLayer, deflateSupported>:: |
| 108 | got_binary() const noexcept |
| 109 | { |
| 110 | return impl_->rd_op == detail::opcode::binary; |
| 111 | } |
| 112 | |
| 113 | template<class NextLayer, bool deflateSupported> |
| 114 | bool |
| 115 | stream<NextLayer, deflateSupported>:: |
| 116 | is_message_done() const noexcept |
| 117 | { |
| 118 | return impl_->rd_done; |
| 119 | } |
| 120 | |
| 121 | template<class NextLayer, bool deflateSupported> |
| 122 | close_reason const& |
| 123 | stream<NextLayer, deflateSupported>:: |
| 124 | reason() const noexcept |
| 125 | { |
| 126 | return impl_->cr; |
| 127 | } |
| 128 | |
| 129 | template<class NextLayer, bool deflateSupported> |
| 130 | std::size_t |
| 131 | stream<NextLayer, deflateSupported>:: |
| 132 | read_size_hint( |
| 133 | std::size_t initial_size) const |
| 134 | { |
| 135 | return impl_->read_size_hint_pmd( |
| 136 | initial_size, impl_->rd_done, |
| 137 | impl_->rd_remain, impl_->rd_fh); |
| 138 | } |
| 139 | |
| 140 | template<class NextLayer, bool deflateSupported> |
| 141 | template<class DynamicBuffer, class> |
| 142 | std::size_t |
| 143 | stream<NextLayer, deflateSupported>:: |
| 144 | read_size_hint(DynamicBuffer& buffer) const |
| 145 | { |
| 146 | static_assert( |
| 147 | net::is_dynamic_buffer<DynamicBuffer>::value, |
| 148 | "DynamicBuffer type requirements not met" ); |
| 149 | return impl_->read_size_hint_db(buffer); |
| 150 | } |
| 151 | |
| 152 | //------------------------------------------------------------------------------ |
| 153 | // |
| 154 | // Settings |
| 155 | // |
| 156 | //------------------------------------------------------------------------------ |
| 157 | |
| 158 | // decorator |
| 159 | |
| 160 | template<class NextLayer, bool deflateSupported> |
| 161 | void |
| 162 | stream<NextLayer, deflateSupported>:: |
| 163 | set_option(decorator opt) |
| 164 | { |
| 165 | impl_->decorator_opt = std::move(opt.d_); |
| 166 | } |
| 167 | |
| 168 | // timeout |
| 169 | |
| 170 | template<class NextLayer, bool deflateSupported> |
| 171 | void |
| 172 | stream<NextLayer, deflateSupported>:: |
| 173 | get_option(timeout& opt) |
| 174 | { |
| 175 | opt = impl_->timeout_opt; |
| 176 | } |
| 177 | |
| 178 | template<class NextLayer, bool deflateSupported> |
| 179 | void |
| 180 | stream<NextLayer, deflateSupported>:: |
| 181 | set_option(timeout const& opt) |
| 182 | { |
| 183 | impl_->set_option(opt); |
| 184 | } |
| 185 | |
| 186 | // |
| 187 | |
| 188 | template<class NextLayer, bool deflateSupported> |
| 189 | void |
| 190 | stream<NextLayer, deflateSupported>:: |
| 191 | set_option(permessage_deflate const& o) |
| 192 | { |
| 193 | impl_->set_option_pmd(o); |
| 194 | } |
| 195 | |
| 196 | template<class NextLayer, bool deflateSupported> |
| 197 | void |
| 198 | stream<NextLayer, deflateSupported>:: |
| 199 | get_option(permessage_deflate& o) |
| 200 | { |
| 201 | impl_->get_option_pmd(o); |
| 202 | } |
| 203 | |
| 204 | template<class NextLayer, bool deflateSupported> |
| 205 | void |
| 206 | stream<NextLayer, deflateSupported>:: |
| 207 | auto_fragment(bool value) |
| 208 | { |
| 209 | impl_->wr_frag_opt = value; |
| 210 | } |
| 211 | |
| 212 | template<class NextLayer, bool deflateSupported> |
| 213 | bool |
| 214 | stream<NextLayer, deflateSupported>:: |
| 215 | auto_fragment() const |
| 216 | { |
| 217 | return impl_->wr_frag_opt; |
| 218 | } |
| 219 | |
| 220 | template<class NextLayer, bool deflateSupported> |
| 221 | void |
| 222 | stream<NextLayer, deflateSupported>:: |
| 223 | binary(bool value) |
| 224 | { |
| 225 | impl_->wr_opcode = value ? |
| 226 | detail::opcode::binary : |
| 227 | detail::opcode::text; |
| 228 | } |
| 229 | |
| 230 | template<class NextLayer, bool deflateSupported> |
| 231 | bool |
| 232 | stream<NextLayer, deflateSupported>:: |
| 233 | binary() const |
| 234 | { |
| 235 | return impl_->wr_opcode == detail::opcode::binary; |
| 236 | } |
| 237 | |
| 238 | template<class NextLayer, bool deflateSupported> |
| 239 | void |
| 240 | stream<NextLayer, deflateSupported>:: |
| 241 | control_callback(std::function< |
| 242 | void(frame_type, string_view)> cb) |
| 243 | { |
| 244 | impl_->ctrl_cb = std::move(cb); |
| 245 | } |
| 246 | |
| 247 | template<class NextLayer, bool deflateSupported> |
| 248 | void |
| 249 | stream<NextLayer, deflateSupported>:: |
| 250 | control_callback() |
| 251 | { |
| 252 | impl_->ctrl_cb = {}; |
| 253 | } |
| 254 | |
| 255 | template<class NextLayer, bool deflateSupported> |
| 256 | void |
| 257 | stream<NextLayer, deflateSupported>:: |
| 258 | read_message_max(std::size_t amount) |
| 259 | { |
| 260 | impl_->rd_msg_max = amount; |
| 261 | } |
| 262 | |
| 263 | template<class NextLayer, bool deflateSupported> |
| 264 | std::size_t |
| 265 | stream<NextLayer, deflateSupported>:: |
| 266 | read_message_max() const |
| 267 | { |
| 268 | return impl_->rd_msg_max; |
| 269 | } |
| 270 | |
| 271 | template<class NextLayer, bool deflateSupported> |
| 272 | void |
| 273 | stream<NextLayer, deflateSupported>:: |
| 274 | secure_prng(bool value) |
| 275 | { |
| 276 | this->impl_->secure_prng_ = value; |
| 277 | } |
| 278 | |
| 279 | template<class NextLayer, bool deflateSupported> |
| 280 | void |
| 281 | stream<NextLayer, deflateSupported>:: |
| 282 | write_buffer_bytes(std::size_t amount) |
| 283 | { |
| 284 | if(amount < 8) |
| 285 | BOOST_THROW_EXCEPTION(std::invalid_argument{ |
| 286 | "write buffer size underflow" }); |
| 287 | impl_->wr_buf_opt = amount; |
| 288 | } |
| 289 | |
| 290 | template<class NextLayer, bool deflateSupported> |
| 291 | std::size_t |
| 292 | stream<NextLayer, deflateSupported>:: |
| 293 | write_buffer_bytes() const |
| 294 | { |
| 295 | return impl_->wr_buf_opt; |
| 296 | } |
| 297 | |
| 298 | template<class NextLayer, bool deflateSupported> |
| 299 | void |
| 300 | stream<NextLayer, deflateSupported>:: |
| 301 | text(bool value) |
| 302 | { |
| 303 | impl_->wr_opcode = value ? |
| 304 | detail::opcode::text : |
| 305 | detail::opcode::binary; |
| 306 | } |
| 307 | |
| 308 | template<class NextLayer, bool deflateSupported> |
| 309 | bool |
| 310 | stream<NextLayer, deflateSupported>:: |
| 311 | text() const |
| 312 | { |
| 313 | return impl_->wr_opcode == detail::opcode::text; |
| 314 | } |
| 315 | |
| 316 | template<class NextLayer, bool deflateSupported> |
| 317 | void |
| 318 | stream<NextLayer, deflateSupported>:: |
| 319 | compress(bool value) |
| 320 | { |
| 321 | impl_->wr_compress_opt = value; |
| 322 | } |
| 323 | |
| 324 | template<class NextLayer, bool deflateSupported> |
| 325 | bool |
| 326 | stream<NextLayer, deflateSupported>:: |
| 327 | compress() const |
| 328 | { |
| 329 | return impl_->wr_compress_opt; |
| 330 | } |
| 331 | |
| 332 | //------------------------------------------------------------------------------ |
| 333 | |
| 334 | // _Fail the WebSocket Connection_ |
| 335 | template<class NextLayer, bool deflateSupported> |
| 336 | void |
| 337 | stream<NextLayer, deflateSupported>:: |
| 338 | do_fail( |
| 339 | std::uint16_t code, // if set, send a close frame first |
| 340 | error_code ev, // error code to use upon success |
| 341 | error_code& ec) // set to the error, else set to ev |
| 342 | { |
| 343 | BOOST_ASSERT(ev); |
| 344 | impl_->change_status(status::closing); |
| 345 | if(code != close_code::none && ! impl_->wr_close) |
| 346 | { |
| 347 | impl_->wr_close = true; |
| 348 | detail::frame_buffer fb; |
| 349 | impl_->template write_close< |
| 350 | flat_static_buffer_base>(fb, code); |
| 351 | net::write(impl_->stream(), fb.data(), ec); |
| 352 | if(impl_->check_stop_now(ec)) |
| 353 | return; |
| 354 | } |
| 355 | using beast::websocket::teardown; |
| 356 | teardown(impl_->role, impl_->stream(), ec); |
| 357 | if(ec == net::error::eof) |
| 358 | { |
| 359 | // Rationale: |
| 360 | // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error |
| 361 | ec = {}; |
| 362 | } |
| 363 | if(! ec) |
| 364 | { |
| 365 | BOOST_BEAST_ASSIGN_EC(ec, ev); |
| 366 | } |
| 367 | if(ec && ec != error::closed) |
| 368 | impl_->change_status(status::failed); |
| 369 | else |
| 370 | impl_->change_status(status::closed); |
| 371 | impl_->close(); |
| 372 | } |
| 373 | |
| 374 | } // websocket |
| 375 | } // beast |
| 376 | } // boost |
| 377 | |
| 378 | #endif |
| 379 | |