This repository was archived by the owner on Sep 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi_0_3_proxy.rs
More file actions
69 lines (58 loc) · 2.21 KB
/
api_0_3_proxy.rs
File metadata and controls
69 lines (58 loc) · 2.21 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
use futures::join;
use test_programs::p3::wasi::http::types::{ErrorCode, Headers, Request, Response};
use test_programs::p3::{wit_future, wit_stream};
use wit_bindgen_rt::async_support::spawn;
struct T;
test_programs::p3::proxy::export!(T);
impl test_programs::p3::proxy::exports::wasi::http::handler::Guest for T {
async fn handle(request: Request) -> Result<Response, ErrorCode> {
assert!(request.scheme().is_some());
assert!(request.authority().is_some());
assert!(request.path_with_query().is_some());
// TODO: adapt below
//test_filesystem();
let header = String::from("custom-forbidden-header");
let req_hdrs = request.headers();
assert!(
!req_hdrs.has(&header),
"forbidden `custom-forbidden-header` found in request"
);
assert!(req_hdrs.delete(&header).is_err());
assert!(req_hdrs.append(&header, b"no".as_ref()).is_err());
assert!(
!req_hdrs.has(&header),
"append of forbidden header succeeded"
);
let hdrs = Headers::new();
let (mut contents_tx, contents_rx) = wit_stream::new();
let (trailers_tx, trailers_rx) = wit_future::new();
let (resp, transmit) = Response::new(hdrs, Some(contents_rx), trailers_rx);
spawn(async {
join!(
async {
let remaining = contents_tx.write_all(b"hello, world!".to_vec()).await;
assert!(remaining.is_empty());
drop(contents_tx);
trailers_tx
.write(Ok(None))
.await
.expect("failed to write trailers");
},
async {
transmit
.await
.expect("failed to transmit response")
.unwrap()
}
);
});
Ok(resp)
}
}
// Technically this should not be here for a proxy, but given the current
// framework for tests it's required since this file is built as a `bin`
fn main() {}
// TODO: adapt below
//fn test_filesystem() {
// assert!(std::fs::File::open(".").is_err());
//}