1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::{fmt, net};
use base64::{Engine, engine::general_purpose::STANDARD as base64};
#[cfg(feature = "cookie")]
use coo_kie::{Cookie, CookieJar};
use crate::http::error::HttpError;
use crate::http::header::{
self, AUTHORIZATION, HeaderMap, HeaderName, HeaderValue, InvalidHeaderValue,
};
use crate::service::cfg::{CfgContext, Configuration};
use crate::time::Millis;
/// Configuration for a WebSocket client connection.
///
/// Store this value in [`SharedCfg`](crate::SharedCfg) and pass the resulting
/// configuration to [`WsClient::new`](super::WsClient::new).
#[derive(Debug)]
pub struct WsClientConfig {
pub(super) addr: Option<net::SocketAddr>,
pub(super) max_size: usize,
pub(super) timeout: Millis,
pub(super) close_timeout: Millis,
pub(super) headers: HeaderMap,
pub(super) server_mode: bool,
#[cfg(feature = "compress")]
pub(super) deflate: Option<super::DeflateConfig>,
#[cfg(feature = "cookie")]
pub(super) cookies: Option<CookieJar>,
config: CfgContext,
}
impl Default for WsClientConfig {
fn default() -> Self {
Self::new()
}
}
impl Configuration for WsClientConfig {
const NAME: &str = "WebSocket client configuration";
fn ctx(&self) -> &CfgContext {
&self.config
}
fn set_ctx(&mut self, ctx: CfgContext) {
self.config = ctx;
}
}
impl WsClientConfig {
#[must_use]
/// Creates a WebSocket client configuration with default values.
pub fn new() -> WsClientConfig {
let mut headers = HeaderMap::new();
headers.insert(header::UPGRADE, HeaderValue::from_static("websocket"));
headers.insert(
header::SEC_WEBSOCKET_VERSION,
HeaderValue::from_static("13"),
);
WsClientConfig {
headers,
addr: None,
max_size: 65_536,
server_mode: false,
timeout: Millis(5_000),
close_timeout: Millis(5_000),
#[cfg(feature = "compress")]
deflate: None,
#[cfg(feature = "cookie")]
cookies: None,
config: CfgContext::default(),
}
}
#[must_use]
/// Sets the server socket address.
///
/// This address is used instead of resolving the URI host name.
pub fn set_address(mut self, addr: net::SocketAddr) -> Self {
self.addr = Some(addr);
self
}
/// Sets the WebSocket subprotocols offered to the server.
///
/// This replaces the current `Sec-WebSocket-Protocol` header. An empty
/// iterator removes the header.
///
/// # Errors
///
/// Returns [`HttpError`] if a protocol is not a valid HTTP token or the
/// resulting list is not a valid HTTP header value.
pub fn set_protocols<U, V>(mut self, protos: U) -> Result<Self, HttpError>
where
U: IntoIterator<Item = V>,
V: AsRef<str>,
{
let mut values = Vec::new();
for proto in protos {
let proto = proto.as_ref();
if !is_token(proto) {
return Err(InvalidHeaderValue::default().into());
}
values.push(proto.to_owned());
}
let protos = values.join(",");
if protos.is_empty() {
self.headers.remove(header::SEC_WEBSOCKET_PROTOCOL);
} else {
self.headers.insert(
header::SEC_WEBSOCKET_PROTOCOL,
HeaderValue::try_from(protos.as_str())?,
);
}
Ok(self)
}
#[must_use]
#[cfg(feature = "cookie")]
/// Adds a cookie to the opening handshake.
pub fn set_cookie<C>(mut self, cookie: C) -> Self
where
C: Into<Cookie<'static>>,
{
if let Some(cookies) = &mut self.cookies {
cookies.add(cookie.into());
} else {
let mut jar = CookieJar::new();
jar.add(cookie.into());
self.cookies = Some(jar);
}
self
}
/// Sets the `Origin` header for the opening handshake.
pub fn set_origin<V, E>(mut self, origin: V) -> Result<Self, HttpError>
where
HeaderValue: TryFrom<V, Error = E>,
HttpError: From<E>,
{
self.headers
.insert(header::ORIGIN, HeaderValue::try_from(origin)?);
Ok(self)
}
#[must_use]
/// Sets the maximum accepted frame payload size.
///
/// The default is 64 KiB.
pub fn set_max_frame_size(mut self, size: usize) -> Self {
self.max_size = size;
self
}
#[must_use]
/// Configures the connection to use server-side masking rules.
///
/// By default, the client masks outgoing frames and expects unmasked
/// incoming frames. Server mode reverses those rules.
pub fn set_server_mode(mut self) -> Self {
self.server_mode = true;
self
}
#[must_use]
#[cfg(feature = "compress")]
/// Offers the `permessage-deflate` extension (RFC 7692) to the server.
///
/// If the server accepts the offer, text and binary messages are
/// compressed in both directions. The server may decline it, then the
/// messages are not compressed. The opening handshake fails if the server
/// accepts the offer with parameters the client did not allow.
///
/// The extension is not offered by default.
///
/// ```rust
/// use ntex::ws::{DeflateConfig, WsClientConfig};
///
/// let cfg = WsClientConfig::new().set_deflate(
/// DeflateConfig::new().set_client_no_context_takeover(true)
/// );
/// ```
pub fn set_deflate(mut self, cfg: super::DeflateConfig) -> Self {
self.deflate = Some(cfg);
self
}
/// Sets a header for the opening handshake.
///
/// This replaces any existing value with the same name.
pub fn set_header<K, V>(mut self, key: K, value: V) -> Result<Self, HttpError>
where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
{
let key = HeaderName::try_from(key).map_err(Into::into)?;
let value = HeaderValue::try_from(value).map_err(Into::into)?;
self.headers.insert(key, value);
Ok(self)
}
/// Sets a handshake header if it is not already present.
pub fn set_header_if_none<K, V>(mut self, key: K, value: V) -> Result<Self, HttpError>
where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
{
let key = HeaderName::try_from(key).map_err(Into::into)?;
if !self.headers.contains_key(&key) {
self.headers
.insert(key, HeaderValue::try_from(value).map_err(Into::into)?);
}
Ok(self)
}
/// Sets the HTTP Basic authentication header.
pub fn set_basic_auth(
self,
username: impl fmt::Display,
password: Option<&str>,
) -> Result<Self, HttpError> {
let auth = match password {
Some(password) => format!("{username}:{password}"),
None => format!("{username}:"),
};
self.set_header(AUTHORIZATION, format!("Basic {}", base64.encode(auth)))
}
/// Sets the HTTP bearer authentication header.
pub fn set_bearer_auth(self, token: impl fmt::Display) -> Result<Self, HttpError> {
self.set_header(AUTHORIZATION, format!("Bearer {token}"))
}
#[must_use]
/// Sets the opening-handshake timeout.
///
/// The timeout covers sending the upgrade request and receiving the
/// response after a connection has been established. The default is
/// 5 seconds. A zero duration disables the timeout.
pub fn set_handshake_timeout(mut self, timeout: impl Into<Millis>) -> Self {
self.timeout = timeout.into();
self
}
#[must_use]
/// Sets the closing-handshake timeout.
///
/// After sending a close frame, the client waits this long for the peer's
/// close response before shutting down the connection. The default is
/// 5 seconds. A zero duration disables the timeout.
pub fn set_close_timeout(mut self, timeout: impl Into<Millis>) -> Self {
self.close_timeout = timeout.into();
self
}
}
pub(crate) fn is_token(value: &str) -> bool {
!value.is_empty()
&& value.bytes().all(|byte| {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
)
})
}