Skip to content

Make file serving helpers public #924

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

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/filters/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,19 @@ fn sanitize_path(base: impl AsRef<Path>, tail: &str) -> Result<PathBuf, Rejectio
Ok(buf)
}

#[derive(Debug)]
struct Conditionals {
if_modified_since: Option<IfModifiedSince>,
if_unmodified_since: Option<IfUnmodifiedSince>,
if_range: Option<IfRange>,
range: Option<Range>,
/// 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<IfModifiedSince>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the fields private. That way we can add more, or change them. We also haven't exposed the headers crate publicly.

/// 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<IfUnmodifiedSince>,
/// 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<IfRange>,
/// 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<Range>,
}

enum Cond {
Expand Down Expand Up @@ -198,7 +205,8 @@ impl Conditionals {
}
}

fn conditionals() -> impl Filter<Extract = One<Conditionals>, Error = Infallible> + Copy {
/// A filter for extracting certain headers related to static file serving.
pub fn conditionals() -> impl Filter<Extract = One<Conditionals>, Error = Infallible> + Copy {
crate::header::optional2()
.and(crate::header::optional2())
.and(crate::header::optional2())
Expand Down Expand Up @@ -245,9 +253,9 @@ impl File {
}
}

// Silly wrapper since Arc<PathBuf> doesn't implement AsRef<Path> ;_;
/// A wrapper for `Arc<PathBuf>` that implements `AsRef<Path>`.
#[derive(Clone, Debug)]
struct ArcPath(Arc<PathBuf>);
pub struct ArcPath(pub Arc<PathBuf>);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this private.


impl AsRef<Path> for ArcPath {
fn as_ref(&self) -> &Path {
Expand All @@ -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(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also kept private.

path: ArcPath,
conditionals: Conditionals,
) -> impl Future<Output = Result<File, Rejection>> + Send {
Expand Down