// // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // Official repository: https://github.com/boostorg/beast // #ifndef BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP #define BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace beast { namespace websocket { // Respond to an upgrade HTTP request template template class stream::response_op : public boost::asio::coroutine { struct data { stream& ws; response_type res; template data(Handler&, stream& ws_, http::request< Body, http::basic_fields> const& req, Decorator const& decorator) : ws(ws_) , res(ws_.build_response(req, decorator)) { } }; handler_ptr d_; public: response_op(response_op&&) = default; response_op(response_op const&) = default; template response_op(DeducedHandler&& h, stream& ws, Args&&... args) : d_(std::forward(h), ws, std::forward(args)...) { } using allocator_type = boost::asio::associated_allocator_t; allocator_type get_allocator() const noexcept { return boost::asio::get_associated_allocator(d_.handler()); } using executor_type = boost::asio::associated_executor_t< Handler, decltype(std::declval&>().get_executor())>; executor_type get_executor() const noexcept { return boost::asio::get_associated_executor( d_.handler(), d_->ws.get_executor()); } void operator()( error_code ec = {}, std::size_t bytes_transferred = 0); friend bool asio_handler_is_continuation(response_op* op) { using boost::asio::asio_handler_is_continuation; return asio_handler_is_continuation( std::addressof(op->d_.handler())); } }; template template void stream:: response_op:: operator()( error_code ec, std::size_t) { auto& d = *d_; BOOST_ASIO_CORO_REENTER(*this) { // Send response BOOST_ASIO_CORO_YIELD http::async_write(d.ws.next_layer(), d.res, std::move(*this)); if(! ec && d.res.result() != http::status::switching_protocols) ec = error::handshake_failed; if(! ec) { pmd_read(d.ws.pmd_config_, d.res); d.ws.open(role_type::server); } d_.invoke(ec); } } //------------------------------------------------------------------------------ // read and respond to an upgrade request // template template class stream::accept_op : public boost::asio::coroutine { struct data { stream& ws; Decorator decorator; http::request_parser p; data(Handler&, stream& ws_, Decorator const& decorator_) : ws(ws_) , decorator(decorator_) { } }; handler_ptr d_; public: accept_op(accept_op&&) = default; accept_op(accept_op const&) = default; template accept_op(DeducedHandler&& h, stream& ws, Args&&... args) : d_(std::forward(h), ws, std::forward(args)...) { } using allocator_type = boost::asio::associated_allocator_t; allocator_type get_allocator() const noexcept { return boost::asio::get_associated_allocator(d_.handler()); } using executor_type = boost::asio::associated_executor_t< Handler, decltype(std::declval&>().get_executor())>; executor_type get_executor() const noexcept { return boost::asio::get_associated_executor( d_.handler(), d_->ws.get_executor()); } template void run(Buffers const& buffers); void operator()( error_code ec = {}, std::size_t bytes_used = 0); friend bool asio_handler_is_continuation(accept_op* op) { using boost::asio::asio_handler_is_continuation; return asio_handler_is_continuation( std::addressof(op->d_.handler())); } }; template template template void stream:: accept_op:: run(Buffers const& buffers) { using boost::asio::buffer_copy; using boost::asio::buffer_size; auto& d = *d_; error_code ec; boost::optional mb; auto const len = buffer_size(buffers); try { mb.emplace(d.ws.rd_buf_.prepare(len)); } catch(std::length_error const&) { ec = error::buffer_overflow; return (*this)(ec); } d.ws.rd_buf_.commit( buffer_copy(*mb, buffers)); (*this)(ec); } template template void stream:: accept_op:: operator()(error_code ec, std::size_t) { auto& d = *d_; BOOST_ASIO_CORO_REENTER(*this) { if(ec) { BOOST_ASIO_CORO_YIELD boost::asio::post( d.ws.get_executor(), bind_handler(std::move(*this), ec)); } else { BOOST_ASIO_CORO_YIELD http::async_read( d.ws.next_layer(), d.ws.rd_buf_, d.p, std::move(*this)); if(ec == http::error::end_of_stream) ec = error::closed; if(! ec) { // Arguments from our state must be // moved to the stack before releasing // the handler. auto& ws = d.ws; auto const req = d.p.release(); auto const decorator = d.decorator; #if 1 return response_op{ d_.release_handler(), ws, req, decorator}(ec); #else // VFALCO This *should* work but breaks // coroutine invariants in the unit test. // Also it calls reset() when it shouldn't. return ws.async_accept_ex( req, decorator, d_.release_handler()); #endif } } d_.invoke(ec); } } //------------------------------------------------------------------------------ template void stream:: accept() { static_assert(is_sync_stream::value, "SyncStream requirements not met"); error_code ec; accept(ec); if(ec) BOOST_THROW_EXCEPTION(system_error{ec}); } template template void stream:: accept_ex(ResponseDecorator const& decorator) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); error_code ec; accept_ex(decorator, ec); if(ec) BOOST_THROW_EXCEPTION(system_error{ec}); } template void stream:: accept(error_code& ec) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); reset(); do_accept(&default_decorate_res, ec); } template template void stream:: accept_ex(ResponseDecorator const& decorator, error_code& ec) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); reset(); do_accept(decorator, ec); } template template typename std::enable_if::value>::type stream:: accept(ConstBufferSequence const& buffers) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); error_code ec; accept(buffers, ec); if(ec) BOOST_THROW_EXCEPTION(system_error{ec}); } template template< class ConstBufferSequence, class ResponseDecorator> typename std::enable_if::value>::type stream:: accept_ex( ConstBufferSequence const& buffers, ResponseDecorator const &decorator) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); error_code ec; accept_ex(buffers, decorator, ec); if(ec) BOOST_THROW_EXCEPTION(system_error{ec}); } template template typename std::enable_if::value>::type stream:: accept( ConstBufferSequence const& buffers, error_code& ec) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); using boost::asio::buffer_copy; using boost::asio::buffer_size; reset(); boost::optional mb; try { mb.emplace(rd_buf_.prepare( buffer_size(buffers))); } catch(std::length_error const&) { ec = error::buffer_overflow; return; } rd_buf_.commit( buffer_copy(*mb, buffers)); do_accept(&default_decorate_res, ec); } template template< class ConstBufferSequence, class ResponseDecorator> typename std::enable_if::value>::type stream:: accept_ex( ConstBufferSequence const& buffers, ResponseDecorator const& decorator, error_code& ec) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); using boost::asio::buffer_copy; using boost::asio::buffer_size; reset(); boost::optional mb; try { mb.emplace(rd_buf_.prepare( buffer_size(buffers))); } catch(std::length_error const&) { ec = error::buffer_overflow; return; } rd_buf_.commit(buffer_copy(*mb, buffers)); do_accept(decorator, ec); } template template void stream:: accept( http::request> const& req) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); error_code ec; accept(req, ec); if(ec) BOOST_THROW_EXCEPTION(system_error{ec}); } template template< class Body, class Allocator, class ResponseDecorator> void stream:: accept_ex( http::request> const& req, ResponseDecorator const& decorator) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); error_code ec; accept_ex(req, decorator, ec); if(ec) BOOST_THROW_EXCEPTION(system_error{ec}); } template template void stream:: accept( http::request> const& req, error_code& ec) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); reset(); do_accept(req, &default_decorate_res, ec); } template template< class Body, class Allocator, class ResponseDecorator> void stream:: accept_ex( http::request> const& req, ResponseDecorator const& decorator, error_code& ec) { static_assert(is_sync_stream::value, "SyncStream requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); reset(); do_accept(req, decorator, ec); } //------------------------------------------------------------------------------ template template< class AcceptHandler> BOOST_ASIO_INITFN_RESULT_TYPE( AcceptHandler, void(error_code)) stream:: async_accept( AcceptHandler&& handler) { static_assert(is_async_stream::value, "AsyncStream requirements requirements not met"); boost::asio::async_completion init{handler}; reset(); accept_op< decltype(&default_decorate_res), BOOST_ASIO_HANDLER_TYPE( AcceptHandler, void(error_code))>{ init.completion_handler, *this, &default_decorate_res}({}); return init.result.get(); } template template< class ResponseDecorator, class AcceptHandler> BOOST_ASIO_INITFN_RESULT_TYPE( AcceptHandler, void(error_code)) stream:: async_accept_ex( ResponseDecorator const& decorator, AcceptHandler&& handler) { static_assert(is_async_stream::value, "AsyncStream requirements requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); boost::asio::async_completion init{handler}; reset(); accept_op< ResponseDecorator, BOOST_ASIO_HANDLER_TYPE( AcceptHandler, void(error_code))>{ init.completion_handler, *this, decorator}({}); return init.result.get(); } template template< class ConstBufferSequence, class AcceptHandler> typename std::enable_if< ! http::detail::is_header::value, BOOST_ASIO_INITFN_RESULT_TYPE( AcceptHandler, void(error_code))>::type stream:: async_accept( ConstBufferSequence const& buffers, AcceptHandler&& handler) { static_assert(is_async_stream::value, "AsyncStream requirements requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); boost::asio::async_completion init{handler}; reset(); accept_op< decltype(&default_decorate_res), BOOST_ASIO_HANDLER_TYPE( AcceptHandler, void(error_code))>{ init.completion_handler, *this, &default_decorate_res}.run(buffers); return init.result.get(); } template template< class ConstBufferSequence, class ResponseDecorator, class AcceptHandler> typename std::enable_if< ! http::detail::is_header::value, BOOST_ASIO_INITFN_RESULT_TYPE( AcceptHandler, void(error_code))>::type stream:: async_accept_ex( ConstBufferSequence const& buffers, ResponseDecorator const& decorator, AcceptHandler&& handler) { static_assert(is_async_stream::value, "AsyncStream requirements requirements not met"); static_assert(boost::asio::is_const_buffer_sequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); boost::asio::async_completion init{handler}; reset(); accept_op< ResponseDecorator, BOOST_ASIO_HANDLER_TYPE( AcceptHandler, void(error_code))>{ init.completion_handler, *this, decorator}.run(buffers); return init.result.get(); } template template< class Body, class Allocator, class AcceptHandler> BOOST_ASIO_INITFN_RESULT_TYPE( AcceptHandler, void(error_code)) stream:: async_accept( http::request> const& req, AcceptHandler&& handler) { static_assert(is_async_stream::value, "AsyncStream requirements requirements not met"); boost::asio::async_completion init{handler}; reset(); using boost::asio::asio_handler_is_continuation; response_op< BOOST_ASIO_HANDLER_TYPE( AcceptHandler, void(error_code))>{ init.completion_handler, *this, req, &default_decorate_res}(); return init.result.get(); } template template< class Body, class Allocator, class ResponseDecorator, class AcceptHandler> BOOST_ASIO_INITFN_RESULT_TYPE( AcceptHandler, void(error_code)) stream:: async_accept_ex( http::request> const& req, ResponseDecorator const& decorator, AcceptHandler&& handler) { static_assert(is_async_stream::value, "AsyncStream requirements requirements not met"); static_assert(detail::is_ResponseDecorator< ResponseDecorator>::value, "ResponseDecorator requirements not met"); boost::asio::async_completion init{handler}; reset(); using boost::asio::asio_handler_is_continuation; response_op< BOOST_ASIO_HANDLER_TYPE( AcceptHandler, void(error_code))>{ init.completion_handler, *this, req, decorator}(); return init.result.get(); } //------------------------------------------------------------------------------ template template void stream:: do_accept( Decorator const& decorator, error_code& ec) { http::request_parser p; http::read(next_layer(), rd_buf_, p, ec); if(ec == http::error::end_of_stream) ec = error::closed; if(ec) return; do_accept(p.get(), decorator, ec); } template template void stream:: do_accept( http::request> const& req, Decorator const& decorator, error_code& ec) { auto const res = build_response(req, decorator); http::write(stream_, res, ec); if(ec) return; if(res.result() != http::status::switching_protocols) { ec = error::handshake_failed; // VFALCO TODO Respect keep alive setting, perform // teardown if Connection: close. return; } pmd_read(pmd_config_, res); open(role_type::server); } } // websocket } // beast } // boost #endif