-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03796be
refactor: made file serving helpers public
arctic-hen7 317f74e
refactor: addressed 2/3 requested changes
arctic-hen7 2bb506f
Merge branch 'seanmonstar:master' into master
arctic-hen7 2590cd1
docs: updated docs for conditionals
arctic-hen7 203a960
Merge branch 'seanmonstar:master' into master
arctic-hen7 122589e
Merge branch 'seanmonstar:master' into master
arctic-hen7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>, | ||
/// 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 { | ||
|
@@ -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()) | ||
|
@@ -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>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.