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
use std::str;
pub trait FmtHeaderField {
fn fmt(_: &Self) -> String;
}
impl<T: FmtHeaderField> FmtHeaderField for &T {
fn fmt(this: &Self) -> String {
T::fmt(this)
}
}
impl<T: std::fmt::Display> FmtHeaderField for [T; 2] {
fn fmt([key, value]: &Self) -> String {
format!("{key}: {value}\r\n")
}
}
impl<K: std::fmt::Display, V: std::fmt::Display> FmtHeaderField for (K, V) {
fn fmt((key, value): &Self) -> String {
format!("{key}: {value}\r\n")
}
}
#[derive(Default, Clone)]
pub struct Record<'a> {
pub schema: &'a [u8],
pub header: Vec<(&'a [u8], &'a [u8])>,
}
const HTTP_EOF_ERR: &str = "HTTP parse error: Unexpected end";
impl<'a> Record<'a> {
pub fn get(&self, key: impl AsRef<[u8]>) -> Option<&[u8]> {
let key = key.as_ref();
self.header
.iter()
.find_map(|(k, v)| k.eq_ignore_ascii_case(key).then_some(*v))
}
fn is_ws_upgrade(&self) -> Option<bool> {
let upgrade = self.get("upgrade")?.eq_ignore_ascii_case(b"websocket");
let connection = self.get("connection")?.eq_ignore_ascii_case(b"upgrade");
Some(upgrade && connection)
}
pub fn get_sec_ws_key(&self) -> Option<&[u8]> {
let suppoted_version = self
.get("sec-websocket-version")?
.windows(2)
.any(|version| version == b"13");
(self.is_ws_upgrade()? && suppoted_version).then_some(self.get("sec-websocket-key")?)
}
pub fn get_sec_ws_accept(&self) -> Option<&[u8]> {
self.is_ws_upgrade()?
.then_some(self.get("sec-websocket-accept")?)
}
pub fn from_raw(bytes: &mut &'a [u8]) -> std::result::Result<Self, &'static str> {
let schema = trim_ascii_end(split_once(bytes, b'\n').ok_or(HTTP_EOF_ERR)?);
let mut header = vec![];
loop {
match split_once(bytes, b'\n').ok_or(HTTP_EOF_ERR)? {
b"" | b"\r" => return Ok(Self { schema, header }),
line => {
let mut value = line;
let key = split_once(&mut value, b':')
.ok_or("HTTP parse error: Invalid header field")?;
header.push((key, trim_ascii_start(trim_ascii_end(value))));
}
}
}
}
}
impl<'a> std::fmt::Debug for Record<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut header = vec![];
for (key, value) in &self.header {
if let (Ok(key), Ok(value)) = (str::from_utf8(key), str::from_utf8(value)) {
header.push((key, value))
}
}
f.debug_struct("HttpRecord")
.field("schema", &str::from_utf8(self.schema))
.field("header", &header)
.finish()
}
}
fn split_once<'a>(reader: &mut &'a [u8], ascii: u8) -> Option<&'a [u8]> {
let index = reader.iter().position(|&byte| ascii == byte)?;
let val = &reader[..index];
*reader = &reader[index + 1..];
Some(val)
}
fn trim_ascii_start(mut bytes: &[u8]) -> &[u8] {
while let [first, rest @ ..] = bytes {
if first.is_ascii_whitespace() {
bytes = rest;
} else {
break;
}
}
bytes
}
fn trim_ascii_end(mut bytes: &[u8]) -> &[u8] {
while let [rest @ .., last] = bytes {
if last.is_ascii_whitespace() {
bytes = rest;
} else {
break;
}
}
bytes
}