domani/src/service/http/proxy.rs

37 lines
1.0 KiB
Rust
Raw Normal View History

use crate::error::unexpected::{self};
use crate::service;
use http::header::HeaderValue;
use std::net;
pub async fn serve_http_request(
config: &service::http::ConfigProxiedDomain,
client_ip: net::IpAddr,
mut req: hyper::Request<hyper::Body>,
req_is_https: bool,
) -> unexpected::Result<hyper::Response<hyper::Body>> {
for (name, value) in config.request_headers.as_ref() {
if value == "" {
req.headers_mut().remove(name);
continue;
}
req.headers_mut().insert(name, value.clone());
}
if req_is_https {
req.headers_mut()
.insert("x-forwarded-proto", HeaderValue::from_static("https"));
}
let url = config.url.as_ref();
match hyper_reverse_proxy::call(client_ip, url, req).await {
Ok(res) => Ok(res),
// ProxyError doesn't actually implement Error :facepalm: so we have to format the error
// manually
Err(e) => Err(unexpected::Error::from(
format!("error while proxying to {url}: {e:?}").as_str(),
)),
}
}