Files
boost_beast/doc/qbk/06_websocket/3_client.qbk

98 lines
3.3 KiB
Plaintext
Raw Normal View History

2017-06-03 18:40:28 -07:00
[/
2017-07-28 19:32:33 -07:00
Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-06-03 18:40:28 -07:00
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)
2017-07-28 19:32:33 -07:00
Official repository: https://github.com/boostorg/beast
2017-06-03 18:40:28 -07:00
]
2017-06-16 19:27:00 -07:00
[section Handshaking (Clients)]
2017-06-03 18:40:28 -07:00
2017-06-08 15:23:30 -07:00
A WebSocket session begins when a client sends the HTTP/1
2017-06-03 18:40:28 -07:00
[@https://tools.ietf.org/html/rfc7230#section-6.7 Upgrade]
request for
[@https://tools.ietf.org/html/rfc6455#section-1.3 websocket],
2017-06-08 15:23:30 -07:00
and the server sends an appropriate response indicating that
2017-06-03 18:40:28 -07:00
the request was accepted and that the connection has been upgraded.
2017-06-08 15:23:30 -07:00
The Upgrade request must include the
2017-06-03 18:40:28 -07:00
[@https://tools.ietf.org/html/rfc7230#section-5.4 Host]
field, and the
[@https://tools.ietf.org/html/rfc7230#section-5.3 target]
of the resource to request. The stream member functions
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.handshake.overload1 `handshake`] and
[link beast.ref.boost__beast__websocket__stream.async_handshake.overload1 `async_handshake`]
2017-06-03 18:40:28 -07:00
are used to send the request with the required host and target strings.
2017-06-08 15:23:30 -07:00
[ws_snippet_8]
The implementation will create and send a request that typically
2017-06-03 18:40:28 -07:00
looks like this:
[table WebSocket Upgrade HTTP Request
[[Serialized Octets][Description]]
[[
```
GET / HTTP/1.1
Host: localhost
Upgrade: websocket
Connection: upgrade
Sec-WebSocket-Key: 2pGeTR0DsE4dfZs2pH+8MA==
Sec-WebSocket-Version: 13
User-Agent: Beast
```
][
The host and target parameters become part of the Host field
and request-target in the resulting HTTP request. The key is
generated by the implementation. Callers may add fields or
modify fields by providing a ['decorator], described below.
]]]
[heading Decorators]
If the caller wishes to add or modify fields, the member functions
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.handshake_ex `handshake_ex`] and
[link beast.ref.boost__beast__websocket__stream.async_handshake_ex `async_handshake_ex`]
2017-06-03 18:40:28 -07:00
are provided which allow an additional function object, called a
['decorator], to be passed. The decorator is invoked to modify
the HTTP Upgrade request as needed. This example sets a subprotocol
on the request:
2017-06-08 15:23:30 -07:00
[ws_snippet_9]
2017-06-03 18:40:28 -07:00
The HTTP Upgrade request produced by the previous call will look thusly:
[table Decorated WebSocket Upgrade HTTP Request
[[Serialized Octets][Description]]
[[
```
GET / HTTP/1.1
Host: localhost
Upgrade: websocket
Connection: upgrade
Sec-WebSocket-Key: 2pGeTR0DsE4dfZs2pH+8MA==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: xmpp;ws-chat
User-Agent: Beast
```
][
Undefined behavior results if the decorator modifies the fields
specific to perform the WebSocket Upgrade , such as the Upgrade
and Connection fields.
]]]
[heading Filtering]
When a client receives an HTTP Upgrade response from the server indicating
a successful upgrade, the caller may wish to perform additional validation
on the received HTTP response message. For example, to check that the
response to a basic authentication challenge is valid. To achieve this,
overloads of the handshake member function allow the caller to store the
received HTTP message in an output reference argument of type
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__response_type `response_type`]
2017-06-03 18:40:28 -07:00
as follows:
2017-06-08 15:23:30 -07:00
[ws_snippet_10]
2017-06-03 18:40:28 -07:00
[endsect]