-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhttp_server_proxy.rs
More file actions
67 lines (60 loc) · 2.47 KB
/
Copy pathhttp_server_proxy.rs
File metadata and controls
67 lines (60 loc) · 2.47 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
#![cfg_attr(not(all(target_os = "wasi", target_env = "p2")), no_main)]
#![cfg(all(target_os = "wasi", target_env = "p2"))]
//! Run the example with:
//! ```sh
//! cargo build --example http_server_proxy --target=wasm32-wasip2
//! wasmtime serve -Scli -Shttp --env TARGET_URL=https://example.com/ target/wasm32-wasip2/debug/examples/http_server_proxy.wasm
//! curl --no-buffer -v 127.0.0.1:8080/proxy/
//! ```
use wstd::http::body::Body;
use wstd::http::{Client, Error, Request, Response, StatusCode, Uri};
const PROXY_PREFIX: &str = "/proxy/";
#[wstd::http_server]
async fn main(server_req: Request<Body>) -> Result<Response<Body>, Error> {
match server_req.uri().path_and_query().unwrap().as_str() {
api_prefixed_path if api_prefixed_path.starts_with(PROXY_PREFIX) => {
// Remove PROXY_PREFIX
let target_url =
std::env::var("TARGET_URL").expect("missing environment variable TARGET_URL");
let target_url: Uri = format!(
"{target_url}{}",
api_prefixed_path
.strip_prefix(PROXY_PREFIX)
.expect("checked above")
)
.parse()
.expect("final target url should be parseable");
println!("Proxying to {target_url}");
proxy(server_req, target_url).await
}
_ => Ok(http_not_found(server_req)),
}
}
async fn proxy(server_req: Request<Body>, target_url: Uri) -> Result<Response<Body>, Error> {
let client = Client::new();
let mut client_req = Request::builder();
client_req = client_req.uri(target_url).method(server_req.method());
// Copy headers from `server_req` to the `client_req`.
for (key, value) in server_req.headers() {
client_req = client_req.header(key, value);
}
// Stream the request body.
let client_req = client_req.body(server_req.into_body())?;
// Send the request.
let client_resp = client.send(client_req).await?;
// Copy headers from `client_resp` to `server_resp`.
let mut server_resp = Response::builder();
for (key, value) in client_resp.headers() {
server_resp
.headers_mut()
.expect("no errors could be in ResponseBuilder")
.append(key, value.clone());
}
Ok(server_resp.body(client_resp.into_body())?)
}
fn http_not_found(_request: Request<Body>) -> Response<Body> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::empty())
.unwrap()
}