]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/websocket/stream.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / websocket / stream.hpp
1 //
2 // Copyright (c) 2016-2017 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_STREAM_HPP
11 #define BOOST_BEAST_WEBSOCKET_STREAM_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/websocket/error.hpp>
15 #include <boost/beast/websocket/option.hpp>
16 #include <boost/beast/websocket/role.hpp>
17 #include <boost/beast/websocket/rfc6455.hpp>
18 #include <boost/beast/websocket/detail/frame.hpp>
19 #include <boost/beast/websocket/detail/hybi13.hpp>
20 #include <boost/beast/websocket/detail/mask.hpp>
21 #include <boost/beast/websocket/detail/pausation.hpp>
22 #include <boost/beast/websocket/detail/pmd_extension.hpp>
23 #include <boost/beast/websocket/detail/utf8_checker.hpp>
24 #include <boost/beast/core/static_buffer.hpp>
25 #include <boost/beast/core/string.hpp>
26 #include <boost/beast/core/detail/type_traits.hpp>
27 #include <boost/beast/http/empty_body.hpp>
28 #include <boost/beast/http/message.hpp>
29 #include <boost/beast/http/string_body.hpp>
30 #include <boost/beast/http/detail/type_traits.hpp>
31 #include <boost/beast/zlib/deflate_stream.hpp>
32 #include <boost/beast/zlib/inflate_stream.hpp>
33 #include <boost/asio/async_result.hpp>
34 #include <boost/asio/error.hpp>
35 #include <algorithm>
36 #include <cstdint>
37 #include <functional>
38 #include <limits>
39 #include <type_traits>
40
41 #include <boost/asio/io_context.hpp> // DEPRECATED
42
43 namespace boost {
44 namespace beast {
45 namespace websocket {
46
47 namespace detail {
48 class frame_test;
49 }
50
51 /// The type of object holding HTTP Upgrade requests
52 using request_type = http::request<http::empty_body>;
53
54 /// The type of object holding HTTP Upgrade responses
55 using response_type = http::response<http::string_body>;
56
57 /** The type of received control frame.
58
59 Values of this type are passed to the control frame
60 callback set using @ref stream::control_callback.
61 */
62 enum class frame_type
63 {
64 /// A close frame was received
65 close,
66
67 /// A ping frame was received
68 ping,
69
70 /// A pong frame was received
71 pong
72 };
73
74 //--------------------------------------------------------------------
75
76 /** Provides message-oriented functionality using WebSocket.
77
78 The @ref stream class template provides asynchronous and blocking
79 message-oriented functionality necessary for clients and servers
80 to utilize the WebSocket protocol.
81
82 For asynchronous operations, the application must ensure
83 that they are are all performed within the same implicit
84 or explicit strand.
85
86 @par Thread Safety
87 @e Distinct @e objects: Safe.@n
88 @e Shared @e objects: Unsafe.
89 The application must also ensure that all asynchronous
90 operations are performed within the same implicit or explicit strand.
91
92 @par Example
93
94 To use the @ref stream template with an `ip::tcp::socket`,
95 you would write:
96
97 @code
98 websocket::stream<ip::tcp::socket> ws{io_context};
99 @endcode
100 Alternatively, you can write:
101 @code
102 ip::tcp::socket sock{io_context};
103 websocket::stream<ip::tcp::socket&> ws{sock};
104 @endcode
105
106 @tparam NextLayer The type representing the next layer, to which
107 data will be read and written during operations. For synchronous
108 operations, the type must support the @b SyncStream concept.
109 For asynchronous operations, the type must support the
110 @b AsyncStream concept.
111
112 @note A stream object must not be moved or destroyed while there
113 are pending asynchronous operations associated with it.
114
115 @par Concepts
116 @b AsyncStream,
117 @b DynamicBuffer,
118 @b SyncStream
119 */
120 template<class NextLayer>
121 class stream
122 {
123 friend class close_test;
124 friend class frame_test;
125 friend class ping_test;
126 friend class read_test;
127 friend class stream_test;
128 friend class write_test;
129
130 /* The read buffer has to be at least as large
131 as the largest possible control frame including
132 the frame header.
133 */
134 static std::size_t constexpr max_control_frame_size = 2 + 8 + 4 + 125;
135 static std::size_t constexpr tcp_frame_size = 1536;
136
137 struct op {};
138
139 using control_cb_type =
140 std::function<void(frame_type, string_view)>;
141
142 // tokens are used to order reads and writes
143 class token
144 {
145 unsigned char id_ = 0;
146 public:
147 token() = default;
148 token(token const&) = default;
149 explicit token(unsigned char id) : id_(id) {}
150 operator bool() const { return id_ != 0; }
151 bool operator==(token const& t) { return id_ == t.id_; }
152 bool operator!=(token const& t) { return id_ != t.id_; }
153 token unique() { token t{id_++}; if(id_ == 0) ++id_; return t; }
154 void reset() { id_ = 0; }
155 };
156
157 // State information for the permessage-deflate extension
158 struct pmd_t
159 {
160 // `true` if current read message is compressed
161 bool rd_set = false;
162
163 zlib::deflate_stream zo;
164 zlib::inflate_stream zi;
165 };
166
167 enum class status
168 {
169 open,
170 closing,
171 closed,
172 failed
173 };
174
175 NextLayer stream_; // the wrapped stream
176 close_reason cr_; // set from received close frame
177 control_cb_type ctrl_cb_; // control callback
178
179 std::size_t rd_msg_max_ // max message size
180 = 16 * 1024 * 1024;
181 std::uint64_t rd_size_ // total size of current message so far
182 = 0;
183 std::uint64_t rd_remain_ // message frame bytes left in current frame
184 = 0;
185 detail::frame_header rd_fh_; // current frame header
186 detail::prepared_key rd_key_ // current stateful mask key
187 = 0;
188 detail::frame_buffer rd_fb_; // to write control frames (during reads)
189 detail::utf8_checker rd_utf8_; // to validate utf8
190 static_buffer<
191 +tcp_frame_size> rd_buf_; // buffer for reads
192 detail::opcode rd_op_ // current message binary or text
193 = detail::opcode::text;
194 bool rd_cont_ // `true` if the next frame is a continuation
195 = false;
196 bool rd_done_ // set when a message is done
197 = true;
198 bool rd_close_ // did we read a close frame?
199 = false;
200 token rd_block_; // op currenly reading
201
202 token tok_; // used to order asynchronous ops
203 role_type role_ // server or client
204 = role_type::client;
205 status status_
206 = status::closed;
207
208 token wr_block_; // op currenly writing
209 bool wr_close_ // did we write a close frame?
210 = false;
211 bool wr_cont_ // next write is a continuation
212 = false;
213 bool wr_frag_ // autofrag the current message
214 = false;
215 bool wr_frag_opt_ // autofrag option setting
216 = true;
217 bool wr_compress_ // compress current message
218 = false;
219 detail::opcode wr_opcode_ // message type
220 = detail::opcode::text;
221 std::unique_ptr<
222 std::uint8_t[]> wr_buf_; // write buffer
223 std::size_t wr_buf_size_ // write buffer size (current message)
224 = 0;
225 std::size_t wr_buf_opt_ // write buffer size option setting
226 = 4096;
227 detail::fh_buffer wr_fb_; // header buffer used for writes
228 detail::maskgen wr_gen_; // source of mask keys
229
230 detail::pausation paused_rd_; // paused read op
231 detail::pausation paused_wr_; // paused write op
232 detail::pausation paused_ping_; // paused ping op
233 detail::pausation paused_close_; // paused close op
234 detail::pausation paused_r_rd_; // paused read op (read)
235 detail::pausation paused_r_close_;// paused close op (read)
236
237 std::unique_ptr<pmd_t> pmd_; // pmd settings or nullptr
238 permessage_deflate pmd_opts_; // local pmd options
239 detail::pmd_offer pmd_config_; // offer (client) or negotiation (server)
240
241 public:
242 /// The type of the next layer.
243 using next_layer_type =
244 typename std::remove_reference<NextLayer>::type;
245
246 /// The type of the lowest layer.
247 using lowest_layer_type =
248 typename get_lowest_layer<next_layer_type>::type;
249
250 /// The type of the executor associated with the object.
251 using executor_type = typename next_layer_type::executor_type;
252
253 /** Destructor
254
255 Destroys the stream and all associated resources.
256
257 @note A stream object must not be destroyed while there
258 are pending asynchronous operations associated with it.
259 */
260 ~stream() = default;
261
262 /** Constructor
263
264 If `NextLayer` is move constructible, this function
265 will move-construct a new stream from the existing stream.
266
267 @note The behavior of move assignment on or from streams
268 with active or pending operations is undefined.
269 */
270 stream(stream&&) = default;
271
272 /** Assignment
273
274 If `NextLayer` is move assignable, this function
275 will move-assign a new stream from the existing stream.
276
277 @note The behavior of move assignment on or from streams
278 with active or pending operations is undefined.
279 */
280 stream& operator=(stream&&) = default;
281
282 /** Constructor
283
284 This constructor creates a websocket stream and initializes
285 the next layer object.
286
287 @throws Any exceptions thrown by the NextLayer constructor.
288
289 @param args The arguments to be passed to initialize the
290 next layer object. The arguments are forwarded to the next
291 layer's constructor.
292 */
293 template<class... Args>
294 explicit
295 stream(Args&&... args);
296
297 //--------------------------------------------------------------------------
298
299 /** Get the executor associated with the object.
300
301 This function may be used to obtain the executor object that the
302 stream uses to dispatch handlers for asynchronous operations.
303
304 @return A copy of the executor that stream will use to dispatch handlers.
305 */
306 executor_type
307 get_executor() noexcept
308 {
309 return stream_.get_executor();
310 }
311
312 /** Get a reference to the next layer
313
314 This function returns a reference to the next layer
315 in a stack of stream layers.
316
317 @return A reference to the next layer in the stack of
318 stream layers.
319 */
320 next_layer_type&
321 next_layer()
322 {
323 return stream_;
324 }
325
326 /** Get a reference to the next layer
327
328 This function returns a reference to the next layer in a
329 stack of stream layers.
330
331 @return A reference to the next layer in the stack of
332 stream layers.
333 */
334 next_layer_type const&
335 next_layer() const
336 {
337 return stream_;
338 }
339
340 /** Get a reference to the lowest layer
341
342 This function returns a reference to the lowest layer
343 in a stack of stream layers.
344
345 @return A reference to the lowest layer in the stack of
346 stream layers.
347 */
348 lowest_layer_type&
349 lowest_layer()
350 {
351 return stream_.lowest_layer();
352 }
353
354 /** Get a reference to the lowest layer
355
356 This function returns a reference to the lowest layer
357 in a stack of stream layers.
358
359 @return A reference to the lowest layer in the stack of
360 stream layers. Ownership is not transferred to the caller.
361 */
362 lowest_layer_type const&
363 lowest_layer() const
364 {
365 return stream_.lowest_layer();
366 }
367
368 //--------------------------------------------------------------------------
369 //
370 // Observers
371 //
372 //--------------------------------------------------------------------------
373
374 /** Returns `true` if the stream is open.
375
376 The stream is open after a successful handshake, and when
377 no error has occurred.
378 */
379 bool
380 is_open() const
381 {
382 return status_ == status::open;
383 }
384
385 /** Returns `true` if the latest message data indicates binary.
386
387 This function informs the caller of whether the last
388 received message frame represents a message with the
389 binary opcode.
390
391 If there is no last message frame, the return value is
392 undefined.
393 */
394 bool
395 got_binary() const
396 {
397 return rd_op_ == detail::opcode::binary;
398 }
399
400 /** Returns `true` if the latest message data indicates text.
401
402 This function informs the caller of whether the last
403 received message frame represents a message with the
404 text opcode.
405
406 If there is no last message frame, the return value is
407 undefined.
408 */
409 bool
410 got_text() const
411 {
412 return ! got_binary();
413 }
414
415 /// Returns `true` if the last completed read finished the current message.
416 bool
417 is_message_done() const
418 {
419 return rd_done_;
420 }
421
422 /** Returns the close reason received from the peer.
423
424 This is only valid after a read completes with error::closed.
425 */
426 close_reason const&
427 reason() const
428 {
429 return cr_;
430 }
431
432 /** Returns a suggested maximum buffer size for the next call to read.
433
434 This function returns a reasonable upper limit on the number
435 of bytes for the size of the buffer passed in the next call
436 to read. The number is determined by the state of the current
437 frame and whether or not the permessage-deflate extension is
438 enabled.
439
440 @param initial_size A non-zero size representing the caller's
441 desired buffer size for when there is no information which may
442 be used to calculate a more specific value. For example, when
443 reading the first frame header of a message.
444 */
445 std::size_t
446 read_size_hint(
447 std::size_t initial_size = +tcp_frame_size) const;
448
449 /** Returns a suggested maximum buffer size for the next call to read.
450
451 This function returns a reasonable upper limit on the number
452 of bytes for the size of the buffer passed in the next call
453 to read. The number is determined by the state of the current
454 frame and whether or not the permessage-deflate extension is
455 enabled.
456
457 @param buffer The buffer which will be used for reading. The
458 implementation will query the buffer to obtain the optimum
459 size of a subsequent call to `buffer.prepare` based on the
460 state of the current frame, if any.
461 */
462 template<class DynamicBuffer
463 #if ! BOOST_BEAST_DOXYGEN
464 , class = typename std::enable_if<
465 ! std::is_integral<DynamicBuffer>::value>::type
466 #endif
467 >
468 std::size_t
469 read_size_hint(
470 DynamicBuffer& buffer) const;
471
472 //--------------------------------------------------------------------------
473 //
474 // Settings
475 //
476 //--------------------------------------------------------------------------
477
478 /// Set the permessage-deflate extension options
479 void
480 set_option(permessage_deflate const& o);
481
482 /// Get the permessage-deflate extension options
483 void
484 get_option(permessage_deflate& o)
485 {
486 o = pmd_opts_;
487 }
488
489 /** Set the automatic fragmentation option.
490
491 Determines if outgoing message payloads are broken up into
492 multiple pieces.
493
494 When the automatic fragmentation size is turned on, outgoing
495 message payloads are broken up into multiple frames no larger
496 than the write buffer size.
497
498 The default setting is to fragment messages.
499
500 @param value A `bool` indicating if auto fragmentation should be on.
501
502 @par Example
503 Setting the automatic fragmentation option:
504 @code
505 ws.auto_fragment(true);
506 @endcode
507 */
508 void
509 auto_fragment(bool value)
510 {
511 wr_frag_opt_ = value;
512 }
513
514 /// Returns `true` if the automatic fragmentation option is set.
515 bool
516 auto_fragment() const
517 {
518 return wr_frag_opt_;
519 }
520
521 /** Set the binary message option.
522
523 This controls whether or not outgoing message opcodes
524 are set to binary or text. The setting is only applied
525 at the start when a caller begins a new message. Changing
526 the opcode after a message is started will only take effect
527 after the current message being sent is complete.
528
529 The default setting is to send text messages.
530
531 @param value `true` if outgoing messages should indicate
532 binary, or `false` if they should indicate text.
533
534 @par Example
535 Setting the message type to binary.
536 @code
537 ws.binary(true);
538 @endcode
539 */
540 void
541 binary(bool value)
542 {
543 wr_opcode_ = value ?
544 detail::opcode::binary :
545 detail::opcode::text;
546 }
547
548 /// Returns `true` if the binary message option is set.
549 bool
550 binary() const
551 {
552 return wr_opcode_ == detail::opcode::binary;
553 }
554
555 /** Set a callback to be invoked on each incoming control frame.
556
557 Sets the callback to be invoked whenever a ping, pong,
558 or close control frame is received during a call to one
559 of the following functions:
560
561 @li @ref beast::websocket::stream::read
562 @li @ref beast::websocket::stream::read_some
563 @li @ref beast::websocket::stream::async_read
564 @li @ref beast::websocket::stream::async_read_some
565
566 Unlike completion handlers, the callback will be invoked
567 for each control frame during a call to any synchronous
568 or asynchronous read function. The operation is passive,
569 with no associated error code, and triggered by reads.
570
571 For close frames, the close reason code may be obtained by
572 calling the function @ref reason.
573
574 @param cb The function object to call, which must be
575 invocable with this equivalent signature:
576 @code
577 void
578 callback(
579 frame_type kind, // The type of frame
580 string_view payload // The payload in the frame
581 );
582 @endcode
583 The implementation type-erases the callback without requiring
584 a dynamic allocation. For this reason, the callback object is
585 passed by a non-constant reference.
586 If the read operation which receives the control frame is
587 an asynchronous operation, the callback will be invoked using
588 the same method as that used to invoke the final handler.
589
590 @note It is not necessary to send a close frame upon receipt
591 of a close frame. The implementation does this automatically.
592 Attempting to send a close frame after a close frame is
593 received will result in undefined behavior.
594 */
595 template<class Callback>
596 void
597 control_callback(Callback& cb)
598 {
599 // Callback may not be constant, caller is responsible for
600 // managing the lifetime of the callback. Copies are not made.
601 BOOST_STATIC_ASSERT(! std::is_const<Callback>::value);
602
603 ctrl_cb_ = std::ref(cb);
604 }
605
606 /** Reset the control frame callback.
607
608 This function removes any previously set control frame callback.
609 */
610 void
611 control_callback()
612 {
613 ctrl_cb_ = {};
614 }
615
616 /** Set the maximum incoming message size option.
617
618 Sets the largest permissible incoming message size. Message
619 frame fields indicating a size that would bring the total
620 message size over this limit will cause a protocol failure.
621
622 The default setting is 16 megabytes. A value of zero indicates
623 a limit of the maximum value of a `std::uint64_t`.
624
625 @par Example
626 Setting the maximum read message size.
627 @code
628 ws.read_message_max(65536);
629 @endcode
630
631 @param amount The limit on the size of incoming messages.
632 */
633 void
634 read_message_max(std::size_t amount)
635 {
636 rd_msg_max_ = amount;
637 }
638
639 /// Returns the maximum incoming message size setting.
640 std::size_t
641 read_message_max() const
642 {
643 return rd_msg_max_;
644 }
645
646 /** Set the write buffer size option.
647
648 Sets the size of the write buffer used by the implementation to
649 send frames. The write buffer is needed when masking payload data
650 in the client role, compressing frames, or auto-fragmenting message
651 data.
652
653 Lowering the size of the buffer can decrease the memory requirements
654 for each connection, while increasing the size of the buffer can reduce
655 the number of calls made to the next layer to write data.
656
657 The default setting is 4096. The minimum value is 8.
658
659 The write buffer size can only be changed when the stream is not
660 open. Undefined behavior results if the option is modified after a
661 successful WebSocket handshake.
662
663 @par Example
664 Setting the write buffer size.
665 @code
666 ws.write_buffer_size(8192);
667 @endcode
668
669 @param amount The size of the write buffer in bytes.
670 */
671 void
672 write_buffer_size(std::size_t amount)
673 {
674 if(amount < 8)
675 BOOST_THROW_EXCEPTION(std::invalid_argument{
676 "write buffer size underflow"});
677 wr_buf_opt_ = amount;
678 };
679
680 /// Returns the size of the write buffer.
681 std::size_t
682 write_buffer_size() const
683 {
684 return wr_buf_opt_;
685 }
686
687 /** Set the text message option.
688
689 This controls whether or not outgoing message opcodes
690 are set to binary or text. The setting is only applied
691 at the start when a caller begins a new message. Changing
692 the opcode after a message is started will only take effect
693 after the current message being sent is complete.
694
695 The default setting is to send text messages.
696
697 @param value `true` if outgoing messages should indicate
698 text, or `false` if they should indicate binary.
699
700 @par Example
701 Setting the message type to text.
702 @code
703 ws.text(true);
704 @endcode
705 */
706 void
707 text(bool value)
708 {
709 wr_opcode_ = value ?
710 detail::opcode::text :
711 detail::opcode::binary;
712 }
713
714 /// Returns `true` if the text message option is set.
715 bool
716 text() const
717 {
718 return wr_opcode_ == detail::opcode::text;
719 }
720
721 //--------------------------------------------------------------------------
722 //
723 // Handshaking (Client)
724 //
725 //--------------------------------------------------------------------------
726
727 /** Send an HTTP WebSocket Upgrade request and receive the response.
728
729 This function is used to synchronously send the WebSocket
730 upgrade HTTP request. The call blocks until one of the
731 following conditions is true:
732
733 @li The request is sent and the response is received.
734
735 @li An error occurs on the stream
736
737 This function is implemented in terms of one or more calls to the
738 next layer's `read_some` and `write_some` functions.
739
740 The operation is successful if the received HTTP response indicates
741 a successful HTTP Upgrade (represented by a Status-Code of 101,
742 "switching protocols").
743
744 @param host The name of the remote host,
745 required by the HTTP protocol.
746
747 @param target The Request Target, which may not be empty,
748 required by the HTTP protocol.
749
750 @throws system_error Thrown on failure.
751
752 @par Example
753 @code
754 websocket::stream<ip::tcp::socket> ws{io_context};
755 ...
756 try
757 {
758 ws.handshake("localhost", "/");
759 }
760 catch(...)
761 {
762 // An error occurred.
763 }
764 @endcode
765 */
766 void
767 handshake(
768 string_view host,
769 string_view target);
770
771 /** Send an HTTP WebSocket Upgrade request and receive the response.
772
773 This function is used to synchronously send the WebSocket
774 upgrade HTTP request. The call blocks until one of the
775 following conditions is true:
776
777 @li The request is sent and the response is received.
778
779 @li An error occurs on the stream
780
781 This function is implemented in terms of one or more calls to the
782 next layer's `read_some` and `write_some` functions.
783
784 The operation is successful if the received HTTP response indicates
785 a successful HTTP Upgrade (represented by a Status-Code of 101,
786 "switching protocols").
787
788 @param res The HTTP Upgrade response returned by the remote
789 endpoint.
790
791 @param host The name of the remote host,
792 required by the HTTP protocol.
793
794 @param target The Request Target, which may not be empty,
795 required by the HTTP protocol.
796
797 @throws system_error Thrown on failure.
798
799 @par Example
800 @code
801 websocket::stream<ip::tcp::socket> ws{io_context};
802 ...
803 try
804 {
805 response_type res;
806 ws.handshake(res, "localhost", "/");
807 }
808 catch(...)
809 {
810 // An error occurred.
811 }
812 @endcode
813 */
814 void
815 handshake(
816 response_type& res,
817 string_view host,
818 string_view target);
819
820 /** Send an HTTP WebSocket Upgrade request and receive the response.
821
822 This function is used to synchronously send the WebSocket
823 upgrade HTTP request. The call blocks until one of the
824 following conditions is true:
825
826 @li The request is sent and the response is received.
827
828 @li An error occurs on the stream
829
830 This function is implemented in terms of one or more calls to the
831 next layer's `read_some` and `write_some` functions.
832
833 The operation is successful if the received HTTP response indicates
834 a successful HTTP Upgrade (represented by a Status-Code of 101,
835 "switching protocols").
836
837 @param host The name of the remote host,
838 required by the HTTP protocol.
839
840 @param target The Request Target, which may not be empty,
841 required by the HTTP protocol.
842
843 @param decorator A function object which will be called to modify
844 the HTTP request object generated by the implementation. This
845 could be used to set the User-Agent field, subprotocols, or other
846 application or HTTP specific fields. The object will be called
847 with this equivalent signature:
848 @code void decorator(
849 request_type& req
850 ); @endcode
851
852 @throws system_error Thrown on failure.
853
854 @par Example
855 @code
856 websocket::stream<ip::tcp::socket> ws{io_context};
857 ...
858 try
859 {
860 ws.handshake("localhost", "/",
861 [](request_type& req)
862 {
863 req.set(field::user_agent, "Beast");
864 });
865 }
866 catch(...)
867 {
868 // An error occurred.
869 }
870 @endcode
871 */
872 template<class RequestDecorator>
873 void
874 handshake_ex(
875 string_view host,
876 string_view target,
877 RequestDecorator const& decorator);
878
879 /** Send an HTTP WebSocket Upgrade request and receive the response.
880
881 This function is used to synchronously send the WebSocket
882 upgrade HTTP request. The call blocks until one of the
883 following conditions is true:
884
885 @li The request is sent and the response is received.
886
887 @li An error occurs on the stream
888
889 This function is implemented in terms of one or more calls to the
890 next layer's `read_some` and `write_some` functions.
891
892 The operation is successful if the received HTTP response indicates
893 a successful HTTP Upgrade (represented by a Status-Code of 101,
894 "switching protocols").
895
896 @param res The HTTP Upgrade response returned by the remote
897 endpoint.
898
899 @param host The name of the remote host,
900 required by the HTTP protocol.
901
902 @param target The Request Target, which may not be empty,
903 required by the HTTP protocol.
904
905 @param decorator A function object which will be called to modify
906 the HTTP request object generated by the implementation. This
907 could be used to set the User-Agent field, subprotocols, or other
908 application or HTTP specific fields. The object will be called
909 with this equivalent signature:
910 @code void decorator(
911 request_type& req
912 ); @endcode
913
914 @throws system_error Thrown on failure.
915
916 @par Example
917 @code
918 websocket::stream<ip::tcp::socket> ws{io_context};
919 ...
920 try
921 {
922 response_type res;
923 ws.handshake(res, "localhost", "/",
924 [](request_type& req)
925 {
926 req.set(field::user_agent, "Beast");
927 });
928 }
929 catch(...)
930 {
931 // An error occurred.
932 }
933 @endcode
934 */
935 template<class RequestDecorator>
936 void
937 handshake_ex(
938 response_type& res,
939 string_view host,
940 string_view target,
941 RequestDecorator const& decorator);
942
943 /** Send an HTTP WebSocket Upgrade request and receive the response.
944
945 This function is used to synchronously send the WebSocket
946 upgrade HTTP request. The call blocks until one of the
947 following conditions is true:
948
949 @li The request is sent and the response is received.
950
951 @li An error occurs on the stream
952
953 This function is implemented in terms of one or more calls to the
954 next layer's `read_some` and `write_some` functions.
955
956 The operation is successful if the received HTTP response indicates
957 a successful HTTP Upgrade (represented by a Status-Code of 101,
958 "switching protocols").
959
960 @param host The name of the remote host,
961 required by the HTTP protocol.
962
963 @param target The Request Target, which may not be empty,
964 required by the HTTP protocol.
965
966 @param ec Set to indicate what error occurred, if any.
967
968 @par Example
969 @code
970 websocket::stream<ip::tcp::socket> ws{io_context};
971 ...
972 error_code ec;
973 ws.handshake(host, target, ec);
974 if(ec)
975 {
976 // An error occurred.
977 }
978 @endcode
979 */
980 void
981 handshake(
982 string_view host,
983 string_view target,
984 error_code& ec);
985
986 /** Send an HTTP WebSocket Upgrade request and receive the response.
987
988 This function is used to synchronously send the WebSocket
989 upgrade HTTP request. The call blocks until one of the
990 following conditions is true:
991
992 @li The request is sent and the response is received.
993
994 @li An error occurs on the stream
995
996 This function is implemented in terms of one or more calls to the
997 next layer's `read_some` and `write_some` functions.
998
999 The operation is successful if the received HTTP response indicates
1000 a successful HTTP Upgrade (represented by a Status-Code of 101,
1001 "switching protocols").
1002
1003 @param res The HTTP Upgrade response returned by the remote
1004 endpoint. If `ec` is set, the returned value is undefined.
1005
1006 @param host The name of the remote host,
1007 required by the HTTP protocol.
1008
1009 @param target The Request Target, which may not be empty,
1010 required by the HTTP protocol.
1011
1012 @param ec Set to indicate what error occurred, if any.
1013
1014 @par Example
1015 @code
1016 websocket::stream<ip::tcp::socket> ws{io_context};
1017 ...
1018 error_code ec;
1019 response_type res;
1020 ws.handshake(res, host, target, ec);
1021 if(ec)
1022 {
1023 // An error occurred.
1024 }
1025 @endcode
1026 */
1027 void
1028 handshake(
1029 response_type& res,
1030 string_view host,
1031 string_view target,
1032 error_code& ec);
1033
1034 /** Send an HTTP WebSocket Upgrade request and receive the response.
1035
1036 This function is used to synchronously send the WebSocket
1037 upgrade HTTP request. The call blocks until one of the
1038 following conditions is true:
1039
1040 @li The request is sent and the response is received.
1041
1042 @li An error occurs on the stream
1043
1044 This function is implemented in terms of one or more calls to the
1045 next layer's `read_some` and `write_some` functions.
1046
1047 The operation is successful if the received HTTP response indicates
1048 a successful HTTP Upgrade (represented by a Status-Code of 101,
1049 "switching protocols").
1050
1051 @param host The name of the remote host,
1052 required by the HTTP protocol.
1053
1054 @param target The Request Target, which may not be empty,
1055 required by the HTTP protocol.
1056
1057 @param decorator A function object which will be called to modify
1058 the HTTP request object generated by the implementation. This
1059 could be used to set the User-Agent field, subprotocols, or other
1060 application or HTTP specific fields. The object will be called
1061 with this equivalent signature:
1062 @code void decorator(
1063 request_type& req
1064 ); @endcode
1065
1066 @param ec Set to indicate what error occurred, if any.
1067
1068 @par Example
1069 @code
1070 websocket::stream<ip::tcp::socket> ws{io_context};
1071 ...
1072 error_code ec;
1073 ws.handshake("localhost", "/",
1074 [](request_type& req)
1075 {
1076 req.set(field::user_agent, "Beast");
1077 },
1078 ec);
1079 if(ec)
1080 {
1081 // An error occurred.
1082 }
1083 @endcode
1084 */
1085 template<class RequestDecorator>
1086 void
1087 handshake_ex(
1088 string_view host,
1089 string_view target,
1090 RequestDecorator const& decorator,
1091 error_code& ec);
1092
1093 /** Send an HTTP WebSocket Upgrade request and receive the response.
1094
1095 This function is used to synchronously send the WebSocket
1096 upgrade HTTP request. The call blocks until one of the
1097 following conditions is true:
1098
1099 @li The request is sent and the response is received.
1100
1101 @li An error occurs on the stream
1102
1103 This function is implemented in terms of one or more calls to the
1104 next layer's `read_some` and `write_some` functions.
1105
1106 The operation is successful if the received HTTP response indicates
1107 a successful HTTP Upgrade (represented by a Status-Code of 101,
1108 "switching protocols").
1109
1110 @param res The HTTP Upgrade response returned by the remote
1111 endpoint.
1112
1113 @param host The name of the remote host,
1114 required by the HTTP protocol.
1115
1116 @param target The Request Target, which may not be empty,
1117 required by the HTTP protocol.
1118
1119 @param decorator A function object which will be called to modify
1120 the HTTP request object generated by the implementation. This
1121 could be used to set the User-Agent field, subprotocols, or other
1122 application or HTTP specific fields. The object will be called
1123 with this equivalent signature:
1124 @code void decorator(
1125 request_type& req
1126 ); @endcode
1127
1128 @param ec Set to indicate what error occurred, if any.
1129
1130 @par Example
1131 @code
1132 websocket::stream<ip::tcp::socket> ws{io_context};
1133 ...
1134 error_code ec;
1135 response_type res;
1136 ws.handshake(res, "localhost", "/",
1137 [](request_type& req)
1138 {
1139 req.set(field::user_agent, "Beast");
1140 },
1141 ec);
1142 if(ec)
1143 {
1144 // An error occurred.
1145 }
1146 @endcode
1147 */
1148 template<class RequestDecorator>
1149 void
1150 handshake_ex(
1151 response_type& res,
1152 string_view host,
1153 string_view target,
1154 RequestDecorator const& decorator,
1155 error_code& ec);
1156
1157 /** Start an asynchronous operation to send an upgrade request and receive the response.
1158
1159 This function is used to asynchronously send the HTTP WebSocket
1160 upgrade request and receive the HTTP WebSocket Upgrade response.
1161 This function call always returns immediately. The asynchronous
1162 operation will continue until one of the following conditions is
1163 true:
1164
1165 @li The request is sent and the response is received.
1166
1167 @li An error occurs on the stream
1168
1169 This operation is implemented in terms of one or more calls to the
1170 next layer's `async_read_some` and `async_write_some` functions, and
1171 is known as a <em>composed operation</em>. The program must ensure
1172 that the stream performs no other operations until this operation
1173 completes.
1174
1175 The operation is successful if the received HTTP response indicates
1176 a successful HTTP Upgrade (represented by a Status-Code of 101,
1177 "switching protocols").
1178
1179 @param host The name of the remote host, required by
1180 the HTTP protocol. Copies may be made as needed.
1181
1182 @param target The Request Target, which may not be empty,
1183 required by the HTTP protocol. Copies of this parameter may
1184 be made as needed.
1185
1186 @param handler The handler to be called when the request completes.
1187 Copies will be made of the handler as required. The equivalent
1188 function signature of the handler must be:
1189 @code void handler(
1190 error_code const& ec // Result of operation
1191 ); @endcode
1192 Regardless of whether the asynchronous operation completes
1193 immediately or not, the handler will not be invoked from within
1194 this function. Invocation of the handler will be performed in a
1195 manner equivalent to using `boost::asio::io_context::post`.
1196 */
1197 template<class HandshakeHandler>
1198 BOOST_ASIO_INITFN_RESULT_TYPE(
1199 HandshakeHandler, void(error_code))
1200 async_handshake(
1201 string_view host,
1202 string_view target,
1203 HandshakeHandler&& handler);
1204
1205 /** Start an asynchronous operation to send an upgrade request and receive the response.
1206
1207 This function is used to asynchronously send the HTTP WebSocket
1208 upgrade request and receive the HTTP WebSocket Upgrade response.
1209 This function call always returns immediately. The asynchronous
1210 operation will continue until one of the following conditions is
1211 true:
1212
1213 @li The request is sent and the response is received.
1214
1215 @li An error occurs on the stream
1216
1217 This operation is implemented in terms of one or more calls to the
1218 next layer's `async_read_some` and `async_write_some` functions, and
1219 is known as a <em>composed operation</em>. The program must ensure
1220 that the stream performs no other operations until this operation
1221 completes.
1222
1223 The operation is successful if the received HTTP response indicates
1224 a successful HTTP Upgrade (represented by a Status-Code of 101,
1225 "switching protocols").
1226
1227 @param res The HTTP Upgrade response returned by the remote
1228 endpoint. The caller must ensure this object is valid for at
1229 least until the completion handler is invoked.
1230
1231 @param host The name of the remote host, required by
1232 the HTTP protocol. Copies may be made as needed.
1233
1234 @param target The Request Target, which may not be empty,
1235 required by the HTTP protocol. Copies of this parameter may
1236 be made as needed.
1237
1238 @param handler The handler to be called when the request completes.
1239 Copies will be made of the handler as required. The equivalent
1240 function signature of the handler must be:
1241 @code void handler(
1242 error_code const& ec // Result of operation
1243 ); @endcode
1244 Regardless of whether the asynchronous operation completes
1245 immediately or not, the handler will not be invoked from within
1246 this function. Invocation of the handler will be performed in a
1247 manner equivalent to using `boost::asio::io_context::post`.
1248 */
1249 template<class HandshakeHandler>
1250 BOOST_ASIO_INITFN_RESULT_TYPE(
1251 HandshakeHandler, void(error_code))
1252 async_handshake(
1253 response_type& res,
1254 string_view host,
1255 string_view target,
1256 HandshakeHandler&& handler);
1257
1258 /** Start an asynchronous operation to send an upgrade request and receive the response.
1259
1260 This function is used to asynchronously send the HTTP WebSocket
1261 upgrade request and receive the HTTP WebSocket Upgrade response.
1262 This function call always returns immediately. The asynchronous
1263 operation will continue until one of the following conditions is
1264 true:
1265
1266 @li The request is sent and the response is received.
1267
1268 @li An error occurs on the stream
1269
1270 This operation is implemented in terms of one or more calls to the
1271 next layer's `async_read_some` and `async_write_some` functions, and
1272 is known as a <em>composed operation</em>. The program must ensure
1273 that the stream performs no other operations until this operation
1274 completes.
1275
1276 The operation is successful if the received HTTP response indicates
1277 a successful HTTP Upgrade (represented by a Status-Code of 101,
1278 "switching protocols").
1279
1280 @param host The name of the remote host, required by
1281 the HTTP protocol. Copies may be made as needed.
1282
1283 @param target The Request Target, which may not be empty,
1284 required by the HTTP protocol. Copies of this parameter may
1285 be made as needed.
1286
1287 @param decorator A function object which will be called to modify
1288 the HTTP request object generated by the implementation. This
1289 could be used to set the User-Agent field, subprotocols, or other
1290 application or HTTP specific fields. The object will be called
1291 with this equivalent signature:
1292 @code void decorator(
1293 request_type& req
1294 ); @endcode
1295
1296 @param handler The handler to be called when the request completes.
1297 Copies will be made of the handler as required. The equivalent
1298 function signature of the handler must be:
1299 @code void handler(
1300 error_code const& ec // Result of operation
1301 ); @endcode
1302 Regardless of whether the asynchronous operation completes
1303 immediately or not, the handler will not be invoked from within
1304 this function. Invocation of the handler will be performed in a
1305 manner equivalent to using `boost::asio::io_context::post`.
1306 */
1307 template<class RequestDecorator, class HandshakeHandler>
1308 BOOST_ASIO_INITFN_RESULT_TYPE(
1309 HandshakeHandler, void(error_code))
1310 async_handshake_ex(
1311 string_view host,
1312 string_view target,
1313 RequestDecorator const& decorator,
1314 HandshakeHandler&& handler);
1315
1316 /** Start an asynchronous operation to send an upgrade request and receive the response.
1317
1318 This function is used to asynchronously send the HTTP WebSocket
1319 upgrade request and receive the HTTP WebSocket Upgrade response.
1320 This function call always returns immediately. The asynchronous
1321 operation will continue until one of the following conditions is
1322 true:
1323
1324 @li The request is sent and the response is received.
1325
1326 @li An error occurs on the stream
1327
1328 This operation is implemented in terms of one or more calls to the
1329 next layer's `async_read_some` and `async_write_some` functions, and
1330 is known as a <em>composed operation</em>. The program must ensure
1331 that the stream performs no other operations until this operation
1332 completes.
1333
1334 The operation is successful if the received HTTP response indicates
1335 a successful HTTP Upgrade (represented by a Status-Code of 101,
1336 "switching protocols").
1337
1338 @param res The HTTP Upgrade response returned by the remote
1339 endpoint. The caller must ensure this object is valid for at
1340 least until the completion handler is invoked.
1341
1342 @param host The name of the remote host, required by
1343 the HTTP protocol. Copies may be made as needed.
1344
1345 @param target The Request Target, which may not be empty,
1346 required by the HTTP protocol. Copies of this parameter may
1347 be made as needed.
1348
1349 @param decorator A function object which will be called to modify
1350 the HTTP request object generated by the implementation. This
1351 could be used to set the User-Agent field, subprotocols, or other
1352 application or HTTP specific fields. The object will be called
1353 with this equivalent signature:
1354 @code void decorator(
1355 request_type& req
1356 ); @endcode
1357
1358 @param handler The handler to be called when the request completes.
1359 Copies will be made of the handler as required. The equivalent
1360 function signature of the handler must be:
1361 @code void handler(
1362 error_code const& ec // Result of operation
1363 ); @endcode
1364 Regardless of whether the asynchronous operation completes
1365 immediately or not, the handler will not be invoked from within
1366 this function. Invocation of the handler will be performed in a
1367 manner equivalent to using `boost::asio::io_context::post`.
1368 */
1369 template<class RequestDecorator, class HandshakeHandler>
1370 BOOST_ASIO_INITFN_RESULT_TYPE(
1371 HandshakeHandler, void(error_code))
1372 async_handshake_ex(
1373 response_type& res,
1374 string_view host,
1375 string_view target,
1376 RequestDecorator const& decorator,
1377 HandshakeHandler&& handler);
1378
1379 //--------------------------------------------------------------------------
1380 //
1381 // Handshaking (Server)
1382 //
1383 //--------------------------------------------------------------------------
1384
1385 /** Read and respond to a WebSocket HTTP Upgrade request.
1386
1387 This function is used to synchronously read an HTTP WebSocket
1388 Upgrade request and send the HTTP response. The call blocks
1389 until one of the following conditions is true:
1390
1391 @li The request is received and the response finishes sending.
1392
1393 @li An error occurs on the stream.
1394
1395 This function is implemented in terms of one or more calls to
1396 the next layer's `read_some` and `write_some` functions.
1397
1398 If the stream receives a valid HTTP WebSocket Upgrade request,
1399 an HTTP response is sent back indicating a successful upgrade.
1400 When this call returns, the stream is then ready to send and
1401 receive WebSocket protocol frames and messages.
1402 If the HTTP Upgrade request is invalid or cannot be satisfied,
1403 an HTTP response is sent indicating the reason and status code
1404 (typically 400, "Bad Request"). This counts as a failure.
1405
1406 The implementation uses fixed size internal storage to
1407 receive the request. If the request is too large, the error
1408 @ref error::buffer_overflow will be indicated. Applications
1409 that wish to receive larger requests should first read the
1410 request using their own buffer and a suitable overload of
1411 @ref http::read or @ref http::async_read, then call @ref accept
1412 or @ref async_accept with the request.
1413
1414 @throws system_error Thrown on failure.
1415 */
1416 void
1417 accept();
1418
1419 /** Read and respond to a WebSocket HTTP Upgrade request.
1420
1421 This function is used to synchronously read an HTTP WebSocket
1422 Upgrade request and send the HTTP response. The call blocks
1423 until one of the following conditions is true:
1424
1425 @li The request is received and the response finishes sending.
1426
1427 @li An error occurs on the stream.
1428
1429 This function is implemented in terms of one or more calls to
1430 the next layer's `read_some` and `write_some` functions.
1431
1432 If the stream receives a valid HTTP WebSocket Upgrade request,
1433 an HTTP response is sent back indicating a successful upgrade.
1434 When this call returns, the stream is then ready to send and
1435 receive WebSocket protocol frames and messages.
1436 If the HTTP Upgrade request is invalid or cannot be satisfied,
1437 an HTTP response is sent indicating the reason and status code
1438 (typically 400, "Bad Request"). This counts as a failure.
1439
1440 The implementation uses fixed size internal storage to
1441 receive the request. If the request is too large, the error
1442 @ref error::buffer_overflow will be indicated. Applications
1443 that wish to receive larger requests should first read the
1444 request using their own buffer and a suitable overload of
1445 @ref http::read or @ref http::async_read, then call @ref accept
1446 or @ref async_accept with the request.
1447
1448 @param decorator A function object which will be called to modify
1449 the HTTP response object delivered by the implementation. This
1450 could be used to set the Server field, subprotocols, or other
1451 application or HTTP specific fields. The object will be called
1452 with this equivalent signature:
1453 @code void decorator(
1454 response_type& res
1455 ); @endcode
1456
1457 @throws system_error Thrown on failure.
1458 */
1459 template<class ResponseDecorator>
1460 void
1461 accept_ex(ResponseDecorator const& decorator);
1462
1463 /** Read and respond to a WebSocket HTTP Upgrade request.
1464
1465 This function is used to synchronously read an HTTP WebSocket
1466 Upgrade request and send the HTTP response. The call blocks
1467 until one of the following conditions is true:
1468
1469 @li The request is received and the response finishes sending.
1470
1471 @li An error occurs on the stream.
1472
1473 This function is implemented in terms of one or more calls to
1474 the next layer's `read_some` and `write_some` functions.
1475
1476 If the stream receives a valid HTTP WebSocket Upgrade request,
1477 an HTTP response is sent back indicating a successful upgrade.
1478 When this call returns, the stream is then ready to send and
1479 receive WebSocket protocol frames and messages.
1480 If the HTTP Upgrade request is invalid or cannot be satisfied,
1481 an HTTP response is sent indicating the reason and status code
1482 (typically 400, "Bad Request"). This counts as a failure.
1483
1484 The implementation uses fixed size internal storage to
1485 receive the request. If the request is too large, the error
1486 @ref error::buffer_overflow will be indicated. Applications
1487 that wish to receive larger requests should first read the
1488 request using their own buffer and a suitable overload of
1489 @ref http::read or @ref http::async_read, then call @ref accept
1490 or @ref async_accept with the request.
1491
1492 @param ec Set to indicate what error occurred, if any.
1493 */
1494 void
1495 accept(error_code& ec);
1496
1497 /** Read and respond to a WebSocket HTTP Upgrade request.
1498
1499 This function is used to synchronously read an HTTP WebSocket
1500 Upgrade request and send the HTTP response. The call blocks
1501 until one of the following conditions is true:
1502
1503 @li The request is received and the response finishes sending.
1504
1505 @li An error occurs on the stream.
1506
1507 This function is implemented in terms of one or more calls to
1508 the next layer's `read_some` and `write_some` functions.
1509
1510 If the stream receives a valid HTTP WebSocket Upgrade request,
1511 an HTTP response is sent back indicating a successful upgrade.
1512 When this call returns, the stream is then ready to send and
1513 receive WebSocket protocol frames and messages.
1514 If the HTTP Upgrade request is invalid or cannot be satisfied,
1515 an HTTP response is sent indicating the reason and status code
1516 (typically 400, "Bad Request"). This counts as a failure.
1517
1518 The implementation uses fixed size internal storage to
1519 receive the request. If the request is too large, the error
1520 @ref error::buffer_overflow will be indicated. Applications
1521 that wish to receive larger requests should first read the
1522 request using their own buffer and a suitable overload of
1523 @ref http::read or @ref http::async_read, then call @ref accept
1524 or @ref async_accept with the request.
1525
1526 @param decorator A function object which will be called to modify
1527 the HTTP response object delivered by the implementation. This
1528 could be used to set the Server field, subprotocols, or other
1529 application or HTTP specific fields. The object will be called
1530 with this equivalent signature:
1531 @code void decorator(
1532 response_type& res
1533 ); @endcode
1534
1535 @param ec Set to indicate what error occurred, if any.
1536 */
1537 template<class ResponseDecorator>
1538 void
1539 accept_ex(
1540 ResponseDecorator const& decorator,
1541 error_code& ec);
1542
1543 /** Read and respond to a WebSocket HTTP Upgrade request.
1544
1545 This function is used to synchronously read an HTTP WebSocket
1546 Upgrade request and send the HTTP response. The call blocks
1547 until one of the following conditions is true:
1548
1549 @li The request is received and the response finishes sending.
1550
1551 @li An error occurs on the stream.
1552
1553 This function is implemented in terms of one or more calls to
1554 the next layer's `read_some` and `write_some` functions.
1555
1556 If the stream receives a valid HTTP WebSocket Upgrade request,
1557 an HTTP response is sent back indicating a successful upgrade.
1558 When this call returns, the stream is then ready to send and
1559 receive WebSocket protocol frames and messages.
1560 If the HTTP Upgrade request is invalid or cannot be satisfied,
1561 an HTTP response is sent indicating the reason and status code
1562 (typically 400, "Bad Request"). This counts as a failure.
1563
1564 The implementation uses fixed size internal storage to
1565 receive the request. If the request is too large, the error
1566 @ref error::buffer_overflow will be indicated. Applications
1567 that wish to receive larger requests should first read the
1568 request using their own buffer and a suitable overload of
1569 @ref http::read or @ref http::async_read, then call @ref accept
1570 or @ref async_accept with the request.
1571
1572 @param buffers Caller provided data that has already been
1573 received on the stream. The implementation will copy the
1574 caller provided data before the function returns.
1575
1576 @throws system_error Thrown on failure.
1577 */
1578 template<class ConstBufferSequence>
1579 #if BOOST_BEAST_DOXYGEN
1580 void
1581 #else
1582 typename std::enable_if<! http::detail::is_header<
1583 ConstBufferSequence>::value>::type
1584 #endif
1585 accept(ConstBufferSequence const& buffers);
1586
1587 /** Read and respond to a WebSocket HTTP Upgrade request.
1588
1589 This function is used to synchronously read an HTTP WebSocket
1590 Upgrade request and send the HTTP response. The call blocks
1591 until one of the following conditions is true:
1592
1593 @li The request is received and the response finishes sending.
1594
1595 @li An error occurs on the stream.
1596
1597 This function is implemented in terms of one or more calls to
1598 the next layer's `read_some` and `write_some` functions.
1599
1600 If the stream receives a valid HTTP WebSocket Upgrade request,
1601 an HTTP response is sent back indicating a successful upgrade.
1602 When this call returns, the stream is then ready to send and
1603 receive WebSocket protocol frames and messages.
1604 If the HTTP Upgrade request is invalid or cannot be satisfied,
1605 an HTTP response is sent indicating the reason and status code
1606 (typically 400, "Bad Request"). This counts as a failure.
1607
1608 The implementation uses fixed size internal storage to
1609 receive the request. If the request is too large, the error
1610 @ref error::buffer_overflow will be indicated. Applications
1611 that wish to receive larger requests should first read the
1612 request using their own buffer and a suitable overload of
1613 @ref http::read or @ref http::async_read, then call @ref accept
1614 or @ref async_accept with the request.
1615
1616 @param buffers Caller provided data that has already been
1617 received on the stream. The implementation will copy the
1618 caller provided data before the function returns.
1619
1620 @param decorator A function object which will be called to modify
1621 the HTTP response object delivered by the implementation. This
1622 could be used to set the Server field, subprotocols, or other
1623 application or HTTP specific fields. The object will be called
1624 with this equivalent signature:
1625 @code void decorator(
1626 response_type& res
1627 ); @endcode
1628
1629 @throws system_error Thrown on failure.
1630 */
1631 template<class ConstBufferSequence,
1632 class ResponseDecorator>
1633 #if BOOST_BEAST_DOXYGEN
1634 void
1635 #else
1636 typename std::enable_if<! http::detail::is_header<
1637 ConstBufferSequence>::value>::type
1638 #endif
1639 accept_ex(
1640 ConstBufferSequence const& buffers,
1641 ResponseDecorator const& decorator);
1642
1643 /** Read and respond to a WebSocket HTTP Upgrade request.
1644
1645 This function is used to synchronously read an HTTP WebSocket
1646 Upgrade request and send the HTTP response. The call blocks
1647 until one of the following conditions is true:
1648
1649 @li The request is received and the response finishes sending.
1650
1651 @li An error occurs on the stream.
1652
1653 This function is implemented in terms of one or more calls to
1654 the next layer's `read_some` and `write_some` functions.
1655
1656 If the stream receives a valid HTTP WebSocket Upgrade request,
1657 an HTTP response is sent back indicating a successful upgrade.
1658 When this call returns, the stream is then ready to send and
1659 receive WebSocket protocol frames and messages.
1660 If the HTTP Upgrade request is invalid or cannot be satisfied,
1661 an HTTP response is sent indicating the reason and status code
1662 (typically 400, "Bad Request"). This counts as a failure.
1663
1664 The implementation uses fixed size internal storage to
1665 receive the request. If the request is too large, the error
1666 @ref error::buffer_overflow will be indicated. Applications
1667 that wish to receive larger requests should first read the
1668 request using their own buffer and a suitable overload of
1669 @ref http::read or @ref http::async_read, then call @ref accept
1670 or @ref async_accept with the request.
1671
1672 @param buffers Caller provided data that has already been
1673 received on the stream. The implementation will copy the
1674 caller provided data before the function returns.
1675
1676 @param ec Set to indicate what error occurred, if any.
1677 */
1678 template<class ConstBufferSequence>
1679 #if BOOST_BEAST_DOXYGEN
1680 void
1681 #else
1682 typename std::enable_if<! http::detail::is_header<
1683 ConstBufferSequence>::value>::type
1684 #endif
1685 accept(
1686 ConstBufferSequence const& buffers,
1687 error_code& ec);
1688
1689 /** Read and respond to a WebSocket HTTP Upgrade request.
1690
1691 This function is used to synchronously read an HTTP WebSocket
1692 Upgrade request and send the HTTP response. The call blocks
1693 until one of the following conditions is true:
1694
1695 @li The request is received and the response finishes sending.
1696
1697 @li An error occurs on the stream.
1698
1699 This function is implemented in terms of one or more calls to
1700 the next layer's `read_some` and `write_some` functions.
1701
1702 If the stream receives a valid HTTP WebSocket Upgrade request,
1703 an HTTP response is sent back indicating a successful upgrade.
1704 When this call returns, the stream is then ready to send and
1705 receive WebSocket protocol frames and messages.
1706 If the HTTP Upgrade request is invalid or cannot be satisfied,
1707 an HTTP response is sent indicating the reason and status code
1708 (typically 400, "Bad Request"). This counts as a failure.
1709
1710 The implementation uses fixed size internal storage to
1711 receive the request. If the request is too large, the error
1712 @ref error::buffer_overflow will be indicated. Applications
1713 that wish to receive larger requests should first read the
1714 request using their own buffer and a suitable overload of
1715 @ref http::read or @ref http::async_read, then call @ref accept
1716 or @ref async_accept with the request.
1717
1718 @param buffers Caller provided data that has already been
1719 received on the stream. The implementation will copy the
1720 caller provided data before the function returns.
1721
1722 @param decorator A function object which will be called to modify
1723 the HTTP response object delivered by the implementation. This
1724 could be used to set the Server field, subprotocols, or other
1725 application or HTTP specific fields. The object will be called
1726 with this equivalent signature:
1727 @code void decorator(
1728 response_type& res
1729 ); @endcode
1730
1731 @param ec Set to indicate what error occurred, if any.
1732 */
1733 template<class ConstBufferSequence, class ResponseDecorator>
1734 #if BOOST_BEAST_DOXYGEN
1735 void
1736 #else
1737 typename std::enable_if<! http::detail::is_header<
1738 ConstBufferSequence>::value>::type
1739 #endif
1740 accept_ex(
1741 ConstBufferSequence const& buffers,
1742 ResponseDecorator const& decorator,
1743 error_code& ec);
1744
1745 /** Respond to a WebSocket HTTP Upgrade request
1746
1747 This function is used to synchronously send the HTTP response
1748 to an HTTP request possibly containing a WebSocket Upgrade.
1749 The call blocks until one of the following conditions is true:
1750
1751 @li The response finishes sending.
1752
1753 @li An error occurs on the stream.
1754
1755 This function is implemented in terms of one or more calls to
1756 the next layer's `read_some` and `write_some` functions.
1757
1758 If the stream receives a valid HTTP WebSocket Upgrade request,
1759 an HTTP response is sent back indicating a successful upgrade.
1760 When this call returns, the stream is then ready to send and
1761 receive WebSocket protocol frames and messages.
1762 If the HTTP Upgrade request is invalid or cannot be satisfied,
1763 an HTTP response is sent indicating the reason and status code
1764 (typically 400, "Bad Request"). This counts as a failure.
1765
1766 @param req An object containing the HTTP Upgrade request.
1767 Ownership is not transferred, the implementation will not
1768 access this object from other threads.
1769
1770 @throws system_error Thrown on failure.
1771 */
1772 template<class Body, class Allocator>
1773 void
1774 accept(http::request<Body,
1775 http::basic_fields<Allocator>> const& req);
1776
1777 /** Respond to a WebSocket HTTP Upgrade request
1778
1779 This function is used to synchronously send the HTTP response
1780 to an HTTP request possibly containing a WebSocket Upgrade.
1781 The call blocks until one of the following conditions is true:
1782
1783 @li The response finishes sending.
1784
1785 @li An error occurs on the stream.
1786
1787 This function is implemented in terms of one or more calls to
1788 the next layer's `read_some` and `write_some` functions.
1789
1790 If the stream receives a valid HTTP WebSocket Upgrade request,
1791 an HTTP response is sent back indicating a successful upgrade.
1792 When this call returns, the stream is then ready to send and
1793 receive WebSocket protocol frames and messages.
1794 If the HTTP Upgrade request is invalid or cannot be satisfied,
1795 an HTTP response is sent indicating the reason and status code
1796 (typically 400, "Bad Request"). This counts as a failure.
1797
1798 @param req An object containing the HTTP Upgrade request.
1799 Ownership is not transferred, the implementation will not
1800 access this object from other threads.
1801
1802 @param decorator A function object which will be called to modify
1803 the HTTP response object delivered by the implementation. This
1804 could be used to set the Server field, subprotocols, or other
1805 application or HTTP specific fields. The object will be called
1806 with this equivalent signature:
1807 @code void decorator(
1808 response_type& res
1809 ); @endcode
1810
1811 @throws system_error Thrown on failure.
1812 */
1813 template<class Body, class Allocator,
1814 class ResponseDecorator>
1815 void
1816 accept_ex(http::request<Body,
1817 http::basic_fields<Allocator>> const& req,
1818 ResponseDecorator const& decorator);
1819
1820 /** Respond to a WebSocket HTTP Upgrade request
1821
1822 This function is used to synchronously send the HTTP response
1823 to an HTTP request possibly containing a WebSocket Upgrade.
1824 The call blocks until one of the following conditions is true:
1825
1826 @li The response finishes sending.
1827
1828 @li An error occurs on the stream.
1829
1830 This function is implemented in terms of one or more calls to
1831 the next layer's `read_some` and `write_some` functions.
1832
1833 If the stream receives a valid HTTP WebSocket Upgrade request,
1834 an HTTP response is sent back indicating a successful upgrade.
1835 When this call returns, the stream is then ready to send and
1836 receive WebSocket protocol frames and messages.
1837 If the HTTP Upgrade request is invalid or cannot be satisfied,
1838 an HTTP response is sent indicating the reason and status code
1839 (typically 400, "Bad Request"). This counts as a failure.
1840
1841 @param req An object containing the HTTP Upgrade request.
1842 Ownership is not transferred, the implementation will not
1843 access this object from other threads.
1844
1845 @param ec Set to indicate what error occurred, if any.
1846 */
1847 template<class Body, class Allocator>
1848 void
1849 accept(http::request<Body,
1850 http::basic_fields<Allocator>> const& req,
1851 error_code& ec);
1852
1853 /** Respond to a WebSocket HTTP Upgrade request
1854
1855 This function is used to synchronously send the HTTP response
1856 to an HTTP request possibly containing a WebSocket Upgrade.
1857 The call blocks until one of the following conditions is true:
1858
1859 @li The response finishes sending.
1860
1861 @li An error occurs on the stream.
1862
1863 This function is implemented in terms of one or more calls to
1864 the next layer's `read_some` and `write_some` functions.
1865
1866 If the stream receives a valid HTTP WebSocket Upgrade request,
1867 an HTTP response is sent back indicating a successful upgrade.
1868 When this call returns, the stream is then ready to send and
1869 receive WebSocket protocol frames and messages.
1870 If the HTTP Upgrade request is invalid or cannot be satisfied,
1871 an HTTP response is sent indicating the reason and status code
1872 (typically 400, "Bad Request"). This counts as a failure.
1873
1874 @param req An object containing the HTTP Upgrade request.
1875 Ownership is not transferred, the implementation will not
1876 access this object from other threads.
1877
1878 @param decorator A function object which will be called to modify
1879 the HTTP response object delivered by the implementation. This
1880 could be used to set the Server field, subprotocols, or other
1881 application or HTTP specific fields. The object will be called
1882 with this equivalent signature:
1883 @code void decorator(
1884 response_type& res
1885 ); @endcode
1886
1887 @param ec Set to indicate what error occurred, if any.
1888 */
1889 template<class Body, class Allocator,
1890 class ResponseDecorator>
1891 void
1892 accept_ex(http::request<Body,
1893 http::basic_fields<Allocator>> const& req,
1894 ResponseDecorator const& decorator,
1895 error_code& ec);
1896
1897 /** Start reading and responding to a WebSocket HTTP Upgrade request.
1898
1899 This function is used to asynchronously read an HTTP WebSocket
1900 Upgrade request and send the HTTP response. The function call
1901 always returns immediately. The asynchronous operation will
1902 continue until one of the following conditions is true:
1903
1904 @li The request is received and the response finishes sending.
1905
1906 @li An error occurs on the stream.
1907
1908 This operation is implemented in terms of one or more calls to
1909 the next layer's `async_read_some` and `async_write_some`
1910 functions, and is known as a <em>composed operation</em>. The
1911 program must ensure that the stream performs no other
1912 asynchronous operations until this operation completes.
1913
1914 If the stream receives a valid HTTP WebSocket Upgrade request,
1915 an HTTP response is sent back indicating a successful upgrade.
1916 When the completion handler is invoked, the stream is then
1917 ready to send and receive WebSocket protocol frames and
1918 messages.
1919 If the HTTP Upgrade request is invalid or cannot be satisfied,
1920 an HTTP response is sent indicating the reason and status code
1921 (typically 400, "Bad Request"). This counts as a failure, and
1922 the completion handler will be invoked with a suitable error
1923 code set.
1924
1925 The implementation uses fixed size internal storage to
1926 receive the request. If the request is too large, the error
1927 @ref error::buffer_overflow will be indicated. Applications
1928 that wish to receive larger requests should first read the
1929 request using their own buffer and a suitable overload of
1930 @ref http::read or @ref http::async_read, then call @ref accept
1931 or @ref async_accept with the request.
1932
1933 @param handler The handler to be called when the request
1934 completes. Copies will be made of the handler as required. The
1935 equivalent function signature of the handler must be:
1936 @code void handler(
1937 error_code const& ec // Result of operation
1938 ); @endcode
1939 Regardless of whether the asynchronous operation completes
1940 immediately or not, the handler will not be invoked from within
1941 this function. Invocation of the handler will be performed in a
1942 manner equivalent to using `boost::asio::io_context::post`.
1943 */
1944 template<class AcceptHandler>
1945 BOOST_ASIO_INITFN_RESULT_TYPE(
1946 AcceptHandler, void(error_code))
1947 async_accept(AcceptHandler&& handler);
1948
1949 /** Start reading and responding to a WebSocket HTTP Upgrade request.
1950
1951 This function is used to asynchronously read an HTTP WebSocket
1952 Upgrade request and send the HTTP response. The function call
1953 always returns immediately. The asynchronous operation will
1954 continue until one of the following conditions is true:
1955
1956 @li The request is received and the response finishes sending.
1957
1958 @li An error occurs on the stream.
1959
1960 This operation is implemented in terms of one or more calls to
1961 the next layer's `async_read_some` and `async_write_some`
1962 functions, and is known as a <em>composed operation</em>. The
1963 program must ensure that the stream performs no other
1964 asynchronous operations until this operation completes.
1965
1966 If the stream receives a valid HTTP WebSocket Upgrade request,
1967 an HTTP response is sent back indicating a successful upgrade.
1968 When the completion handler is invoked, the stream is then
1969 ready to send and receive WebSocket protocol frames and
1970 messages.
1971 If the HTTP Upgrade request is invalid or cannot be satisfied,
1972 an HTTP response is sent indicating the reason and status code
1973 (typically 400, "Bad Request"). This counts as a failure, and
1974 the completion handler will be invoked with a suitable error
1975 code set.
1976
1977 The implementation uses fixed size internal storage to
1978 receive the request. If the request is too large, the error
1979 @ref error::buffer_overflow will be indicated. Applications
1980 that wish to receive larger requests should first read the
1981 request using their own buffer and a suitable overload of
1982 @ref http::read or @ref http::async_read, then call @ref accept
1983 or @ref async_accept with the request.
1984
1985 @param decorator A function object which will be called to modify
1986 the HTTP response object delivered by the implementation. This
1987 could be used to set the Server field, subprotocols, or other
1988 application or HTTP specific fields. The object will be called
1989 with this equivalent signature:
1990 @code void decorator(
1991 response_type& res
1992 ); @endcode
1993
1994 @param handler The handler to be called when the request
1995 completes. Copies will be made of the handler as required. The
1996 equivalent function signature of the handler must be:
1997 @code void handler(
1998 error_code const& ec // Result of operation
1999 ); @endcode
2000 Regardless of whether the asynchronous operation completes
2001 immediately or not, the handler will not be invoked from within
2002 this function. Invocation of the handler will be performed in a
2003 manner equivalent to using `boost::asio::io_context::post`.
2004 */
2005 template<
2006 class ResponseDecorator,
2007 class AcceptHandler>
2008 BOOST_ASIO_INITFN_RESULT_TYPE(
2009 AcceptHandler, void(error_code))
2010 async_accept_ex(
2011 ResponseDecorator const& decorator,
2012 AcceptHandler&& handler);
2013
2014 /** Start reading and responding to a WebSocket HTTP Upgrade request.
2015
2016 This function is used to asynchronously read an HTTP WebSocket
2017 Upgrade request and send the HTTP response. The function call
2018 always returns immediately. The asynchronous operation will
2019 continue until one of the following conditions is true:
2020
2021 @li The request is received and the response finishes sending.
2022
2023 @li An error occurs on the stream.
2024
2025 This operation is implemented in terms of one or more calls to
2026 the next layer's `async_read_some` and `async_write_some`
2027 functions, and is known as a <em>composed operation</em>. The
2028 program must ensure that the stream performs no other
2029 asynchronous operations until this operation completes.
2030
2031 If the stream receives a valid HTTP WebSocket Upgrade request,
2032 an HTTP response is sent back indicating a successful upgrade.
2033 When the completion handler is invoked, the stream is then
2034 ready to send and receive WebSocket protocol frames and
2035 messages.
2036 If the HTTP Upgrade request is invalid or cannot be satisfied,
2037 an HTTP response is sent indicating the reason and status code
2038 (typically 400, "Bad Request"). This counts as a failure, and
2039 the completion handler will be invoked with a suitable error
2040 code set.
2041
2042 The implementation uses fixed size internal storage to
2043 receive the request. If the request is too large, the error
2044 @ref error::buffer_overflow will be indicated. Applications
2045 that wish to receive larger requests should first read the
2046 request using their own buffer and a suitable overload of
2047 @ref http::read or @ref http::async_read, then call @ref accept
2048 or @ref async_accept with the request.
2049
2050 @param buffers Caller provided data that has already been
2051 received on the stream. This may be used for implementations
2052 allowing multiple protocols on the same stream. The
2053 buffered data will first be applied to the handshake, and
2054 then to received WebSocket frames. The implementation will
2055 copy the caller provided data before the function returns.
2056
2057 @param handler The handler to be called when the request
2058 completes. Copies will be made of the handler as required. The
2059 equivalent function signature of the handler must be:
2060 @code void handler(
2061 error_code const& ec // Result of operation
2062 ); @endcode
2063 Regardless of whether the asynchronous operation completes
2064 immediately or not, the handler will not be invoked from within
2065 this function. Invocation of the handler will be performed in a
2066 manner equivalent to using `boost::asio::io_context::post`.
2067 */
2068 template<
2069 class ConstBufferSequence,
2070 class AcceptHandler>
2071 #if BOOST_BEAST_DOXYGEN
2072 void_or_deduced
2073 #else
2074 typename std::enable_if<
2075 ! http::detail::is_header<ConstBufferSequence>::value,
2076 BOOST_ASIO_INITFN_RESULT_TYPE(
2077 AcceptHandler, void(error_code))>::type
2078 #endif
2079 async_accept(
2080 ConstBufferSequence const& buffers,
2081 AcceptHandler&& handler);
2082
2083 /** Start reading and responding to a WebSocket HTTP Upgrade request.
2084
2085 This function is used to asynchronously read an HTTP WebSocket
2086 Upgrade request and send the HTTP response. The function call
2087 always returns immediately. The asynchronous operation will
2088 continue until one of the following conditions is true:
2089
2090 @li The request is received and the response finishes sending.
2091
2092 @li An error occurs on the stream.
2093
2094 This operation is implemented in terms of one or more calls to
2095 the next layer's `async_read_some` and `async_write_some`
2096 functions, and is known as a <em>composed operation</em>. The
2097 program must ensure that the stream performs no other
2098 asynchronous operations until this operation completes.
2099
2100 If the stream receives a valid HTTP WebSocket Upgrade request,
2101 an HTTP response is sent back indicating a successful upgrade.
2102 When the completion handler is invoked, the stream is then
2103 ready to send and receive WebSocket protocol frames and
2104 messages.
2105 If the HTTP Upgrade request is invalid or cannot be satisfied,
2106 an HTTP response is sent indicating the reason and status code
2107 (typically 400, "Bad Request"). This counts as a failure, and
2108 the completion handler will be invoked with a suitable error
2109 code set.
2110
2111 The implementation uses fixed size internal storage to
2112 receive the request. If the request is too large, the error
2113 @ref error::buffer_overflow will be indicated. Applications
2114 that wish to receive larger requests should first read the
2115 request using their own buffer and a suitable overload of
2116 @ref http::read or @ref http::async_read, then call @ref accept
2117 or @ref async_accept with the request.
2118
2119 @param buffers Caller provided data that has already been
2120 received on the stream. This may be used for implementations
2121 allowing multiple protocols on the same stream. The
2122 buffered data will first be applied to the handshake, and
2123 then to received WebSocket frames. The implementation will
2124 copy the caller provided data before the function returns.
2125
2126 @param decorator A function object which will be called to modify
2127 the HTTP response object delivered by the implementation. This
2128 could be used to set the Server field, subprotocols, or other
2129 application or HTTP specific fields. The object will be called
2130 with this equivalent signature:
2131 @code void decorator(
2132 response_type& res
2133 ); @endcode
2134
2135 @param handler The handler to be called when the request
2136 completes. Copies will be made of the handler as required. The
2137 equivalent function signature of the handler must be:
2138 @code void handler(
2139 error_code const& ec // Result of operation
2140 ); @endcode
2141 Regardless of whether the asynchronous operation completes
2142 immediately or not, the handler will not be invoked from within
2143 this function. Invocation of the handler will be performed in a
2144 manner equivalent to using `boost::asio::io_context::post`.
2145 */
2146 template<
2147 class ConstBufferSequence,
2148 class ResponseDecorator,
2149 class AcceptHandler>
2150 #if BOOST_BEAST_DOXYGEN
2151 void_or_deduced
2152 #else
2153 typename std::enable_if<
2154 ! http::detail::is_header<ConstBufferSequence>::value,
2155 BOOST_ASIO_INITFN_RESULT_TYPE(
2156 AcceptHandler, void(error_code))>::type
2157 #endif
2158 async_accept_ex(
2159 ConstBufferSequence const& buffers,
2160 ResponseDecorator const& decorator,
2161 AcceptHandler&& handler);
2162
2163 /** Start responding to a WebSocket HTTP Upgrade request.
2164
2165 This function is used to asynchronously send the HTTP response
2166 to an HTTP request possibly containing a WebSocket Upgrade
2167 request. The function call always returns immediately. The
2168 asynchronous operation will continue until one of the following
2169 conditions is true:
2170
2171 @li The response finishes sending.
2172
2173 @li An error occurs on the stream.
2174
2175 This operation is implemented in terms of one or more calls to
2176 the next layer's `async_write_some` functions, and is known as
2177 a <em>composed operation</em>. The program must ensure that the
2178 stream performs no other operations until this operation
2179 completes.
2180
2181 If the stream receives a valid HTTP WebSocket Upgrade request,
2182 an HTTP response is sent back indicating a successful upgrade.
2183 When the completion handler is invoked, the stream is then
2184 ready to send and receive WebSocket protocol frames and
2185 messages.
2186 If the HTTP Upgrade request is invalid or cannot be satisfied,
2187 an HTTP response is sent indicating the reason and status code
2188 (typically 400, "Bad Request"). This counts as a failure, and
2189 the completion handler will be invoked with a suitable error
2190 code set.
2191
2192 @param req An object containing the HTTP Upgrade request.
2193 Ownership is not transferred, the implementation will not access
2194 this object from other threads.
2195
2196 @param handler The handler to be called when the request
2197 completes. Copies will be made of the handler as required. The
2198 equivalent function signature of the handler must be:
2199 @code void handler(
2200 error_code const& ec // Result of operation
2201 ); @endcode
2202 Regardless of whether the asynchronous operation completes
2203 immediately or not, the handler will not be invoked from within
2204 this function. Invocation of the handler will be performed in a
2205 manner equivalent to using `boost::asio::io_context::post`.
2206 */
2207 template<
2208 class Body, class Allocator,
2209 class AcceptHandler>
2210 BOOST_ASIO_INITFN_RESULT_TYPE(
2211 AcceptHandler, void(error_code))
2212 async_accept(
2213 http::request<Body,
2214 http::basic_fields<Allocator>> const& req,
2215 AcceptHandler&& handler);
2216
2217 /** Start responding to a WebSocket HTTP Upgrade request.
2218
2219 This function is used to asynchronously send the HTTP response
2220 to an HTTP request possibly containing a WebSocket Upgrade
2221 request. The function call always returns immediately. The
2222 asynchronous operation will continue until one of the following
2223 conditions is true:
2224
2225 @li The response finishes sending.
2226
2227 @li An error occurs on the stream.
2228
2229 This operation is implemented in terms of one or more calls to
2230 the next layer's `async_write_some` functions, and is known as
2231 a <em>composed operation</em>. The program must ensure that the
2232 stream performs no other operations until this operation
2233 completes.
2234
2235 If the stream receives a valid HTTP WebSocket Upgrade request,
2236 an HTTP response is sent back indicating a successful upgrade.
2237 When the completion handler is invoked, the stream is then
2238 ready to send and receive WebSocket protocol frames and
2239 messages.
2240 If the HTTP Upgrade request is invalid or cannot be satisfied,
2241 an HTTP response is sent indicating the reason and status code
2242 (typically 400, "Bad Request"). This counts as a failure, and
2243 the completion handler will be invoked with a suitable error
2244 code set.
2245
2246 @param req An object containing the HTTP Upgrade request.
2247 Ownership is not transferred, the implementation will not access
2248 this object from other threads.
2249
2250 @param decorator A function object which will be called to modify
2251 the HTTP response object delivered by the implementation. This
2252 could be used to set the Server field, subprotocols, or other
2253 application or HTTP specific fields. The object will be called
2254 with this equivalent signature:
2255 @code void decorator(
2256 response_type& res
2257 ); @endcode
2258
2259 @param handler The handler to be called when the request
2260 completes. Copies will be made of the handler as required. The
2261 equivalent function signature of the handler must be:
2262 @code void handler(
2263 error_code const& ec // Result of operation
2264 ); @endcode
2265 Regardless of whether the asynchronous operation completes
2266 immediately or not, the handler will not be invoked from within
2267 this function. Invocation of the handler will be performed in a
2268 manner equivalent to using `boost::asio::io_context::post`.
2269 */
2270 template<
2271 class Body, class Allocator,
2272 class ResponseDecorator,
2273 class AcceptHandler>
2274 BOOST_ASIO_INITFN_RESULT_TYPE(
2275 AcceptHandler, void(error_code))
2276 async_accept_ex(
2277 http::request<Body,
2278 http::basic_fields<Allocator>> const& req,
2279 ResponseDecorator const& decorator,
2280 AcceptHandler&& handler);
2281
2282 //--------------------------------------------------------------------------
2283 //
2284 // Control Frames
2285 //
2286 //--------------------------------------------------------------------------
2287
2288 /** Send a WebSocket close frame.
2289
2290 This function is used to synchronously send a close frame on
2291 the stream. The call blocks until one of the following is true:
2292
2293 @li The close frame finishes sending.
2294
2295 @li An error occurs on the stream.
2296
2297 This function is implemented in terms of one or more calls
2298 to the next layer's `write_some` functions.
2299
2300 If the close reason specifies a close code other than
2301 @ref beast::websocket::close_code::none, the close frame is
2302 sent with the close code and optional reason string. Otherwise,
2303 the close frame is sent with no payload.
2304
2305 Callers should not attempt to write WebSocket data after
2306 initiating the close. Instead, callers should continue
2307 reading until an error occurs. A read returning @ref error::closed
2308 indicates a successful connection closure.
2309
2310 @param cr The reason for the close.
2311
2312 @throws system_error Thrown on failure.
2313 */
2314 void
2315 close(close_reason const& cr);
2316
2317 /** Send a WebSocket close frame.
2318
2319 This function is used to synchronously send a close frame on
2320 the stream. The call blocks until one of the following is true:
2321
2322 @li The close frame finishes sending.
2323
2324 @li An error occurs on the stream.
2325
2326 This function is implemented in terms of one or more calls
2327 to the next layer's `write_some` functions.
2328
2329 If the close reason specifies a close code other than
2330 @ref beast::websocket::close_code::none, the close frame is
2331 sent with the close code and optional reason string. Otherwise,
2332 the close frame is sent with no payload.
2333
2334 Callers should not attempt to write WebSocket data after
2335 initiating the close. Instead, callers should continue
2336 reading until an error occurs. A read returning @ref error::closed
2337 indicates a successful connection closure.
2338
2339 @param cr The reason for the close.
2340
2341 @param ec Set to indicate what error occurred, if any.
2342 */
2343 void
2344 close(close_reason const& cr, error_code& ec);
2345
2346 /** Start an asynchronous operation to send a WebSocket close frame.
2347
2348 This function is used to asynchronously send a close frame on
2349 the stream. This function call always returns immediately. The
2350 asynchronous operation will continue until one of the following
2351 conditions is true:
2352
2353 @li The close frame finishes sending.
2354
2355 @li An error occurs on the stream.
2356
2357 This operation is implemented in terms of one or more calls to the
2358 next layer's `async_write_some` functions, and is known as a
2359 <em>composed operation</em>. The program must ensure that the
2360 stream performs no other write operations (such as @ref async_ping,
2361 @ref stream::async_write, @ref stream::async_write_some, or
2362 @ref stream::async_close) until this operation completes.
2363
2364 If the close reason specifies a close code other than
2365 @ref beast::websocket::close_code::none, the close frame is
2366 sent with the close code and optional reason string. Otherwise,
2367 the close frame is sent with no payload.
2368
2369 Callers should not attempt to write WebSocket data after
2370 initiating the close. Instead, callers should continue
2371 reading until an error occurs. A read returning @ref error::closed
2372 indicates a successful connection closure.
2373
2374 @param cr The reason for the close.
2375
2376 @param handler The handler to be called when the close operation
2377 completes. Copies will be made of the handler as required. The
2378 function signature of the handler must be:
2379 @code
2380 void handler(
2381 error_code const& ec // Result of operation
2382 );
2383 @endcode
2384 Regardless of whether the asynchronous operation completes
2385 immediately or not, the handler will not be invoked from within
2386 this function. Invocation of the handler will be performed in a
2387 manner equivalent to using `boost::asio::io_context::post`.
2388 */
2389 template<class CloseHandler>
2390 BOOST_ASIO_INITFN_RESULT_TYPE(
2391 CloseHandler, void(error_code))
2392 async_close(close_reason const& cr, CloseHandler&& handler);
2393
2394 /** Send a WebSocket ping frame.
2395
2396 This function is used to synchronously send a ping frame on
2397 the stream. The call blocks until one of the following is true:
2398
2399 @li The ping frame finishes sending.
2400
2401 @li An error occurs on the stream.
2402
2403 This function is implemented in terms of one or more calls to the
2404 next layer's `write_some` functions.
2405
2406 @param payload The payload of the ping message, which may be empty.
2407
2408 @throws system_error Thrown on failure.
2409 */
2410 void
2411 ping(ping_data const& payload);
2412
2413 /** Send a WebSocket ping frame.
2414
2415 This function is used to synchronously send a ping frame on
2416 the stream. The call blocks until one of the following is true:
2417
2418 @li The ping frame finishes sending.
2419
2420 @li An error occurs on the stream.
2421
2422 This function is implemented in terms of one or more calls to the
2423 next layer's `write_some` functions.
2424
2425 @param payload The payload of the ping message, which may be empty.
2426
2427 @param ec Set to indicate what error occurred, if any.
2428 */
2429 void
2430 ping(ping_data const& payload, error_code& ec);
2431
2432 /** Start an asynchronous operation to send a WebSocket ping frame.
2433
2434 This function is used to asynchronously send a ping frame to
2435 the stream. The function call always returns immediately. The
2436 asynchronous operation will continue until one of the following
2437 is true:
2438
2439 @li The entire ping frame is sent.
2440
2441 @li An error occurs on the stream.
2442
2443 This operation is implemented in terms of one or more calls to the
2444 next layer's `async_write_some` functions, and is known as a
2445 <em>composed operation</em>. The program must ensure that the
2446 stream performs no other writes until this operation completes.
2447
2448 If a close frame is sent or received before the ping frame is
2449 sent, the completion handler will be called with the error
2450 set to `boost::asio::error::operation_aborted`.
2451
2452 @param payload The payload of the ping message, which may be empty.
2453
2454 @param handler The handler to be called when the read operation
2455 completes. Copies will be made of the handler as required. The
2456 function signature of the handler must be:
2457 @code
2458 void handler(
2459 error_code const& ec // Result of operation
2460 );
2461 @endcode
2462 Regardless of whether the asynchronous operation completes
2463 immediately or not, the handler will not be invoked from within
2464 this function. Invocation of the handler will be performed in a
2465 manner equivalent to using `boost::asio::io_context::post`.
2466 */
2467 template<class WriteHandler>
2468 BOOST_ASIO_INITFN_RESULT_TYPE(
2469 WriteHandler, void(error_code))
2470 async_ping(ping_data const& payload, WriteHandler&& handler);
2471
2472 /** Send a WebSocket pong frame.
2473
2474 This function is used to synchronously send a pong frame on
2475 the stream. The call blocks until one of the following is true:
2476
2477 @li The pong frame finishes sending.
2478
2479 @li An error occurs on the stream.
2480
2481 This function is implemented in terms of one or more calls to the
2482 next layer's `write_some` functions.
2483
2484 The WebSocket protocol allows pong frames to be sent from either
2485 end at any time. It is not necessary to first receive a ping in
2486 order to send a pong. The remote peer may use the receipt of a
2487 pong frame as an indication that the connection is not dead.
2488
2489 @param payload The payload of the pong message, which may be empty.
2490
2491 @throws system_error Thrown on failure.
2492 */
2493 void
2494 pong(ping_data const& payload);
2495
2496 /** Send a WebSocket pong frame.
2497
2498 This function is used to synchronously send a pong frame on
2499 the stream. The call blocks until one of the following is true:
2500
2501 @li The pong frame finishes sending.
2502
2503 @li An error occurs on the stream.
2504
2505 This function is implemented in terms of one or more calls to the
2506 next layer's `write_some` functions.
2507
2508 The WebSocket protocol allows pong frames to be sent from either
2509 end at any time. It is not necessary to first receive a ping in
2510 order to send a pong. The remote peer may use the receipt of a
2511 pong frame as an indication that the connection is not dead.
2512
2513 @param payload The payload of the pong message, which may be empty.
2514
2515 @param ec Set to indicate what error occurred, if any.
2516 */
2517 void
2518 pong(ping_data const& payload, error_code& ec);
2519
2520 /** Start an asynchronous operation to send a WebSocket pong frame.
2521
2522 This function is used to asynchronously send a pong frame to
2523 the stream. The function call always returns immediately. The
2524 asynchronous operation will continue until one of the following
2525 is true:
2526
2527 @li The entire pong frame is sent.
2528
2529 @li An error occurs on the stream.
2530
2531 This operation is implemented in terms of one or more calls to the
2532 next layer's `async_write_some` functions, and is known as a
2533 <em>composed operation</em>. The program must ensure that the
2534 stream performs no other writes until this operation completes.
2535
2536 The WebSocket protocol allows pong frames to be sent from either
2537 end at any time. It is not necessary to first receive a ping in
2538 order to send a pong. The remote peer may use the receipt of a
2539 pong frame as an indication that the connection is not dead.
2540
2541 If a close frame is sent or received before the pong frame is
2542 sent, the completion handler will be called with the error
2543 set to `boost::asio::error::operation_aborted`.
2544
2545 @param payload The payload of the pong message, which may be empty.
2546
2547 @param handler The handler to be called when the read operation
2548 completes. Copies will be made of the handler as required. The
2549 function signature of the handler must be:
2550 @code
2551 void handler(
2552 error_code const& ec // Result of operation
2553 );
2554 @endcode
2555 Regardless of whether the asynchronous operation completes
2556 immediately or not, the handler will not be invoked from within
2557 this function. Invocation of the handler will be performed in a
2558 manner equivalent to using `boost::asio::io_context::post`.
2559 */
2560 template<class WriteHandler>
2561 BOOST_ASIO_INITFN_RESULT_TYPE(
2562 WriteHandler, void(error_code))
2563 async_pong(ping_data const& payload, WriteHandler&& handler);
2564
2565 //--------------------------------------------------------------------------
2566 //
2567 // Reading
2568 //
2569 //--------------------------------------------------------------------------
2570
2571 /** Read a message
2572
2573 This function is used to synchronously read a complete
2574 message from the stream.
2575 The call blocks until one of the following is true:
2576
2577 @li A complete message is received.
2578
2579 @li A close frame is received. In this case the error indicated by
2580 the function will be @ref error::closed.
2581
2582 @li An error occurs on the stream.
2583
2584 This operation is implemented in terms of one or more calls to the next
2585 layer's `read_some` and `write_some` functions.
2586
2587 Received message data, if any, is appended to the input area of the
2588 buffer. The functions @ref got_binary and @ref got_text may be used
2589 to query the stream and determine the type of the last received message.
2590
2591 While this operation is active, the implementation will read incoming
2592 control frames and handle them automatically as follows:
2593
2594 @li The @ref control_callback will be invoked for each control frame.
2595
2596 @li For each received ping frame, a pong frame will be
2597 automatically sent.
2598
2599 @li If a close frame is received, the WebSocket close procedure is
2600 performed. In this case, when the function returns, the error
2601 @ref error::closed will be indicated.
2602
2603 @return The number of message payload bytes appended to the buffer.
2604
2605 @param buffer A dynamic buffer to hold the message data after any
2606 masking or decompression has been applied.
2607
2608 @throws system_error Thrown to indicate an error. The corresponding
2609 error code may be retrieved from the exception object for inspection.
2610 */
2611 template<class DynamicBuffer>
2612 std::size_t
2613 read(DynamicBuffer& buffer);
2614
2615 /** Read a message
2616
2617 This function is used to synchronously read a complete
2618 message from the stream.
2619 The call blocks until one of the following is true:
2620
2621 @li A complete message is received.
2622
2623 @li A close frame is received. In this case the error indicated by
2624 the function will be @ref error::closed.
2625
2626 @li An error occurs on the stream.
2627
2628 This operation is implemented in terms of one or more calls to the next
2629 layer's `read_some` and `write_some` functions.
2630
2631 Received message data, if any, is appended to the input area of the
2632 buffer. The functions @ref got_binary and @ref got_text may be used
2633 to query the stream and determine the type of the last received message.
2634
2635 While this operation is active, the implementation will read incoming
2636 control frames and handle them automatically as follows:
2637
2638 @li The @ref control_callback will be invoked for each control frame.
2639
2640 @li For each received ping frame, a pong frame will be
2641 automatically sent.
2642
2643 @li If a close frame is received, the WebSocket close procedure is
2644 performed. In this case, when the function returns, the error
2645 @ref error::closed will be indicated.
2646
2647 @return The number of message payload bytes appended to the buffer.
2648
2649 @param buffer A dynamic buffer to hold the message data after any
2650 masking or decompression has been applied.
2651
2652 @param ec Set to indicate what error occurred, if any.
2653 */
2654 template<class DynamicBuffer>
2655 std::size_t
2656 read(DynamicBuffer& buffer, error_code& ec);
2657
2658 /** Read a message asynchronously
2659
2660 This function is used to asynchronously read a complete
2661 message from the stream.
2662 The function call always returns immediately.
2663 The asynchronous operation will continue until one of the
2664 following is true:
2665
2666 @li A complete message is received.
2667
2668 @li A close frame is received. In this case the error indicated by
2669 the function will be @ref error::closed.
2670
2671 @li An error occurs on the stream.
2672
2673 This operation is implemented in terms of one or more calls to the
2674 next layer's `async_read_some` and `async_write_some` functions,
2675 and is known as a <em>composed operation</em>. The program must
2676 ensure that the stream performs no other reads until this operation
2677 completes.
2678
2679 Received message data, if any, is appended to the input area of the
2680 buffer. The functions @ref got_binary and @ref got_text may be used
2681 to query the stream and determine the type of the last received message.
2682
2683 While this operation is active, the implementation will read incoming
2684 control frames and handle them automatically as follows:
2685
2686 @li The @ref control_callback will be invoked for each control frame.
2687
2688 @li For each received ping frame, a pong frame will be
2689 automatically sent.
2690
2691 @li If a close frame is received, the WebSocket close procedure is
2692 performed. In this case, when the function returns, the error
2693 @ref error::closed will be indicated.
2694
2695 Because of the need to handle control frames, asynchronous read
2696 operations can cause writes to take place. These writes are managed
2697 transparently; callers can still have one active asynchronous
2698 read and asynchronous write operation pending simultaneously
2699 (a user initiated call to @ref async_close counts as a write).
2700
2701 @param buffer A dynamic buffer to hold the message data after
2702 any masking or decompression has been applied. This object must
2703 remain valid until the handler is called.
2704
2705 @param handler The handler to be called when the read operation
2706 completes. Copies will be made of the handler as required. The
2707 equivalent function signature of the handler must be:
2708 @code
2709 void handler(
2710 error_code const& ec, // Result of operation
2711 std::size_t bytes_written // Number of bytes appended to buffer
2712 );
2713 @endcode
2714 Regardless of whether the asynchronous operation completes
2715 immediately or not, the handler will not be invoked from within
2716 this function. Invocation of the handler will be performed in a
2717 manner equivalent to using `boost::asio::io_context::post`.
2718 */
2719 template<class DynamicBuffer, class ReadHandler>
2720 BOOST_ASIO_INITFN_RESULT_TYPE(
2721 ReadHandler, void(error_code, std::size_t))
2722 async_read(
2723 DynamicBuffer& buffer,
2724 ReadHandler&& handler);
2725
2726 //--------------------------------------------------------------------------
2727
2728 /** Read part of a message
2729
2730 This function is used to synchronously read some
2731 message data from the stream.
2732 The call blocks until one of the following is true:
2733
2734 @li Some or all of the message is received.
2735
2736 @li A close frame is received. In this case the error indicated by
2737 the function will be @ref error::closed.
2738
2739 @li An error occurs on the stream.
2740
2741 This operation is implemented in terms of one or more calls to the next
2742 layer's `read_some` and `write_some` functions.
2743
2744 Received message data, if any, is appended to the input area of the
2745 buffer. The functions @ref got_binary and @ref got_text may be used
2746 to query the stream and determine the type of the last received message.
2747 The function @ref is_message_done may be called to determine if the
2748 message received by the last read operation is complete.
2749
2750 While this operation is active, the implementation will read incoming
2751 control frames and handle them automatically as follows:
2752
2753 @li The @ref control_callback will be invoked for each control frame.
2754
2755 @li For each received ping frame, a pong frame will be
2756 automatically sent.
2757
2758 @li If a close frame is received, the WebSocket close procedure is
2759 performed. In this case, when the function returns, the error
2760 @ref error::closed will be indicated.
2761
2762 @return The number of message payload bytes appended to the buffer.
2763
2764 @param buffer A dynamic buffer to hold the message data after any
2765 masking or decompression has been applied.
2766
2767 @param limit An upper limit on the number of bytes this function
2768 will append into the buffer. If this value is zero, then a reasonable
2769 size will be chosen automatically.
2770
2771 @throws system_error Thrown to indicate an error. The corresponding
2772 error code may be retrieved from the exception object for inspection.
2773 */
2774 template<class DynamicBuffer>
2775 std::size_t
2776 read_some(
2777 DynamicBuffer& buffer,
2778 std::size_t limit);
2779
2780 /** Read part of a message
2781
2782 This function is used to synchronously read some
2783 message data from the stream.
2784 The call blocks until one of the following is true:
2785
2786 @li Some or all of the message is received.
2787
2788 @li A close frame is received. In this case the error indicated by
2789 the function will be @ref error::closed.
2790
2791 @li An error occurs on the stream.
2792
2793 This operation is implemented in terms of one or more calls to the next
2794 layer's `read_some` and `write_some` functions.
2795
2796 Received message data, if any, is appended to the input area of the
2797 buffer. The functions @ref got_binary and @ref got_text may be used
2798 to query the stream and determine the type of the last received message.
2799 The function @ref is_message_done may be called to determine if the
2800 message received by the last read operation is complete.
2801
2802 While this operation is active, the implementation will read incoming
2803 control frames and handle them automatically as follows:
2804
2805 @li The @ref control_callback will be invoked for each control frame.
2806
2807 @li For each received ping frame, a pong frame will be
2808 automatically sent.
2809
2810 @li If a close frame is received, the WebSocket close procedure is
2811 performed. In this case, when the function returns, the error
2812 @ref error::closed will be indicated.
2813
2814 @return The number of message payload bytes appended to the buffer.
2815
2816 @param buffer A dynamic buffer to hold the message data after any
2817 masking or decompression has been applied.
2818
2819 @param limit An upper limit on the number of bytes this function
2820 will append into the buffer. If this value is zero, then a reasonable
2821 size will be chosen automatically.
2822
2823 @param ec Set to indicate what error occurred, if any.
2824 */
2825 template<class DynamicBuffer>
2826 std::size_t
2827 read_some(
2828 DynamicBuffer& buffer,
2829 std::size_t limit,
2830 error_code& ec);
2831
2832 /** Read part of a message asynchronously
2833
2834 This function is used to asynchronously read part of a
2835 message from the stream.
2836 The function call always returns immediately.
2837 The asynchronous operation will continue until one of the
2838 following is true:
2839
2840 @li Some or all of the message is received.
2841
2842 @li A close frame is received. In this case the error indicated by
2843 the function will be @ref error::closed.
2844
2845 @li An error occurs on the stream.
2846
2847 This operation is implemented in terms of one or more calls to the
2848 next layer's `async_read_some` and `async_write_some` functions,
2849 and is known as a <em>composed operation</em>. The program must
2850 ensure that the stream performs no other reads until this operation
2851 completes.
2852
2853 Received message data, if any, is appended to the input area of the
2854 buffer. The functions @ref got_binary and @ref got_text may be used
2855 to query the stream and determine the type of the last received message.
2856 The function @ref is_message_done may be called to determine if the
2857 message received by the last read operation is complete.
2858
2859 While this operation is active, the implementation will read incoming
2860 control frames and handle them automatically as follows:
2861
2862 @li The @ref control_callback will be invoked for each control frame.
2863
2864 @li For each received ping frame, a pong frame will be
2865 automatically sent.
2866
2867 @li If a close frame is received, the WebSocket close procedure is
2868 performed. In this case, when the function returns, the error
2869 @ref error::closed will be indicated.
2870
2871 Because of the need to handle control frames, asynchronous read
2872 operations can cause writes to take place. These writes are managed
2873 transparently; callers can still have one active asynchronous
2874 read and asynchronous write operation pending simultaneously
2875 (a user initiated call to @ref async_close counts as a write).
2876
2877 @param buffer A dynamic buffer to hold the message data after
2878 any masking or decompression has been applied. This object must
2879 remain valid until the handler is called.
2880
2881 @param limit An upper limit on the number of bytes this function
2882 will append into the buffer. If this value is zero, then a reasonable
2883 size will be chosen automatically.
2884
2885 @param handler The handler to be called when the read operation
2886 completes. Copies will be made of the handler as required. The
2887 equivalent function signature of the handler must be:
2888 @code
2889 void handler(
2890 error_code const& ec, // Result of operation
2891 std::size_t bytes_written // Number of bytes appended to buffer
2892 );
2893 @endcode
2894 Regardless of whether the asynchronous operation completes
2895 immediately or not, the handler will not be invoked from within
2896 this function. Invocation of the handler will be performed in a
2897 manner equivalent to using `boost::asio::io_context::post`.
2898 */
2899 template<class DynamicBuffer, class ReadHandler>
2900 BOOST_ASIO_INITFN_RESULT_TYPE(
2901 ReadHandler, void(error_code, std::size_t))
2902 async_read_some(
2903 DynamicBuffer& buffer,
2904 std::size_t limit,
2905 ReadHandler&& handler);
2906
2907 //--------------------------------------------------------------------------
2908
2909 /** Read part of a message
2910
2911 This function is used to synchronously read some
2912 message data from the stream.
2913 The call blocks until one of the following is true:
2914
2915 @li Some or all of the message is received.
2916
2917 @li A close frame is received. In this case the error indicated by
2918 the function will be @ref error::closed.
2919
2920 @li An error occurs on the stream.
2921
2922 This operation is implemented in terms of one or more calls to the next
2923 layer's `read_some` and `write_some` functions.
2924
2925 Received message data, if any, is written to the buffer sequence.
2926 The functions @ref got_binary and @ref got_text may be used
2927 to query the stream and determine the type of the last received message.
2928 The function @ref is_message_done may be called to determine if the
2929 message received by the last read operation is complete.
2930
2931 While this operation is active, the implementation will read incoming
2932 control frames and handle them automatically as follows:
2933
2934 @li The @ref control_callback will be invoked for each control frame.
2935
2936 @li For each received ping frame, a pong frame will be
2937 automatically sent.
2938
2939 @li If a close frame is received, the WebSocket close procedure is
2940 performed. In this case, when the function returns, the error
2941 @ref error::closed will be indicated.
2942
2943 @return The number of message payload bytes written to the
2944 buffer sequence.
2945
2946 @param buffers A buffer sequence to hold the message data after any
2947 masking or decompression has been applied.
2948
2949 @throws system_error Thrown to indicate an error. The corresponding
2950 error code may be retrieved from the exception object for inspection.
2951 */
2952 template<class MutableBufferSequence>
2953 std::size_t
2954 read_some(
2955 MutableBufferSequence const& buffers);
2956
2957 /** Read part of a message
2958
2959 This function is used to synchronously read some
2960 message data from the stream.
2961 The call blocks until one of the following is true:
2962
2963 @li Some or all of the message is received.
2964
2965 @li A close frame is received. In this case the error indicated by
2966 the function will be @ref error::closed.
2967
2968 @li An error occurs on the stream.
2969
2970 This operation is implemented in terms of one or more calls to the next
2971 layer's `read_some` and `write_some` functions.
2972
2973 Received message data, if any, is written to the buffer sequence.
2974 The functions @ref got_binary and @ref got_text may be used
2975 to query the stream and determine the type of the last received message.
2976 The function @ref is_message_done may be called to determine if the
2977 message received by the last read operation is complete.
2978
2979 While this operation is active, the implementation will read incoming
2980 control frames and handle them automatically as follows:
2981
2982 @li The @ref control_callback will be invoked for each control frame.
2983
2984 @li For each received ping frame, a pong frame will be
2985 automatically sent.
2986
2987 @li If a close frame is received, the WebSocket close procedure is
2988 performed. In this case, when the function returns, the error
2989 @ref error::closed will be indicated.
2990
2991 @return The number of message payload bytes written to the
2992 buffer sequence.
2993
2994 @param buffers A buffer sequence to hold the message data after any
2995 masking or decompression has been applied.
2996
2997 @param ec Set to indicate what error occurred, if any.
2998 */
2999 template<class MutableBufferSequence>
3000 std::size_t
3001 read_some(
3002 MutableBufferSequence const& buffers,
3003 error_code& ec);
3004
3005 /** Read part of a message asynchronously
3006
3007 This function is used to asynchronously read part of a
3008 message from the stream.
3009 The function call always returns immediately.
3010 The asynchronous operation will continue until one of the
3011 following is true:
3012
3013 @li Some or all of the message is received.
3014
3015 @li A close frame is received. In this case the error indicated by
3016 the function will be @ref error::closed.
3017
3018 @li An error occurs on the stream.
3019
3020 This operation is implemented in terms of one or more calls to the
3021 next layer's `async_read_some` and `async_write_some` functions,
3022 and is known as a <em>composed operation</em>. The program must
3023 ensure that the stream performs no other reads until this operation
3024 completes.
3025
3026 Received message data, if any, is written to the buffer sequence.
3027 The functions @ref got_binary and @ref got_text may be used
3028 to query the stream and determine the type of the last received message.
3029 The function @ref is_message_done may be called to determine if the
3030 message received by the last read operation is complete.
3031
3032 While this operation is active, the implementation will read incoming
3033 control frames and handle them automatically as follows:
3034
3035 @li The @ref control_callback will be invoked for each control frame.
3036
3037 @li For each received ping frame, a pong frame will be
3038 automatically sent.
3039
3040 @li If a close frame is received, the WebSocket close procedure is
3041 performed. In this case, when the function returns, the error
3042 @ref error::closed will be indicated.
3043
3044 Because of the need to handle control frames, asynchronous read
3045 operations can cause writes to take place. These writes are managed
3046 transparently; callers can still have one active asynchronous
3047 read and asynchronous write operation pending simultaneously
3048 (a user initiated call to @ref async_close counts as a write).
3049
3050 @param buffers The buffer sequence into which message data will
3051 be placed after any masking or decompresison has been applied.
3052 The implementation will make copies of this object as needed,
3053 but ownership of the underlying memory is not transferred.
3054 The caller is responsible for ensuring that the memory
3055 locations pointed to by the buffer sequence remains valid
3056 until the completion handler is called.
3057
3058 @param handler The handler to be called when the read operation
3059 completes. Copies will be made of the handler as required. The
3060 equivalent function signature of the handler must be:
3061 @code
3062 void handler(
3063 error_code const& ec, // Result of operation
3064 std::size_t bytes_written // Number of bytes written to the buffer sequence
3065 );
3066 @endcode
3067 Regardless of whether the asynchronous operation completes
3068 immediately or not, the handler will not be invoked from within
3069 this function. Invocation of the handler will be performed in a
3070 manner equivalent to using `boost::asio::io_context::post`.
3071 */
3072 template<class MutableBufferSequence, class ReadHandler>
3073 BOOST_ASIO_INITFN_RESULT_TYPE(
3074 ReadHandler, void(error_code, std::size_t))
3075 async_read_some(
3076 MutableBufferSequence const& buffers,
3077 ReadHandler&& handler);
3078
3079 //--------------------------------------------------------------------------
3080 //
3081 // Writing
3082 //
3083 //--------------------------------------------------------------------------
3084
3085 /** Write a message to the stream.
3086
3087 This function is used to synchronously write a message to
3088 the stream. The call blocks until one of the following conditions
3089 is met:
3090
3091 @li The entire message is sent.
3092
3093 @li An error occurs.
3094
3095 This operation is implemented in terms of one or more calls to the
3096 next layer's `write_some` function.
3097
3098 The current setting of the @ref binary option controls
3099 whether the message opcode is set to text or binary. If the
3100 @ref auto_fragment option is set, the message will be split
3101 into one or more frames as necessary. The actual payload contents
3102 sent may be transformed as per the WebSocket protocol settings.
3103
3104 @param buffers The buffers containing the entire message
3105 payload. The implementation will make copies of this object
3106 as needed, but ownership of the underlying memory is not
3107 transferred. The caller is responsible for ensuring that
3108 the memory locations pointed to by buffers remains valid
3109 until the completion handler is called.
3110
3111 @return The number of bytes written from the buffers.
3112 If an error occurred, this will be less than the sum
3113 of the buffer sizes.
3114
3115 @throws system_error Thrown on failure.
3116
3117 @note This function always sends an entire message. To
3118 send a message in fragments, use @ref write_some.
3119 */
3120 template<class ConstBufferSequence>
3121 std::size_t
3122 write(ConstBufferSequence const& buffers);
3123
3124 /** Write a message to the stream.
3125
3126 This function is used to synchronously write a message to
3127 the stream. The call blocks until one of the following conditions
3128 is met:
3129
3130 @li The entire message is sent.
3131
3132 @li An error occurs.
3133
3134 This operation is implemented in terms of one or more calls to the
3135 next layer's `write_some` function.
3136
3137 The current setting of the @ref binary option controls
3138 whether the message opcode is set to text or binary. If the
3139 @ref auto_fragment option is set, the message will be split
3140 into one or more frames as necessary. The actual payload contents
3141 sent may be transformed as per the WebSocket protocol settings.
3142
3143 @param buffers The buffers containing the entire message
3144 payload. The implementation will make copies of this object
3145 as needed, but ownership of the underlying memory is not
3146 transferred. The caller is responsible for ensuring that
3147 the memory locations pointed to by buffers remains valid
3148 until the completion handler is called.
3149
3150 @return The number of bytes written from the buffers.
3151 If an error occurred, this will be less than the sum
3152 of the buffer sizes.
3153
3154 @param ec Set to indicate what error occurred, if any.
3155
3156 @throws system_error Thrown on failure.
3157
3158 @note This function always sends an entire message. To
3159 send a message in fragments, use @ref write_some.
3160 */
3161 template<class ConstBufferSequence>
3162 std::size_t
3163 write(ConstBufferSequence const& buffers, error_code& ec);
3164
3165 /** Start an asynchronous operation to write a message to the stream.
3166
3167 This function is used to asynchronously write a message to
3168 the stream. The function call always returns immediately.
3169 The asynchronous operation will continue until one of the
3170 following conditions is true:
3171
3172 @li The entire message is sent.
3173
3174 @li An error occurs.
3175
3176 This operation is implemented in terms of one or more calls
3177 to the next layer's `async_write_some` functions, and is known
3178 as a <em>composed operation</em>. The program must ensure that
3179 the stream performs no other write operations (such as
3180 stream::async_write, stream::async_write_some, or
3181 stream::async_close).
3182
3183 The current setting of the @ref binary option controls
3184 whether the message opcode is set to text or binary. If the
3185 @ref auto_fragment option is set, the message will be split
3186 into one or more frames as necessary. The actual payload contents
3187 sent may be transformed as per the WebSocket protocol settings.
3188
3189 @param buffers The buffers containing the entire message
3190 payload. The implementation will make copies of this object
3191 as needed, but ownership of the underlying memory is not
3192 transferred. The caller is responsible for ensuring that
3193 the memory locations pointed to by buffers remains valid
3194 until the completion handler is called.
3195
3196 @param handler The handler to be called when the write operation
3197 completes. Copies will be made of the handler as required. The
3198 function signature of the handler must be:
3199 @code
3200 void handler(
3201 error_code const& ec, // Result of operation
3202 std::size_t bytes_transferred // Number of bytes written from the
3203 // buffers. If an error occurred,
3204 // this will be less than the sum
3205 // of the buffer sizes.
3206 );
3207 @endcode
3208 Regardless of whether the asynchronous operation completes
3209 immediately or not, the handler will not be invoked from within
3210 this function. Invocation of the handler will be performed in a
3211 manner equivalent to using `boost::asio::io_context::post`.
3212 */
3213 template<
3214 class ConstBufferSequence,
3215 class WriteHandler>
3216 BOOST_ASIO_INITFN_RESULT_TYPE(
3217 WriteHandler, void(error_code, std::size_t))
3218 async_write(
3219 ConstBufferSequence const& buffers,
3220 WriteHandler&& handler);
3221
3222 /** Write partial message data on the stream.
3223
3224 This function is used to write some or all of a message's
3225 payload to the stream. The call will block until one of the
3226 following conditions is true:
3227
3228 @li A frame is sent.
3229
3230 @li Message data is transferred to the write buffer.
3231
3232 @li An error occurs.
3233
3234 This operation is implemented in terms of one or more calls
3235 to the stream's `write_some` function.
3236
3237 If this is the beginning of a new message, the message opcode
3238 will be set to text or binary as per the current setting of
3239 the @ref binary option. The actual payload sent may be
3240 transformed as per the WebSocket protocol settings.
3241
3242 @param fin `true` if this is the last part of the message.
3243
3244 @param buffers The input buffer sequence holding the data to write.
3245
3246 @return The number of bytes written from the buffers.
3247 If an error occurred, this will be less than the sum
3248 of the buffer sizes.
3249
3250 @throws system_error Thrown on failure.
3251 */
3252 template<class ConstBufferSequence>
3253 std::size_t
3254 write_some(bool fin, ConstBufferSequence const& buffers);
3255
3256 /** Write partial message data on the stream.
3257
3258 This function is used to write some or all of a message's
3259 payload to the stream. The call will block until one of the
3260 following conditions is true:
3261
3262 @li A frame is sent.
3263
3264 @li Message data is transferred to the write buffer.
3265
3266 @li An error occurs.
3267
3268 This operation is implemented in terms of one or more calls
3269 to the stream's `write_some` function.
3270
3271 If this is the beginning of a new message, the message opcode
3272 will be set to text or binary as per the current setting of
3273 the @ref binary option. The actual payload sent may be
3274 transformed as per the WebSocket protocol settings.
3275
3276 @param fin `true` if this is the last part of the message.
3277
3278 @param buffers The input buffer sequence holding the data to write.
3279
3280 @param ec Set to indicate what error occurred, if any.
3281
3282 @return The number of bytes written from the buffers.
3283 If an error occurred, this will be less than the sum
3284 of the buffer sizes.
3285
3286 @return The number of bytes consumed in the input buffers.
3287 */
3288 template<class ConstBufferSequence>
3289 std::size_t
3290 write_some(bool fin,
3291 ConstBufferSequence const& buffers, error_code& ec);
3292
3293 /** Start an asynchronous operation to send a message frame on the stream.
3294
3295 This function is used to asynchronously write a message frame
3296 on the stream. This function call always returns immediately.
3297 The asynchronous operation will continue until one of the following
3298 conditions is true:
3299
3300 @li The entire frame is sent.
3301
3302 @li An error occurs.
3303
3304 This operation is implemented in terms of one or more calls
3305 to the next layer's `async_write_some` functions, and is known
3306 as a <em>composed operation</em>. The actual payload sent
3307 may be transformed as per the WebSocket protocol settings. The
3308 program must ensure that the stream performs no other write
3309 operations (such as stream::async_write, stream::async_write_some,
3310 or stream::async_close).
3311
3312 If this is the beginning of a new message, the message opcode
3313 will be set to text or binary as per the current setting of
3314 the @ref binary option. The actual payload sent may be
3315 transformed as per the WebSocket protocol settings.
3316
3317 @param fin `true` if this is the last part of the message.
3318
3319 @param buffers A object meeting the requirements of
3320 ConstBufferSequence which holds the payload data before any
3321 masking or compression. Although the buffers object may be copied
3322 as necessary, ownership of the underlying buffers is retained by
3323 the caller, which must guarantee that they remain valid until
3324 the handler is called.
3325
3326 @param handler The handler to be called when the write completes.
3327 Copies will be made of the handler as required. The equivalent
3328 function signature of the handler must be:
3329 @code void handler(
3330 error_code const& ec, // Result of operation
3331 std::size_t bytes_transferred // Number of bytes written from the
3332 // buffers. If an error occurred,
3333 // this will be less than the sum
3334 // of the buffer sizes.
3335 ); @endcode
3336 */
3337 template<class ConstBufferSequence, class WriteHandler>
3338 BOOST_ASIO_INITFN_RESULT_TYPE(
3339 WriteHandler, void(error_code, std::size_t))
3340 async_write_some(bool fin,
3341 ConstBufferSequence const& buffers, WriteHandler&& handler);
3342
3343 private:
3344 template<class, class> class accept_op;
3345 template<class> class close_op;
3346 template<class> class fail_op;
3347 template<class> class handshake_op;
3348 template<class> class ping_op;
3349 template<class> class read_fh_op;
3350 template<class, class> class read_some_op;
3351 template<class, class> class read_op;
3352 template<class> class response_op;
3353 template<class, class> class write_some_op;
3354 template<class, class> class write_op;
3355
3356 static void default_decorate_req(request_type&) {}
3357 static void default_decorate_res(response_type&) {}
3358
3359 void open(role_type role);
3360 void close();
3361 void reset();
3362 void begin_msg();
3363
3364 bool
3365 check_open(error_code& ec)
3366 {
3367 if(status_ != status::open)
3368 {
3369 ec = boost::asio::error::operation_aborted;
3370 return false;
3371 }
3372 ec.assign(0, ec.category());
3373 return true;
3374 }
3375
3376 bool
3377 check_ok(error_code& ec)
3378 {
3379 if(ec)
3380 {
3381 if(status_ != status::closed)
3382 status_ = status::failed;
3383 return false;
3384 }
3385 return true;
3386 }
3387
3388 template<class DynamicBuffer>
3389 bool
3390 parse_fh(detail::frame_header& fh,
3391 DynamicBuffer& b, close_code& code);
3392
3393 template<class DynamicBuffer>
3394 void
3395 write_close(DynamicBuffer& b, close_reason const& rc);
3396
3397 template<class DynamicBuffer>
3398 void
3399 write_ping(DynamicBuffer& b,
3400 detail::opcode op, ping_data const& data);
3401
3402 template<class Decorator>
3403 request_type
3404 build_request(detail::sec_ws_key_type& key,
3405 string_view host,
3406 string_view target,
3407 Decorator const& decorator);
3408
3409 template<class Body,
3410 class Allocator, class Decorator>
3411 response_type
3412 build_response(http::request<Body,
3413 http::basic_fields<Allocator>> const& req,
3414 Decorator const& decorator);
3415
3416 void
3417 on_response(response_type const& resp,
3418 detail::sec_ws_key_type const& key, error_code& ec);
3419
3420 template<class Decorator>
3421 void
3422 do_accept(Decorator const& decorator,
3423 error_code& ec);
3424
3425 template<class Body, class Allocator,
3426 class Decorator>
3427 void
3428 do_accept(http::request<Body,
3429 http::basic_fields<Allocator>> const& req,
3430 Decorator const& decorator, error_code& ec);
3431
3432 template<class RequestDecorator>
3433 void
3434 do_handshake(response_type* res_p,
3435 string_view host, string_view target,
3436 RequestDecorator const& decorator,
3437 error_code& ec);
3438
3439 void
3440 do_fail(
3441 std::uint16_t code,
3442 error_code ev,
3443 error_code& ec);
3444 };
3445
3446 } // websocket
3447 } // beast
3448 } // boost
3449
3450 #include <boost/beast/websocket/impl/accept.ipp>
3451 #include <boost/beast/websocket/impl/close.ipp>
3452 #include <boost/beast/websocket/impl/handshake.ipp>
3453 #include <boost/beast/websocket/impl/ping.ipp>
3454 #include <boost/beast/websocket/impl/read.ipp>
3455 #include <boost/beast/websocket/impl/stream.ipp>
3456 #include <boost/beast/websocket/impl/write.ipp>
3457
3458 #endif