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
use header::{Header, HeaderFormat};
use std::fmt;
use std::str::FromStr;
use header::shared::util::{from_comma_delimited, fmt_comma_delimited};
use self::Protocol::{WebSocket, ProtocolExt};
#[derive(Clone, PartialEq, Show)]
pub struct Upgrade(pub Vec<Protocol>);
deref!(Upgrade => Vec<Protocol>);
#[derive(Clone, PartialEq, Show)]
pub enum Protocol {
WebSocket,
ProtocolExt(String),
}
impl FromStr for Protocol {
fn from_str(s: &str) -> Option<Protocol> {
match s {
"websocket" => Some(WebSocket),
s => Some(ProtocolExt(s.to_string()))
}
}
}
impl fmt::String for Protocol {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", match *self {
WebSocket => "websocket",
ProtocolExt(ref s) => s.as_slice()
})
}
}
impl Header for Upgrade {
fn header_name(_: Option<Upgrade>) -> &'static str {
"Upgrade"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Upgrade> {
from_comma_delimited(raw).map(|vec| Upgrade(vec))
}
}
impl HeaderFormat for Upgrade {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let Upgrade(ref parts) = *self;
fmt_comma_delimited(fmt, &parts[])
}
}
bench_header!(bench, Upgrade, { vec![b"HTTP/2.0, RTA/x11, websocket".to_vec()] });