-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathsec_websocket_extensions.rs
More file actions
338 lines (280 loc) · 9.58 KB
/
sec_websocket_extensions.rs
File metadata and controls
338 lines (280 loc) · 9.58 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use std::{borrow::Cow, fmt::Debug, iter::FromIterator, str::FromStr};
use bytes::BytesMut;
use headers_core::Error;
use http::HeaderValue;
use crate::util::{csv, TryFromValues};
/// The `Sec-Websocket-Extensions` header.
///
/// This header is used in the Websocket handshake, sent by the client to the
/// server and then from the server to the client. It is a proposed and
/// agreed-upon list of websocket protocol extensions to use.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct SecWebsocketExtensions(Vec<WebsocketProtocolExtension>);
/// An extension listed in a [`SecWebsocketExtensions`] header.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct WebsocketProtocolExtension {
name: Cow<'static, str>,
params: Vec<WebsocketExtensionParam>,
}
/// Named parameter for an extension in a `Sec-Websocket-Extensions` header.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct WebsocketExtensionParam {
name: Cow<'static, str>,
value: Option<String>,
}
impl SecWebsocketExtensions {
/// Constructs a new header with the provided extensions.
pub fn new(extensions: impl IntoIterator<Item = WebsocketProtocolExtension>) -> Self {
Self(extensions.into_iter().collect())
}
/// Returns an iterator over the extensions in this header.
pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
self.into_iter()
}
}
impl WebsocketProtocolExtension {
/// Constructs a new extension directive with the given name and parameters.
pub fn new(
name: impl Into<Cow<'static, str>>,
params: impl IntoIterator<Item = WebsocketExtensionParam>,
) -> Self {
Self {
name: name.into(),
params: params.into_iter().collect(),
}
}
/// The name of this extension directive.
pub fn name(&self) -> &str {
&self.name
}
/// Returns an iterator over the parameters for this extension directive.
pub fn params(&self) -> impl Iterator<Item = &WebsocketExtensionParam> {
self.params.iter()
}
}
impl WebsocketExtensionParam {
/// Constructs a new parameter with the given name and optional value.
#[inline]
pub fn new(name: impl Into<Cow<'static, str>>, value: Option<String>) -> Self {
Self {
name: name.into(),
value,
}
}
/// The name of the parameter.
#[inline]
pub fn name(&self) -> &str {
&self.name
}
/// The parameter value, if there is one.
#[inline]
pub fn value(&self) -> Option<&str> {
self.value.as_deref()
}
}
impl crate::Header for SecWebsocketExtensions {
fn name() -> &'static ::http::header::HeaderName {
&::http::header::SEC_WEBSOCKET_EXTENSIONS
}
fn decode<'i, I>(values: &mut I) -> Result<Self, crate::Error>
where
I: Iterator<Item = &'i HeaderValue>,
{
crate::util::TryFromValues::try_from_values(values).map(SecWebsocketExtensions)
}
fn encode<E: Extend<crate::HeaderValue>>(&self, values: &mut E) {
values.extend(std::iter::once(to_header_value(&self.0)));
}
}
impl TryFromValues for Vec<WebsocketProtocolExtension> {
fn try_from_values<'i, I>(values: &mut I) -> Result<Self, Error>
where
Self: Sized,
I: Iterator<Item = &'i HeaderValue>,
{
csv::from_comma_delimited(values)
}
}
impl FromIterator<WebsocketProtocolExtension> for SecWebsocketExtensions {
fn from_iter<T: IntoIterator<Item = WebsocketProtocolExtension>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl IntoIterator for SecWebsocketExtensions {
type Item = WebsocketProtocolExtension;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a SecWebsocketExtensions {
type Item = &'a WebsocketProtocolExtension;
type IntoIter = std::slice::Iter<'a, WebsocketProtocolExtension>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl FromStr for WebsocketProtocolExtension {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (name, tail) = s
.split_once(';')
.map(|(n, t)| (n, Some(t)))
.unwrap_or((s, None));
let params = csv::from_delimited(&mut tail.into_iter(), ';')?;
Ok(Self {
name: name.trim().to_owned().into(),
params,
})
}
}
impl std::fmt::Display for WebsocketProtocolExtension {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, params } = self;
write!(f, "{}", name)?;
for param in params {
f.write_str("; ")?;
write!(f, "{}", param)?;
}
Ok(())
}
}
impl FromStr for WebsocketExtensionParam {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (name, value) = s
.split_once('=')
.map(|(n, t)| (n, Some(t)))
.unwrap_or((s, None));
let value = value.map(|value| value.trim().to_owned());
Ok(Self {
name: name.trim().to_owned().into(),
value,
})
}
}
impl std::fmt::Display for WebsocketExtensionParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, value } = self;
write!(f, "{}", name)?;
if let Some(value) = value {
write!(f, "={}", value)?;
}
Ok(())
}
}
impl WebsocketProtocolExtension {
fn encoded_len(&self) -> usize {
let Self { name, params } = self;
let params_len: usize = params.iter().map(|p| p.encoded_len() + 2).sum();
name.len() + params_len
}
}
impl WebsocketExtensionParam {
fn encoded_len(&self) -> usize {
let Self { name, value } = self;
name.len() + value.as_ref().map(|s| s.len() + 1).unwrap_or_default()
}
fn write_to_buffer(&self, buffer: &mut BytesMut) {
let Self { name, value } = self;
buffer.extend_from_slice(b"; ");
buffer.extend_from_slice(name.as_bytes());
if let Some(value) = value {
buffer.extend_from_slice(b"=");
buffer.extend_from_slice(value.as_bytes());
}
}
}
fn to_header_value(extensions: &[WebsocketProtocolExtension]) -> HeaderValue {
let mut buffer = BytesMut::with_capacity(encoded_len(extensions));
for extension in extensions {
if !buffer.is_empty() {
buffer.extend_from_slice(b", ");
}
let WebsocketProtocolExtension { name, params } = extension;
buffer.extend_from_slice(name.as_bytes());
for param in params {
param.write_to_buffer(&mut buffer);
}
}
HeaderValue::from_maybe_shared(buffer).expect("valid construction")
}
fn encoded_len(extensions: &[WebsocketProtocolExtension]) -> usize {
let all_encoded_len: usize = extensions
.iter()
.map(WebsocketProtocolExtension::encoded_len)
.sum();
let all_separators_len = extensions
.len()
.checked_sub(1)
.map(|num_separators| num_separators * 2)
.unwrap_or_default();
all_encoded_len + all_separators_len
}
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use crate::Header;
use super::super::{test_decode, test_encode};
use super::*;
#[test]
fn parse_separate_headers() {
// From https://tools.ietf.org/html/rfc6455#section-9.1
let extensions =
test_decode::<SecWebsocketExtensions>(&["foo", "bar; baz=2"]).expect("valid");
assert_eq!(
extensions,
SecWebsocketExtensions(vec![
WebsocketProtocolExtension {
name: "foo".into(),
params: vec![],
},
WebsocketProtocolExtension {
name: "bar".into(),
params: vec![WebsocketExtensionParam {
name: "baz".into(),
value: Some("2".to_owned())
}],
}
])
);
}
#[test]
fn round_trip_complex() {
let extensions = test_decode::<SecWebsocketExtensions>(&[
"deflate-stream",
"mux; max-channels=4; flow-control, deflate-stream",
"private-extension",
])
.expect("valid");
let headers = test_encode(extensions);
assert_eq!(
headers["sec-websocket-extensions"],
"deflate-stream, mux; max-channels=4; flow-control, deflate-stream, private-extension"
);
}
#[test]
fn to_header_value_exact() {
// This isn't a required property for correctness but if the length
// precomputation is wrong we'll over- or under-allocate during
// conversion.
let cases = [
SecWebsocketExtensions::new([
WebsocketProtocolExtension::from_str("extension-name").unwrap(),
WebsocketProtocolExtension::from_str("with-params; a=5; b=8").unwrap(),
]),
SecWebsocketExtensions::new([]),
SecWebsocketExtensions::new([
WebsocketProtocolExtension::from_str("duplicate-name").unwrap(),
WebsocketProtocolExtension::from_str("duplicate-name").unwrap(),
WebsocketProtocolExtension::from_str("duplicate-name").unwrap(),
]),
];
for case in cases {
let mut values = Vec::new();
case.encode(&mut values);
let [value] = values.try_into().unwrap();
assert_eq!(value.len(), encoded_len(&case.0));
}
}
}