Skip to content

Commit 0a8a741

Browse files
committed
Send content types for some content
Fixes #784
1 parent 0dcdfff commit 0a8a741

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

site/src/server.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,18 @@ async fn serve_req(ctx: Arc<Server>, req: Request) -> Result<Response, ServerErr
15251525

15261526
if Path::new(&fs_path).is_file() {
15271527
let source = fs::read(&fs_path).unwrap();
1528-
return Ok(Response::new(hyper::Body::from(source)));
1528+
let mut response = http::Response::builder();
1529+
let p = Path::new(&fs_path);
1530+
match p.extension().and_then(|x| x.to_str()) {
1531+
Some("html") => response = response.header_typed(ContentType::html()),
1532+
Some("png") => response = response.header_typed(ContentType::png()),
1533+
Some("json") => response = response.header_typed(ContentType::json()),
1534+
Some("svg") => response = response.header("Content-Type", "image/svg+xml"),
1535+
Some("css") => response = response.header("Content-Type", "text/css"),
1536+
Some("js") => response = response.header("Content-Type", "application/javascript"),
1537+
_ => (),
1538+
}
1539+
return Ok(response.body(hyper::Body::from(source)).unwrap());
15291540
}
15301541

15311542
match req.uri().path() {

0 commit comments

Comments
 (0)