-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_connect.rs
More file actions
235 lines (199 loc) · 6.65 KB
/
http_connect.rs
File metadata and controls
235 lines (199 loc) · 6.65 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
use std::io;
use thiserror::Error;
use tokio::io::AsyncReadExt;
const MAX_HEADERS: usize = 64;
const MAX_REQUEST_BYTES: usize = 16 * 1024;
pub const UPSTREAM_PROFILE_HEADER: &str = "X-PolyTLS-Upstream-Profile";
#[derive(Error, Debug)]
pub enum ConnectError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("HTTP parse error: {0}")]
HttpParse(#[from] httparse::Error),
#[error("unexpected EOF while reading CONNECT request (read {bytes_read} bytes)")]
UnexpectedEof { bytes_read: usize },
#[error("request too large")]
RequestTooLarge,
#[error("unsupported method: {0}")]
UnsupportedMethod(String),
#[error("invalid CONNECT authority: {0}")]
InvalidAuthority(String),
}
#[derive(Debug)]
pub struct ConnectRequest {
pub authority: String,
pub host: String,
pub port: u16,
pub profile: Option<String>,
pub leftover: Vec<u8>,
}
pub async fn read_connect_request<S>(stream: &mut S) -> Result<ConnectRequest, ConnectError>
where
S: tokio::io::AsyncRead + Unpin,
{
let mut buf = Vec::with_capacity(1024);
let mut tmp = [0u8; 1024];
let header_end = loop {
if buf.len() > MAX_REQUEST_BYTES {
return Err(ConnectError::RequestTooLarge);
}
let n = stream.read(&mut tmp).await?;
if n == 0 {
return Err(ConnectError::UnexpectedEof {
bytes_read: buf.len(),
});
}
buf.extend_from_slice(&tmp[..n]);
if let Some(end) = find_header_end(&buf) {
break end;
}
};
let leftover = buf[header_end..].to_vec();
let header_bytes = &buf[..header_end];
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
let mut req = httparse::Request::new(&mut headers);
let status = req.parse(header_bytes)?;
if !status.is_complete() {
return Err(ConnectError::InvalidAuthority("incomplete request".into()));
}
let method = req
.method
.ok_or_else(|| ConnectError::InvalidAuthority("missing method".into()))?;
if method != "CONNECT" {
return Err(ConnectError::UnsupportedMethod(method.to_string()));
}
let authority = req
.path
.ok_or_else(|| ConnectError::InvalidAuthority("missing request target".into()))?
.to_string();
let (host, port) = parse_connect_authority(&authority)?;
let profile = match req
.headers
.iter()
.find(|h| h.name.eq_ignore_ascii_case(UPSTREAM_PROFILE_HEADER))
{
None => None,
Some(header) => {
let value = std::str::from_utf8(header.value)
.map_err(|_| {
ConnectError::InvalidAuthority(format!(
"invalid {UPSTREAM_PROFILE_HEADER} header value"
))
})?
.trim();
if value.is_empty() {
None
} else {
Some(value.to_string())
}
}
};
Ok(ConnectRequest {
authority,
host,
port,
profile,
leftover,
})
}
fn find_header_end(buf: &[u8]) -> Option<usize> {
memchr::memmem::find(buf, b"\r\n\r\n").map(|idx| idx + 4)
}
fn parse_connect_authority(authority: &str) -> Result<(String, u16), ConnectError> {
if authority.starts_with('[') {
let close = authority
.find(']')
.ok_or_else(|| ConnectError::InvalidAuthority(authority.to_string()))?;
let host = &authority[1..close];
let rest = &authority[(close + 1)..];
let port = rest
.strip_prefix(':')
.ok_or_else(|| ConnectError::InvalidAuthority(authority.to_string()))?;
let port: u16 = port
.parse()
.map_err(|_| ConnectError::InvalidAuthority(authority.to_string()))?;
return Ok((host.to_string(), port));
}
let (host, port) = authority
.rsplit_once(':')
.ok_or_else(|| ConnectError::InvalidAuthority(authority.to_string()))?;
if host.is_empty() || host.contains(':') {
return Err(ConnectError::InvalidAuthority(authority.to_string()));
}
let port: u16 = port
.parse()
.map_err(|_| ConnectError::InvalidAuthority(authority.to_string()))?;
Ok((host.to_string(), port))
}
#[cfg(test)]
mod tests {
use super::*;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::AsyncRead;
struct OneShotReader {
bytes: Vec<u8>,
pos: usize,
}
impl OneShotReader {
fn new(bytes: Vec<u8>) -> Self {
Self { bytes, pos: 0 }
}
}
impl AsyncRead for OneShotReader {
fn poll_read(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
if self.pos >= self.bytes.len() {
return Poll::Ready(Ok(()));
}
let remaining = &self.bytes[self.pos..];
let to_copy = remaining.len().min(buf.remaining());
buf.put_slice(&remaining[..to_copy]);
self.pos += to_copy;
Poll::Ready(Ok(()))
}
}
#[tokio::test]
async fn read_connect_request_parses_upstream_profile_header() {
let mut bytes = Vec::new();
bytes.extend_from_slice(
b"CONNECT example.com:443 HTTP/1.1\r\n\
Host: example.com:443\r\n\
X-PolyTLS-Upstream-Profile: chrome-143-macos-x86_64\r\n\
\r\n",
);
bytes.extend_from_slice(b"\x16\x03\x01\x00\x01");
let mut reader = OneShotReader::new(bytes);
let req = read_connect_request(&mut reader)
.await
.expect("request should parse");
assert_eq!(req.host, "example.com");
assert_eq!(req.port, 443);
assert_eq!(req.profile.as_deref(), Some("chrome-143-macos-x86_64"));
assert_eq!(req.leftover, b"\x16\x03\x01\x00\x01");
}
#[tokio::test]
async fn read_connect_request_rejects_non_utf8_profile_header_value() {
let mut bytes = Vec::new();
bytes.extend_from_slice(
b"CONNECT example.com:443 HTTP/1.1\r\n\
Host: example.com:443\r\n\
X-PolyTLS-Upstream-Profile: ",
);
bytes.extend_from_slice(&[0xff, 0xff, 0xff]);
bytes.extend_from_slice(b"\r\n\r\n");
let mut reader = OneShotReader::new(bytes);
let err = read_connect_request(&mut reader)
.await
.expect_err("request should fail");
match err {
ConnectError::InvalidAuthority(msg) => {
assert!(msg.contains(UPSTREAM_PROFILE_HEADER));
}
other => panic!("expected InvalidAuthority, got {other:?}"),
}
}
}