Add parameter to disable rewriting.

This commit is contained in:
Kavin 2022-12-02 19:41:28 +00:00
parent 82d5b16d85
commit 71cb04d028
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD

View file

@ -98,6 +98,14 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
let res = query.get("host");
let rewrite = {
if let Some(rewrite) = query.get("rewrite") {
rewrite == "true"
} else {
true
}
};
if res.is_none() {
return Err("No host provided".into());
}
@ -123,8 +131,17 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
return Err("Domain not allowed".into());
}
let qs = {
let qs = query.clone();
let collected = qs.into_pairs()
.into_iter()
.filter(|(key, _)| key != "host" && key != "rewrite")
.collect::<Vec<_>>();
QString::new(collected)
};
let mut url = Url::parse(&format!("https://{}{}", host, req.path()))?;
url.set_query(Some(req.query_string()));
url.set_query(Some(qs.to_string().as_str()));
let mut request = Request::new(
req.method().clone(),
@ -157,6 +174,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
}
}
if rewrite {
if let Some(content_type) = resp.headers().get("content-type") {
if content_type == "image/jpeg" {
let resp_bytes = resp.bytes().await.unwrap();
@ -204,6 +222,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
return Ok(response.body(resp_str));
}
}
}
// Stream response
Ok(response.streaming(resp.bytes_stream()))