Browse Source

Create proper header for normal fetch

main
Johannes Wünsche 1 year ago
parent
commit
4e9c42f2ef
Signed by: johannes
GPG Key ID: 2511CF80B7C283AB
  1. 29
      src/main.rs

29
src/main.rs

@ -199,6 +199,21 @@ async fn main() -> std::io::Result<()> {
Ok(())
}
fn build_response(bytes: &Bytes, path: &str) -> Response<Body> {
let mut rsp = Response::builder()
// Never thought I had to add headers so people don't get spied on (https://plausible.io/blog/google-floc)
.header("Permissions-Policy", "interest-cohort=()");
let guessed_type = mime_guess::MimeGuess::from_path(path);
info!("Fetching: {}", path);
if let Some(mime) = guessed_type.first() {
info!("Guessed type {} for {}", mime, path );
rsp = rsp.header(hyper::header::CONTENT_TYPE, format!("{}", mime));
}
return rsp.body(Body::from(bytes.clone())).unwrap();
}
async fn serve_file(
init: &Initialization,
req: Request<Body>,
@ -211,7 +226,7 @@ async fn serve_file(
req.uri()
);
if let Some(bytes) = init.get_file(path).await {
return Ok(Response::new(Body::from(bytes.clone())));
return Ok(build_response(bytes, path));
} else if init.mode == Serving::Web {
if let Some(bytes) = {
let sub = if path.len() > 0 {
@ -221,17 +236,7 @@ async fn serve_file(
};
init.get_file(&format!("{}{}", path, sub)).await
} {
let mut rsp = Response::builder()
// Never thought I had to add headers so people don't get spied on (https://plausible.io/blog/google-floc)
.header("Permissions-Policy", "interest-cohort=()");
let guessed_type = mime_guess::MimeGuess::from_path(path);
if let Some(mime) = guessed_type.first() {
rsp = rsp.header(hyper::header::CONTENT_TYPE, format!("{}", mime));
}
return Ok(rsp.body(Body::from(bytes.clone())).unwrap());
return Ok(build_response(bytes, path));
}
}

Loading…
Cancel
Save