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
use base64;
use bytes::Bytes;
use sha1::{Digest, Sha1};
use super::SecWebsocketKey;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SecWebsocketAccept(::HeaderValue);
derive_header! {
SecWebsocketAccept(_),
name: SEC_WEBSOCKET_ACCEPT
}
impl From<SecWebsocketKey> for SecWebsocketAccept {
fn from(key: SecWebsocketKey) -> SecWebsocketAccept {
sign(key.0.as_bytes())
}
}
fn sign(key: &[u8]) -> SecWebsocketAccept {
let mut sha1 = Sha1::default();
sha1.input(key);
sha1.input(&b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"[..]);
let b64 = Bytes::from(base64::encode(&sha1.result()));
let val = ::HeaderValue::from_maybe_shared(b64).expect("base64 is a valid value");
SecWebsocketAccept(val)
}
#[cfg(test)]
mod tests {
use super::super::{test_decode, test_encode};
use super::*;
#[test]
fn key_to_accept() {
let key = test_decode::<SecWebsocketKey>(&["dGhlIHNhbXBsZSBub25jZQ=="]).expect("key");
let accept = SecWebsocketAccept::from(key);
let headers = test_encode(accept);
assert_eq!(
headers["sec-websocket-accept"],
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
);
}
}