forked from WebAssembly/wasi-testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-request.rs
More file actions
284 lines (257 loc) · 9.74 KB
/
http-request.rs
File metadata and controls
284 lines (257 loc) · 9.74 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
use test_wasm32_wasip3::cli::{export, exports::wasi::cli::run::Guest};
use test_wasm32_wasip3::http::{
wasi::http::types::{Fields, HeaderError, Method, Request, Scheme},
wit_future,
};
fn test_request_field_default_values(request: &Request) {
assert_eq!(request.get_method(), Method::Get);
assert_eq!(request.get_path_with_query(), None);
assert_eq!(request.get_scheme(), None);
assert_eq!(request.get_authority(), None);
assert!(request.get_options().is_none());
}
fn compute_valid_method_chars(len: usize) -> Vec<bool> {
// https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.2
// field-name = token
// token = 1*tchar
//
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
// / DIGIT / ALPHA
// ; any VCHAR, except delimiters
let mut ret = Vec::<bool>::new();
ret.resize(len, false);
for ch in "#$%&'!*+-.^_`|~".chars() {
ret[ch as usize] = true;
}
for ch in 'a'..='z' {
ret[ch as usize] = true;
}
for ch in 'A'..='Z' {
ret[ch as usize] = true;
}
for ch in '0'..='9' {
ret[ch as usize] = true;
}
ret
}
fn test_method_names(request: &Request) {
for (m, name) in [
(Method::Get, "GET"),
(Method::Head, "HEAD"),
(Method::Post, "POST"),
(Method::Put, "PUT"),
(Method::Delete, "DELETE"),
(Method::Connect, "CONNECT"),
(Method::Options, "OPTIONS"),
(Method::Trace, "TRACE"),
(Method::Patch, "PATCH"),
] {
assert_eq!(request.set_method(&m), Ok(()));
assert_eq!(request.get_method(), m);
// https://github.com/WebAssembly/wasi-http/issues/194
assert_eq!(request.set_method(&Method::Other(name.to_string())), Ok(()));
assert_eq!(request.get_method(), m);
}
request
.set_method(&Method::Other("coucou".to_string()))
.unwrap();
assert_eq!(request.get_method(), Method::Other("coucou".to_string()));
request
.set_method(&Method::Other("".to_string()))
.unwrap_err();
assert_eq!(request.get_method(), Method::Other("coucou".to_string()));
let max_codepoint_to_test = 1024;
let valid = compute_valid_method_chars(max_codepoint_to_test);
for ch in 0..max_codepoint_to_test {
let method_name = String::from(char::from_u32(ch as u32).unwrap());
if "#$%&'".contains(&method_name) {
// https://github.com/bytecodealliance/wasmtime/issues/11772
continue;
}
let method = Method::Other(method_name);
if valid[ch] {
assert_eq!(request.set_method(&method), Ok(()));
assert_eq!(request.get_method(), method);
} else {
assert_eq!(request.set_method(&method), Err(()));
}
}
}
fn test_schemes(request: &Request) {
for s in [Scheme::Http, Scheme::Https] {
assert_eq!(request.set_scheme(Some(&s)), Ok(()));
assert_eq!(request.get_scheme(), Some(s));
}
// https://github.com/WebAssembly/wasi-http/issues/194
request
.set_scheme(Some(&Scheme::Other("https".to_string())))
.unwrap();
assert_eq!(request.get_scheme(), Some(Scheme::Https));
request
.set_scheme(Some(&Scheme::Other("http".to_string())))
.unwrap();
assert_eq!(request.get_scheme(), Some(Scheme::Http));
// https://github.com/WebAssembly/wasi-http/issues/194
// https://github.com/bytecodealliance/wasmtime/issues/11778#issuecomment-3359677161
// request.set_scheme(Some(&Scheme::Other("HTTPS".to_string())));
// assert_eq!(request.get_scheme(), Some(Scheme::Https));
// request.set_scheme(Some(&Scheme::Other("HTTP".to_string())));
// assert_eq!(request.get_scheme(), Some(Scheme::Http));
let max_codepoint_to_test = 1024;
for ch in 0..max_codepoint_to_test {
let ch = char::from_u32(ch as u32).unwrap();
let scheme = Scheme::Other(String::from(ch));
if "+-.0123456789~".contains(ch) {
// https://github.com/bytecodealliance/wasmtime/issues/11778
continue;
}
if ch.is_ascii_alphabetic() {
assert_eq!(request.set_scheme(Some(&scheme)), Ok(()));
assert_eq!(request.get_scheme(), Some(scheme));
} else {
assert_eq!(request.set_scheme(Some(&scheme)), Err(()));
}
}
}
fn is_valid_path_char(ch: char) -> bool {
// https://www.rfc-editor.org/rfc/rfc3986#section-3.3
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
ch.is_ascii_alphanumeric() || "-._~".contains(ch) // unreserved
|| ch == '%' // pct-encoded
|| "!$&'()*+,;=".contains(ch) // sub-delims
|| ":@".contains(ch)
|| ch as u32 >= 0x80 // Raw UTF-8. It happens!
}
fn test_path_with_query(request: &Request) {
request.set_scheme(Some(&Scheme::Http)).unwrap();
request.set_method(&Method::Get).unwrap();
for abs in ["/", "/a/b/c", "/a/../../bar", "/?foo"] {
request
.set_path_with_query(Some(&abs.to_string()))
.expect(abs);
assert_eq!(request.get_path_with_query(), Some(abs.to_string()));
}
// https://github.com/WebAssembly/wasi-http/issues/178#issuecomment-3359974132
for rel in ["a/b/c", "../..", "?foo"] {
request
.set_path_with_query(Some(&rel.to_string()))
.expect(rel);
assert_eq!(request.get_path_with_query(), Some(rel.to_string()));
}
request.set_path_with_query(Some(&"".to_string())).unwrap();
assert_eq!(request.get_path_with_query(), Some("/".to_string()));
for ch in 0..1024 {
let ch = char::from_u32(ch).unwrap();
if ch != '?' && ch != '/' {
let s = '/'.to_string() + &String::from(ch);
if is_valid_path_char(ch) {
request.set_path_with_query(Some(&s)).unwrap();
assert_eq!(request.get_path_with_query(), Some(s));
} else if "\"{|}^[]\\#".contains(ch) {
// https://github.com/bytecodealliance/wasmtime/issues/11779
continue;
} else if ch as u32 == 0x7F {
// Bonkers; https://github.com/hyperium/http/issues/820.
} else {
request.set_path_with_query(Some(&s)).unwrap_err();
}
}
}
request.set_method(&Method::Options).unwrap();
request.set_path_with_query(Some(&"".to_string())).unwrap();
// https://github.com/bytecodealliance/wasmtime/issues/11780
// assert_eq!(request.get_path_with_query(),
// Some("".to_string()));
}
fn is_valid_authority_char(ch: char) -> bool {
// https://www.rfc-editor.org/rfc/rfc3986#section-3.2.2
// host = IP-literal / IPv4address / reg-name
// reg-name = unreserved / pct-encoded / sub-delims
// IPv4address is a subset of reg-name. IP-literal is IPv6: [...]
ch.is_ascii_alphanumeric() || "-._~".contains(ch) // unreserved
|| ch == '%' // pct-encoded
|| "!$&'()*+,;=".contains(ch) // sub-delims
}
fn test_authority(request: &Request) {
for valid in [
"1.2.3.4",
"example.com",
"localhost",
"1.2.3.4:80",
"example.com:80",
"localhost:80",
"user@1.2.3.4:80",
"user@example.com:80",
"user@localhost:80",
"user:pass@1.2.3.4:80",
"user:pass@example.com:80",
"user:pass@localhost:80",
"user:pass%20@localhost:80",
// https://github.com/WebAssembly/wasi-http/issues/196
// "[2001:db8::1]"
] {
let authority = String::from(valid);
request.set_authority(Some(&authority)).unwrap();
assert_eq!(request.get_authority(), Some(authority));
}
for invalid in ["::", ":@", "@@", "@:", " ", "", "#", "localhost:what"] {
let authority = String::from(invalid);
request.set_authority(Some(&authority)).expect_err(invalid);
}
for ch in 0..1024 {
let ch = char::from_u32(ch).unwrap();
if ch != '[' {
// IP-literal, which we aren't properly testing here.
continue;
} else if ch == '%' {
// We aren't properly testing percent encoding here, and
// % is invalid in a host name otherwise.
continue;
} else {
let authority = String::from(ch);
if is_valid_authority_char(ch) {
request.set_authority(Some(&authority)).unwrap();
assert_eq!(request.get_authority(), Some(authority));
} else {
request.set_authority(Some(&authority)).unwrap_err();
}
}
}
}
fn test_immutable_headers(headers: &Fields) {
assert_eq!(
headers.append("Last-Modified", b"whatever"),
Err(HeaderError::Immutable)
);
}
fn test_headers_same(left: &Fields, right: &Fields) {
assert_eq!(left.copy_all(), right.copy_all());
}
fn test() {
let headers = Fields::new();
headers.append("Last-Modified", b"whatever").unwrap();
let contents = None;
let (_, trailers_rx) = wit_future::new(|| Ok(None));
let options = None;
let headers_copy = headers.clone();
let (request, _sent_future) = Request::new(headers, contents, trailers_rx, options);
test_request_field_default_values(&request);
test_schemes(&request);
test_method_names(&request);
test_path_with_query(&request);
test_authority(&request);
test_immutable_headers(&request.get_headers());
test_headers_same(&request.get_headers(), &headers_copy);
}
struct Component;
export!(Component);
impl Guest for Component {
async fn run() -> Result<(), ()> {
test();
Ok(())
}
}
fn main() {
unreachable!("main is a stub");
}