diff --git a/src/filters/fs.rs b/src/filters/fs.rs index fdfa70968..5492bbef7 100644 --- a/src/filters/fs.rs +++ b/src/filters/fs.rs @@ -135,12 +135,19 @@ fn sanitize_path(base: impl AsRef, tail: &str) -> Result, - if_unmodified_since: Option, - if_range: Option, - range: Option, +/// Conditionals that define certain aspects of file serving. These are usually based on the headers of a request, though they can be manually constructed. +#[derive(Debug, Default)] +pub struct Conditionals { + /// Only respond with the file if it has been modified since this date. [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since). + pub if_modified_since: Option, + /// Only respond with the file if it hasn't been modified since this date. [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since). + pub if_unmodified_since: Option, + /// The `range` conditional should only be used if the document has not been modified since the `Last Modified` header + /// or the last ETag creation date. This is typically used to resume downloads after a pause to make sure that the requested + /// resource hasn't been changed since the first partial download. [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Range). + pub if_range: Option, + /// The part of the file that should be responded with. [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range). + pub range: Option, } enum Cond { @@ -198,7 +205,8 @@ impl Conditionals { } } -fn conditionals() -> impl Filter, Error = Infallible> + Copy { +/// A filter for extracting certain headers related to static file serving. +pub fn conditionals() -> impl Filter, Error = Infallible> + Copy { crate::header::optional2() .and(crate::header::optional2()) .and(crate::header::optional2()) @@ -245,9 +253,9 @@ impl File { } } -// Silly wrapper since Arc doesn't implement AsRef ;_; +/// A wrapper for `Arc` that implements `AsRef`. #[derive(Clone, Debug)] -struct ArcPath(Arc); +pub struct ArcPath(pub Arc); impl AsRef for ArcPath { fn as_ref(&self) -> &Path { @@ -261,7 +269,11 @@ impl Reply for File { } } -fn file_reply( +/// An internal helper for serving static files. +/// +/// Usually, you'll want to use this with `warp::fs::file`, but if the file's path is based on something extracted with a filter, +/// you'll need to use this manually. +pub fn file_reply( path: ArcPath, conditionals: Conditionals, ) -> impl Future> + Send {