1 use test_programs::wasi::http::types::{
2     Headers, IncomingRequest, Method, OutgoingBody, OutgoingResponse, ResponseOutparam,
3 };
4 
5 struct T;
6 
7 test_programs::proxy::export!(T);
8 
9 impl test_programs::proxy::exports::wasi::http::incoming_handler::Guest for T {
10     fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
11         assert!(request.scheme().is_some());
12         assert!(request.authority().is_some());
13         assert!(request.path_with_query().is_some());
14 
15         test_filesystem();
16 
17         match (request.method(), request.path_with_query().as_deref()) {
18             (Method::Get, Some("/early_drop")) => {
19                 // Ignore all the errors for this endpoint.
20                 let resp = OutgoingResponse::new(Headers::new());
21                 let body = resp.body().expect("outgoing response");
22                 ResponseOutparam::set(outparam, Ok(resp));
23                 let _ = body.write().and_then(|out| {
24                     let _ = out.blocking_write_and_flush(b"hello, world!");
25                     drop(out);
26                     Ok(())
27                 });
28                 let _ = OutgoingBody::finish(body, None);
29 
30                 return;
31             }
32 
33             _ => {}
34         }
35 
36         let header = String::from("custom-forbidden-header");
37         let req_hdrs = request.headers();
38 
39         assert!(
40             !req_hdrs.has(&header),
41             "forbidden `custom-forbidden-header` found in request"
42         );
43 
44         assert!(req_hdrs.delete(&header).is_err());
45         assert!(req_hdrs.append(&header, b"no".as_ref()).is_err());
46 
47         assert!(
48             !req_hdrs.has(&header),
49             "append of forbidden header succeeded"
50         );
51 
52         assert!(
53             !req_hdrs.has("host"),
54             "forbidden host header present in incoming request"
55         );
56 
57         let hdrs = Headers::new();
58         let resp = OutgoingResponse::new(hdrs);
59         let body = resp.body().expect("outgoing response");
60 
61         ResponseOutparam::set(outparam, Ok(resp));
62 
63         let out = body.write().expect("outgoing stream");
64         out.blocking_write_and_flush(b"hello, world!")
65             .expect("writing response");
66 
67         drop(out);
68         OutgoingBody::finish(body, None).expect("outgoing-body.finish");
69     }
70 }
71 
72 // Technically this should not be here for a proxy, but given the current
73 // framework for tests it's required since this file is built as a `bin`
74 fn main() {}
75 
76 fn test_filesystem() {
77     assert!(std::fs::File::open(".").is_err());
78 }
79