Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/devdoc/uws_client_requirements.md

raw
1 # uws_client requirements
2
3 ## Overview
4
5 uws_client is module that provides the public API for a WebSocket client. It is part of uWS, a WebSocket library for small devices.
6
7 ## References
8
9 RFC6455 - The WebSocket Protocol.
10
11 ## Exposed API
12
13 ```c
14 typedef struct UWS_CLIENT_INSTANCE_TAG* UWS_CLIENT_HANDLE;
15
16 #define WS_SEND_FRAME_RESULT_VALUES \
17 WS_SEND_FRAME_OK, \
18 WS_SEND_FRAME_ERROR, \
19 WS_SEND_FRAME_CANCELLED
20
21 MU_DEFINE_ENUM(WS_SEND_FRAME_RESULT, WS_SEND_FRAME_RESULT_VALUES);
22
23 #define WS_OPEN_RESULT_VALUES \
24 WS_OPEN_OK, \
25 WS_OPEN_ERROR_UNDERLYING_IO_OPEN_FAILED, \
26 WS_OPEN_ERROR_UNDERLYING_IO_OPEN_CANCELLED, \
27 WS_OPEN_ERROR_NOT_ENOUGH_MEMORY, \
28 WS_OPEN_ERROR_CANNOT_CONSTRUCT_UPGRADE_REQUEST, \
29 WS_OPEN_ERROR_CANNOT_SEND_UPGRADE_REQUEST, \
30 WS_OPEN_ERROR_MULTIPLE_UNDERLYING_IO_OPEN_EVENTS, \
31 WS_OPEN_ERROR_CONSTRUCTING_UPGRADE_REQUEST, \
32 WS_OPEN_ERROR_INVALID_BYTES_RECEIVED_ARGUMENTS, \
33 WS_OPEN_ERROR_BYTES_RECEIVED_BEFORE_UNDERLYING_OPEN, \
34 WS_OPEN_CANCELLED, \
35 WS_OPEN_ERROR_UNDERLYING_IO_ERROR, \
36 WS_OPEN_ERROR_BAD_UPGRADE_RESPONSE, \
37 WS_OPEN_ERROR_BAD_RESPONSE_STATUS
38
39 MU_DEFINE_ENUM(WS_OPEN_RESULT, WS_OPEN_RESULT_VALUES);
40
41 #define WS_ERROR_VALUES \
42 WS_ERROR_NOT_ENOUGH_MEMORY, \
43 WS_ERROR_BAD_FRAME_RECEIVED, \
44 WS_ERROR_CANNOT_REMOVE_SENT_ITEM_FROM_LIST, \
45 WS_ERROR_UNDERLYING_IO_ERROR, \
46 WS_ERROR_CANNOT_CLOSE_UNDERLYING_IO
47
48 MU_DEFINE_ENUM(WS_ERROR, WS_ERROR_VALUES);
49
50 #define WS_FRAME_TYPE_TEXT 0x01
51 #define WS_FRAME_TYPE_BINARY 0x02
52
53 #define CLOSE_NORMAL 1000
54 #define CLOSE_GOING_AWAY 1001
55 #define CLOSE_PROTOCOL_ERROR 1002
56 #define CLOSE_CANNOT_ACCEPT_DATA_TYPE 1003
57 #define CLOSE_UNDEFINED_1004 1004
58 #define CLOSE_RESERVED_1005 1005
59 #define CLOSE_RESERVED_1006 1006
60 #define CLOSE_INCONSISTENT_DATA_IN_MESSAGE 1007
61 #define CLOSE_POLICY_VIOLATION 1008
62 #define CLOSE_MESSAGE_TOO_BIG 1009
63 #define CLOSE_UNSUPPORTED_EXTENSION_LIST 1010
64 #define CLOSE_UNEXPECTED_CONDITION 1011
65 #define CLOSE_RESERVED_1015 1015
66
67 typedef void(*ON_WS_FRAME_RECEIVED)(void* context, unsigned char frame_type, const unsigned char* buffer, size_t size);
68 typedef void(*ON_WS_SEND_FRAME_COMPLETE)(void* context, WS_SEND_FRAME_RESULT ws_send_frame_result);
69 typedef void(*ON_WS_OPEN_COMPLETE)(void* context, WS_OPEN_RESULT ws_open_result);
70 typedef void(*ON_WS_CLOSE_COMPLETE)(void* context);
71 typedef void(*ON_WS_PEER_CLOSED)(void* context, uint16_t* close_code, const unsigned char* extra_data, size_t extra_data_length);
72 typedef void(*ON_WS_ERROR)(void* context, WS_ERROR error_code);
73
74 typedef struct WS_PROTOCOL_TAG
75 {
76 const char* protocol;
77 } WS_PROTOCOL;
78
79 MOCKABLE_FUNCTION(, UWS_CLIENT_HANDLE, uws_client_create, const char*, hostname, unsigned int, port, const char*, resource_name, bool, use_ssl, const WS_PROTOCOL*, protocols, size_t, protocol_count);
80 MOCKABLE_FUNCTION(, UWS_CLIENT_HANDLE, uws_client_create_with_io, const IO_INTERFACE_DESCRIPTION*, io_interface, void*, io_create_parameters, const char*, hostname, unsigned int, port, const char*, resource_name, const WS_PROTOCOL*, protocols, size_t, protocol_count);
81 MOCKABLE_FUNCTION(, void, uws_client_destroy, UWS_CLIENT_HANDLE, uws_client);
82 MOCKABLE_FUNCTION(, int, uws_client_open_async, UWS_CLIENT_HANDLE, uws_client, ON_WS_OPEN_COMPLETE, on_ws_open_complete, void*, on_ws_open_complete_context, ON_WS_FRAME_RECEIVED, on_ws_frame_received, void*, on_ws_frame_received_context, ON_WS_PEER_CLOSED, on_ws_peer_closed, void*, on_ws_peer_closed_context, ON_WS_ERROR, on_ws_error, void*, on_ws_error_context);
83 MOCKABLE_FUNCTION(, int, uws_client_close_async, UWS_CLIENT_HANDLE, uws_client, ON_WS_CLOSE_COMPLETE, on_ws_close_complete, void*, on_ws_close_complete_context);
84 MOCKABLE_FUNCTION(, int, uws_client_close_handshake_async, UWS_CLIENT_HANDLE, uws_client, uint16_t, close_code, const char*, close_reason, ON_WS_CLOSE_COMPLETE, on_ws_close_complete, void*, on_ws_close_complete_context);
85 MOCKABLE_FUNCTION(, int, uws_client_send_frame_async, UWS_CLIENT_HANDLE, uws_client, unsigned char, frame_type, const unsigned char*, buffer, size_t, size, bool, is_final, ON_WS_SEND_FRAME_COMPLETE, on_ws_send_frame_complete, void*, callback_context);
86 MOCKABLE_FUNCTION(, void, uws_client_dowork, UWS_CLIENT_HANDLE, uws_client);
87 MOCKABLE_FUNCTION(, int, uws_client_set_request_header, UWS_CLIENT_HANDLE, uws_client, const char*, name, const char*, value);
88 MOCKABLE_FUNCTION(, int, uws_client_set_option, UWS_CLIENT_HANDLE, uws_client, const char*, option_name, const void*, value);
89 MOCKABLE_FUNCTION(, OPTIONHANDLER_HANDLE, uws_client_retrieve_options, UWS_CLIENT_HANDLE, uws_client);
90 ```
91
92 ### uws_client_create
93
94 ```c
95 extern UWS_CLIENT_HANDLE uws_client_create(const char* hostname, unsigned int port, const char* resource_name, bool use_ssl, const WS_PROTOCOL* protocols, size_t protocol_count);
96 ```
97
98 XX**SRS_UWS_CLIENT_01_001: [** `uws_client_create` shall create an instance of uws and return a non-NULL handle to it.**]**
99 XX**SRS_UWS_CLIENT_01_002: [** If any of the arguments `hostname` and `resource_name` is NULL then `uws_client_create` shall return NULL. **]**
100 XX**SRS_UWS_CLIENT_01_003: [** If allocating memory for the new uws instance fails then `uws_client_create` shall return NULL. **]**
101 XX**SRS_UWS_CLIENT_01_004: [** The argument `hostname` shall be copied for later use. **]**
102 XX**SRS_UWS_CLIENT_01_392: [** If allocating memory for the copy of the `hostname` argument fails, then `uws_client_create` shall return NULL. **]**
103 XX**SRS_UWS_CLIENT_01_403: [** The argument `port` shall be copied for later use. **]**
104 XX**SRS_UWS_CLIENT_01_459: [** Any previously allocated underlying IO handle shall be destroyed by calling `xio_destroy`. **]**
105 XX**SRS_UWS_CLIENT_01_005: [** If `use_ssl` is false then `uws_client_create` shall obtain the interface used to create a socketio instance by calling `socketio_get_interface_description`. **]**
106 XX**SRS_UWS_CLIENT_01_006: [** If `use_ssl` is true then `uws_client_create` shall obtain the interface used to create a tlsio instance by calling `platform_get_default_tlsio`. **]**
107 XX**SRS_UWS_CLIENT_01_007: [** If obtaining the underlying IO interface fails, then `uws_client_create` shall fail and return NULL. **]**
108 XX**SRS_UWS_CLIENT_01_008: [** The obtained interface shall be used to create the IO used as underlying IO by the newly created uws instance. **]**
109 XX**SRS_UWS_CLIENT_01_009: [** The underlying IO shall be created by calling `xio_create`. **]**
110 XX**SRS_UWS_CLIENT_01_010: [** The create arguments for the socket IO (when `use_ssl` is 0) shall have: **]**
111 XX**SRS_UWS_CLIENT_01_011: [** - `hostname` set to the `hostname` argument passed to `uws_client_create`. **]**
112 XX**SRS_UWS_CLIENT_01_012: [** - `port` set to the `port` argument passed to `uws_client_create`. **]**
113 XX**SRS_UWS_CLIENT_01_013: [** The create arguments for the tls IO (when `use_ssl` is 1) shall have: **]**
114 XX**SRS_UWS_CLIENT_01_014: [** - `hostname` set to the `hostname` argument passed to `uws_client_create`. **]**
115 XX**SRS_UWS_CLIENT_01_015: [** - `port` set to the `port` argument passed to `uws_client_create`. **]**
116 XX**SRS_UWS_CLIENT_01_016: [** If `xio_create` fails, then `uws_client_create` shall fail and return NULL. **]**
117 XX**SRS_UWS_CLIENT_01_404: [** The argument `resource_name` shall be copied for later use. **]**
118 XX**SRS_UWS_CLIENT_01_410: [** The `protocols` argument shall be allowed to be NULL, in which case no protocol is to be specified by the client in the upgrade request. **]**
119 XX**SRS_UWS_CLIENT_01_411: [** If `protocol_count` is non zero and `protocols` is NULL then `uws_client_create` shall fail and return NULL. **]**
120 XX**SRS_UWS_CLIENT_01_412: [** If the `protocol` member of any of the items in the `protocols` argument is NULL, then `uws_client_create` shall fail and return NULL. **]**
121 XX**SRS_UWS_CLIENT_01_413: [** The protocol information indicated by `protocols` and `protocol_count` shall be copied for later use (for constructing the upgrade request). **]**
122 XX**SRS_UWS_CLIENT_01_414: [** If allocating memory for the copied protocol information fails then `uws_client_create` shall fail and return NULL. **]**
123 XX**SRS_UWS_CLIENT_01_405: [** If allocating memory for the copy of the `resource_name` argument fails, then `uws_client_create` shall return NULL. **]**
124 XX**SRS_UWS_CLIENT_01_017: [** `uws_client_create` shall create a pending send IO list that is to be used to queue send packets by calling `singlylinkedlist_create`. **]**
125 XX**SRS_UWS_CLIENT_01_018: [** If `singlylinkedlist_create` fails then `uws_client_create` shall fail and return NULL. **]**
126
127 ### uws_client_create_with_io
128
129 ```c
130 UWS_CLIENT_HANDLE uws_client_create_with_io(const IO_INTERFACE_DESCRIPTION* io_interface, void* io_create_parameters, const char* hostname, unsigned int port, const char* resource_name, const WS_PROTOCOL* protocols, size_t protocol_count)
131 ```
132
133 XX**SRS_UWS_CLIENT_01_515: [** `uws_client_create_with_io` shall create an instance of uws and return a non-NULL handle to it. **]**
134 XX**SRS_UWS_CLIENT_01_516: [** If any of the arguments `io_interface`, `hostname` and `resource_name` is NULL then `uws_client_create_with_io` shall return NULL. **]**
135 XX**SRS_UWS_CLIENT_01_517: [** If allocating memory for the new uws instance fails then `uws_client_create_with_io` shall return NULL. **]**
136 XX**SRS_UWS_CLIENT_01_518: [** The argument `hostname` shall be copied for later use. **]**
137 XX**SRS_UWS_CLIENT_01_519: [** If allocating memory for the copy of the `hostname` argument fails, then `uws_client_create` shall return NULL. **]**
138 XX**SRS_UWS_CLIENT_01_520: [** The argument `port` shall be copied for later use. **]**
139 XX**SRS_UWS_CLIENT_01_521: [** The underlying IO shall be created by calling `xio_create`, while passing as arguments the `io_interface` and `io_create_parameters` argument values. **]**
140 XX**SRS_UWS_CLIENT_01_522: [** If `xio_create` fails, then `uws_client_create_with_io` shall fail and return NULL. **]**
141 XX**SRS_UWS_CLIENT_01_523: [** The argument `resource_name` shall be copied for later use. **]**
142 XX**SRS_UWS_CLIENT_01_529: [** If allocating memory for the copy of the `resource_name` argument fails, then `uws_client_create_with_io` shall return NULL. **]**
143 XX**SRS_UWS_CLIENT_01_524: [** The `protocols` argument shall be allowed to be NULL, in which case no protocol is to be specified by the client in the upgrade request. **]**
144 XX**SRS_UWS_CLIENT_01_525: [** If `protocol_count` is non zero and `protocols` is NULL then `uws_client_create_with_io` shall fail and return NULL. **]**
145 XX**SRS_UWS_CLIENT_01_526: [** If the `protocol` member of any of the items in the `protocols` argument is NULL, then `uws_client_create_with_io` shall fail and return NULL. **]**
146 XX**SRS_UWS_CLIENT_01_527: [** The protocol information indicated by `protocols` and `protocol_count` shall be copied for later use (for constructing the upgrade request). **]**
147 XX**SRS_UWS_CLIENT_01_528: [** If allocating memory for the copied protocol information fails then `uws_client_create_with_io` shall fail and return NULL. **]**
148 XX**SRS_UWS_CLIENT_01_530: [** `uws_client_create_with_io` shall create a pending send IO list that is to be used to queue send packets by calling `singlylinkedlist_create`. **]**
149 XX**SRS_UWS_CLIENT_01_531: [** If `singlylinkedlist_create` fails then `uws_client_create_with_io` shall fail and return NULL. **]**
150
151 ### uws_client_destroy
152
153 ```c
154 extern void uws_client_destroy(UWS_CLIENT_HANDLE uws);
155 ```
156
157 XX**SRS_UWS_CLIENT_01_019: [** `uws_client_destroy` shall free all resources associated with the uws instance. **]**
158 XX**SRS_UWS_CLIENT_01_020: [** If `uws_client` is NULL, `uws_client_destroy` shall do nothing. **]**
159 XX**SRS_UWS_CLIENT_01_021: [** `uws_client_destroy` shall perform a close action if the uws instance has already been open. **]**
160 XX**SRS_UWS_CLIENT_01_023: [** `uws_client_destroy` shall destroy the underlying IO created in `uws_client_create` by calling `xio_destroy`. **]**
161 XX**SRS_UWS_CLIENT_01_024: [** `uws_client_destroy` shall free the list used to track the pending sends by calling `singlylinkedlist_destroy`. **]**
162 XX**SRS_UWS_CLIENT_01_437: [** `uws_client_destroy` shall free the protocols array allocated in `uws_client_create`. **]**
163
164 ### uws_client_open_async
165
166 ```c
167 extern int uws_client_open_async(UWS_CLIENT_HANDLE uws, ON_WS_OPEN_COMPLETE on_ws_open_complete, void* on_ws_open_complete_context, ON_WS_FRAME_RECEIVED on_ws_frame_received, void* on_ws_frame_received_context, ON_WS_PEER_CLOSED, on_ws_peer_closed, void*, on_ws_peer_closed_context, ON_WS_ERROR on_ws_error, void* on_ws_error_context);
168 ```
169 XX**SRS_UWS_CLIENT_01_025: [** `uws_client_open_async` shall open the underlying IO by calling `xio_open` and providing the IO handle created in `uws_client_create` as argument. **]**
170 XX**SRS_UWS_CLIENT_01_367: [** The callbacks `on_underlying_io_open_complete`, `on_underlying_io_bytes_received` and `on_underlying_io_error` shall be passed as arguments to `xio_open`. **]**
171 XX**SRS_UWS_CLIENT_01_026: [** On success, `uws_client_open_async` shall return 0. **]**
172 XX**SRS_UWS_CLIENT_01_027: [** If `uws_client`, `on_ws_open_complete`, `on_ws_frame_received`, `on_ws_peer_closed` or `on_ws_error` is NULL, `uws_client_open_async` shall fail and return a non-zero value. **]**
173 XX**SRS_UWS_CLIENT_01_393: [** The context arguments for the callbacks shall be allowed to be NULL. **]**
174 XX**SRS_UWS_CLIENT_01_028: [** If opening the underlying IO fails then `uws_client_open_async` shall fail and return a non-zero value. **]**
175 XX**SRS_UWS_CLIENT_01_394: [** `uws_client_open_async` while the uws instance is already OPEN or OPENING shall fail and return a non-zero value. **]**
176 XX**SRS_UWS_CLIENT_01_400: [** `uws_client_open_async` while CLOSING shall fail and return a non-zero value. **]**
177
178 ### uws_client_close_async
179
180 ```c
181 extern int uws_client_close_async(UWS_CLIENT_HANDLE uws, ON_WS_CLOSE_COMPLETE on_ws_close_complete, void* on_ws_close_complete_context);
182 ```
183
184 XX**SRS_UWS_CLIENT_01_029: [** `uws_client_close_async` shall close the uws instance connection if an open action is either pending or has completed successfully (if the IO is open). **]**
185 XX**SRS_UWS_CLIENT_01_396: [** On success `uws_client_close_async` shall return 0. **]**
186 XX**SRS_UWS_CLIENT_01_030: [** if `uws_client` is NULL, `uws_client_close_async` shall return a non-zero value. **]**
187 XX**SRS_UWS_CLIENT_01_399: [** `on_ws_close_complete` and `on_ws_close_complete_context` shall be saved and the callback `on_ws_close_complete` shall be triggered when the close is complete. **]**
188 XX**SRS_UWS_CLIENT_01_397: [** The `on_ws_close_complete` argument shall be allowed to be NULL, in which case no callback shall be called when the close is complete. **]**
189 XX**SRS_UWS_CLIENT_01_398: [** `on_ws_close_complete_context` shall also be allowed to be NULL. **]**
190 XX**SRS_UWS_CLIENT_01_031: [** `uws_client_close_async` shall close the connection by calling `xio_close` while passing as argument the IO handle created in `uws_client_create`. **]**
191 XX**SRS_UWS_CLIENT_01_368: [** The callback `on_underlying_io_close` shall be passed as argument to `xio_close`. **]**
192 XX**SRS_UWS_CLIENT_01_395: [** If `xio_close` fails, `uws_client_close_async` shall fail and return a non-zero value. **]**
193 XX**SRS_UWS_CLIENT_01_032: [** `uws_client_close_async` when no open action has been issued shall fail and return a non-zero value. **]**
194 XX**SRS_UWS_CLIENT_01_033: [** `uws_client_close_async` after a `uws_client_close_async` shall fail and return a non-zero value. **]**
195 XX**SRS_UWS_CLIENT_01_034: [** `uws_client_close_async` shall obtain all the pending send frames by repetitively querying for the head of the pending IO list and freeing that head item. **]**
196 XX**SRS_UWS_CLIENT_01_035: [** Obtaining the head of the pending send frames list shall be done by calling `singlylinkedlist_get_head_item`. **]**
197 XX**SRS_UWS_CLIENT_01_036: [** For each pending send frame the send complete callback shall be called with `UWS_SEND_FRAME_CANCELLED`. **]**
198 XX**SRS_UWS_CLIENT_01_037: [** When indicating pending send frames as cancelled the callback context passed to the `on_ws_send_frame_complete` callback shall be the context given to `uws_client_send_frame_async`. **]**
199
200 ### uws_client_close_handshake_async
201
202 ```c
203 extern int uws_client_close_handshake_async(UWS_CLIENT_HANDLE uws, uint16 close_code, const char* close_reason, ON_WS_CLOSE_COMPLETE on_ws_close_complete, void* on_ws_close_complete_context);
204 ```
205
206 XX**SRS_UWS_CLIENT_01_465: [** `uws_client_close_handshake_async` shall initiate the close handshake by sending a close frame to the peer. **]**
207 XX**SRS_UWS_CLIENT_01_466: [** On success `uws_client_close_handshake_async` shall return 0. **]**
208 XX**SRS_UWS_CLIENT_01_467: [** if `uws_client` is NULL, `uws_client_close_handshake_async` shall return a non-zero value. **]**
209 XX**SRS_UWS_CLIENT_01_468: [** `on_ws_close_complete` and `on_ws_close_complete_context` shall be saved and the callback `on_ws_close_complete` shall be triggered when the close is complete. **]**
210 XX**SRS_UWS_CLIENT_01_469: [** The `on_ws_close_complete` argument shall be allowed to be NULL, in which case no callback shall be called when the close is complete. **]**
211 XX**SRS_UWS_CLIENT_01_470: [** `on_ws_close_complete_context` shall also be allowed to be NULL. **]**
212 XX**SRS_UWS_CLIENT_01_472: [** If `xio_send` fails, `uws_client_close_handshake_async` shall fail and return a non-zero value. **]**
213 XX**SRS_UWS_CLIENT_01_473: [** `uws_client_close_handshake_async` when no open action has been issued shall fail and return a non-zero value. **]**
214 XX**SRS_UWS_CLIENT_01_474: [** `uws_client_close_handshake_async` when already CLOSING shall fail and return a non-zero value. **]**
215
216 ### uws_client_send_frame_async
217
218 ```c
219 extern int uws_client_send_frame_async(UWS_CLIENT_HANDLE uws, const unsigned char* buffer, size_t size, bool is_final, ON_WS_SEND_FRAME_COMPLETE on_ws_send_frame_complete, void* on_ws_send_frame_complete_context);
220 ```
221
222 XX**SRS_UWS_CLIENT_01_038: [** `uws_client_send_frame_async` shall create and queue a structure that contains: **]**
223 XX**SRS_UWS_CLIENT_01_040: [** - the send complete callback `on_ws_send_frame_complete` **]**
224 XX**SRS_UWS_CLIENT_01_041: [** - the send complete callback context `on_ws_send_frame_complete_context` **]**
225 XX**SRS_UWS_CLIENT_01_042: [** On success, `uws_client_send_frame_async` shall return 0. **]**
226 XX**SRS_UWS_CLIENT_01_425: [** Encoding shall be done by calling `uws_frame_encoder_encode` and passing to it the `buffer` and `size` argument for payload, the `is_final` flag and setting `is_masked` to true. **]**
227 XX**SRS_UWS_CLIENT_01_426: [** If `uws_frame_encoder_encode` fails, `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
228 XX**SRS_UWS_CLIENT_01_428: [** The encoded frame buffer memory shall be obtained by calling `BUFFER_u_char` on the encode buffer. **]**
229 XX**SRS_UWS_CLIENT_01_429: [** The encoded frame size shall be obtained by calling `BUFFER_length` on the encode buffer. **]**
230 XX**SRS_UWS_CLIENT_01_431: [** Once encoded the frame shall be sent by using `xio_send` with the following arguments: **]**
231 XX**SRS_UWS_CLIENT_01_053: [** - the io handle shall be the underlyiong IO handle created in `uws_client_create`. **]**
232 XX**SRS_UWS_CLIENT_01_054: [** - the `buffer` argument shall point to the complete websocket frame to be sent. **]**
233 XX**SRS_UWS_CLIENT_01_055: [** - the `size` argument shall indicate the websocket frame length. **]**
234 XX**SRS_UWS_CLIENT_01_056: [** - the `send_complete` callback shall be the `on_underlying_io_send_complete` function. **]**
235 XX**SRS_UWS_CLIENT_01_057: [** - the `send_complete_context` argument shall identify the pending send. **]**
236 XX**SRS_UWS_CLIENT_01_058: [** If `xio_send` fails, `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
237 XX**SRS_UWS_CLIENT_09_001: [** If `xio_send` fails and the message is still queued, it shall be de-queued and destroyed. **]**
238 XX**SRS_UWS_CLIENT_01_043: [** If the uws instance is not OPEN (open has not been called or is still in progress) then `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
239 XX**SRS_UWS_CLIENT_01_044: [** If the argument `uws_client` is NULL, `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
240 XX**SRS_UWS_CLIENT_01_045: [** If `size` is non-zero and `buffer` is NULL then `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
241 XX**SRS_UWS_CLIENT_01_047: [** If allocating memory for the newly queued item fails, `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
242 XX**SRS_UWS_CLIENT_01_048: [** Queueing shall be done by calling `singlylinkedlist_add`. **]**
243 XX**SRS_UWS_CLIENT_01_049: [** If `singlylinkedlist_add` fails, `uws_client_send_frame_async` shall fail and return a non-zero value. **]**
244 XX**SRS_UWS_CLIENT_01_050: [** The argument `on_ws_send_frame_complete` shall be optional, if NULL is passed by the caller then no send complete callback shall be triggered. **]**
245
246 ### uws_client_dowork
247
248 ```c
249 extern void uws_client_dowork(UWS_CLIENT_HANDLE uws);
250 ```
251
252 XX**SRS_UWS_CLIENT_01_059: [** If the `uws_client` argument is NULL, `uws_client_dowork` shall do nothing. **]**
253 XX**SRS_UWS_CLIENT_01_060: [** If the IO is not yet open, `uws_client_dowork` shall do nothing. **]**
254 XX**SRS_UWS_CLIENT_01_430: [** `uws_client_dowork` shall call `xio_dowork` with the IO handle argument set to the underlying IO created in `uws_client_create`. **]**
255
256
257 ### uws_client_set_request_header
258
259 ```c
260 int uws_client_set_request_header(UWS_CLIENT_HANDLE uws_client, const char* name, const char* value);
261 ```
262
263 XX**SRS_UWS_CLIENT_09_002: [** If any of the arguments `uws_client` or `name` or `value` is NULL `uws_client_set_request_header` shall fail and return a non-zero value. **]**
264 XX**SRS_UWS_CLIENT_09_003: [** A copy of `name` and `value` shall be stored for later sending in the request message. **]**
265 XX**SRS_UWS_CLIENT_09_004: [** If `name` or `value` fail to be stored the function shall fail and return a non-zero value. **]**
266 XX**SRS_UWS_CLIENT_09_005: [** If no failures occur the function shall return zero. **]**
267
268
269 ### uws_setoption
270
271 ```c
272 extern int uws_client_set_option(UWS_CLIENT_HANDLE uws, const char* option_name, const void* value);
273 ```
274
275 XX**SRS_UWS_CLIENT_01_440: [** If any of the arguments `uws_client` or `option_name` is NULL `uws_client_set_option` shall return a non-zero value. **]**
276 XX**SRS_UWS_CLIENT_01_510: [** If the option name is `uWSClientOptions` then `uws_client_set_option` shall call `OptionHandler_FeedOptions` and pass to it the underlying IO handle and the `value` argument. **]**
277 XX**SRS_UWS_CLIENT_01_511: [** If `OptionHandler_FeedOptions` fails, `uws_client_set_option` shall fail and return a non-zero value. **]**
278 XX**SRS_UWS_CLIENT_01_441: [** Otherwise all options shall be passed as they are to the underlying IO by calling `xio_setoption`. **]**
279 XX**SRS_UWS_CLIENT_01_442: [** On success, `uws_client_set_option` shall return 0. **]**
280 XX**SRS_UWS_CLIENT_01_443: [** If `xio_setoption` fails, `uws_client_set_option` shall fail and return a non-zero value. **]**
281
282 ### uws_client_retrieve_options
283
284 ```c
285 OPTIONHANDLER_HANDLE uws_client_retrieve_options(UWS_CLIENT_HANDLE uws)
286 ```
287
288 XX**SRS_UWS_CLIENT_01_444: [** If parameter `uws_client` is `NULL` then `uws_client_retrieve_options` shall fail and return NULL. **]**
289 XX**SRS_UWS_CLIENT_01_445: [** `uws_client_retrieve_options` shall call `OptionHandler_Create` to produce an `OPTIONHANDLER_HANDLE` and on success return the new `OPTIONHANDLER_HANDLE` handle. **]**
290 XX**SRS_UWS_CLIENT_01_446: [** If `OptionHandler_Create` fails then `uws_client_retrieve_options` shall fail and return NULL. **]**
291 XX**SRS_UWS_CLIENT_01_501: [** `uws_client_retrieve_options` shall add to the option handler one option, whose name shall be `uWSClientOptions` and the value shall be queried by calling `xio_retrieveoptions`. **]**
292 XX**SRS_UWS_CLIENT_01_502: [** When calling `xio_retrieveoptions` the underlying IO handle shall be passed to it. **]**
293 XX**SRS_UWS_CLIENT_01_503: [** If `xio_retrieveoptions` fails, `uws_client_retrieve_options` shall fail and return NULL. **]**
294 XX**SRS_UWS_CLIENT_01_504: [** Adding the option shall be done by calling `OptionHandler_AddOption`. **]**
295 XX**SRS_UWS_CLIENT_01_505: [** If `OptionHandler_AddOption` fails, `uws_client_retrieve_options` shall fail and return NULL. **]**
296
297 ### uws_client_clone_option
298
299 `uws_client_clone_option` is the implementation provided to the option handler instance created as part of `uws_client_retrieve_options`.
300
301 ```c
302 void* uws_client_clone_option(const char* name, const void* value)
303 ```
304
305 XX**SRS_UWS_CLIENT_01_507: [** `uws_client_clone_option` called with `name` being `uWSClientOptions` shall clone the options by calling `OptionHandler_Clone`. **]**
306 XX**SRS_UWS_CLIENT_01_514: [** If `OptionHandler_Clone` fails, `uws_client_clone_option` shall fail and return NULL. **]**
307 XX**SRS_UWS_CLIENT_01_512: [** `uws_client_clone_option` called with any other option name than `uWSClientOptions` shall return NULL. **]**
308 XX**SRS_UWS_CLIENT_01_506: [** If `uws_client_clone_option` is called with NULL `name` or `value` it shall return NULL. **]**
309
310 ### uws_client_destroy_option
311
312 `uws_client_destroy_option` is the implementation provided to the option handler instance created as part of `uws_client_retrieve_options`.
313
314 ```c
315 void uws_client_destroy_option(const char* name, const void* value)
316 ```
317
318 XX**SRS_UWS_CLIENT_01_508: [** `uws_client_destroy_option` called with the option `name` being `uWSClientOptions` shall destroy the value by calling `OptionHandler_Destroy`. **]**
319 XX**SRS_UWS_CLIENT_01_513: [** If `uws_client_destroy_option` is called with any other `name` it shall do nothing. **]**
320 XX**SRS_UWS_CLIENT_01_509: [** If `uws_client_destroy_option` is called with NULL `name` or `value` it shall do nothing. **]**
321
322 ### on_underlying_io_open_complete
323
324 XX**SRS_UWS_CLIENT_01_369: [** When `on_underlying_io_open_complete` is called with `IO_OPEN_ERROR` while uws is OPENING (`uws_client_open_async` was called), uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_UNDERLYING_IO_OPEN_FAILED`. **]**
325 XX**SRS_UWS_CLIENT_01_402: [** When `on_underlying_io_open_complete` is called with `IO_OPEN_CANCELLED` while uws is OPENING (`uws_client_open_async` was called), uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_UNDERLYING_IO_OPEN_CANCELLED`. **]**
326 XX**SRS_UWS_CLIENT_01_401: [** If `on_underlying_io_open_complete` is called with a NULL context, `on_underlying_io_open_complete` shall do nothing. **]**
327 XX**SRS_UWS_CLIENT_01_371: [** When `on_underlying_io_open_complete` is called with `IO_OPEN_OK` while uws is OPENING (`uws_client_open_async` was called), uws shall prepare the WebSockets upgrade request. **]**
328 X**SRS_UWS_CLIENT_01_408: [** If constructing of the WebSocket upgrade request fails, uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_CONSTRUCTING_UPGRADE_REQUEST`. **]**
329 XX**SRS_UWS_CLIENT_01_497: [** The nonce needed for the upgrade request shall be Base64 encoded with `Azure_Base64_Encode_Bytes`. **]**
330 XX**SRS_UWS_CLIENT_01_498: [** If Base64 encoding the nonce for the upgrade request fails, then the uws client shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_BASE64_ENCODE_FAILED`. **]**
331
332 XX**SRS_UWS_CLIENT_01_406: [** If not enough memory can be allocated to construct the WebSocket upgrade request, uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_NOT_ENOUGH_MEMORY`. **]**
333 XX**SRS_UWS_CLIENT_01_372: [** Once prepared the WebSocket upgrade request shall be sent by calling `xio_send`. **]**
334 XX**SRS_UWS_CLIENT_01_373: [** If `xio_send` fails then uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_CANNOT_SEND_UPGRADE_REQUEST`. **]**
335 **SRS_UWS_CLIENT_01_374: [** When `on_underlying_io_open_complete` is called when the uws instance is already OPEN, an error shall be reported to the user by calling the `on_ws_error` callback that was passed to `uws_client_open_async`. **]**
336 XX**SRS_UWS_CLIENT_01_407: [** When `on_underlying_io_open_complete` is called when the uws instance has send the upgrade request but it is waiting for the response, an error shall be reported to the user by calling the `on_ws_open_complete` with `WS_OPEN_ERROR_MULTIPLE_UNDERLYING_IO_OPEN_EVENTS`. **]**
337 XX**SRS_UWS_CLIENT_01_409: [** After any error is indicated by `on_ws_open_complete`, a subsequent `uws_client_open_async` shall be possible. **]**
338
339 ### on_underlying_io_error
340
341 XX**SRS_UWS_CLIENT_01_375: [** When `on_underlying_io_error` is called while uws is OPENING, uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR`. **]**
342 XX**SRS_UWS_CLIENT_01_376: [** When `on_underlying_io_error` is called while the uws instance is OPEN, an error shall be reported to the user by calling the `on_ws_error` callback that was passed to `uws_client_open_async` with the argument `WS_ERROR_UNDERLYING_IO_ERROR`. **]**
343 XX**SRS_UWS_CLIENT_01_377: [** When `on_underlying_io_error` is called while the uws instance is CLOSING the underlying IO shall be closed by calling `xio_close`. **]**
344 XX**SRS_UWS_CLIENT_01_500: [** The callback `on_ws_close_complete` shall be called, while passing the `on_ws_close_complete_context` argument to it. **]**
345 XX**SRS_UWS_CLIENT_01_499: [** If the CLOSE was due to the peer closing, the callback `on_ws_close_complete` shall not be called. **]**
346
347 ### on_underlying_io_bytes_received
348
349 XX**SRS_UWS_CLIENT_01_415: [** If called with a NULL `context` argument, `on_underlying_io_bytes_received` shall do nothing. **]**
350 XX**SRS_UWS_CLIENT_01_416: [** If called with NULL `buffer` or zero `size` and the state of the iws is OPENING, uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_INVALID_BYTES_RECEIVED_ARGUMENTS`. **]**
351 XX**SRS_UWS_CLIENT_01_378: [** When `on_underlying_io_bytes_received` is called while the uws is OPENING, the received bytes shall be accumulated in order to attempt parsing the WebSocket Upgrade response. **]**
352 XX**SRS_UWS_CLIENT_01_417: [** When `on_underlying_io_bytes_received` is called while OPENING but before the `on_underlying_io_open_complete` has been called, uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_BYTES_RECEIVED_BEFORE_UNDERLYING_OPEN`. **]**
353 XX**SRS_UWS_CLIENT_01_379: [** If allocating memory for accumulating the bytes fails, uws shall report that the open failed by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_NOT_ENOUGH_MEMORY`. **]**
354 XX**SRS_UWS_CLIENT_01_380: [** If an WebSocket Upgrade request can be parsed from the accumulated bytes, the status shall be read from the WebSocket upgrade response. **]**
355 XX**SRS_UWS_CLIENT_01_381: [** If the status is 101, uws shall be considered OPEN and this shall be indicated by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `IO_OPEN_OK`. **]**
356 XX**SRS_UWS_CLIENT_01_382: [** If a negative status is decoded from the WebSocket upgrade request, an error shall be indicated by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_BAD_RESPONSE_STATUS`. **]**
357 XX**SRS_UWS_CLIENT_01_383: [** If the WebSocket upgrade request cannot be decoded an error shall be indicated by calling the `on_ws_open_complete` callback passed to `uws_client_open_async` with `WS_OPEN_ERROR_BAD_UPGRADE_RESPONSE`. **]**
358 XX**SRS_UWS_CLIENT_01_384: [** Any extra bytes that are left unconsumed after decoding a succesfull WebSocket upgrade response shall be used for decoding WebSocket frames **]**
359 XX**SRS_UWS_CLIENT_01_385: [** If the state of the uws instance is OPEN, the received bytes shall be used for decoding WebSocket frames. **]**
360 XX**SRS_UWS_CLIENT_01_418: [** If allocating memory for the bytes accumulated for decoding WebSocket frames fails, an error shall be indicated by calling the `on_ws_error` callback with `WS_ERROR_NOT_ENOUGH_MEMORY`. **]**
361 XX**SRS_UWS_CLIENT_01_386: [** When a WebSocket data frame is decoded succesfully it shall be indicated via the callback `on_ws_frame_received`. **]**
362 XX**SRS_UWS_CLIENT_01_419: [** If there is an error decoding the WebSocket frame, an error shall be indicated by calling the `on_ws_error` callback with `WS_ERROR_BAD_FRAME_RECEIVED`. **]**
363 XX**SRS_UWS_CLIENT_01_460: [** When a CLOSE frame is received the callback `on_ws_peer_closed` passed to `uws_client_open_async` shall be called, while passing to it the argument `on_ws_peer_closed_context`. **]**
364 XX**SRS_UWS_CLIENT_01_461: [** The argument `close_code` shall be set to point to the code extracted from the CLOSE frame. **]**
365 XX**SRS_UWS_CLIENT_01_462: [** If no code can be extracted then `close_code` shall be NULL. **]**
366 XX**SRS_UWS_CLIENT_01_463: [** The extra bytes (besides the close code) shall be passed to the `on_ws_peer_closed` callback by using `extra_data` and `extra_data_length`. **]**
367
368 ### on_underlying_io_close_complete
369
370 XX**SRS_UWS_CLIENT_01_495: [** When `on_underlying_io_close_complete` is called with NULL `context`, it shall do nothing. **]**
371 XX**SRS_UWS_CLIENT_01_387: [** When `on_underlying_io_close_complete` is called when the uws instance is closing, the `on_ws_close_complete` callback passed to `uws_client_close_async` shall be called. **]**
372 XX**SRS_UWS_CLIENT_01_476: [** If the close is as a result of receiving a CLOSE frame, no callback shall be called. **]**
373 XX**SRS_UWS_CLIENT_01_475: [** When `on_underlying_io_close_complete` is called while closing the underlying IO a subsequent `uws_client_open_async` shall succeed. **]**
374 XX**SRS_UWS_CLIENT_01_477: [** When `on_underlying_io_close_complete` is called with a NULL context, it shall do nothing. **]**
375
376 ### on_underlying_io_send_complete
377
378 XX**SRS_UWS_CLIENT_01_432: [** The indicated sent frame shall be removed from the list by calling `singlylinkedlist_remove`. **]**
379 XX**SRS_UWS_CLIENT_01_433: [** If `singlylinkedlist_remove` fails an error shall be indicated by calling the `on_ws_error` callback with `WS_ERROR_CANNOT_REMOVE_SENT_ITEM_FROM_LIST`. **]**
380 XX**SRS_UWS_CLIENT_01_434: [** The memory associated with the sent frame shall be freed. **]**
381 XX**SRS_UWS_CLIENT_01_389: [** When `on_underlying_io_send_complete` is called with `IO_SEND_OK` as a result of sending a WebSocket frame to the underlying IO, the send shall be indicated to the uws user by calling `on_ws_send_frame_complete` with `WS_SEND_FRAME_OK`. **]**
382 XX**SRS_UWS_CLIENT_01_390: [** When `on_underlying_io_send_complete` is called with `IO_SEND_ERROR` as a result of sending a WebSocket frame to the underlying IO, the send shall be indicated to the uws user by calling `on_ws_send_frame_complete` with `WS_SEND_FRAME_ERROR`. **]**
383 XX**SRS_UWS_CLIENT_01_391: [** When `on_underlying_io_send_complete` is called with `IO_SEND_CANCELLED` as a result of sending a WebSocket frame to the underlying IO, the send shall be indicated to the uws user by calling `on_ws_send_frame_complete` with `WS_SEND_FRAME_CANCELLED`. **]**
384 XX**SRS_UWS_CLIENT_01_435: [** When `on_underlying_io_send_complete` is called with a NULL `context`, it shall do nothing. **]**
385 XX**SRS_UWS_CLIENT_01_436: [** When `on_underlying_io_send_complete` is called with any other error code, the send shall be indicated to the uws user by calling `on_ws_send_frame_complete` with `WS_SEND_FRAME_ERROR`. **]**
386
387 ### on_underlying_io_close_sent
388
389 XX**SRS_UWS_CLIENT_01_489: [** When `on_underlying_io_close_sent` is called with NULL context, it shall do nothing. **]**
390 XX**SRS_UWS_CLIENT_01_490: [** When `on_underlying_io_close_sent` is called while the uws client is CLOSING, `on_underlying_io_close_sent` shall close the underlying IO by calling `xio_close`. **]**
391 XX**SRS_UWS_CLIENT_01_496: [** If the close was initiated by the peer no `on_ws_close_complete` shall be called. **]**
392
393 ### RFC6455
394
395 3. WebSocket URIs
396
397 This specification defines two URI schemes, using the ABNF syntax defined in RFC 5234 [RFC5234], and terminology and ABNF productions defined by the URI specification RFC 3986 [RFC3986].
398
399 ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
400 wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
401
402 host = <host, defined in [RFC3986], Section 3.2.2>
403 port = <port, defined in [RFC3986], Section 3.2.3>
404 path = <path-abempty, defined in [RFC3986], Section 3.3>
405 query = <query, defined in [RFC3986], Section 3.4>
406
407 The port component is OPTIONAL; the default for "ws" is port 80, while the default for "wss" is port 443.
408
409 The URI is called "secure" (and it is said that "the secure flag is set") if the scheme component matches "wss" case-insensitively.
410
411 The "resource-name" (also known as /resource name/ in Section 4.1) can be constructed by concatenating the following:
412
413 - "/" if the path component is empty
414
415 - the path component
416
417 - "?" if the query component is non-empty
418
419 - the query component
420
421 Fragment identifiers are meaningless in the context of WebSocket URIs and MUST NOT be used on these URIs.
422 As with any URI scheme, the character "#", when not indicating the start of a fragment, MUST be escaped as %23.
423
424 4. Opening Handshake
425
426 4.1. Client Requirements
427
428 XX**SRS_UWS_CLIENT_01_061: [** To _Establish a WebSocket Connection_, a client opens a connection and sends a handshake as defined in this section. **]**
429 A connection is defined to initially be in a CONNECTING state.
430 XX**SRS_UWS_CLIENT_01_063: [** A client will need to supply a /host/, /port/, /resource name/, and a /secure/ flag, which are the components of a WebSocket URI as discussed in Section 3, along with a list of /protocols/ and /extensions/ to be used. **]**
431 **SRS_UWS_CLIENT_01_064: [** Additionally, if the client is a web browser, it supplies /origin/. **]**
432
433 Clients running in controlled environments, e.g., browsers on mobile handsets tied to specific carriers, MAY offload the management of the connection to another agent on the network.
434 In such a situation, the client for the purposes of this specification is considered to include both the handset software and any such agents.
435
436 XX**SRS_UWS_CLIENT_01_065: [** When the client is to _Establish a WebSocket Connection_ given a set of (/host/, /port/, /resource name/, and /secure/ flag), along with a list of /protocols/ and /extensions/ to be used, and an /origin/ in the case of web browsers, it MUST open a connection, send an opening handshake, and read the server's handshake in response. **]**
437 The exact requirements of how the connection should be opened, what should be sent in the opening handshake, and how the server's response should be interpreted are as follows in this section.
438 In the following text, we will use terms from Section 3, such as "/host/" and "/secure/ flag" as defined in that section.
439
440 1. **SRS_UWS_CLIENT_01_066: [** The components of the WebSocket URI passed into this algorithm (/host/, /port/, /resource name/, and /secure/ flag) MUST be valid according to the specification of WebSocket URIs specified in Section 3. **]**
441 **SRS_UWS_CLIENT_01_067: [** If any of the components are invalid, the client MUST _Fail the WebSocket Connection_ and abort these steps. **]**
442
443 2. If the client already has a WebSocket connection to the remote host (IP address) identified by /host/ and port /port/ pair, even if the remote host is known by another name, the client MUST wait until that connection has been established or for that connection to have failed.
444 There MUST be no more than one connection in a CONNECTING state.
445 If multiple connections to the same IP address are attempted simultaneously, the client MUST serialize them so that there is no more than one connection at a time running through the following steps.
446
447 If the client cannot determine the IP address of the remote host (for example, because all communication is being done through a proxy server that performs DNS queries itself), then the client MUST assume for the purposes of this step that each host name refers to a distinct remote host, and instead the client SHOULD limit the total number of simultaneous pending connections to a reasonably low number (e.g., the client might allow simultaneous pending connections to a.example.com and b.example.com, but if thirty simultaneous connections to a single host are requested, that may not be allowed).
448 For example, in a web browser context, the client needs to consider the number of tabs the user has open in setting a limit to the number of simultaneous pending connections.
449
450 NOTE: This makes it harder for a script to perform a denial-of-service attack by just opening a large number of WebSocket connections to a remote host.
451 A server can further reduce the load on itself when attacked by pausing before closing the connection, as that will reduce the rate at which the client reconnects.
452
453 NOTE: There is no limit to the number of established WebSocket connections a client can have with a single remote host.
454 Servers can refuse to accept connections from hosts/IP addresses with an excessive number of existing connections or disconnect resource-hogging connections when suffering high load.
455
456 3. **SRS_UWS_CLIENT_01_072: [** _Proxy Usage_: If the client is configured to use a proxy when using the WebSocket Protocol to connect to host /host/ and port /port/, then the client SHOULD connect to that proxy and ask it to open a TCP connection to the host given by /host/ and the port given by /port/. **]**
457
458 EXAMPLE: For example, if the client uses an HTTP proxy for all traffic, then if it was to try to connect to port 80 on server example.com, it might send the following lines to the proxy server:
459
460 CONNECT example.com:80 HTTP/1.1
461 Host: example.com
462
463 If there was a password, the connection might look like:
464
465 CONNECT example.com:80 HTTP/1.1
466 Host: example.com
467 Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE=
468
469 **SRS_UWS_CLIENT_01_073: [** If the client is not configured to use a proxy, then a direct TCP connection SHOULD be opened to the host given by /host/ and the port given by /port/. **]**
470
471 NOTE: Implementations that do not expose explicit UI for selecting a proxy for WebSocket connections separate from other proxies are encouraged to use a SOCKS5 [RFC1928] proxy for WebSocket connections, if available, or failing that, to prefer the proxy configured for HTTPS connections over the proxy configured for HTTP connections.
472
473 **SRS_UWS_CLIENT_01_074: [** For the purpose of proxy autoconfiguration scripts, the URI to pass the function MUST be constructed from /host/, /port/, /resource name/, and the /secure/ flag using the definition of a WebSocket URI as given in Section 3. **]**
474
475 NOTE: The WebSocket Protocol can be identified in proxy autoconfiguration scripts from the scheme ("ws" for unencrypted connections and "wss" for encrypted connections).
476
477 4. XX**SRS_UWS_CLIENT_01_075: [** If the connection could not be opened, either because a direct connection failed or because any proxy used returned an error, then the client MUST _Fail the WebSocket Connection_ and abort the connection attempt. **]**
478
479 5. XX**SRS_UWS_CLIENT_01_076: [** If /secure/ is true, the client MUST perform a TLS handshake over the connection after opening the connection and before sending the handshake data [RFC2818]. **]**
480 XX**SRS_UWS_CLIENT_01_077: [** If this fails (e.g., the server's certificate could not be verified), then the client MUST _Fail the WebSocket Connection_ and abort the connection. **]**
481 XX**SRS_UWS_CLIENT_01_078: [** Otherwise, all further communication on this channel MUST run through the encrypted tunnel [RFC5246]. **]**
482
483 **SRS_UWS_CLIENT_01_079: [** Clients MUST use the Server Name Indication extension in the TLS handshake [RFC6066]. **]**
484
485 XX**SRS_UWS_CLIENT_01_080: [** Once a connection to the server has been established (including a connection via a proxy or over a TLS-encrypted tunnel), the client MUST send an opening handshake to the server. **]**
486 XX**SRS_UWS_CLIENT_01_081: [** The handshake consists of an HTTP Upgrade request, along with a list of required and optional header fields. **]**
487 The requirements for this handshake are as follows.
488
489 1. XX**SRS_UWS_CLIENT_01_082: [** The handshake MUST be a valid HTTP request as specified by [RFC2616]. **]**
490
491 2. XX**SRS_UWS_CLIENT_01_083: [** The method of the request MUST be GET, and the HTTP version MUST be at least 1.1. **]**
492
493 For example, if the WebSocket URI is "ws://example.com/chat", the first line sent should be "GET /chat HTTP/1.1".
494
495 3. XX**SRS_UWS_CLIENT_01_084: [** The "Request-URI" part of the request MUST match the /resource name/ defined in Section 3 (a relative URI) or be an absolute http/https URI that, when parsed, has a /resource name/, /host/, and /port/ that match the corresponding ws/wss URI. **]**
496
497 4. XX**SRS_UWS_CLIENT_01_085: [** The request MUST contain a |Host| header field whose value contains /host/ plus optionally ":" followed by /port/ (when not using the default port). **]**
498
499 5. XX**SRS_UWS_CLIENT_01_086: [** The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword. **]**
500
501 6. XX**SRS_UWS_CLIENT_01_087: [** The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token. **]**
502
503 7. XX**SRS_UWS_CLIENT_01_088: [** The request MUST include a header field with the name |Sec-WebSocket-Key|. **]**
504 XX**SRS_UWS_CLIENT_01_089: [** The value of this header field MUST be a nonce consisting of a randomly selected 16-byte value that has been base64-encoded (see Section 4 of [RFC4648]). **]**
505 XX**SRS_UWS_CLIENT_01_090: [** The nonce MUST be selected randomly for each connection. **]**
506
507 NOTE: As an example, if the randomly selected value was the sequence of bytes 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10, the value of the header field would be "AQIDBAUGBwgJCgsMDQ4PEC=="
508
509 8. **SRS_UWS_CLIENT_01_091: [** The request MUST include a header field with the name |Origin| [RFC6454] if the request is coming from a browser client. **]**
510 **SRS_UWS_CLIENT_01_092: [** If the connection is from a non-browser client, the request MAY include this header field if the semantics of that client match the use-case described here for browser clients. **]**
511 **SRS_UWS_CLIENT_01_093: [** The value of this header field is the ASCII serialization of origin of the context in which the code establishing the connection is running. **]**
512 See [RFC6454] for the details of how this header field value is constructed.
513
514 As an example, if code downloaded from www.example.com attempts to establish a connection to ww2.example.com, the value of the header field would be "http://www.example.com".
515
516 9. XX**SRS_UWS_CLIENT_01_094: [** The request MUST include a header field with the name |Sec-WebSocket-Version|. **]**
517 XX**SRS_UWS_CLIENT_01_095: [** The value of this header field MUST be 13. **]**
518
519 NOTE: Although draft versions of this document (-09, -10, -11, and -12) were posted (they were mostly comprised of editorial changes and clarifications and not changes to the wire protocol), values 9, 10, 11, and 12 were not used as valid values for Sec-WebSocket-Version.
520 These values were reserved in the IANA registry but were not and will not be used.
521
522 10. XX**SRS_UWS_CLIENT_01_096: [** The request MAY include a header field with the name |Sec-WebSocket-Protocol|. **]**
523 **SRS_UWS_CLIENT_01_097: [** If present, this value indicates one or more comma-separated subprotocol the client wishes to speak, ordered by preference. **]**
524 **SRS_UWS_CLIENT_01_098: [** The elements that comprise this value MUST be non-empty strings with characters in the range U+0021 to U+007E not including separator characters as defined in [RFC2616] **]** **SRS_UWS_CLIENT_01_099: [** and MUST all be unique strings **]**.
525 The ABNF for the value of this header field is 1#token, where the definitions of constructs and rules are as given in [RFC2616].
526
527 11. **SRS_UWS_CLIENT_01_100: [** The request MAY include a header field with the name |Sec-WebSocket-Extensions|. **]**
528 If present, this value indicates the protocol-level extension(s) the client wishes to speak.
529 The interpretation and format of this header field is described in Section 9.1.
530
531 12. **SRS_UWS_CLIENT_01_101: [** The request MAY include any other header fields, for example, cookies [RFC6265] and/or authentication-related header fields such as the |Authorization| header field [RFC2616], which are processed according to documents that define them. **]**
532
533 XX**SRS_UWS_CLIENT_01_102: [** Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data. **]**
534 **SRS_UWS_CLIENT_01_103: [** The client MUST validate the server's response as follows: **]**
535
536 1. **SRS_UWS_CLIENT_01_104: [** If the status code received from the server is not 101, the client handles the response per HTTP [RFC2616] procedures. **]**
537 In particular, **SRS_UWS_CLIENT_01_105: [** the client might perform authentication if it receives a 401 status code; **]** **SRS_UWS_CLIENT_01_106: [** the server might redirect the client using a 3xx status code **]** **SRS_UWS_CLIENT_01_107: [** (but clients are not required to follow them) **]**, etc.
538 Otherwise, proceed as follows.
539
540 2. **SRS_UWS_CLIENT_01_108: [** If the response lacks an |Upgrade| header field or the |Upgrade| header field contains a value that is not an ASCII case-insensitive match for the value "websocket", the client MUST _Fail the WebSocket Connection_. **]**
541
542 3. **SRS_UWS_CLIENT_01_109: [** If the response lacks a |Connection| header field or the |Connection| header field doesn't contain a token that is an ASCII case-insensitive match for the value "Upgrade", the client MUST _Fail the WebSocket Connection_. **]**
543
544 4. **SRS_UWS_CLIENT_01_110: [** If the response lacks a |Sec-WebSocket-Accept| header field or the |Sec-WebSocket-Accept| contains a value other than the base64-encoded SHA-1 of the concatenation of the |Sec-WebSocket-Key| (as a string, not base64-decoded) with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" but ignoring any leading and trailing whitespace, the client MUST _Fail the WebSocket Connection_. **]**
545
546 5. **SRS_UWS_CLIENT_01_111: [** If the response includes a |Sec-WebSocket-Extensions| header field and this header field indicates the use of an extension that was not present in the client's handshake (the server has indicated an extension not requested by the client), the client MUST _Fail the WebSocket Connection_. **]** (The parsing of this header field to determine which extensions are requested is discussed in Section 9.1.)
547
548 6. **SRS_UWS_CLIENT_01_112: [** If the response includes a |Sec-WebSocket-Protocol| header field and this header field indicates the use of a subprotocol that was not present in the client's handshake (the server has indicated a subprotocol not requested by the client), the client MUST _Fail the WebSocket Connection_. **]**
549
550 **SRS_UWS_CLIENT_01_113: [** If the server's response does not conform to the requirements for the server's handshake as defined in this section and in Section 4.2.2, the client MUST _Fail the WebSocket Connection_. **]**
551
552 **SRS_UWS_CLIENT_01_114: [** Please note that according to [RFC2616], all header field names in both HTTP requests and HTTP responses are case-insensitive. **]**
553
554 XX**SRS_UWS_CLIENT_01_115: [** If the server's response is validated as provided for above, it is said that _The WebSocket Connection is Established_ and that the WebSocket Connection is in the OPEN state. **]**
555 **SRS_UWS_CLIENT_01_116: [** The _Extensions In Use_ is defined to be a (possibly empty) string, the value of which is equal to the value of the |Sec-WebSocket-Extensions| header field supplied by the server's handshake or the null value if that header field was not present in the server's handshake. **]**
556 **SRS_UWS_CLIENT_01_117: [** The _Subprotocol In Use_ is defined to be the value of the |Sec-WebSocket-Protocol| header field in the server's handshake or the null value if that header field was not present in the server's handshake. **]**
557 **SRS_UWS_CLIENT_01_118: [** Additionally, if any header fields in the server's handshake indicate that cookies should be set (as defined by [RFC6265]), these cookies are referred to as _Cookies Set During the Server's Opening Handshake_. **]**
558
559 4.2. Server-Side Requirements
560
561 Servers MAY offload the management of the connection to other agents on the network, for example, load balancers and reverse proxies.
562 In such a situation, the server for the purposes of this specification is considered to include all parts of the server-side infrastructure from the first device to terminate the TCP connection all the way to the server that processes requests and sends responses.
563
564 EXAMPLE: A data center might have a server that responds to WebSocket requests with an appropriate handshake and then passes the connection to another server to actually process the data frames.
565 For the purposes of this specification, the "server" is the combination of both computers.
566
567 4.2.1. Reading the Client's Opening Handshake
568
569 When a client starts a WebSocket connection, it sends its part of the opening handshake.
570 The server must parse at least part of this handshake in order to obtain the necessary information to generate the server part of the handshake.
571
572 The client's opening handshake consists of the following parts.
573 If the server, while reading the handshake, finds that the client did not send a handshake that matches the description below (note that as per [RFC2616], the order of the header fields is not important), including but not limited to any violations of the ABNF grammar specified for the components of the handshake, the server MUST stop processing the client's handshake and return an HTTP response with an appropriate error code (such as 400 Bad Request).
574
575 1. An HTTP/1.1 or higher GET request, including a "Request-URI" [RFC2616] that should be interpreted as a /resource name/ defined in Section 3 (or an absolute HTTP/HTTPS URI containing the /resource name/).
576
577 2. A |Host| header field containing the server's authority.
578
579 3. An |Upgrade| header field containing the value "websocket", treated as an ASCII case-insensitive value.
580
581 4. A |Connection| header field that includes the token "Upgrade", treated as an ASCII case-insensitive value.
582
583 5. A |Sec-WebSocket-Key| header field with a base64-encoded (see Section 4 of [RFC4648]) value that, when decoded, is 16 bytes in length.
584
585 6. A |Sec-WebSocket-Version| header field, with a value of 13.
586
587 7. Optionally, an |Origin| header field. This header field is sent by all browser clients.
588 A connection attempt lacking this header field SHOULD NOT be interpreted as coming from a browser client.
589
590 8. Optionally, a |Sec-WebSocket-Protocol| header field, with a list of values indicating which protocols the client would like to speak, ordered by preference.
591
592 9. Optionally, a |Sec-WebSocket-Extensions| header field, with a list of values indicating which extensions the client would like to speak.
593 The interpretation of this header field is discussed in Section 9.1.
594
595 10. Optionally, other header fields, such as those used to send cookies or request authentication to a server.
596 Unknown header fields are ignored, as per [RFC2616].
597
598 4.2.2. Sending the Server's Opening Handshake
599
600 When a client establishes a WebSocket connection to a server, the server MUST complete the following steps to accept the connection and send the server's opening handshake.
601
602 1. If the connection is happening on an HTTPS (HTTP-over-TLS) port, perform a TLS handshake over the connection.
603 If this fails (e.g., the client indicated a host name in the extended client hello "server_name" extension that the server does not host), then close the connection; otherwise, all further communication for the connection (including the server's handshake) MUST run through the encrypted tunnel [RFC5246].
604
605 2. The server can perform additional client authentication, for example, by returning a 401 status code with the corresponding |WWW-Authenticate| header field as described in [RFC2616].
606
607 3. The server MAY redirect the client using a 3xx status code [RFC2616].
608 Note that this step can happen together with, before, or after the optional authentication step described above.
609
610 4. Establish the following information:
611
612 /origin/
613 The |Origin| header field in the client's handshake indicates the origin of the script establishing the connection.
614 The origin is serialized to ASCII and converted to lowercase.
615 The server MAY use this information as part of a determination of whether to accept the incoming connection.
616 If the server does not validate the origin, it will accept connections from anywhere.
617 If the server does not wish to accept this connection, it MUST return an appropriate HTTP error code (e.g., 403 Forbidden) and abort the WebSocket handshake described in this section.
618 For more detail, refer to Section 10.
619
620 /key/
621 The |Sec-WebSocket-Key| header field in the client's handshake includes a base64-encoded value that, if decoded, is 16 bytes in length.
622 This (encoded) value is used in the creation of the server's handshake to indicate an acceptance of the connection.
623 It is not necessary for the server to base64-decode the |Sec-WebSocket-Key| value.
624
625 /version/
626 The |Sec-WebSocket-Version| header field in the client's handshake includes the version of the WebSocket Protocol with which the client is attempting to communicate.
627 If this version does not match a version understood by the server, the server MUST abort the WebSocket handshake described in this section and instead send an appropriate HTTP error code (such as 426 Upgrade Required) and a |Sec-WebSocket-Version| header field indicating the version(s) the server is capable of understanding.
628
629 /resource name/
630 An identifier for the service provided by the server.
631 If the server provides multiple services, then the value should be derived from the resource name given in the client's handshake in the "Request-URI" [RFC2616] of the GET method.
632 If the requested service is not available, the server MUST send an appropriate HTTP error code (such as 404 Not Found) and abort the WebSocket handshake.
633
634 /subprotocol/
635 Either a single value representing the subprotocol the server is ready to use or null.
636 The value chosen MUST be derived from the client's handshake, specifically by selecting one of the values from the |Sec-WebSocket-Protocol| field that the server is willing to use for this connection (if any).
637 If the client's handshake did not contain such a header field or if the server does not agree to any of the client's requested subprotocols, the only acceptable value is null.
638 The absence of such a field is equivalent to the null value (meaning that if the server does not wish to agree to one of the suggested subprotocols, it MUST NOT send back a |Sec-WebSocket-Protocol| header field in its response).
639 The empty string is not the same as the null value for these purposes and is not a legal value for this field.
640 The ABNF for the value of this header field is (token), where the definitions of constructs and rules are as given in [RFC2616].
641
642 /extensions/
643 A (possibly empty) list representing the protocol-level extensions the server is ready to use.
644 If the server supports multiple extensions, then the value MUST be derived from the client's handshake, specifically by selecting one or more of the values from the |Sec-WebSocket-Extensions| field.
645 The absence of such a field is equivalent to the null value.
646 The empty string is not the same as the null value for these purposes.
647 Extensions not listed by the client MUST NOT be listed.
648 The method by which these values should be selected and interpreted is discussed in Section 9.1.
649
650 5. If the server chooses to accept the incoming connection, it MUST reply with a valid HTTP response indicating the following.
651
652 1. XX**SRS_UWS_CLIENT_01_478: [** A Status-Line with a 101 response code as per RFC 2616 [RFC2616]. **]**
653 Such a response could look like "HTTP/1.1 101 Switching Protocols".
654
655 2. **SRS_UWS_CLIENT_01_479: [** An |Upgrade| header field with value "websocket" as per RFC 2616 [RFC2616]. **]**
656
657 3. **SRS_UWS_CLIENT_01_480: [** A |Connection| header field with value "Upgrade". **]**
658
659 4. **SRS_UWS_CLIENT_01_481: [** A |Sec-WebSocket-Accept| header field. **]** **SRS_UWS_CLIENT_01_482: [** The value of this header field is constructed by concatenating /key/, defined above in step 4 in Section 4.2.2, with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" **]**, **SRS_UWS_CLIENT_01_483: [** taking the SHA-1 hash of this concatenated value to obtain a 20-byte value **]** **SRS_UWS_CLIENT_01_484: [** and base64-encoding (see Section 4 of [RFC4648]) this 20-byte hash. **]**
660
661 The ABNF [RFC2616] of this header field is defined as follows:
662
663 Sec-WebSocket-Accept = base64-value-non-empty
664 base64-value-non-empty = (1*base64-data [ base64-padding ]) |
665 base64-padding
666 base64-data = 4base64-character
667 base64-padding = (2base64-character "==") |
668 (3base64-character "=")
669 base64-character = ALPHA | DIGIT | "+" | "/"
670
671 NOTE: As an example, if the value of the |Sec-WebSocket-Key| header field in the client's handshake were "dGhlIHNhbXBsZSBub25jZQ==", the server would append the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to form the string "dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11".
672 The server would then take the SHA-1 hash of this string, giving the value 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea.
673 This value is then base64-encoded, to give the value "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", which would be returned in the |Sec-WebSocket-Accept| header field.
674
675 5. **SRS_UWS_CLIENT_01_485: [** Optionally, a |Sec-WebSocket-Protocol| header field, with a value /subprotocol/ as defined in step 4 in Section 4.2.2. **]**
676
677 6. **SRS_UWS_CLIENT_01_486: [** Optionally, a |Sec-WebSocket-Extensions| header field, with a value /extensions/ as defined in step 4 in Section 4.2.2. **]**
678 **SRS_UWS_CLIENT_01_487: [** If multiple extensions are to be used, they can all be listed in a single |Sec-WebSocket-Extensions| header field or split between multiple instances of the |Sec-WebSocket-Extensions| header field. **]**
679
680 This completes the server's handshake. If the server finishes these steps without aborting the WebSocket handshake, the server considers the WebSocket connection to be established and that the WebSocket connection is in the OPEN state.
681 At this point, the server may begin sending (and receiving) data.
682
683 4.3. Collected ABNF for New Header Fields Used in Handshake
684
685 This section is using ABNF syntax/rules from Section 2.1 of [RFC2616], including the "implied *LWS rule".
686
687 Note that the following ABNF conventions are used in this section.
688 Some names of the rules correspond to names of the corresponding header fields.
689 Such rules express values of the corresponding header fields, for example, the Sec-WebSocket-Key ABNF rule describes syntax of the |Sec-WebSocket-Key| header field value.
690 ABNF rules with the "-Client" suffix in the name are only used in requests sent by the client to the server; ABNF rules with the "-Server" suffix in the name are only used in responses sent by the server to the client.
691 For example, the ABNF rule Sec-WebSocket-Protocol-Client describes syntax of the |Sec-WebSocket-Protocol| header field value sent by the client to the server.
692
693 The following new header fields can be sent during the handshake from the client to the server:
694
695 Sec-WebSocket-Key = base64-value-non-empty
696 Sec-WebSocket-Extensions = extension-list
697 Sec-WebSocket-Protocol-Client = 1#token
698 Sec-WebSocket-Version-Client = version
699
700 base64-value-non-empty = (1*base64-data [ base64-padding ]) |
701 base64-padding
702 base64-data = 4base64-character
703 base64-padding = (2base64-character "==") |
704 (3base64-character "=")
705 base64-character = ALPHA | DIGIT | "+" | "/"
706 extension-list = 1#extension
707 extension = extension-token *( ";" extension-param )
708 extension-token = registered-token
709 registered-token = token
710 extension-param = token [ "=" (token | quoted-string) ]
711 ; When using the quoted-string syntax variant, the value
712 ; after quoted-string unescaping MUST conform to the
713 ; 'token' ABNF.
714 NZDIGIT = "1" | "2" | "3" | "4" | "5" | "6" |
715 "7" | "8" | "9"
716 version = DIGIT | (NZDIGIT DIGIT) |
717 ("1" DIGIT DIGIT) | ("2" DIGIT DIGIT)
718 ; Limited to 0-255 range, with no leading zeros
719
720 The following new header fields can be sent during the handshake from the server to the client:
721
722 **SRS_UWS_CLIENT_01_131: [** Sec-WebSocket-Extensions = extension-list **]**
723 **SRS_UWS_CLIENT_01_132: [** Sec-WebSocket-Accept = base64-value-non-empty **]**
724 **SRS_UWS_CLIENT_01_133: [** Sec-WebSocket-Protocol-Server = token **]**
725 **SRS_UWS_CLIENT_01_134: [** Sec-WebSocket-Version-Server = 1#version **]**
726
727 4.4. Supporting Multiple Versions of WebSocket Protocol
728
729 This section provides some guidance on supporting multiple versions of the WebSocket Protocol in clients and servers.
730
731 **SRS_UWS_CLIENT_01_135: [** Using the WebSocket version advertisement capability (the |Sec-WebSocket-Version| header field), a client can initially request the version of the WebSocket Protocol that it prefers (which doesn't necessarily have to be the latest supported by the client). **]**
732 **SRS_UWS_CLIENT_01_136: [** If the server supports the requested version and the handshake message is otherwise valid, the server will accept that version. **]**
733 **SRS_UWS_CLIENT_01_137: [** If the server doesn't support the requested version, it MUST respond with a |Sec-WebSocket-Version| header field (or multiple |Sec-WebSocket-Version| header fields) containing all versions it is willing to use. **]**
734 **SRS_UWS_CLIENT_01_138: [** At this point, if the client supports one of the advertised versions, it can repeat the WebSocket handshake using a new version value. **]**
735
736 The following example demonstrates version negotiation described above:
737
738 GET /chat HTTP/1.1
739 Host: server.example.com
740 Upgrade: websocket
741 Connection: Upgrade
742 ...
743 Sec-WebSocket-Version: 25
744
745 The response from the server might look as follows:
746
747 HTTP/1.1 400 Bad Request
748 ...
749 Sec-WebSocket-Version: 13, 8, 7
750
751 Note that the last response from the server might also look like:
752
753 HTTP/1.1 400 Bad Request
754 ...
755 Sec-WebSocket-Version: 13
756 Sec-WebSocket-Version: 8, 7
757
758 The client now repeats the handshake that conforms to version 13:
759
760 GET /chat HTTP/1.1
761 Host: server.example.com
762 Upgrade: websocket
763 Connection: Upgrade
764 ...
765 Sec-WebSocket-Version: 13
766
767 5. Data Framing
768
769 5.1. Overview
770
771 In the WebSocket Protocol, data is transmitted using a sequence of frames.
772 XX**SRS_UWS_CLIENT_01_140: [** To avoid confusing network intermediaries (such as intercepting proxies) and for security reasons that are further discussed in Section 10.3, a client MUST mask all frames that it sends to the server (see Section 5.3 for further details). **]**
773 (Note that XX**SRS_UWS_CLIENT_01_141: [** masking is done whether or not the WebSocket Protocol is running over TLS. **]**)
774 The server MUST close the connection upon receiving a frame that is not masked.
775 In this case, a server MAY send a Close frame with a status code of 1002 (protocol error) as defined in Section 7.4.1.
776 A server MUST NOT mask any frames that it sends to the client.
777 XX**SRS_UWS_CLIENT_01_144: [** A client MUST close a connection if it detects a masked frame. **]**
778 XX**SRS_UWS_CLIENT_01_145: [** In this case, it MAY use the status code 1002 (protocol error) as defined in Section 7.4.1. (These rules might be relaxed in a future specification.) **]**
779
780 The base framing protocol defines a frame type with an opcode, a payload length, and designated locations for "Extension data" and "Application data", which together define the "Payload data".
781 Certain bits and opcodes are reserved for future expansion of the protocol.
782
783 XX**SRS_UWS_CLIENT_01_146: [** A data frame MAY be transmitted by either the client or the server at any time after opening handshake completion and before that endpoint has sent a Close frame (Section 5.5.1). **]**
784
785 5.2. Base Framing Protocol
786
787 This wire format for the data transfer part is described by the ABNF [RFC5234] given in detail in this section.
788 (Note that, unlike in other sections of this document, the ABNF in this section is operating on groups of bits.
789 The length of each group of bits is indicated in a comment.
790 When encoded on the wire, the most significant bit is the leftmost in the ABNF).
791 A high-level overview of the framing is given in the following figure.
792 In a case of conflict between the figure below and the ABNF specified later in this section, the figure is authoritative.
793
794 0 1 2 3
795 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
796 +-+-+-+-+-------+-+-------------+-------------------------------+
797 |F|R|R|R| opcode|M| Payload len | Extended payload length |
798 |I|S|S|S| (4) |A| (7) | (16/64) |
799 |N|V|V|V| |S| | (if payload len==126/127) |
800 | |1|2|3| |K| | |
801 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
802 | Extended payload length continued, if payload len == 127 |
803 + - - - - - - - - - - - - - - - +-------------------------------+
804 | |Masking-key, if MASK set to 1 |
805 +-------------------------------+-------------------------------+
806 | Masking-key (continued) | Payload Data |
807 +-------------------------------- - - - - - - - - - - - - - - - +
808 : Payload Data continued ... :
809 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
810 | Payload Data continued ... |
811 +---------------------------------------------------------------+
812
813 FIN: 1 bit
814
815 X**SRS_UWS_CLIENT_01_147: [** Indicates that this is the final fragment in a message. **]**
816 X**SRS_UWS_CLIENT_01_148: [** The first fragment MAY also be the final fragment. **]**
817
818 RSV1, RSV2, RSV3: 1 bit each
819
820 **SRS_UWS_CLIENT_01_149: [** MUST be 0 unless an extension is negotiated that defines meanings for non-zero values. **]**
821 **SRS_UWS_CLIENT_01_150: [** If a nonzero value is received and none of the negotiated extensions defines the meaning of such a nonzero value, the receiving endpoint MUST _Fail the WebSocket Connection_. **]**
822
823 Opcode: 4 bits
824
825 Defines the interpretation of the "Payload data".
826 **SRS_UWS_CLIENT_01_151: [** If an unknown opcode is received, the receiving endpoint MUST _Fail the WebSocket Connection_. **]**
827 The following values are defined.
828
829 **SRS_UWS_CLIENT_01_152: [** * %x0 denotes a continuation frame **]**
830
831 XX**SRS_UWS_CLIENT_01_153: [** * %x1 denotes a text frame **]**
832
833 XX**SRS_UWS_CLIENT_01_154: [** * %x2 denotes a binary frame **]**
834
835 **SRS_UWS_CLIENT_01_155: [** * %x3-7 are reserved for further non-control frames **]**
836
837 XX**SRS_UWS_CLIENT_01_156: [** * %x8 denotes a connection close **]**
838
839 XX**SRS_UWS_CLIENT_01_157: [** * %x9 denotes a ping **]**
840
841 **SRS_UWS_CLIENT_01_158: [** * %xA denotes a pong **]**
842
843 **SRS_UWS_CLIENT_01_159: [** * %xB-F are reserved for further control frames **]**
844
845 Mask: 1 bit
846
847 XX**SRS_UWS_CLIENT_01_160: [** Defines whether the "Payload data" is masked. **]**
848 **SRS_UWS_CLIENT_01_161: [** If set to 1, a masking key is present in masking-key, and this is used to unmask the "Payload data" as per Section 5.3. **]**
849 **SRS_UWS_CLIENT_01_162: [** All frames sent from client to server have this bit set to 1. **]**
850
851 Payload length: 7 bits, 7+16 bits, or 7+64 bits
852
853 XX**SRS_UWS_CLIENT_01_163: [** The length of the "Payload data", in bytes: **]** XX**SRS_UWS_CLIENT_01_164: [** if 0-125, that is the payload length. **]**
854 XX**SRS_UWS_CLIENT_01_165: [** If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length. **]**
855 XX**SRS_UWS_CLIENT_01_166: [** If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0) are the payload length. **]**
856 XX**SRS_UWS_CLIENT_01_167: [** Multibyte length quantities are expressed in network byte order. **]**
857 XX**SRS_UWS_CLIENT_01_168: [** Note that in all cases, the minimal number of bytes MUST be used to encode the length, for example, the length of a 124-byte-long string can't be encoded as the sequence 126, 0, 124. **]**
858 XX**SRS_UWS_CLIENT_01_169: [** The payload length is the length of the "Extension data" + the length of the "Application data". **]**
859 **SRS_UWS_CLIENT_01_170: [** The length of the "Extension data" may be zero, in which case the payload length is the length of the "Application data". **]**
860
861 Masking-key: 0 or 4 bytes
862
863 **SRS_UWS_CLIENT_01_171: [** All frames sent from the client to the server are masked by a 32-bit value that is contained within the frame. **]**
864 **SRS_UWS_CLIENT_01_172: [** This field is present if the mask bit is set to 1 and is absent if the mask bit is set to 0. **]**
865 See Section 5.3 for further information on client-to-server masking.
866
867 Payload data: (x+y) bytes
868
869 XX**SRS_UWS_CLIENT_01_173: [** The "Payload data" is defined as "Extension data" concatenated with "Application data". **]**
870
871 Extension data: x bytes
872
873 **SRS_UWS_CLIENT_01_174: [** The "Extension data" is 0 bytes unless an extension has been negotiated. **]**
874 **SRS_UWS_CLIENT_01_175: [** Any extension MUST specify the length of the "Extension data", or how that length may be calculated, and how the extension use MUST be negotiated during the opening handshake. **]**
875 **SRS_UWS_CLIENT_01_176: [** If present, the "Extension data" is included in the total payload length. **]**
876
877 Application data: y bytes
878
879 **SRS_UWS_CLIENT_01_177: [** Arbitrary "Application data", taking up the remainder of the frame after any "Extension data". **]**
880 **SRS_UWS_CLIENT_01_178: [** The length of the "Application data" is equal to the payload length minus the length of the "Extension data". **]**
881
882 The base framing protocol is formally defined by the following ABNF [RFC5234].
883 It is important to note that the representation of this data is binary, not ASCII characters.
884 As such, a field with a length of 1 bit that takes values %x0 / %x1 is represented as a single bit whose value is 0 or 1, not a full byte (octet) that stands for the characters "0" or "1" in the ASCII encoding.
885 A field with a length of 4 bits with values between %x0-F again is represented by 4 bits, again NOT by an ASCII character or full byte (octet) with these values.
886 [RFC5234] does not specify a character encoding: "Rules resolve into a string of terminal values, sometimes called characters. In ABNF, a character is merely a non-negative integer. In certain contexts, a specific mapping (encoding) of values into a character set (such as ASCII) will be specified."
887 Here, the specified encoding is a binary encoding where each terminal value is encoded in the specified number of bits, which varies for each field.
888
889 ws-frame = frame-fin ; 1 bit in length
890 frame-rsv1 ; 1 bit in length
891 frame-rsv2 ; 1 bit in length
892 frame-rsv3 ; 1 bit in length
893 frame-opcode ; 4 bits in length
894 frame-masked ; 1 bit in length
895 frame-payload-length ; either 7, 7+16,
896 ; or 7+64 bits in
897 ; length
898 [ frame-masking-key ] ; 32 bits in length
899 frame-payload-data ; n*8 bits in
900 ; length, where
901 ; n >= 0
902
903 frame-fin = %x0 ; more frames of this message follow
904 / %x1 ; final frame of this message
905 ; 1 bit in length
906
907 frame-rsv1 = %x0 / %x1
908 ; 1 bit in length, MUST be 0 unless
909 ; negotiated otherwise
910
911 frame-rsv2 = %x0 / %x1
912 ; 1 bit in length, MUST be 0 unless
913 ; negotiated otherwise
914
915 frame-rsv3 = %x0 / %x1
916 ; 1 bit in length, MUST be 0 unless
917 ; negotiated otherwise
918
919 frame-opcode = frame-opcode-non-control /
920 frame-opcode-control /
921 frame-opcode-cont
922
923 frame-opcode-cont = %x0 ; frame continuation
924
925 frame-opcode-non-control= %x1 ; text frame
926 / %x2 ; binary frame
927 / %x3-7
928 ; 4 bits in length,
929 ; reserved for further non-control frames
930
931 frame-opcode-control = %x8 ; connection close
932 / %x9 ; ping
933 / %xA ; pong
934 / %xB-F ; reserved for further control
935 ; frames
936 ; 4 bits in length
937
938 frame-masked = %x0
939 ; frame is not masked, no frame-masking-key
940 / %x1
941 ; frame is masked, frame-masking-key present
942 ; 1 bit in length
943
944 frame-payload-length = ( %x00-7D )
945 / ( %x7E frame-payload-length-16 )
946 / ( %x7F frame-payload-length-63 )
947 ; 7, 7+16, or 7+64 bits in length,
948 ; respectively
949
950 frame-payload-length-16 = %x0000-FFFF ; 16 bits in length
951
952 frame-payload-length-63 = %x0000000000000000-7FFFFFFFFFFFFFFF
953 ; 64 bits in length
954
955 frame-masking-key = 4( %x00-FF )
956 ; present only if frame-masked is 1
957 ; 32 bits in length
958
959 frame-payload-data = (frame-masked-extension-data
960 frame-masked-application-data)
961 ; when frame-masked is 1
962 / (frame-unmasked-extension-data
963 frame-unmasked-application-data)
964 ; when frame-masked is 0
965
966 frame-masked-extension-data = *( %x00-FF )
967 ; reserved for future extensibility
968 ; n*8 bits in length, where n >= 0
969
970 frame-masked-application-data = *( %x00-FF )
971 ; n*8 bits in length, where n >= 0
972
973 frame-unmasked-extension-data = *( %x00-FF )
974 ; reserved for future extensibility
975 ; n*8 bits in length, where n >= 0
976
977 frame-unmasked-application-data = *( %x00-FF )
978 ; n*8 bits in length, where n >= 0
979
980 5.3. Client-to-Server Masking
981
982 A masked frame MUST have the field frame-masked set to 1, as defined in Section 5.2.
983
984 The masking key is contained completely within the frame, as defined in Section 5.2 as frame-masking-key.
985 It is used to mask the "Payload data" defined in the same section as frame-payload-data, which includes "Extension data" and "Application data".
986
987 The masking key is a 32-bit value chosen at random by the client.
988 When preparing a masked frame, the client MUST pick a fresh masking key from the set of allowed 32-bit values.
989 The masking key needs to be unpredictable; thus, the masking key MUST be derived from a strong source of entropy, and the masking key for a given frame MUST NOT make it simple for a server/proxy to predict the masking key for a subsequent frame.
990 The unpredictability of the masking key is essential to prevent authors of malicious applications from selecting the bytes that appear on the wire.
991 RFC 4086 [RFC4086] discusses what entails a suitable source of entropy for security-sensitive applications.
992
993 The masking does not affect the length of the "Payload data".
994 To convert masked data into unmasked data, or vice versa, the following algorithm is applied.
995 The same algorithm applies regardless of the direction of the translation, e.g., the same steps are applied to mask the data as to unmask the data.
996
997 Octet i of the transformed data ("transformed-octet-i") is the XOR of octet i of the original data ("original-octet-i") with octet at index i modulo 4 of the masking key ("masking-key-octet-j"):
998
999 j = i MOD 4
1000 transformed-octet-i = original-octet-i XOR masking-key-octet-j
1001
1002 The payload length, indicated in the framing as frame-payload-length, does NOT include the length of the masking key.
1003 It is the length of the "Payload data", e.g., the number of bytes following the masking key.
1004
1005 5.4. Fragmentation
1006
1007 The primary purpose of fragmentation is to allow sending a message that is of unknown size when the message is started without having to buffer that message.
1008 If messages couldn't be fragmented, then an endpoint would have to buffer the entire message so its length could be counted before the first byte is sent.
1009 With fragmentation, a server or intermediary may choose a reasonable size buffer and, when the buffer is full, write a fragment to the network.
1010
1011 A secondary use-case for fragmentation is for multiplexing, where it is not desirable for a large message on one logical channel to monopolize the output channel, so the multiplexing needs to be free to split the message into smaller fragments to better share the output channel.
1012 (Note that the multiplexing extension is not described in this document.)
1013
1014 Unless specified otherwise by an extension, frames have no semantic meaning.
1015 An intermediary might coalesce and/or split frames, if no extensions were negotiated by the client and the server or if some extensions were negotiated, but the intermediary understood all the extensions negotiated and knows how to coalesce and/or split frames in the presence of these extensions.
1016 XX**SRS_UWS_CLIENT_01_211: [** One implication of this is that in absence of extensions, senders and receivers must not depend on the presence of specific frame boundaries. **]**
1017
1018 The following rules apply to fragmentation:
1019
1020 o **SRS_UWS_CLIENT_01_212: [** An unfragmented message consists of a single frame with the FIN bit set (Section 5.2) and an opcode other than 0. **]**
1021
1022 o **SRS_UWS_CLIENT_01_213: [** A fragmented message consists of a single frame with the FIN bit clear and an opcode other than 0, followed by zero or more frames with the FIN bit clear and the opcode set to 0, and terminated by a single frame with the FIN bit set and an opcode of 0. **]**
1023 A fragmented message is conceptually equivalent to a single larger message whose payload is equal to the concatenation of the payloads of the fragments in order; however, in the presence of extensions, this may not hold true as the extension defines the interpretation of the "Extension data" present.
1024 For instance, "Extension data" may only be present at the beginning of the first fragment and apply to subsequent fragments, or there may be "Extension data" present in each of the fragments that applies only to that particular fragment.
1025 In the absence of "Extension data", the following example demonstrates how fragmentation works.
1026
1027 EXAMPLE: For a text message sent as three fragments, the first fragment would have an opcode of 0x1 and a FIN bit clear, the second fragment would have an opcode of 0x0 and a FIN bit clear, and the third fragment would have an opcode of 0x0 and a FIN bit that is set.
1028
1029 o **SRS_UWS_CLIENT_01_214: [** Control frames (see Section 5.5) MAY be injected in the middle of a fragmented message. **]**
1030 **SRS_UWS_CLIENT_01_215: [** Control frames themselves MUST NOT be fragmented. **]**
1031
1032 o **SRS_UWS_CLIENT_01_216: [** Message fragments MUST be delivered to the recipient in the order sent by the sender. **]**
1033
1034 o **SRS_UWS_CLIENT_01_217: [** The fragments of one message MUST NOT be interleaved between the fragments of another message unless an extension has been negotiated that can interpret the interleaving. **]**
1035
1036 o **SRS_UWS_CLIENT_01_218: [** An endpoint MUST be capable of handling control frames in the middle of a fragmented message. **]**
1037
1038 o **SRS_UWS_CLIENT_01_219: [** A sender MAY create fragments of any size for non-control messages. **]**
1039
1040 o **SRS_UWS_CLIENT_01_220: [** Clients and servers MUST support receiving both fragmented and unfragmented messages. **]**
1041
1042 o As control frames cannot be fragmented, an intermediary MUST NOT attempt to change the fragmentation of a control frame.
1043
1044 o An intermediary MUST NOT change the fragmentation of a message if any reserved bit values are used and the meaning of these values is not known to the intermediary.
1045
1046 o An intermediary MUST NOT change the fragmentation of any message in the context of a connection where extensions have been negotiated and the intermediary is not aware of the semantics of the negotiated extensions.
1047 Similarly, an intermediary that didn't see the WebSocket handshake (and wasn't notified about its content) that resulted in a WebSocket connection MUST NOT change the fragmentation of any message of such connection.
1048
1049 o **SRS_UWS_CLIENT_01_225: [** As a consequence of these rules, all fragments of a message are of the same type, as set by the first fragment's opcode. **]**
1050 **SRS_UWS_CLIENT_01_226: [** Since control frames cannot be fragmented, the type for all fragments in a message MUST be either text, binary, or one of the reserved opcodes. **]**
1051
1052 NOTE: If control frames could not be interjected, the latency of a ping, for example, would be very long if behind a large message.
1053 Hence, the requirement of handling control frames in the middle of a fragmented message.
1054
1055 IMPLEMENTATION NOTE: **SRS_UWS_CLIENT_01_227: [** In the absence of any extension, a receiver doesn't have to buffer the whole frame in order to process it. **]**
1056 For example, if a streaming API is used, a part of a frame can be delivered to the application.
1057 However, note that this assumption might not hold true for all future WebSocket extensions.
1058
1059 5.5. Control Frames
1060
1061 **SRS_UWS_CLIENT_01_228: [** Control frames are identified by opcodes where the most significant bit of the opcode is 1. **]**
1062 **SRS_UWS_CLIENT_01_229: [** Currently defined opcodes for control frames include 0x8 (Close), 0x9 (Ping), and 0xA (Pong). **]**
1063 **SRS_UWS_CLIENT_01_230: [** Opcodes 0xB-0xF are reserved for further control frames yet to be defined. **]**
1064
1065 **SRS_UWS_CLIENT_01_231: [** Control frames are used to communicate state about the WebSocket. **]**
1066 **SRS_UWS_CLIENT_01_232: [** Control frames can be interjected in the middle of a fragmented message. **]**
1067
1068 **SRS_UWS_CLIENT_01_233: [** All control frames MUST have a payload length of 125 bytes or less and MUST NOT be fragmented. **]**
1069
1070 5.5.1. Close
1071
1072 XX**SRS_UWS_CLIENT_01_234: [** The Close frame contains an opcode of 0x8. **]**
1073
1074 XX**SRS_UWS_CLIENT_01_235: [** The Close frame MAY contain a body (the "Application data" portion of the frame) that indicates a reason for closing, such as an endpoint shutting down, an endpoint having received a frame too large, or an endpoint having received a frame that does not conform to the format expected by the endpoint. **]**
1075 XX**SRS_UWS_CLIENT_01_236: [** If there is a body, the first two bytes of the body MUST be a 2-byte unsigned integer (in network byte order) representing a status code with value /code/ defined in Section 7.4. **]**
1076 XX**SRS_UWS_CLIENT_01_237: [** Following the 2-byte integer, the body MAY contain UTF-8-encoded data with value /reason/, the interpretation of which is not defined by this specification. **]**
1077 This data is not necessarily human readable but may be useful for debugging or passing information relevant to the script that opened the connection.
1078 XX**SRS_UWS_CLIENT_01_238: [** As the data is not guaranteed to be human readable, clients MUST NOT show it to end users. **]**
1079
1080 XX**SRS_UWS_CLIENT_01_239: [** Close frames sent from client to server must be masked as per Section 5.3. **]**
1081
1082 XX**SRS_UWS_CLIENT_01_240: [** The application MUST NOT send any more data frames after sending a Close frame. **]**
1083
1084 XX**SRS_UWS_CLIENT_01_241: [** If an endpoint receives a Close frame and did not previously send a Close frame, the endpoint MUST send a Close frame in response. **]**
1085 **SRS_UWS_CLIENT_01_439: [** (When sending a Close frame in response, the endpoint typically echos the status code it received.) **]**
1086 XX**SRS_UWS_CLIENT_01_242: [** It SHOULD do so as soon as practical. **]**
1087 **SRS_UWS_CLIENT_01_243: [** An endpoint MAY delay sending a Close frame until its current message is sent (for instance, if the majority of a fragmented message is already sent, an endpoint MAY send the remaining fragments before sending a Close frame). **]**
1088 However, there is no guarantee that the endpoint that has already sent a Close frame will continue to process data.
1089
1090 **SRS_UWS_CLIENT_01_244: [** After both sending and receiving a Close message, an endpoint considers the WebSocket connection closed and MUST close the underlying TCP connection. **]**
1091 The server MUST close the underlying TCP connection immediately; **SRS_UWS_CLIENT_01_245: [** the client SHOULD wait for the server to close the connection but MAY close the connection at any time after sending and receiving a Close message **]**, e.g., if it has not received a TCP Close from the server in a reasonable time period.
1092
1093 If a client and server both send a Close message at the same time, both endpoints will have sent and received a Close message and should consider the WebSocket connection closed and close the underlying TCP connection.
1094
1095 5.5.2. Ping
1096
1097 XX**SRS_UWS_CLIENT_01_247: [** The Ping frame contains an opcode of 0x9. **]**
1098
1099 XX**SRS_UWS_CLIENT_01_248: [** A Ping frame MAY include "Application data". **]**
1100
1101 XX**SRS_UWS_CLIENT_01_249: [** Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response **]**, **SRS_UWS_CLIENT_01_438: [** unless it already received a Close frame. **]**
1102 XX**SRS_UWS_CLIENT_01_250: [** It SHOULD respond with Pong frame as soon as is practical. **]**
1103 Pong frames are discussed in Section 5.5.3.
1104
1105 **SRS_UWS_CLIENT_01_251: [** An endpoint MAY send a Ping frame any time after the connection is established and before the connection is closed. **]**
1106
1107 NOTE: A Ping frame may serve either as a keepalive or as a means to verify that the remote endpoint is still responsive.
1108
1109 5.5.3. Pong
1110
1111 **SRS_UWS_CLIENT_01_252: [** The Pong frame contains an opcode of 0xA. **]**
1112
1113 Section 5.5.2 details requirements that apply to both Ping and Pong frames.
1114
1115 XX**SRS_UWS_CLIENT_01_253: [** A Pong frame sent in response to a Ping frame must have identical "Application data" as found in the message body of the Ping frame being replied to. **]**
1116
1117 XX**SRS_UWS_CLIENT_01_254: [** If an endpoint receives a Ping frame and has not yet sent Pong frame(s) in response to previous Ping frame(s), the endpoint MAY elect to send a Pong frame for only the most recently processed Ping frame. **]**
1118
1119 XX**SRS_UWS_CLIENT_01_255: [** A Pong frame MAY be sent unsolicited. **]**
1120 This serves as a unidirectional heartbeat.
1121 XX**SRS_UWS_CLIENT_01_256: [** A response to an unsolicited Pong frame is not expected. **]**
1122
1123 5.6. Data Frames
1124
1125 **SRS_UWS_CLIENT_01_257: [** Data frames (e.g., non-control frames) are identified by opcodes where the most significant bit of the opcode is 0. **]**
1126 XX**SRS_UWS_CLIENT_01_258: [** Currently defined opcodes for data frames include 0x1 (Text), 0x2 (Binary). **]**
1127 **SRS_UWS_CLIENT_01_259: [** Opcodes 0x3-0x7 are reserved for further non-control frames yet to be defined. **]**
1128
1129 Data frames carry application-layer and/or extension-layer data. **SRS_UWS_CLIENT_01_260: [** The opcode determines the interpretation of the data: **]**
1130
1131 Text
1132
1133 **SRS_UWS_CLIENT_01_261: [** The "Payload data" is text data encoded as UTF-8. **]** Note that **SRS_UWS_CLIENT_01_262: [** a particular text frame might include a partial UTF-8 sequence; however, the whole message MUST contain valid UTF-8. **]**
1134 **SRS_UWS_CLIENT_01_263: [** Invalid UTF-8 in reassembled messages is handled as described in Section 8.1. **]**
1135
1136 Binary
1137
1138 XX**SRS_UWS_CLIENT_01_264: [** The "Payload data" is arbitrary binary data whose interpretation is solely up to the application layer. **]**
1139
1140 5.7. Examples
1141
1142 o A single-frame unmasked text message
1143
1144 * 0x81 0x05 0x48 0x65 0x6c 0x6c 0x6f (contains "Hello")
1145
1146 o A single-frame masked text message
1147
1148 * 0x81 0x85 0x37 0xfa 0x21 0x3d 0x7f 0x9f 0x4d 0x51 0x58 (contains "Hello")
1149
1150 o A fragmented unmasked text message
1151
1152 * 0x01 0x03 0x48 0x65 0x6c (contains "Hel")
1153
1154 * 0x80 0x02 0x6c 0x6f (contains "lo")
1155
1156 o Unmasked Ping request and masked Ping response
1157
1158 * 0x89 0x05 0x48 0x65 0x6c 0x6c 0x6f (contains a body of "Hello", but the contents of the body are arbitrary)
1159
1160 * 0x8a 0x85 0x37 0xfa 0x21 0x3d 0x7f 0x9f 0x4d 0x51 0x58 (contains a body of "Hello", matching the body of the ping)
1161
1162 o 256 bytes binary message in a single unmasked frame
1163
1164 * 0x82 0x7E 0x0100 [256 bytes of binary data]
1165
1166 o 64KiB binary message in a single unmasked frame
1167
1168 * 0x82 0x7F 0x0000000000010000 [65536 bytes of binary data]
1169
1170 5.8. Extensibility
1171
1172 The protocol is designed to allow for extensions, which will add capabilities to the base protocol.
1173 **SRS_UWS_CLIENT_01_265: [** The endpoints of a connection MUST negotiate the use of any extensions during the opening handshake. **]**
1174 **SRS_UWS_CLIENT_01_266: [** This specification provides opcodes 0x3 through 0x7 and 0xB through 0xF, the "Extension data" field, and the frame-rsv1, frame-rsv2, and frame-rsv3 bits of the frame header for use by extensions. **]**
1175 The negotiation of extensions is discussed in further detail in Section 9.1.
1176 Below are some anticipated uses of extensions.
1177 This list is neither complete nor prescriptive.
1178
1179 o "Extension data" may be placed in the "Payload data" before the "Application data".
1180
1181 o Reserved bits can be allocated for per-frame needs.
1182
1183 o Reserved opcode values can be defined.
1184
1185 o Reserved bits can be allocated to the opcode field if more opcode values are needed.
1186
1187 o A reserved bit or an "extension" opcode can be defined that allocates additional bits out of the "Payload data" to define larger opcodes or more per-frame bits.
1188
1189 6. Sending and Receiving Data
1190
1191 6.1. Sending Data
1192
1193 To _Send a WebSocket Message_ comprising of /data/ over a WebSocket connection, an endpoint MUST perform the following steps.
1194
1195 1. XX**SRS_UWS_CLIENT_01_268: [** The endpoint MUST ensure the WebSocket connection is in the OPEN state **]** (cf. Sections 4.1 and 4.2.2.)
1196 XX**SRS_UWS_CLIENT_01_269: [** If at any point the state of the WebSocket connection changes, the endpoint MUST abort the following steps. **]**
1197
1198 2. XX**SRS_UWS_CLIENT_01_270: [** An endpoint MUST encapsulate the /data/ in a WebSocket frame as defined in Section 5.2. **]**
1199 **SRS_UWS_CLIENT_01_271: [** If the data to be sent is large or if the data is not available in its entirety at the point the endpoint wishes to begin sending the data, the endpoint MAY alternately encapsulate the data in a series of frames as defined in Section 5.4. **]**
1200
1201 3. XX**SRS_UWS_CLIENT_01_272: [** The opcode (frame-opcode) of the first frame containing the data MUST be set to the appropriate value from Section 5.2 for data that is to be interpreted by the recipient as text or binary data. **]**
1202
1203 4. **SRS_UWS_CLIENT_01_273: [** The FIN bit (frame-fin) of the last frame containing the data MUST be set to 1 as defined in Section 5.2. **]**
1204
1205 5. XX**SRS_UWS_CLIENT_01_274: [** If the data is being sent by the client, the frame(s) MUST be masked as defined in Section 5.3. **]**
1206
1207 6. **SRS_UWS_CLIENT_01_275: [** If any extensions (Section 9) have been negotiated for the WebSocket connection, additional considerations may apply as per the definition of those extensions. **]**
1208
1209 7. XX**SRS_UWS_CLIENT_01_276: [** The frame(s) that have been formed MUST be transmitted over the underlying network connection. **]**
1210
1211 6.2. Receiving Data
1212
1213 XX**SRS_UWS_CLIENT_01_277: [** To receive WebSocket data, an endpoint listens on the underlying network connection. **]**
1214 XX**SRS_UWS_CLIENT_01_278: [** Incoming data MUST be parsed as WebSocket frames as defined in Section 5.2. **]**
1215 **SRS_UWS_CLIENT_01_279: [** If a control frame (Section 5.5) is received, the frame MUST be handled as defined by Section 5.5. **]**
1216 XX**SRS_UWS_CLIENT_01_280: [** Upon receiving a data frame (Section 5.6), the endpoint MUST note the /type/ of the data as defined by the opcode (frame-opcode) from Section 5.2. **]**
1217 XX**SRS_UWS_CLIENT_01_281: [** The "Application data" from this frame is defined as the /data/ of the message. **]**
1218 XX**SRS_UWS_CLIENT_01_282: [** If the frame comprises an unfragmented message (Section 5.4), it is said that _A WebSocket Message Has Been Received_ with type /type/ and data /data/. **]**
1219 **SRS_UWS_CLIENT_01_283: [** If the frame is part of a fragmented message, the "Application data" of the subsequent data frames is concatenated to form the /data/. **]**
1220 **SRS_UWS_CLIENT_01_284: [** When the last fragment is received as indicated by the FIN bit (frame-fin), it is said that _A WebSocket Message Has Been Received_ with data /data/ (comprised of the concatenation of the "Application data" of the fragments) and type /type/ (noted from the first frame of the fragmented message). **]**
1221 **SRS_UWS_CLIENT_01_285: [** Subsequent data frames MUST be interpreted as belonging to a new WebSocket message. **]**
1222
1223 **SRS_UWS_CLIENT_01_286: [** Extensions (Section 9) MAY change the semantics of how data is read, specifically including what comprises a message boundary. **]**
1224 **SRS_UWS_CLIENT_01_287: [** Extensions, in addition to adding "Extension data" before the "Application data" in a payload, MAY also modify the "Application data" (such as by compressing it). **]**
1225
1226 A server MUST remove masking for data frames received from a client as described in Section 5.3.
1227
1228 7. Closing the Connection
1229
1230 7.1. Definitions
1231
1232 7.1.1. Close the WebSocket Connection
1233
1234 XX**SRS_UWS_CLIENT_01_288: [** To _Close the WebSocket Connection_, an endpoint closes the underlying TCP connection. **]**
1235 XX**SRS_UWS_CLIENT_01_289: [** An endpoint SHOULD use a method that cleanly closes the TCP connection, as well as the TLS session, if applicable, discarding any trailing bytes that may have been received. **]**
1236 XX**SRS_UWS_CLIENT_01_290: [** An endpoint MAY close the connection via any means available when necessary, such as when under attack. **]**
1237
1238 **SRS_UWS_CLIENT_01_291: [** The underlying TCP connection, in most normal cases, SHOULD be closed first by the server **]**, so that it holds the TIME_WAIT state and not the client (as this would prevent it from re-opening the connection for 2 maximum segment lifetimes (2MSL), while there is no corresponding server impact as a TIME_WAIT connection is immediately reopened upon a new SYN with a higher seq number).
1239 **SRS_UWS_CLIENT_01_292: [** In abnormal cases (such as not having received a TCP Close from the server after a reasonable amount of time) a client MAY initiate the TCP Close. **]**
1240 As such, when a server is instructed to _Close the WebSocket Connection_ it SHOULD initiate a TCP Close immediately, and **SRS_UWS_CLIENT_01_293: [** when a client is instructed to do the same, it SHOULD wait for a TCP Close from the server. **]**
1241
1242 As an example of how to obtain a clean closure in C using Berkeley sockets, one would call shutdown() with SHUT_WR on the socket, call recv() until obtaining a return value of 0 indicating that the peer has also performed an orderly shutdown, and finally call close() on the socket.
1243
1244 7.1.2. Start the WebSocket Closing Handshake
1245
1246 **SRS_UWS_CLIENT_01_294: [** To _Start the WebSocket Closing Handshake_ with a status code (Section 7.4) /code/ and an optional close reason (Section 7.1.6) /reason/, an endpoint MUST send a Close control frame, as described in Section 5.5.1, whose status code is set to /code/ and whose close reason is set to /reason/. **]**
1247 **SRS_UWS_CLIENT_01_295: [** Once an endpoint has both sent and received a Close control frame, that endpoint SHOULD _Close the WebSocket Connection_ as defined in Section 7.1.1. **]**
1248
1249 7.1.3. The WebSocket Closing Handshake is Started
1250
1251 XX**SRS_UWS_CLIENT_01_296: [** Upon either sending or receiving a Close control frame, it is said that _The WebSocket Closing Handshake is Started_ and that the WebSocket connection is in the CLOSING state. **]**
1252
1253 7.1.4. The WebSocket Connection is Closed
1254
1255 **SRS_UWS_CLIENT_01_297: [** When the underlying TCP connection is closed, it is said that _The WebSocket Connection is Closed_ and that the WebSocket connection is in the CLOSED state. **]**
1256 **SRS_UWS_CLIENT_01_298: [** If the TCP connection was closed after the WebSocket closing handshake was completed, the WebSocket connection is said to have been closed _cleanly_. **]**
1257
1258 **SRS_UWS_CLIENT_01_299: [** If the WebSocket connection could not be established, it is also said that _The WebSocket Connection is Closed_, but not _cleanly_. **]**
1259
1260 7.1.5. The WebSocket Connection Close Code
1261
1262 **SRS_UWS_CLIENT_01_300: [** As defined in Sections 5.5.1 and 7.4, a Close control frame may contain a status code indicating a reason for closure. **]**
1263 **SRS_UWS_CLIENT_01_301: [** A closing of the WebSocket connection may be initiated by either endpoint, potentially simultaneously. **]**
1264 **SRS_UWS_CLIENT_01_302: [** _The WebSocket Connection Close Code_ is defined as the status code (Section 7.4) contained in the first Close control frame received by the application implementing this protocol. **]**
1265 **SRS_UWS_CLIENT_01_303: [** If this Close control frame contains no status code, _The WebSocket Connection Close Code_ is considered to be 1005. **]**
1266 **SRS_UWS_CLIENT_01_304: [** If _The WebSocket Connection is Closed_ and no Close control frame was received by the endpoint (such as could occur if the underlying transport connection is lost), _The WebSocket Connection Close Code_ is considered to be 1006. **]**
1267
1268 NOTE: Two endpoints may not agree on the value of _The WebSocket Connection Close Code_.
1269 As an example, if the remote endpoint sent a Close frame but the local application has not yet read the data containing the Close frame from its socket's receive buffer, and the local application independently decided to close the connection and send a Close frame, both endpoints will have sent and received a Close frame and will not send further Close frames.
1270 Each endpoint will see the status code sent by the other end as _The WebSocket Connection Close Code_.
1271 As such, it is possible that the two endpoints may not agree on the value of _The WebSocket Connection Close Code_ in the case that both endpoints _Start the WebSocket Closing Handshake_ independently and at roughly the same time.
1272
1273 7.1.6. The WebSocket Connection Close Reason
1274
1275 **SRS_UWS_CLIENT_01_305: [** As defined in Sections 5.5.1 and 7.4, a Close control frame may contain a status code indicating a reason for closure, followed by UTF-8-encoded data, the interpretation of said data being left to the endpoints and not defined by this protocol. **]**
1276 **SRS_UWS_CLIENT_01_308: [** A closing of the WebSocket connection may be initiated by either endpoint, potentially simultaneously. **]**
1277 **SRS_UWS_CLIENT_01_307: [** _The WebSocket Connection Close Reason_ is defined as the UTF-8-encoded data following the status code (Section 7.4) contained in the first Close control frame received by the application implementing this protocol. **]**
1278 **SRS_UWS_CLIENT_01_306: [** If there is no such data in the Close control frame, _The WebSocket Connection Close Reason_ is the empty string. **]**
1279
1280 NOTE: Following the same logic as noted in Section 7.1.5, two endpoints may not agree on _The WebSocket Connection Close Reason_.
1281
1282 7.1.7. Fail the WebSocket Connection
1283
1284 Certain algorithms and specifications require an endpoint to _Fail the WebSocket Connection_.
1285 **SRS_UWS_CLIENT_01_309: [** To do so, the client MUST _Close the WebSocket Connection_, and MAY report the problem to the user (which would be especially useful for developers) in an appropriate manner. **]**
1286 Similarly, to do so, the server MUST _Close the WebSocket Connection_, and SHOULD log the problem.
1287
1288 **SRS_UWS_CLIENT_01_310: [** If _The WebSocket Connection is Established_ prior to the point where the endpoint is required to _Fail the WebSocket Connection_, the endpoint SHOULD send a Close frame with an appropriate status code (Section 7.4) before proceeding to _Close the WebSocket Connection_. **]**
1289 **SRS_UWS_CLIENT_01_311: [** An endpoint MAY omit sending a Close frame if it believes the other side is unlikely to be able to receive and process the Close frame, due to the nature of the error that led the WebSocket connection to fail in the first place. **]**
1290 **SRS_UWS_CLIENT_01_312: [** An endpoint MUST NOT continue to attempt to process data (including a responding Close frame) from the remote endpoint after being instructed to _Fail the WebSocket Connection_. **]**
1291
1292 Except as indicated above or as specified by the application layer (e.g., a script using the WebSocket API), clients SHOULD NOT close the connection.
1293
1294 7.2. Abnormal Closures
1295
1296 7.2.1. Client-Initiated Closure
1297
1298 **SRS_UWS_CLIENT_01_313: [** Certain algorithms, in particular during the opening handshake, require the client to _Fail the WebSocket Connection_. **]**
1299 **SRS_UWS_CLIENT_01_314: [** To do so, the client MUST _Fail the WebSocket Connection_ as defined in Section 7.1.7. **]**
1300
1301 **SRS_UWS_CLIENT_01_315: [** If at any point the underlying transport layer connection is unexpectedly lost, the client MUST _Fail the WebSocket Connection_. **]**
1302
1303 **SRS_UWS_CLIENT_01_316: [** Except as indicated above or as specified by the application layer (e.g., a script using the WebSocket API), clients SHOULD NOT close the connection. **]**
1304
1305 7.2.2. Server-Initiated Closure
1306
1307 Certain algorithms require or recommend that the server _Abort the WebSocket Connection_ during the opening handshake. To do so, the server MUST simply _Close the WebSocket Connection_ (Section 7.1.1).
1308
1309 7.2.3. Recovering from Abnormal Closure
1310
1311 Abnormal closures may be caused by any number of reasons.
1312 Such closures could be the result of a transient error, in which case reconnecting may lead to a good connection and a resumption of normal operations.
1313 Such closures may also be the result of a nontransient problem, in which case if each deployed client experiences an abnormal closure and immediately and persistently tries to reconnect, the server may experience what amounts to a denial-of-service attack by a large number of clients trying to reconnect.
1314 The end result of such a scenario could be that the service is unable to recover in a timely manner or recovery is made much more difficult.
1315
1316 To prevent this, clients SHOULD use some form of backoff when trying to reconnect after abnormal closures as described in this section.
1317
1318 The first reconnect attempt SHOULD be delayed by a random amount of time.
1319 The parameters by which this random delay is chosen are left to the client to decide; a value chosen randomly between 0 and 5 seconds is a reasonable initial delay though clients MAY choose a different interval from which to select a delay length based on implementation experience and particular application.
1320
1321 Should the first reconnect attempt fail, subsequent reconnect attempts SHOULD be delayed by increasingly longer amounts of time, using a method such as truncated binary exponential backoff.
1322
1323 7.3. Normal Closure of Connections
1324
1325 XX**SRS_UWS_CLIENT_01_318: [** Servers MAY close the WebSocket connection whenever desired. **]**
1326 XX**SRS_UWS_CLIENT_01_317: [** Clients SHOULD NOT close the WebSocket connection arbitrarily. **]**
1327 **SRS_UWS_CLIENT_01_319: [** In either case, an endpoint initiates a closure by following the procedures to _Start the WebSocket Closing Handshake_ (Section 7.1.2). **]**
1328
1329 7.4. Status Codes
1330
1331 **SRS_UWS_CLIENT_01_320: [** When closing an established connection (e.g., when sending a Close frame, after the opening handshake has completed), an endpoint MAY indicate a reason for closure. **]**
1332 **SRS_UWS_CLIENT_01_321: [** The interpretation of this reason by an endpoint, and the action an endpoint should take given this reason, are left undefined by this specification. **]**
1333 **SRS_UWS_CLIENT_01_322: [** This specification defines a set of pre-defined status codes and specifies which ranges may be used by extensions, frameworks, and end applications. **]**
1334 **SRS_UWS_CLIENT_01_323: [** The status code and any associated textual message are optional components of a Close frame. **]**
1335
1336 7.4.1. Defined Status Codes
1337
1338 Endpoints MAY use the following pre-defined status codes when sending a Close frame.
1339
1340 1000
1341
1342 XX**SRS_UWS_CLIENT_01_324: [** 1000 indicates a normal closure, meaning that the purpose for which the connection was established has been fulfilled. **]**
1343
1344 1001
1345
1346 XX**SRS_UWS_CLIENT_01_325: [** 1001 indicates that an endpoint is "going away", such as a server going down or a browser having navigated away from a page. **]**
1347
1348 1002
1349
1350 XX**SRS_UWS_CLIENT_01_326: [** 1002 indicates that an endpoint is terminating the connection due to a protocol error. **]**
1351
1352 1003
1353
1354 XX**SRS_UWS_CLIENT_01_327: [** 1003 indicates that an endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message). **]**
1355
1356 1004
1357
1358 XX**SRS_UWS_CLIENT_01_328: [** Reserved. The specific meaning might be defined in the future. **]**
1359
1360 1005
1361
1362 XX**SRS_UWS_CLIENT_01_329: [** 1005 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. **]**
1363 It is designated for use in applications expecting a status code to indicate that no status code was actually present.
1364
1365 1006
1366
1367 XX**SRS_UWS_CLIENT_01_330: [** 1006 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. **]**
1368 It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, e.g., without sending or receiving a Close control frame.
1369
1370 1007
1371
1372 XX**SRS_UWS_CLIENT_01_331: [** 1007 indicates that an endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [RFC3629] data within a text message). **]**
1373
1374 1008
1375
1376 XX**SRS_UWS_CLIENT_01_332: [** 1008 indicates that an endpoint is terminating the connection because it has received a message that violates its policy. **]**
1377 This is a generic status code that can be returned when there is no other more suitable status code (e.g., 1003 or 1009) or if there is a need to hide specific details about the policy.
1378
1379 1009
1380
1381 XX**SRS_UWS_CLIENT_01_333: [** 1009 indicates that an endpoint is terminating the connection because it has received a message that is too big for it to process. **]**
1382
1383 1010
1384
1385 XX**SRS_UWS_CLIENT_01_334: [** 1010 indicates that an endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. **]**
1386 **SRS_UWS_CLIENT_01_335: [** The list of extensions that are needed SHOULD appear in the /reason/ part of the Close frame. **]**
1387 Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
1388
1389 1011
1390
1391 XX**SRS_UWS_CLIENT_01_336: [** 1011 indicates that a server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. **]**
1392
1393 1015
1394
1395 XX**SRS_UWS_CLIENT_01_337: [** 1015 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. **]**
1396 It is designated for use in applications expecting a status code to indicate that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).
1397
1398 7.4.2. Reserved Status Code Ranges
1399
1400 0-999
1401
1402 Status codes in the range 0-999 are not used.
1403
1404 1000-2999
1405
1406 Status codes in the range 1000-2999 are reserved for definition by this protocol, its future revisions, and extensions specified in a permanent and readily available public specification.
1407
1408 3000-3999
1409
1410 Status codes in the range 3000-3999 are reserved for use by libraries, frameworks, and applications.
1411 These status codes are registered directly with IANA.
1412 The interpretation of these codes is undefined by this protocol.
1413
1414 4000-4999
1415
1416 Status codes in the range 4000-4999 are reserved for private use and thus can't be registered.
1417 Such codes can be used by prior agreements between WebSocket applications.
1418 The interpretation of these codes is undefined by this protocol.
1419
1420 8. Error Handling
1421
1422 8.1. Handling Errors in UTF-8-Encoded Data
1423
1424 **SRS_UWS_CLIENT_01_342: [** When an endpoint is to interpret a byte stream as UTF-8 but finds that the byte stream is not, in fact, a valid UTF-8 stream, that endpoint MUST _Fail the WebSocket Connection_. **]**
1425 **SRS_UWS_CLIENT_01_343: [** This rule applies both during the opening handshake and during subsequent data exchange. **]**
1426
1427 9. Extensions
1428
1429 **SRS_UWS_CLIENT_01_344: [** WebSocket clients MAY request extensions to this specification, and WebSocket servers MAY accept some or all extensions requested by the client. **]**
1430 **SRS_UWS_CLIENT_01_345: [** A server MUST NOT respond with any extension not requested by the client. **]**
1431 **SRS_UWS_CLIENT_01_346: [** If extension parameters are included in negotiations between the client and the server, those parameters MUST be chosen in accordance with the specification of the extension to which the parameters apply. **]**
1432
1433 9.1. Negotiating Extensions
1434
1435 **SRS_UWS_CLIENT_01_347: [** A client requests extensions by including a |Sec-WebSocket-Extensions| header field, which follows the normal rules for HTTP header fields (see [RFC2616], Section 4.2) and the value of the header field is defined by the following ABNF [RFC2616]. **]**
1436 Note that this section is using ABNF syntax/rules from [RFC2616], including the "implied *LWS rule".
1437 **SRS_UWS_CLIENT_01_348: [** If a value is received by either the client or the server during negotiation that does not conform to the ABNF below, the recipient of such malformed data MUST immediately _Fail the WebSocket Connection_. **]**
1438
1439 Sec-WebSocket-Extensions = extension-list
1440 extension-list = 1#extension
1441 extension = extension-token *( ";" extension-param )
1442 extension-token = registered-token
1443 registered-token = token
1444 extension-param = token [ "=" (token | quoted-string) ]
1445 ;When using the quoted-string syntax variant, the value
1446 ;after quoted-string unescaping MUST conform to the
1447 ;'token' ABNF.
1448
1449 **SRS_UWS_CLIENT_01_349: [** Note that like other HTTP header fields, this header field MAY be split or combined across multiple lines. **]**
1450 Ergo, the following are equivalent:
1451
1452 Sec-WebSocket-Extensions: foo
1453 Sec-WebSocket-Extensions: bar; baz=2
1454
1455 is exactly equivalent to
1456
1457 Sec-WebSocket-Extensions: foo, bar; baz=2
1458
1459 **SRS_UWS_CLIENT_01_350: [** Any extension-token used MUST be a registered token (see Section 11.4). **]**
1460 **SRS_UWS_CLIENT_01_351: [** The parameters supplied with any given extension MUST be defined for that extension. **]**
1461 **SRS_UWS_CLIENT_01_352: [** Note that the client is only offering to use any advertised extensions and MUST NOT use them unless the server indicates that it wishes to use the extension. **]**
1462
1463 **SRS_UWS_CLIENT_01_353: [** Note that the order of extensions is significant. **]**
1464 Any interactions between multiple extensions MAY be defined in the documents defining the extensions.
1465 **SRS_UWS_CLIENT_01_354: [** In the absence of such definitions, the interpretation is that the header fields listed by the client in its request represent a preference of the header fields it wishes to use, with the first options listed being most preferable. **]**
1466 **SRS_UWS_CLIENT_01_355: [** The extensions listed by the server in response represent the extensions actually in use for the connection. **]**
1467 **SRS_UWS_CLIENT_01_356: [** Should the extensions modify the data and/or framing, the order of operations on the data should be assumed to be the same as the order in which the extensions are listed in the server's response in the opening handshake. **]**
1468
1469 For example, if there are two extensions "foo" and "bar" and if the header field |Sec-WebSocket-Extensions| sent by the server has the value "foo, bar", then operations on the data will be made as bar(foo(data)), be those changes to the data itself (such as compression) or changes to the framing that may "stack".
1470
1471 Non-normative examples of acceptable extension header fields (note that long lines are folded for readability):
1472
1473 Sec-WebSocket-Extensions: deflate-stream
1474 Sec-WebSocket-Extensions: mux; max-channels=4; flow-control,
1475 deflate-stream
1476 Sec-WebSocket-Extensions: private-extension
1477
1478 **SRS_UWS_CLIENT_01_357: [** A server accepts one or more extensions by including a |Sec-WebSocket-Extensions| header field containing one or more extensions that were requested by the client. **]**
1479 **SRS_UWS_CLIENT_01_358: [** The interpretation of any extension parameters, and what constitutes a valid response by a server to a requested set of parameters by a client, will be defined by each such extension. **]**
1480
1481 9.2. Known Extensions
1482
1483 Extensions provide a mechanism for implementations to opt-in to additional protocol features.
1484 This document doesn't define any extension, but implementations MAY use extensions defined separately.
1485
1486 10. Security Considerations
1487
1488 This section describes some security considerations applicable to the WebSocket Protocol.
1489 Specific security considerations are described in subsections of this section.
1490
1491 10.1. Non-Browser Clients
1492
1493 The WebSocket Protocol protects against malicious JavaScript running inside a trusted application such as a web browser, for example, by checking of the |Origin| header field (see below).
1494 See Section 1.6 for additional details.
1495 Such assumptions don't hold true in the case of a more-capable client.
1496
1497 While this protocol is intended to be used by scripts in web pages, it can also be used directly by hosts.
1498 Such hosts are acting on their own behalf and can therefore send fake |Origin| header fields, misleading the server.
1499 Servers should therefore be careful about assuming that they are talking directly to scripts from known origins and must consider that they might be accessed in unexpected ways.
1500 In particular, a server should not trust that any input is valid.
1501
1502 EXAMPLE: If the server uses input as part of SQL queries, all input text should be escaped before being passed to the SQL server, lest the server be susceptible to SQL injection.
1503
1504 10.2. Origin Considerations
1505
1506 Servers that are not intended to process input from any web page but only for certain sites SHOULD verify the |Origin| field is an origin they expect.
1507 If the origin indicated is unacceptable to the server, then it SHOULD respond to the WebSocket handshake with a reply containing HTTP 403 Forbidden status code.
1508
1509 The |Origin| header field protects from the attack cases when the untrusted party is typically the author of a JavaScript application that is executing in the context of the trusted client.
1510 The client itself can contact the server and, via the mechanism of the |Origin| header field, determine whether to extend those communication privileges to the JavaScript application.
1511 The intent is not to prevent non-browsers from establishing connections but rather to ensure that trusted browsers under the control of potentially malicious JavaScript cannot fake a WebSocket handshake.
1512
1513 10.3. Attacks On Infrastructure (Masking)
1514
1515 In addition to endpoints being the target of attacks via WebSockets, other parts of web infrastructure, such as proxies, may be the subject of an attack.
1516
1517 As this protocol was being developed, an experiment was conducted to demonstrate a class of attacks on proxies that led to the poisoning of caching proxies deployed in the wild [TALKING].
1518 The general form of the attack was to establish a connection to a server under the "attacker's" control, perform an UPGRADE on the HTTP connection similar to what the WebSocket Protocol does to establish a connection, and subsequently send data over that UPGRADEd connection that looked like a GET request for a specific known resource (which in an attack would likely be something like a widely deployed script for tracking hits or a resource on an ad-serving network).
1519 The remote server would respond with something that looked like a response to the fake GET request, and this response would be cached by a nonzero percentage of deployed intermediaries, thus poisoning the cache.
1520 The net effect of this attack would be that if a user could be convinced to visit a website the attacker controlled, the attacker could potentially poison the cache for that user and other users behind the same cache and run malicious script on other origins, compromising the web security model.
1521
1522 To avoid such attacks on deployed intermediaries, it is not sufficient to prefix application-supplied data with framing that is not compliant with HTTP, as it is not possible to exhaustively discover and test that each nonconformant intermediary does not skip such non-HTTP framing and act incorrectly on the frame payload.
1523 Thus, the defense adopted is to mask all data from the client to the server, so that the remote script (attacker) does not have control over how the data being sent appears on the wire and thus cannot construct a message that could be misinterpreted by an intermediary as an HTTP request.
1524
1525 Clients MUST choose a new masking key for each frame, using an algorithm that cannot be predicted by end applications that provide data.
1526 For example, each masking could be drawn from a cryptographically strong random number generator.
1527 If the same key is used or a decipherable pattern exists for how the next key is chosen, the attacker can send a message that, when masked, could appear to be an HTTP request (by taking the message the attacker wishes to see on the wire and masking it with the next masking key to be used, the masking key will effectively unmask the data when the client applies it).
1528
1529 It is also necessary that once the transmission of a frame from a client has begun, the payload (application-supplied data) of that frame must not be capable of being modified by the application.
1530 Otherwise, an attacker could send a long frame where the initial data was a known value (such as all zeros), compute the masking key being used upon receipt of the first part of the data, and then modify the data that is yet to be sent in the frame to appear as an HTTP request when masked.
1531 (This is essentially the same problem described in the previous paragraph with using a known or predictable masking key.)
1532 If additional data is to be sent or data to be sent is somehow changed, that new or changed data must be sent in a new frame and thus with a new masking key.
1533 In short, once transmission of a frame begins, the contents must not be modifiable by the remote script (application).
1534
1535 The threat model being protected against is one in which the client sends data that appears to be an HTTP request.
1536 As such, the channel that needs to be masked is the data from the client to the server.
1537 The data from the server to the client can be made to look like a response, but to accomplish this request, the client must also be able to forge a request.
1538 As such, it was not deemed necessary to mask data in both directions (the data from the server to the client is not masked).
1539
1540 Despite the protection provided by masking, non-compliant HTTP proxies will still be vulnerable to poisoning attacks of this type by clients and servers that do not apply masking.
1541
1542 10.4. Implementation-Specific Limits
1543
1544 **SRS_UWS_CLIENT_01_359: [** Implementations that have implementation- and/or platform-specific limitations regarding the frame size or total message size after reassembly from multiple frames MUST protect themselves against exceeding those limits. **]**
1545 (For example, a malicious endpoint can try to exhaust its peer's memory or mount a denial-of-service attack by sending either a single big frame (e.g., of size 2**60) or by sending a long stream of small frames that are a part of a fragmented message.)
1546 Such an implementation SHOULD impose a limit on frame sizes and the total message size after reassembly from multiple frames.
1547
1548 10.5. WebSocket Client Authentication
1549
1550 This protocol doesn't prescribe any particular way that servers can authenticate clients during the WebSocket handshake.
1551 The WebSocket server can use any client authentication mechanism available to a generic HTTP server, such as cookies, HTTP authentication, or TLS authentication.
1552
1553 10.6. Connection Confidentiality and Integrity
1554
1555 XX**SRS_UWS_CLIENT_01_360: [** Connection confidentiality and integrity is provided by running the WebSocket Protocol over TLS (wss URIs). **]**
1556 XX**SRS_UWS_CLIENT_01_361: [** WebSocket implementations MUST support TLS and SHOULD employ it when communicating with their peers. **]**
1557
1558 For connections using TLS, the amount of benefit provided by TLS depends greatly on the strength of the algorithms negotiated during the TLS handshake.
1559 For example, some TLS cipher mechanisms don't provide connection confidentiality.
1560 XX**SRS_UWS_CLIENT_01_362: [** To achieve reasonable levels of protection, clients should use only Strong TLS algorithms. **]**
1561 "Web Security Context: User Interface Guidelines" [W3C.REC-wsc-ui-20100812] discusses what constitutes Strong TLS algorithms.
1562 [RFC5246] provides additional guidance in Appendix A.5 and Appendix D.3.
1563
1564 10.7. Handling of Invalid Data
1565
1566 **SRS_UWS_CLIENT_01_363: [** Incoming data MUST always be validated by both clients and servers. **]**
1567 **SRS_UWS_CLIENT_01_364: [** If, at any time, an endpoint is faced with data that it does not understand or that violates some criteria by which the endpoint determines safety of input, or when the endpoint sees an opening handshake that does not correspond to the values it is expecting (e.g., incorrect path or origin in the client request), the endpoint MAY drop the TCP connection. **]**
1568 **SRS_UWS_CLIENT_01_365: [** If the invalid data was received after a successful WebSocket handshake, the endpoint SHOULD send a Close frame with an appropriate status code (Section 7.4) before proceeding to _Close the WebSocket Connection_. **]**
1569 Use of a Close frame with an appropriate status code can help in diagnosing the problem.
1570 If the invalid data is sent during the WebSocket handshake, the server SHOULD return an appropriate HTTP [RFC2616] status code.
1571
1572 A common class of security problems arises when sending text data using the wrong encoding.
1573 This protocol specifies that messages with a Text data type (as opposed to Binary or other types) contain UTF-8-encoded data.
1574 Although the length is still indicated and applications implementing this protocol should use the length to determine where the frame actually ends, sending data in an improper encoding may still break assumptions that applications built on top of this protocol may make, leading to anything from misinterpretation of data to loss of data or potential security bugs.
1575
1576 10.8. Use of SHA-1 by the WebSocket Handshake
1577
1578 The WebSocket handshake described in this document doesn't depend on any security properties of SHA-1, such as collision resistance or resistance to the second pre-image attack (as described in [RFC4270]).