Skip to content

Commit

Permalink
Add ServeDir::{fallback, not_found_service}‘ (#243)
Browse files Browse the repository at this point in the history
* Fix clippy lints

* Add `ServeDir::fallback`

* bodies don't need `+ Sync`

* remove unused imports

* update changelog

* Add `not_found_service`

* remove todo is isn't very important

* fix wrong docs link

* Apply suggestions from code review

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

* remove `unreachable!`

* review feedback

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
  • Loading branch information
davidpdrsn and jplatte committed Apr 24, 2022
1 parent fd13e2a commit 1737566
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 147 deletions.
2 changes: 2 additions & 0 deletions tower-http/CHANGELOG.md
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Added

- Add `ServeDir::{fallback, not_found_service}` for calling another service if
the file cannot be found.
- Add `SetStatus` to override status codes.
- **cors**: Added `CorsLayer::very_permissive` which is like
`CorsLayer::permissive` except it (truly) allows credentials. This is made
Expand Down
2 changes: 1 addition & 1 deletion tower-http/Cargo.toml
Expand Up @@ -79,7 +79,7 @@ auth = ["base64"]
catch-panic = ["tracing", "futures-util/std"]
cors = []
follow-redirect = ["iri-string", "tower/util"]
fs = ["tokio/fs", "tokio-util/io", "tokio/io-util", "mime_guess", "mime", "percent-encoding", "httpdate"]
fs = ["tokio/fs", "tokio-util/io", "tokio/io-util", "mime_guess", "mime", "percent-encoding", "httpdate", "set-status"]
map-request-body = []
map-response-body = []
metrics = ["tokio/time"]
Expand Down
21 changes: 3 additions & 18 deletions tower-http/src/services/fs/mod.rs
Expand Up @@ -2,8 +2,8 @@

use bytes::Bytes;
use futures_util::Stream;
use http::{HeaderMap, Response, StatusCode};
use http_body::{combinators::BoxBody, Body, Empty};
use http::{HeaderMap, StatusCode};
use http_body::Body;
use httpdate::HttpDate;
use pin_project_lite::pin_project;
use std::fs::Metadata;
Expand All @@ -28,6 +28,7 @@ use crate::content_encoding::{Encoding, QValue, SupportedEncodings};

pub use self::{
serve_dir::{
DefaultServeDirFallback,
// The response body and future are used for both ServeDir and ServeFile
ResponseBody as ServeFileSystemResponseBody,
ResponseFuture as ServeFileSystemResponseFuture,
Expand Down Expand Up @@ -189,22 +190,6 @@ where
}
}

fn response_from_io_error(
err: io::Error,
) -> Result<Response<BoxBody<Bytes, io::Error>>, io::Error> {
match err.kind() {
io::ErrorKind::NotFound | io::ErrorKind::PermissionDenied => {
let res = Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Empty::new().map_err(|err| match err {}).boxed())
.unwrap();

Ok(res)
}
_ => Err(err),
}
}

struct LastModified(HttpDate);

impl From<SystemTime> for LastModified {
Expand Down

0 comments on commit 1737566

Please sign in to comment.