Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/salvo-rs/salvo
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jul 29, 2023
2 parents 2c8615f + fe605a1 commit 040d8ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions crates/core/src/writing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ where
}
}

#[async_trait]
impl<P> Writer for Option<P>
where
P: Piece + Sized + Send,
{
#[inline]
async fn write(mut self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) {
match self {
Some(v) => v.render(res),
None => {
res.status_code(StatusCode::NOT_FOUND);
}
}
}
}

#[async_trait]
impl<T, E> Writer for Result<T, E>
where
Expand Down
5 changes: 3 additions & 2 deletions crates/serve-static/src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ fn render_embedded_data(
res: &mut Response,
mime: Option<Mime>,
) {
let mime = mime.unwrap_or_else(|| mime_guess::from_path(req.uri().path()).first_or_octet_stream());
res.headers_mut().insert(CONTENT_TYPE, mime.as_ref().parse().unwrap());

let hash = hex::encode(metadata.sha256_hash());
// if etag is matched, return 304
if req
Expand All @@ -71,8 +74,6 @@ fn render_embedded_data(
// otherwise, return 200 with etag hash
res.headers_mut().insert(ETAG, hash.parse().unwrap());

let mime = mime.unwrap_or_else(|| mime_guess::from_path(req.uri().path()).first_or_octet_stream());
res.headers_mut().insert(CONTENT_TYPE, mime.as_ref().parse().unwrap());
match data {
Cow::Borrowed(data) => {
res.write_body(data).ok();
Expand Down
2 changes: 2 additions & 0 deletions examples/static-embed-file/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ async fn serve_file(req: &mut Request, res: &mut Response) {
let path = req.param::<String>("**path").unwrap();
if let Some(file) = Assets::get(&path) {
file.render(req, res);
} else {
res.status_code(StatusCode::NOT_FOUND);
}
}

0 comments on commit 040d8ef

Please sign in to comment.