forked from boostorg/beast
detect_ssl, async_detect_ssl are public interfaces
This commit is contained in:
1 parent
b2807ae3ee
commit
90b783cb62
28 files changed
+1057
-784
No files matched your search
@@ -95,6 +95,5 @@ effect:
|
||||
[include 4_buffers.qbk]
|
||||
[include 5_files.qbk]
|
||||
[include 6_composed.qbk]
|
||||
[include 7_detect_ssl.qbk]
|
||||
|
||||
[endsect]
|
||||
@@ -8,7 +8,6 @@
|
||||
]
|
||||
|
||||
[section Writing Composed Operations]
|
||||
[block'''<?dbhtml stop-chunking?>''']
|
||||
|
||||
Asynchronous operations are started by calling a free function or member
|
||||
function known as an asynchronous ['__async_initfn__]. This function accepts
|
||||
@@ -111,80 +110,7 @@ composed operations:
|
||||
]]
|
||||
]
|
||||
|
||||
|
||||
|
||||
[section Echo]
|
||||
|
||||
This example develops an initiating function called [*echo].
|
||||
The operation will read up to the first newline on a stream, and
|
||||
then write the same line including the newline back on the stream.
|
||||
First we define the input parameters and results, then declare our
|
||||
initiation function. For our echo operation the only inputs are the
|
||||
stream and the completion token. The output is the error code which
|
||||
is usually included in all completion handler signatures.
|
||||
|
||||
[example_core_echo_op_2]
|
||||
|
||||
Now that we have a declaration, we will define the body of the function.
|
||||
We want to achieve the following goals: perform static type checking on
|
||||
the input parameters, set up the return value as per __N3747__, and launch
|
||||
the composed operation by constructing an intermediate, stateful completion
|
||||
handler and invoking it.
|
||||
|
||||
The initiating function contains a few relatively simple parts. There is
|
||||
the customization of the return value type, static type checking, building
|
||||
the return value type using the helper, and creating and launching the
|
||||
`echo_op` composed operation object.
|
||||
|
||||
The implementation strategy is to make the composed object meet the
|
||||
requirements of a completion handler by being movable, and by making it
|
||||
invocable so it can be used as a continuation for the asynchronous operations
|
||||
it launches. Rather than using `std::bind` or `boost::bind`, which destroys
|
||||
the type information and therefore breaks the allocation and invocation hooks,
|
||||
we will simply pass `std::move(*this)` as the completion handler parameter for
|
||||
any operations that we initiate. For the move to work correctly, care must be
|
||||
taken to ensure that no access to data members are made after the move takes
|
||||
place. Here is the complete implementation of our composed operation:
|
||||
|
||||
[example_core_echo_op_3]
|
||||
|
||||
There are some common mistakes that should be avoided when writing
|
||||
composed operations:
|
||||
|
||||
* Type erasing the final handler. This will cause undefined behavior.
|
||||
|
||||
* Forgetting to include a return statement after calling an
|
||||
initiating function.
|
||||
|
||||
* Calling a synchronous function by accident. In general composed
|
||||
operations should not block for long periods of time, since this
|
||||
ties up a thread running on the __io_context__.
|
||||
|
||||
* Forgetting to provide `executor_type` and `get_executor` for the
|
||||
composed operation. This will cause undefined behavior. For example,
|
||||
if someone calls the initiating function with a strand-wrapped
|
||||
function object, and there is more than thread running on the
|
||||
__io_context__, the underlying stream may be accessed in a fashion
|
||||
that violates safety guarantees. Beast provides class templates
|
||||
to take care of this boilerplate for you.
|
||||
|
||||
* Forgetting to create an object of type __executor_work_guard__ with the
|
||||
type of executor returned by the stream's `get_executor` member function.
|
||||
|
||||
* For operations which complete immediately (i.e. without calling an
|
||||
intermediate initiating function), forgetting to use __post__ to
|
||||
invoke the final handler. This breaks the following initiating
|
||||
function guarantee: ['Regardless of whether the asynchronous operation
|
||||
completes immediately or not, the handler will not be invoked from
|
||||
within this function. Invocation of the handler will be performed
|
||||
in a manner equivalent to using __post__]. The function
|
||||
__bind_handler__ is provided for this purpose.
|
||||
|
||||
A complete, runnable version of this example may be found in the examples
|
||||
directory.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[include 6a_echo.qbk]
|
||||
[include 6b_detect_ssl.qbk]
|
||||
|
||||
[endsect]
|
||||
@@ -0,0 +1,80 @@
|
||||
[/
|
||||
Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
Official repository: https://github.com/boostorg/beast
|
||||
]
|
||||
|
||||
[section:echo Echo]
|
||||
|
||||
This example develops an initiating function called [*echo].
|
||||
The operation will read up to the first newline on a stream, and
|
||||
then write the same line including the newline back on the stream.
|
||||
First we define the input parameters and results, then declare our
|
||||
initiation function. For our echo operation the only inputs are the
|
||||
stream and the completion token. The output is the error code which
|
||||
is usually included in all completion handler signatures.
|
||||
|
||||
[example_core_echo_op_2]
|
||||
|
||||
Now that we have a declaration, we will define the body of the function.
|
||||
We want to achieve the following goals: perform static type checking on
|
||||
the input parameters, set up the return value as per __N3747__, and launch
|
||||
the composed operation by constructing an intermediate, stateful completion
|
||||
handler and invoking it.
|
||||
|
||||
The initiating function contains a few relatively simple parts. There is
|
||||
the customization of the return value type, static type checking, building
|
||||
the return value type using the helper, and creating and launching the
|
||||
`echo_op` composed operation object.
|
||||
|
||||
The implementation strategy is to make the composed object meet the
|
||||
requirements of a completion handler by being movable, and by making it
|
||||
invocable so it can be used as a continuation for the asynchronous operations
|
||||
it launches. Rather than using `std::bind` or `boost::bind`, which destroys
|
||||
the type information and therefore breaks the allocation and invocation hooks,
|
||||
we will simply pass `std::move(*this)` as the completion handler parameter for
|
||||
any operations that we initiate. For the move to work correctly, care must be
|
||||
taken to ensure that no access to data members are made after the move takes
|
||||
place. Here is the complete implementation of our composed operation:
|
||||
|
||||
[example_core_echo_op_3]
|
||||
|
||||
There are some common mistakes that should be avoided when writing
|
||||
composed operations:
|
||||
|
||||
* Type erasing the final handler. This will cause undefined behavior.
|
||||
|
||||
* Forgetting to include a return statement after calling an
|
||||
initiating function.
|
||||
|
||||
* Calling a synchronous function by accident. In general composed
|
||||
operations should not block for long periods of time, since this
|
||||
ties up a thread running on the __io_context__.
|
||||
|
||||
* Forgetting to provide `executor_type` and `get_executor` for the
|
||||
composed operation. This will cause undefined behavior. For example,
|
||||
if someone calls the initiating function with a strand-wrapped
|
||||
function object, and there is more than thread running on the
|
||||
__io_context__, the underlying stream may be accessed in a fashion
|
||||
that violates safety guarantees. Beast provides class templates
|
||||
to take care of this boilerplate for you.
|
||||
|
||||
* Forgetting to create an object of type __executor_work_guard__ with the
|
||||
type of executor returned by the stream's `get_executor` member function.
|
||||
|
||||
* For operations which complete immediately (i.e. without calling an
|
||||
intermediate initiating function), forgetting to use __post__ to
|
||||
invoke the final handler. This breaks the following initiating
|
||||
function guarantee: ['Regardless of whether the asynchronous operation
|
||||
completes immediately or not, the handler will not be invoked from
|
||||
within this function. Invocation of the handler will be performed
|
||||
in a manner equivalent to using __post__]. The function
|
||||
__bind_handler__ is provided for this purpose.
|
||||
|
||||
A complete, runnable version of this example may be found in the examples
|
||||
directory.
|
||||
|
||||
[endsect]
|
||||
@@ -0,0 +1,109 @@
|
||||
[/
|
||||
Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
Official repository: https://github.com/boostorg/beast
|
||||
]
|
||||
|
||||
[section:detect_ssl Detect SSL]
|
||||
|
||||
In this example we will build a simple function to detect the presence of the
|
||||
[@https://tools.ietf.org/html/rfc2246#section-7.4 TLS client handshake]
|
||||
given an input buffer sequence. Then we build on the example by adding a
|
||||
synchronous stream algorithm. Finally, we implement an asynchronous
|
||||
detection function using a composed operation. This SSL detector may
|
||||
be used to allow a server to accept both TLS and plain (unencrypted)
|
||||
connections at the same port.
|
||||
|
||||
Here is the declaration for a function template to detect the SSL client
|
||||
handshake. The function accepts any object whose type meets the requirements
|
||||
of __ConstBufferSequence__. This gives callers flexibility to use a buffer
|
||||
object whose behavior is appropriate to the task.
|
||||
|
||||
[example_core_detect_ssl_1]
|
||||
|
||||
The algorithm examines the buffer starting from the beginning, and
|
||||
performs a series of qualifying checks against the TLS specification.
|
||||
When not enough data exists to be certain, the returned value of
|
||||
`boost::indeterminate` informs the caller to read more data into the buffer.
|
||||
The function definition for the declaration above follows:
|
||||
|
||||
[example_core_detect_ssl_2]
|
||||
|
||||
The detection function above is suitably generic and targeted in
|
||||
focus that it may be used as a building block to create higher level
|
||||
abstractions. Our goal is to create a ['stream algorithm]: a function
|
||||
which is invoked with a stream, that reads or writes (or both) to achieve
|
||||
a purpose. In this case, to detect the TLS client handshake. Stream
|
||||
algorithms may be synchronous or asynchronous. Because synchronous algorithms
|
||||
are easier to write, we start there. Then we build the asynchronous version,
|
||||
trying to model it similarly to make reasoning about it easier.
|
||||
|
||||
The synchronous version is implemented thusly:
|
||||
|
||||
[example_core_detect_ssl_3]
|
||||
|
||||
Now that we have the synchronous version, we can attempt to model the
|
||||
asynchronous version similarly. A function which launches an asynchronous
|
||||
operation is called an ['initiating function]. While the synchronous
|
||||
version above produces an error code through an output parameter, the
|
||||
asynchronous version delivers the error code to a completion handler
|
||||
or other custom mechanism defined by the completion token. The signature
|
||||
of the initiating function reflects these differences.
|
||||
|
||||
First we declare the initiating function and document the requirments,
|
||||
parameters, preconditions, and effects:
|
||||
|
||||
[example_core_detect_ssl_4]
|
||||
|
||||
There are two additional components required to implement the initiating
|
||||
function:
|
||||
|
||||
* An intermediate completion handler, called the "composed operation"
|
||||
object, which holds the state of the operation while it is in progress,
|
||||
and also holds the user's completion handler to be invoked when the
|
||||
opeartion completes, and
|
||||
|
||||
* An "initiation" function object which when invoked with parameters
|
||||
captured at the call site of the initiating function, constructs the
|
||||
composed operation with the captured arguments and launches it.
|
||||
|
||||
Here we forward declare the composed operation type, and provide the
|
||||
definition of the initiation function object. They are placed in the
|
||||
`detail` namespace since they should not be public:
|
||||
|
||||
[example_core_detect_ssl_5]
|
||||
|
||||
The initiating function definition itself is straightforward. We perform
|
||||
type checking on the parameters, and then let `net::async_initiate`
|
||||
capture the parameter list along with a copy of our initiation function
|
||||
object. Depending on the specialization of `async_result` for the type
|
||||
of `CompletionToken`, the initiation function may be invoked immediately.
|
||||
Alternatively, it may be invoked later, after the initiating function
|
||||
returns. This is known as "lazy execution," and allows efficient and
|
||||
expressive abstractions to be written.
|
||||
|
||||
[example_core_detect_ssl_6]
|
||||
|
||||
Now we will declare our composed operation. There is a considerable
|
||||
amount of necessary boilerplate to get this right, but the result
|
||||
is worth the effort.
|
||||
|
||||
[example_core_detect_ssl_7]
|
||||
|
||||
The boilerplate is all done, and now we need to implement the function
|
||||
call operator that turns this composed operation a completion handler
|
||||
with the signature `void(error_code, std::size_t)` which is exactly
|
||||
the signature needed when performing asynchronous reads. This function
|
||||
is a transformation of the synchronous version of `detect_ssl` above,
|
||||
but with the inversion of flow that characterizes code written in the
|
||||
callback style:
|
||||
|
||||
[example_core_detect_ssl_8]
|
||||
|
||||
This SSL detector is used in the advanced-flex and http-flex servers
|
||||
in the example directory.
|
||||
|
||||
[endsect]
|
||||
@@ -1,69 +0,0 @@
|
||||
[/
|
||||
Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
Official repository: https://github.com/boostorg/beast
|
||||
]
|
||||
|
||||
[section Example: Detect SSL]
|
||||
|
||||
In this example we will build a simple function to detect the presence
|
||||
of the SSL handshake given an input buffer sequence. Then we build on
|
||||
the example by adding a synchronous stream algorithm. Finally, we
|
||||
implement an asynchronous detection function using a composed operation.
|
||||
This SSL detector may be used to allow a server to accept both SSL/TLS and
|
||||
unencrypted connections at the same port.
|
||||
|
||||
Here is the declaration for a function to detect the SSL client handshake.
|
||||
The input to the function is simply a buffer sequence, no stream. This
|
||||
allows the detection algorithm to be used elsewhere.
|
||||
|
||||
[example_core_detect_ssl_1]
|
||||
|
||||
The implementation checks the buffer for the presence of the SSL
|
||||
Handshake message octet sequence and returns an apporopriate value:
|
||||
|
||||
[example_core_detect_ssl_2]
|
||||
|
||||
Now we define a stream operation. We start with the simple,
|
||||
synchronous version which takes the stream and buffer as input:
|
||||
|
||||
[example_core_detect_ssl_3]
|
||||
|
||||
The synchronous algorithm is the model for building the asynchronous
|
||||
operation which has more boilerplate. First, we declare the asynchronous
|
||||
initiating function:
|
||||
|
||||
[example_core_detect_ssl_4]
|
||||
|
||||
The implementation of the initiating function is straightforward
|
||||
and contains mostly boilerplate. It is to construct the return
|
||||
type customization helper to obtain the actual handler, and
|
||||
then create the composed operation and launch it. The actual
|
||||
code for interacting with the stream is in the composed operation,
|
||||
which is written as a separate class.
|
||||
|
||||
[example_core_detect_ssl_5]
|
||||
|
||||
Now we will declare our composed operation. There is a considerable
|
||||
amount of necessary boilerplate to get this right, but the result
|
||||
is worth the effort.
|
||||
|
||||
[example_core_detect_ssl_6]
|
||||
|
||||
The boilerplate is all done, and now we need to implement the function
|
||||
call operator that turns this composed operation a completion handler
|
||||
with the signature `void(error_code, std::size_t)` which is exactly
|
||||
the signature needed when performing asynchronous reads. This function
|
||||
is a transformation of the synchronous version of `detect_ssl` above,
|
||||
but with the inversion of flow that characterizes code written in the
|
||||
callback style:
|
||||
|
||||
[example_core_detect_ssl_7]
|
||||
|
||||
This SSL detector is used by the server framework in the example
|
||||
directory.
|
||||
|
||||
[endsect]
|
||||
+1
-1
@@ -117,7 +117,6 @@
|
||||
the paths close to absolute.
|
||||
]
|
||||
|
||||
[import ../../example/common/detect_ssl.hpp]
|
||||
[import ../../example/doc/http_examples.hpp]
|
||||
[import ../../example/echo-op/echo_op.cpp]
|
||||
[import ../../example/http/client/sync/http_client_sync.cpp]
|
||||
@@ -134,6 +133,7 @@
|
||||
[import ../../test/doc/core_3_layers.cpp]
|
||||
[import ../../test/doc/websocket_3_handshake.cpp]
|
||||
|
||||
[import ../../include/boost/beast/core/detect_ssl.hpp]
|
||||
[import ../../test/beast/core/rate_policy.cpp]
|
||||
|
||||
[section:quickref Reference]
|
||||
|
||||
@@ -64,10 +64,12 @@
|
||||
<bridgehead renderas="sect3">Functions</bridgehead>
|
||||
<simplelist type="vert" columns="1">
|
||||
<member><link linkend="beast.ref.boost__beast__allocate_stable">allocate_stable</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__async_detect_ssl">async_detect_ssl</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__beast_close_socket">beast_close_socket</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__bind_front_handler">bind_front_handler</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__bind_handler">bind_handler</link></member>
|
||||
<member><link linkend="beast.ref.boost__beast__close_socket">close_socket</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__detect_ssl">detect_ssl</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__generic_category">generic_category</link></member>
|
||||
<member><link linkend="beast.ref.boost__beast__get_lowest_layer">get_lowest_layer</link> <emphasis role="green">🞲</emphasis></member>
|
||||
<member><link linkend="beast.ref.boost__beast__iequals">iequals</link></member>
|
||||
|
||||
@@ -35,6 +35,13 @@
|
||||
!
|
||||
* Chat with us at the [*#beast] and [*#boost] channels in the
|
||||
[@https://cppalliance.org/slack/ [*C++ Slack Workspace]].
|
||||
* [role green [*More tutorials]], code like the pros!
|
||||
* [link beast.using_io.asio_refresher Networking Refresher] teaches you from the ground up.
|
||||
* Updated [link beast.using_io.writing_composed_operations.echo Asynchronous Echo] example
|
||||
* Updated [link beast.using_io.writing_composed_operations.detect_ssl [*Detect SSL Handshake]],
|
||||
now a [link beast.ref.boost__beast__async_detect_ssl public api]!
|
||||
* [@../../example/websocket/server/chat-multi websocket-chat-multi]
|
||||
threaded chat server with a JavaScript browser client.
|
||||
* [link beast.ref.boost__beast__basic_stream `basic_stream`] and
|
||||
[link beast.ref.boost__beast__basic_stream `tcp_stream`] offer:
|
||||
* Timeouts:
|
||||
@@ -47,8 +54,9 @@
|
||||
[link beast.ref.boost__beast__unlimited_rate_policy `unlimited`],
|
||||
or a user-defined
|
||||
[link beast.concepts.RatePolicy ['RatePolicy]]!
|
||||
* [[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1322r0.html P1322R0]]
|
||||
Put the strand on the socket itself, no more `bind_executor` at call sites!
|
||||
* Put the strand directly on the socket using
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1322r0.html P1322R0],
|
||||
no more `bind_executor` at call sites!
|
||||
* Base classes
|
||||
[link beast.ref.boost__beast__async_op_base `async_op_base`] and
|
||||
[link beast.ref.boost__beast__stable_async_op_base `stable_async_op_base`]
|
||||
@@ -56,10 +64,7 @@
|
||||
* All asynchronous operations use Asio's
|
||||
[@boost:/doc/html/boost_asio/reference/async_initiate.html `async_initiate`]
|
||||
for efficient integration with Coroutines TS.
|
||||
* New [@../../example/websocket/server/chat-multi websocket-chat-multi]
|
||||
multi-threaded websocket chat server with a JavaScript browser client.
|
||||
* New [link beast.using_io.asio_refresher Networking Refresher] explains basic concepts.
|
||||
* OpenSSL is required to build tests and examples
|
||||
* OpenSSL is now required to build tests and examples
|
||||
* See the full [link beast.release_notes [*Release Notes]] for a complete list
|
||||
of changes.
|
||||
|
||||
|
||||
Reference in new issue
Block a user