123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386 |
- //
- // Copyright (c) 2016-2019 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_READ_HPP
- #define BOOST_BEAST_WEBSOCKET_IMPL_READ_HPP
- #include <boost/beast/core/buffer_traits.hpp>
- #include <boost/beast/websocket/teardown.hpp>
- #include <boost/beast/websocket/detail/mask.hpp>
- #include <boost/beast/websocket/impl/stream_impl.hpp>
- #include <boost/beast/core/async_base.hpp>
- #include <boost/beast/core/bind_handler.hpp>
- #include <boost/beast/core/buffers_prefix.hpp>
- #include <boost/beast/core/buffers_suffix.hpp>
- #include <boost/beast/core/flat_static_buffer.hpp>
- #include <boost/beast/core/read_size.hpp>
- #include <boost/beast/core/stream_traits.hpp>
- #include <boost/beast/core/detail/bind_continuation.hpp>
- #include <boost/beast/core/detail/buffer.hpp>
- #include <boost/beast/core/detail/clamp.hpp>
- #include <boost/beast/core/detail/config.hpp>
- #include <boost/asio/coroutine.hpp>
- #include <boost/asio/post.hpp>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/optional.hpp>
- #include <boost/throw_exception.hpp>
- #include <algorithm>
- #include <limits>
- #include <memory>
- namespace boost {
- namespace beast {
- namespace websocket {
- /* Read some message data into a buffer sequence.
- Also reads and handles control frames.
- */
- template<class NextLayer, bool deflateSupported>
- template<class Handler, class MutableBufferSequence>
- class stream<NextLayer, deflateSupported>::read_some_op
- : public beast::async_base<
- Handler, beast::executor_type<stream>>
- , public asio::coroutine
- {
- boost::weak_ptr<impl_type> wp_;
- MutableBufferSequence bs_;
- buffers_suffix<MutableBufferSequence> cb_;
- std::size_t bytes_written_ = 0;
- error_code result_;
- close_code code_;
- bool did_read_ = false;
- public:
- static constexpr int id = 1; // for soft_mutex
- template<class Handler_>
- read_some_op(
- Handler_&& h,
- boost::shared_ptr<impl_type> const& sp,
- MutableBufferSequence const& bs)
- : async_base<
- Handler, beast::executor_type<stream>>(
- std::forward<Handler_>(h),
- sp->stream().get_executor())
- , wp_(sp)
- , bs_(bs)
- , cb_(bs)
- , code_(close_code::none)
- {
- (*this)({}, 0, false);
- }
- void operator()(
- error_code ec = {},
- std::size_t bytes_transferred = 0,
- bool cont = true)
- {
- using beast::detail::clamp;
- auto sp = wp_.lock();
- if(! sp)
- {
- ec = net::error::operation_aborted;
- bytes_written_ = 0;
- return this->complete(cont, ec, bytes_written_);
- }
- auto& impl = *sp;
- BOOST_ASIO_CORO_REENTER(*this)
- {
- impl.update_timer(this->get_executor());
- acquire_read_lock:
- // Acquire the read lock
- if(! impl.rd_block.try_lock(this))
- {
- do_suspend:
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.op_r_rd.emplace(std::move(*this));
- }
- impl.rd_block.lock(this);
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::post(std::move(*this));
- }
- BOOST_ASSERT(impl.rd_block.is_locked(this));
- BOOST_ASSERT(!ec);
- if(impl.check_stop_now(ec))
- {
- BOOST_ASSERT(ec == net::error::operation_aborted);
- goto upcall;
- }
- // VFALCO Should never get here
- // The only way to get read blocked is if
- // a `close_op` wrote a close frame
- BOOST_ASSERT(impl.wr_close);
- BOOST_ASSERT(impl.status_ != status::open);
- ec = net::error::operation_aborted;
- goto upcall;
- }
- else
- {
- // Make sure the stream is not closed
- if( impl.status_ == status::closed ||
- impl.status_ == status::failed)
- {
- ec = net::error::operation_aborted;
- goto upcall;
- }
- }
- // if status_ == status::closing, we want to suspend
- // the read operation until the close completes,
- // then finish the read with operation_aborted.
- loop:
- BOOST_ASSERT(impl.rd_block.is_locked(this));
- // See if we need to read a frame header. This
- // condition is structured to give the decompressor
- // a chance to emit the final empty deflate block
- //
- if(impl.rd_remain == 0 &&
- (! impl.rd_fh.fin || impl.rd_done))
- {
- // Read frame header
- while(! impl.parse_fh(
- impl.rd_fh, impl.rd_buf, result_))
- {
- if(result_)
- {
- // _Fail the WebSocket Connection_
- if(result_ == error::message_too_big)
- code_ = close_code::too_big;
- else
- code_ = close_code::protocol_error;
- goto close;
- }
- BOOST_ASSERT(impl.rd_block.is_locked(this));
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.stream().async_read_some(
- impl.rd_buf.prepare(read_size(
- impl.rd_buf, impl.rd_buf.max_size())),
- std::move(*this));
- }
- BOOST_ASSERT(impl.rd_block.is_locked(this));
- impl.rd_buf.commit(bytes_transferred);
- if(impl.check_stop_now(ec))
- goto upcall;
- impl.reset_idle();
- // Allow a close operation
- // to acquire the read block
- impl.rd_block.unlock(this);
- if( impl.op_r_close.maybe_invoke())
- {
- // Suspend
- BOOST_ASSERT(impl.rd_block.is_locked());
- goto do_suspend;
- }
- // Acquire read block
- impl.rd_block.lock(this);
- }
- // Immediately apply the mask to the portion
- // of the buffer holding payload data.
- if(impl.rd_fh.len > 0 && impl.rd_fh.mask)
- detail::mask_inplace(buffers_prefix(
- clamp(impl.rd_fh.len),
- impl.rd_buf.data()),
- impl.rd_key);
- if(detail::is_control(impl.rd_fh.op))
- {
- // Clear this otherwise the next
- // frame will be considered final.
- impl.rd_fh.fin = false;
- // Handle ping frame
- if(impl.rd_fh.op == detail::opcode::ping)
- {
- if(impl.ctrl_cb)
- {
- if(! cont)
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::post(std::move(*this));
- }
- BOOST_ASSERT(cont);
- // VFALCO call check_stop_now() here?
- }
- }
- {
- auto const b = buffers_prefix(
- clamp(impl.rd_fh.len),
- impl.rd_buf.data());
- auto const len = buffer_bytes(b);
- BOOST_ASSERT(len == impl.rd_fh.len);
- ping_data payload;
- detail::read_ping(payload, b);
- impl.rd_buf.consume(len);
- // Ignore ping when closing
- if(impl.status_ == status::closing)
- goto loop;
- if(impl.ctrl_cb)
- impl.ctrl_cb(
- frame_type::ping, payload);
- impl.rd_fb.clear();
- impl.template write_ping<
- flat_static_buffer_base>(impl.rd_fb,
- detail::opcode::pong, payload);
- }
- // Allow a close operation
- // to acquire the read block
- impl.rd_block.unlock(this);
- impl.op_r_close.maybe_invoke();
- // Acquire the write lock
- if(! impl.wr_block.try_lock(this))
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.op_rd.emplace(std::move(*this));
- }
- impl.wr_block.lock(this);
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::post(std::move(*this));
- }
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- if(impl.check_stop_now(ec))
- goto upcall;
- }
- // Send pong
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::async_write(
- impl.stream(), net::const_buffer(impl.rd_fb.data()),
- beast::detail::bind_continuation(std::move(*this)));
- }
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- if(impl.check_stop_now(ec))
- goto upcall;
- impl.wr_block.unlock(this);
- impl.op_close.maybe_invoke()
- || impl.op_idle_ping.maybe_invoke()
- || impl.op_ping.maybe_invoke()
- || impl.op_wr.maybe_invoke();
- goto acquire_read_lock;
- }
- // Handle pong frame
- if(impl.rd_fh.op == detail::opcode::pong)
- {
- // Ignore pong when closing
- if(! impl.wr_close && impl.ctrl_cb)
- {
- if(! cont)
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::post(std::move(*this));
- }
- BOOST_ASSERT(cont);
- }
- }
- auto const cb = buffers_prefix(clamp(
- impl.rd_fh.len), impl.rd_buf.data());
- auto const len = buffer_bytes(cb);
- BOOST_ASSERT(len == impl.rd_fh.len);
- ping_data payload;
- detail::read_ping(payload, cb);
- impl.rd_buf.consume(len);
- // Ignore pong when closing
- if(! impl.wr_close && impl.ctrl_cb)
- impl.ctrl_cb(frame_type::pong, payload);
- goto loop;
- }
- // Handle close frame
- BOOST_ASSERT(impl.rd_fh.op == detail::opcode::close);
- {
- if(impl.ctrl_cb)
- {
- if(! cont)
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::post(std::move(*this));
- }
- BOOST_ASSERT(cont);
- }
- }
- auto const cb = buffers_prefix(clamp(
- impl.rd_fh.len), impl.rd_buf.data());
- auto const len = buffer_bytes(cb);
- BOOST_ASSERT(len == impl.rd_fh.len);
- BOOST_ASSERT(! impl.rd_close);
- impl.rd_close = true;
- close_reason cr;
- detail::read_close(cr, cb, result_);
- if(result_)
- {
- // _Fail the WebSocket Connection_
- code_ = close_code::protocol_error;
- goto close;
- }
- impl.cr = cr;
- impl.rd_buf.consume(len);
- if(impl.ctrl_cb)
- impl.ctrl_cb(frame_type::close,
- impl.cr.reason);
- // See if we are already closing
- if(impl.status_ == status::closing)
- {
- // _Close the WebSocket Connection_
- BOOST_ASSERT(impl.wr_close);
- code_ = close_code::none;
- result_ = error::closed;
- goto close;
- }
- // _Start the WebSocket Closing Handshake_
- code_ = cr.code == close_code::none ?
- close_code::normal :
- static_cast<close_code>(cr.code);
- result_ = error::closed;
- goto close;
- }
- }
- if(impl.rd_fh.len == 0 && ! impl.rd_fh.fin)
- {
- // Empty non-final frame
- goto loop;
- }
- impl.rd_done = false;
- }
- if(! impl.rd_deflated())
- {
- if(impl.rd_remain > 0)
- {
- if(impl.rd_buf.size() == 0 && impl.rd_buf.max_size() >
- (std::min)(clamp(impl.rd_remain),
- buffer_bytes(cb_)))
- {
- // Fill the read buffer first, otherwise we
- // get fewer bytes at the cost of one I/O.
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.stream().async_read_some(
- impl.rd_buf.prepare(read_size(
- impl.rd_buf, impl.rd_buf.max_size())),
- std::move(*this));
- }
- impl.rd_buf.commit(bytes_transferred);
- if(impl.check_stop_now(ec))
- goto upcall;
- impl.reset_idle();
- if(impl.rd_fh.mask)
- detail::mask_inplace(buffers_prefix(clamp(
- impl.rd_remain), impl.rd_buf.data()),
- impl.rd_key);
- }
- if(impl.rd_buf.size() > 0)
- {
- // Copy from the read buffer.
- // The mask was already applied.
- bytes_transferred = net::buffer_copy(cb_,
- impl.rd_buf.data(), clamp(impl.rd_remain));
- auto const mb = buffers_prefix(
- bytes_transferred, cb_);
- impl.rd_remain -= bytes_transferred;
- if(impl.rd_op == detail::opcode::text)
- {
- if(! impl.rd_utf8.write(mb) ||
- (impl.rd_remain == 0 && impl.rd_fh.fin &&
- ! impl.rd_utf8.finish()))
- {
- // _Fail the WebSocket Connection_
- code_ = close_code::bad_payload;
- result_ = error::bad_frame_payload;
- goto close;
- }
- }
- bytes_written_ += bytes_transferred;
- impl.rd_size += bytes_transferred;
- impl.rd_buf.consume(bytes_transferred);
- }
- else
- {
- // Read into caller's buffer
- BOOST_ASSERT(impl.rd_remain > 0);
- BOOST_ASSERT(buffer_bytes(cb_) > 0);
- BOOST_ASSERT(buffer_bytes(buffers_prefix(
- clamp(impl.rd_remain), cb_)) > 0);
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.stream().async_read_some(buffers_prefix(
- clamp(impl.rd_remain), cb_), std::move(*this));
- }
- if(impl.check_stop_now(ec))
- goto upcall;
- impl.reset_idle();
- BOOST_ASSERT(bytes_transferred > 0);
- auto const mb = buffers_prefix(
- bytes_transferred, cb_);
- impl.rd_remain -= bytes_transferred;
- if(impl.rd_fh.mask)
- detail::mask_inplace(mb, impl.rd_key);
- if(impl.rd_op == detail::opcode::text)
- {
- if(! impl.rd_utf8.write(mb) ||
- (impl.rd_remain == 0 && impl.rd_fh.fin &&
- ! impl.rd_utf8.finish()))
- {
- // _Fail the WebSocket Connection_
- code_ = close_code::bad_payload;
- result_ = error::bad_frame_payload;
- goto close;
- }
- }
- bytes_written_ += bytes_transferred;
- impl.rd_size += bytes_transferred;
- }
- }
- BOOST_ASSERT( ! impl.rd_done );
- if( impl.rd_remain == 0 && impl.rd_fh.fin )
- impl.rd_done = true;
- }
- else
- {
- // Read compressed message frame payload:
- // inflate even if rd_fh_.len == 0, otherwise we
- // never emit the end-of-stream deflate block.
- while(buffer_bytes(cb_) > 0)
- {
- if( impl.rd_remain > 0 &&
- impl.rd_buf.size() == 0 &&
- ! did_read_)
- {
- // read new
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.stream().async_read_some(
- impl.rd_buf.prepare(read_size(
- impl.rd_buf, impl.rd_buf.max_size())),
- std::move(*this));
- }
- if(impl.check_stop_now(ec))
- goto upcall;
- impl.reset_idle();
- BOOST_ASSERT(bytes_transferred > 0);
- impl.rd_buf.commit(bytes_transferred);
- if(impl.rd_fh.mask)
- detail::mask_inplace(
- buffers_prefix(clamp(impl.rd_remain),
- impl.rd_buf.data()), impl.rd_key);
- did_read_ = true;
- }
- zlib::z_params zs;
- {
- auto const out = buffers_front(cb_);
- zs.next_out = out.data();
- zs.avail_out = out.size();
- BOOST_ASSERT(zs.avail_out > 0);
- }
- // boolean to track the end of the message.
- bool fin = false;
- if(impl.rd_remain > 0)
- {
- if(impl.rd_buf.size() > 0)
- {
- // use what's there
- auto const in = buffers_prefix(
- clamp(impl.rd_remain), buffers_front(
- impl.rd_buf.data()));
- zs.avail_in = in.size();
- zs.next_in = in.data();
- }
- else
- {
- break;
- }
- }
- else if(impl.rd_fh.fin)
- {
- // append the empty block codes
- static std::uint8_t constexpr
- empty_block[4] = { 0x00, 0x00, 0xff, 0xff };
- zs.next_in = empty_block;
- zs.avail_in = sizeof(empty_block);
- fin = true;
- }
- else
- {
- break;
- }
- impl.inflate(zs, zlib::Flush::sync, ec);
- if(impl.check_stop_now(ec))
- goto upcall;
- if(fin && zs.total_out == 0) {
- impl.do_context_takeover_read(impl.role);
- impl.rd_done = true;
- break;
- }
- if(impl.rd_msg_max && beast::detail::sum_exceeds(
- impl.rd_size, zs.total_out, impl.rd_msg_max))
- {
- // _Fail the WebSocket Connection_
- code_ = close_code::too_big;
- result_ = error::message_too_big;
- goto close;
- }
- cb_.consume(zs.total_out);
- impl.rd_size += zs.total_out;
- if (! fin) {
- impl.rd_remain -= zs.total_in;
- impl.rd_buf.consume(zs.total_in);
- }
- bytes_written_ += zs.total_out;
- }
- if(impl.rd_op == detail::opcode::text)
- {
- // check utf8
- if(! impl.rd_utf8.write(
- buffers_prefix(bytes_written_, bs_)) || (
- impl.rd_done && ! impl.rd_utf8.finish()))
- {
- // _Fail the WebSocket Connection_
- code_ = close_code::bad_payload;
- result_ = error::bad_frame_payload;
- goto close;
- }
- }
- }
- goto upcall;
- close:
- // Acquire the write lock
- if(! impl.wr_block.try_lock(this))
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- impl.op_rd.emplace(std::move(*this));
- }
- impl.wr_block.lock(this);
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::post(std::move(*this));
- }
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- if(impl.check_stop_now(ec))
- goto upcall;
- }
- impl.change_status(status::closing);
- if(! impl.wr_close)
- {
- impl.wr_close = true;
- // Serialize close frame
- impl.rd_fb.clear();
- impl.template write_close<
- flat_static_buffer_base>(
- impl.rd_fb, code_);
- // Send close frame
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- net::async_write(impl.stream(), net::const_buffer(impl.rd_fb.data()),
- beast::detail::bind_continuation(std::move(*this)));
- }
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- if(impl.check_stop_now(ec))
- goto upcall;
- }
- // Teardown
- using beast::websocket::async_teardown;
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read_some"));
- async_teardown(impl.role, impl.stream(),
- beast::detail::bind_continuation(std::move(*this)));
- }
- BOOST_ASSERT(impl.wr_block.is_locked(this));
- if(ec == net::error::eof)
- {
- // Rationale:
- // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
- ec = {};
- }
- if(! ec)
- ec = result_;
- if(ec && ec != error::closed)
- impl.change_status(status::failed);
- else
- impl.change_status(status::closed);
- impl.close();
- upcall:
- impl.rd_block.try_unlock(this);
- impl.op_r_close.maybe_invoke();
- if(impl.wr_block.try_unlock(this))
- impl.op_close.maybe_invoke()
- || impl.op_idle_ping.maybe_invoke()
- || impl.op_ping.maybe_invoke()
- || impl.op_wr.maybe_invoke();
- this->complete(cont, ec, bytes_written_);
- }
- }
- };
- //------------------------------------------------------------------------------
- template<class NextLayer, bool deflateSupported>
- template<class Handler, class DynamicBuffer>
- class stream<NextLayer, deflateSupported>::read_op
- : public beast::async_base<
- Handler, beast::executor_type<stream>>
- , public asio::coroutine
- {
- boost::weak_ptr<impl_type> wp_;
- DynamicBuffer& b_;
- std::size_t limit_;
- std::size_t bytes_written_ = 0;
- bool some_;
- public:
- template<class Handler_>
- read_op(
- Handler_&& h,
- boost::shared_ptr<impl_type> const& sp,
- DynamicBuffer& b,
- std::size_t limit,
- bool some)
- : async_base<Handler,
- beast::executor_type<stream>>(
- std::forward<Handler_>(h),
- sp->stream().get_executor())
- , wp_(sp)
- , b_(b)
- , limit_(limit ? limit : (
- std::numeric_limits<std::size_t>::max)())
- , some_(some)
- {
- (*this)({}, 0, false);
- }
- void operator()(
- error_code ec = {},
- std::size_t bytes_transferred = 0,
- bool cont = true)
- {
- using beast::detail::clamp;
- auto sp = wp_.lock();
- if(! sp)
- {
- ec = net::error::operation_aborted;
- bytes_written_ = 0;
- return this->complete(cont, ec, bytes_written_);
- }
- auto& impl = *sp;
- using mutable_buffers_type = typename
- DynamicBuffer::mutable_buffers_type;
- BOOST_ASIO_CORO_REENTER(*this)
- {
- do
- {
- // VFALCO TODO use boost::beast::bind_continuation
- BOOST_ASIO_CORO_YIELD
- {
- auto mb = beast::detail::dynamic_buffer_prepare(b_,
- clamp(impl.read_size_hint_db(b_), limit_),
- ec, error::buffer_overflow);
- if(impl.check_stop_now(ec))
- goto upcall;
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "websocket::async_read"));
- read_some_op<read_op, mutable_buffers_type>(
- std::move(*this), sp, *mb);
- }
- b_.commit(bytes_transferred);
- bytes_written_ += bytes_transferred;
- if(ec)
- goto upcall;
- }
- while(! some_ && ! impl.rd_done);
- upcall:
- this->complete(cont, ec, bytes_written_);
- }
- }
- };
- template<class NextLayer, bool deflateSupported>
- struct stream<NextLayer, deflateSupported>::
- run_read_some_op
- {
- template<
- class ReadHandler,
- class MutableBufferSequence>
- void
- operator()(
- ReadHandler&& h,
- boost::shared_ptr<impl_type> const& sp,
- MutableBufferSequence const& b)
- {
- // If you get an error on the following line it means
- // that your handler does not meet the documented type
- // requirements for the handler.
- static_assert(
- beast::detail::is_invocable<ReadHandler,
- void(error_code, std::size_t)>::value,
- "ReadHandler type requirements not met");
- read_some_op<
- typename std::decay<ReadHandler>::type,
- MutableBufferSequence>(
- std::forward<ReadHandler>(h),
- sp,
- b);
- }
- };
- template<class NextLayer, bool deflateSupported>
- struct stream<NextLayer, deflateSupported>::
- run_read_op
- {
- template<
- class ReadHandler,
- class DynamicBuffer>
- void
- operator()(
- ReadHandler&& h,
- boost::shared_ptr<impl_type> const& sp,
- DynamicBuffer* b,
- std::size_t limit,
- bool some)
- {
- // If you get an error on the following line it means
- // that your handler does not meet the documented type
- // requirements for the handler.
- static_assert(
- beast::detail::is_invocable<ReadHandler,
- void(error_code, std::size_t)>::value,
- "ReadHandler type requirements not met");
- read_op<
- typename std::decay<ReadHandler>::type,
- DynamicBuffer>(
- std::forward<ReadHandler>(h),
- sp,
- *b,
- limit,
- some);
- }
- };
- //------------------------------------------------------------------------------
- template<class NextLayer, bool deflateSupported>
- template<class DynamicBuffer>
- std::size_t
- stream<NextLayer, deflateSupported>::
- read(DynamicBuffer& buffer)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream type requirements not met");
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
- error_code ec;
- auto const bytes_written = read(buffer, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_written;
- }
- template<class NextLayer, bool deflateSupported>
- template<class DynamicBuffer>
- std::size_t
- stream<NextLayer, deflateSupported>::
- read(DynamicBuffer& buffer, error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream type requirements not met");
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
- std::size_t bytes_written = 0;
- do
- {
- bytes_written += read_some(buffer, 0, ec);
- if(ec)
- return bytes_written;
- }
- while(! is_message_done());
- return bytes_written;
- }
- template<class NextLayer, bool deflateSupported>
- template<class DynamicBuffer, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
- BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
- stream<NextLayer, deflateSupported>::
- async_read(DynamicBuffer& buffer, ReadHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream type requirements not met");
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
- return net::async_initiate<
- ReadHandler,
- void(error_code, std::size_t)>(
- run_read_op{},
- handler,
- impl_,
- &buffer,
- 0,
- false);
- }
- //------------------------------------------------------------------------------
- template<class NextLayer, bool deflateSupported>
- template<class DynamicBuffer>
- std::size_t
- stream<NextLayer, deflateSupported>::
- read_some(
- DynamicBuffer& buffer,
- std::size_t limit)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream type requirements not met");
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
- error_code ec;
- auto const bytes_written =
- read_some(buffer, limit, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_written;
- }
- template<class NextLayer, bool deflateSupported>
- template<class DynamicBuffer>
- std::size_t
- stream<NextLayer, deflateSupported>::
- read_some(
- DynamicBuffer& buffer,
- std::size_t limit,
- error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream type requirements not met");
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
- using beast::detail::clamp;
- if(! limit)
- limit = (std::numeric_limits<std::size_t>::max)();
- auto const size =
- clamp(read_size_hint(buffer), limit);
- BOOST_ASSERT(size > 0);
- auto mb = beast::detail::dynamic_buffer_prepare(
- buffer, size, ec, error::buffer_overflow);
- if(impl_->check_stop_now(ec))
- return 0;
- auto const bytes_written = read_some(*mb, ec);
- buffer.commit(bytes_written);
- return bytes_written;
- }
- template<class NextLayer, bool deflateSupported>
- template<class DynamicBuffer, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
- BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
- stream<NextLayer, deflateSupported>::
- async_read_some(
- DynamicBuffer& buffer,
- std::size_t limit,
- ReadHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream type requirements not met");
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
- return net::async_initiate<
- ReadHandler,
- void(error_code, std::size_t)>(
- run_read_op{},
- handler,
- impl_,
- &buffer,
- limit,
- true);
- }
- //------------------------------------------------------------------------------
- template<class NextLayer, bool deflateSupported>
- template<class MutableBufferSequence>
- std::size_t
- stream<NextLayer, deflateSupported>::
- read_some(
- MutableBufferSequence const& buffers)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream type requirements not met");
- static_assert(net::is_mutable_buffer_sequence<
- MutableBufferSequence>::value,
- "MutableBufferSequence type requirements not met");
- error_code ec;
- auto const bytes_written = read_some(buffers, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_written;
- }
- template<class NextLayer, bool deflateSupported>
- template<class MutableBufferSequence>
- std::size_t
- stream<NextLayer, deflateSupported>::
- read_some(
- MutableBufferSequence const& buffers,
- error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream type requirements not met");
- static_assert(net::is_mutable_buffer_sequence<
- MutableBufferSequence>::value,
- "MutableBufferSequence type requirements not met");
- using beast::detail::clamp;
- auto& impl = *impl_;
- close_code code{};
- std::size_t bytes_written = 0;
- ec = {};
- // Make sure the stream is open
- if(impl.check_stop_now(ec))
- return bytes_written;
- loop:
- // See if we need to read a frame header. This
- // condition is structured to give the decompressor
- // a chance to emit the final empty deflate block
- //
- if(impl.rd_remain == 0 && (
- ! impl.rd_fh.fin || impl.rd_done))
- {
- // Read frame header
- error_code result;
- while(! impl.parse_fh(impl.rd_fh, impl.rd_buf, result))
- {
- if(result)
- {
- // _Fail the WebSocket Connection_
- if(result == error::message_too_big)
- code = close_code::too_big;
- else
- code = close_code::protocol_error;
- do_fail(code, result, ec);
- return bytes_written;
- }
- auto const bytes_transferred =
- impl.stream().read_some(
- impl.rd_buf.prepare(read_size(
- impl.rd_buf, impl.rd_buf.max_size())),
- ec);
- impl.rd_buf.commit(bytes_transferred);
- if(impl.check_stop_now(ec))
- return bytes_written;
- }
- // Immediately apply the mask to the portion
- // of the buffer holding payload data.
- if(impl.rd_fh.len > 0 && impl.rd_fh.mask)
- detail::mask_inplace(buffers_prefix(
- clamp(impl.rd_fh.len), impl.rd_buf.data()),
- impl.rd_key);
- if(detail::is_control(impl.rd_fh.op))
- {
- // Get control frame payload
- auto const b = buffers_prefix(
- clamp(impl.rd_fh.len), impl.rd_buf.data());
- auto const len = buffer_bytes(b);
- BOOST_ASSERT(len == impl.rd_fh.len);
- // Clear this otherwise the next
- // frame will be considered final.
- impl.rd_fh.fin = false;
- // Handle ping frame
- if(impl.rd_fh.op == detail::opcode::ping)
- {
- ping_data payload;
- detail::read_ping(payload, b);
- impl.rd_buf.consume(len);
- if(impl.wr_close)
- {
- // Ignore ping when closing
- goto loop;
- }
- if(impl.ctrl_cb)
- impl.ctrl_cb(frame_type::ping, payload);
- detail::frame_buffer fb;
- impl.template write_ping<flat_static_buffer_base>(fb,
- detail::opcode::pong, payload);
- net::write(impl.stream(), fb.data(), ec);
- if(impl.check_stop_now(ec))
- return bytes_written;
- goto loop;
- }
- // Handle pong frame
- if(impl.rd_fh.op == detail::opcode::pong)
- {
- ping_data payload;
- detail::read_ping(payload, b);
- impl.rd_buf.consume(len);
- if(impl.ctrl_cb)
- impl.ctrl_cb(frame_type::pong, payload);
- goto loop;
- }
- // Handle close frame
- BOOST_ASSERT(impl.rd_fh.op == detail::opcode::close);
- {
- BOOST_ASSERT(! impl.rd_close);
- impl.rd_close = true;
- close_reason cr;
- detail::read_close(cr, b, result);
- if(result)
- {
- // _Fail the WebSocket Connection_
- do_fail(close_code::protocol_error,
- result, ec);
- return bytes_written;
- }
- impl.cr = cr;
- impl.rd_buf.consume(len);
- if(impl.ctrl_cb)
- impl.ctrl_cb(frame_type::close, impl.cr.reason);
- BOOST_ASSERT(! impl.wr_close);
- // _Start the WebSocket Closing Handshake_
- do_fail(
- cr.code == close_code::none ?
- close_code::normal :
- static_cast<close_code>(cr.code),
- error::closed, ec);
- return bytes_written;
- }
- }
- if(impl.rd_fh.len == 0 && ! impl.rd_fh.fin)
- {
- // Empty non-final frame
- goto loop;
- }
- impl.rd_done = false;
- }
- else
- {
- ec = {};
- }
- if(! impl.rd_deflated())
- {
- if(impl.rd_remain > 0)
- {
- if(impl.rd_buf.size() == 0 && impl.rd_buf.max_size() >
- (std::min)(clamp(impl.rd_remain),
- buffer_bytes(buffers)))
- {
- // Fill the read buffer first, otherwise we
- // get fewer bytes at the cost of one I/O.
- impl.rd_buf.commit(impl.stream().read_some(
- impl.rd_buf.prepare(read_size(impl.rd_buf,
- impl.rd_buf.max_size())), ec));
- if(impl.check_stop_now(ec))
- return bytes_written;
- if(impl.rd_fh.mask)
- detail::mask_inplace(
- buffers_prefix(clamp(impl.rd_remain),
- impl.rd_buf.data()), impl.rd_key);
- }
- if(impl.rd_buf.size() > 0)
- {
- // Copy from the read buffer.
- // The mask was already applied.
- auto const bytes_transferred = net::buffer_copy(
- buffers, impl.rd_buf.data(),
- clamp(impl.rd_remain));
- auto const mb = buffers_prefix(
- bytes_transferred, buffers);
- impl.rd_remain -= bytes_transferred;
- if(impl.rd_op == detail::opcode::text)
- {
- if(! impl.rd_utf8.write(mb) ||
- (impl.rd_remain == 0 && impl.rd_fh.fin &&
- ! impl.rd_utf8.finish()))
- {
- // _Fail the WebSocket Connection_
- do_fail(close_code::bad_payload,
- error::bad_frame_payload, ec);
- return bytes_written;
- }
- }
- bytes_written += bytes_transferred;
- impl.rd_size += bytes_transferred;
- impl.rd_buf.consume(bytes_transferred);
- }
- else
- {
- // Read into caller's buffer
- BOOST_ASSERT(impl.rd_remain > 0);
- BOOST_ASSERT(buffer_bytes(buffers) > 0);
- BOOST_ASSERT(buffer_bytes(buffers_prefix(
- clamp(impl.rd_remain), buffers)) > 0);
- auto const bytes_transferred =
- impl.stream().read_some(buffers_prefix(
- clamp(impl.rd_remain), buffers), ec);
- // VFALCO What if some bytes were written?
- if(impl.check_stop_now(ec))
- return bytes_written;
- BOOST_ASSERT(bytes_transferred > 0);
- auto const mb = buffers_prefix(
- bytes_transferred, buffers);
- impl.rd_remain -= bytes_transferred;
- if(impl.rd_fh.mask)
- detail::mask_inplace(mb, impl.rd_key);
- if(impl.rd_op == detail::opcode::text)
- {
- if(! impl.rd_utf8.write(mb) ||
- (impl.rd_remain == 0 && impl.rd_fh.fin &&
- ! impl.rd_utf8.finish()))
- {
- // _Fail the WebSocket Connection_
- do_fail(close_code::bad_payload,
- error::bad_frame_payload, ec);
- return bytes_written;
- }
- }
- bytes_written += bytes_transferred;
- impl.rd_size += bytes_transferred;
- }
- }
- BOOST_ASSERT( ! impl.rd_done );
- if( impl.rd_remain == 0 && impl.rd_fh.fin )
- impl.rd_done = true;
- }
- else
- {
- // Read compressed message frame payload:
- // inflate even if rd_fh_.len == 0, otherwise we
- // never emit the end-of-stream deflate block.
- //
- bool did_read = false;
- buffers_suffix<MutableBufferSequence> cb(buffers);
- while(buffer_bytes(cb) > 0)
- {
- zlib::z_params zs;
- {
- auto const out = beast::buffers_front(cb);
- zs.next_out = out.data();
- zs.avail_out = out.size();
- BOOST_ASSERT(zs.avail_out > 0);
- }
- // boolean to track the end of the message.
- bool fin = false;
- if(impl.rd_remain > 0)
- {
- if(impl.rd_buf.size() > 0)
- {
- // use what's there
- auto const in = buffers_prefix(
- clamp(impl.rd_remain), beast::buffers_front(
- impl.rd_buf.data()));
- zs.avail_in = in.size();
- zs.next_in = in.data();
- }
- else if(! did_read)
- {
- // read new
- auto const bytes_transferred =
- impl.stream().read_some(
- impl.rd_buf.prepare(read_size(
- impl.rd_buf, impl.rd_buf.max_size())),
- ec);
- if(impl.check_stop_now(ec))
- return bytes_written;
- BOOST_ASSERT(bytes_transferred > 0);
- impl.rd_buf.commit(bytes_transferred);
- if(impl.rd_fh.mask)
- detail::mask_inplace(
- buffers_prefix(clamp(impl.rd_remain),
- impl.rd_buf.data()), impl.rd_key);
- auto const in = buffers_prefix(
- clamp(impl.rd_remain), buffers_front(
- impl.rd_buf.data()));
- zs.avail_in = in.size();
- zs.next_in = in.data();
- did_read = true;
- }
- else
- {
- break;
- }
- }
- else if(impl.rd_fh.fin)
- {
- // append the empty block codes
- static std::uint8_t constexpr
- empty_block[4] = { 0x00, 0x00, 0xff, 0xff };
- zs.next_in = empty_block;
- zs.avail_in = sizeof(empty_block);
- fin = true;
- }
- else
- {
- break;
- }
- impl.inflate(zs, zlib::Flush::sync, ec);
- if(impl.check_stop_now(ec))
- return bytes_written;
- if (fin && zs.total_out == 0) {
- impl.do_context_takeover_read(impl.role);
- impl.rd_done = true;
- break;
- }
- if(impl.rd_msg_max && beast::detail::sum_exceeds(
- impl.rd_size, zs.total_out, impl.rd_msg_max))
- {
- do_fail(close_code::too_big,
- error::message_too_big, ec);
- return bytes_written;
- }
- cb.consume(zs.total_out);
- impl.rd_size += zs.total_out;
- if (! fin) {
- impl.rd_remain -= zs.total_in;
- impl.rd_buf.consume(zs.total_in);
- }
- bytes_written += zs.total_out;
- }
- if(impl.rd_op == detail::opcode::text)
- {
- // check utf8
- if(! impl.rd_utf8.write(beast::buffers_prefix(
- bytes_written, buffers)) || (
- impl.rd_done && ! impl.rd_utf8.finish()))
- {
- // _Fail the WebSocket Connection_
- do_fail(close_code::bad_payload,
- error::bad_frame_payload, ec);
- return bytes_written;
- }
- }
- }
- return bytes_written;
- }
- template<class NextLayer, bool deflateSupported>
- template<class MutableBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
- BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
- stream<NextLayer, deflateSupported>::
- async_read_some(
- MutableBufferSequence const& buffers,
- ReadHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream type requirements not met");
- static_assert(net::is_mutable_buffer_sequence<
- MutableBufferSequence>::value,
- "MutableBufferSequence type requirements not met");
- return net::async_initiate<
- ReadHandler,
- void(error_code, std::size_t)>(
- run_read_some_op{},
- handler,
- impl_,
- buffers);
- }
- } // websocket
- } // beast
- } // boost
- #endif
|