Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ServeDir::fallback #243

Merged
merged 12 commits into from Apr 24, 2022
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