From 892d48b3f3a508d6f133af039b63b62f966b51c3 Mon Sep 17 00:00:00 2001 From: silvioprog Date: Sun, 24 Jan 2021 20:19:24 -0300 Subject: [PATCH 1/2] Add simple reverse proxy example I found this example useful in understanding how a tide-based server can handle reverse proxy. --- examples/proxy.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 examples/proxy.rs diff --git a/examples/proxy.rs b/examples/proxy.rs new file mode 100644 index 000000000..23e57f5ae --- /dev/null +++ b/examples/proxy.rs @@ -0,0 +1,28 @@ +// Example: HTTP GET to http://localhost:8080/http-rs/tide +// $ curl "http://localhost:8080/http-rs/tide" +// I'll show the Tide page at GitHub +#[async_std::main] +async fn main() -> Result<(), std::io::Error> { + tide::log::start(); + let mut app = tide::new(); + app.at("*").get(|req: tide::Request<()>| async move { + let url = "https://github.com"; // Change to your reverse proxy URL + let mut req_builder = surf::get(format!("{}{}", url, req.url().path())); + for (n, v) in req.iter().filter(|(n, _)| *n != "host") { + let v: String = v.iter().map(|s| s.as_str()).collect(); + req_builder = req_builder.header(n, v); + } + let mut proxy_res = req_builder.send().await?; + let mut res = tide::http::Response::new(proxy_res.status()); + proxy_res.iter().for_each(|(n, v)| { + res.append_header(n, v); + }); + if let Some(mime) = proxy_res.content_type() { + res.set_content_type(mime); + } + res.set_body(proxy_res.take_body()); + Ok(res) + }); + app.listen("127.0.0.1:8080").await?; + Ok(()) +} From 503cfed27421dc1db8723bd5014c386f3f19616f Mon Sep 17 00:00:00 2001 From: silvioprog Date: Thu, 4 Feb 2021 21:44:57 -0300 Subject: [PATCH 2/2] Add warning regarding security to the reverse proxy example --- examples/proxy.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/proxy.rs b/examples/proxy.rs index 23e57f5ae..e21a0c9c0 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -1,3 +1,6 @@ +// WARNING: Don't not use it in production! +// This is just an example, so there is much more to a correct secure reverse proxy implementation. +// // Example: HTTP GET to http://localhost:8080/http-rs/tide // $ curl "http://localhost:8080/http-rs/tide" // I'll show the Tide page at GitHub