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

Supporting Last-Modified Header for StaticFiles #1219

Closed
wants to merge 1 commit into from

Conversation

schrieveslaach
Copy link
Contributor

@schrieveslaach schrieveslaach commented Feb 1, 2020

This commit adds basic support of Last-Modified and If-Modified-Since headers in order to enable HTTP caching while serving static files. #95

Copy link
Collaborator

@jebrosen jebrosen left a comment

Choose a reason for hiding this comment

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

I am generally in favor of this proposal, and I agree that it makes sense to add it as an opt-in to NamedFile instead of unconditionally to File.

contrib/lib/src/serve.rs Outdated Show resolved Hide resolved
contrib/lib/src/serve.rs Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
@schrieveslaach
Copy link
Contributor Author

@jebrosen, I addressed your comments and update the PR.

contrib/lib/src/serve.rs Outdated Show resolved Hide resolved
core/lib/Cargo.toml Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
@SergioBenitez
Copy link
Member

Besides needed a rebase, what is the status of this?

@schrieveslaach
Copy link
Contributor Author

schrieveslaach commented Nov 1, 2020

@SergioBenitez, the PR was stuck due to incompatible APIs (see comments above). I updated the PR and pushed a new version that use the headers crate (suggested by @jebrosen).

Copy link
Collaborator

@jebrosen jebrosen left a comment

Choose a reason for hiding this comment

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

Overall LGTM; I requested a few minor changes. The encoding/decoding is a little bit wonky, but I think that would best be resolved by implementing #1067 with the headers crate.

core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@jebrosen jebrosen left a comment

Choose a reason for hiding this comment

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

This looks great to me, with a small expansion to the docs.

I think this PR already satisfies my concerns from #1376 (review) by being opt-in via a new constructor. So my vote is for this PR, which also includes tests already.

contrib/lib/src/serve.rs Outdated Show resolved Hide resolved
core/lib/src/response/named_file.rs Show resolved Hide resolved
@schrieveslaach
Copy link
Contributor Author

@SergioBenitez, did you have the chance to review this pull request?

@schrieveslaach
Copy link
Contributor Author

@SergioBenitez, @jebrosen do you have any idea why the build “Linux Debug (nightly)” after I rebased this PR? The error message seems unrelated to the changes in this PR.

@SergioBenitez
Copy link
Member

Likely a change in how diagnostics are rendered. I'll take a close look now.

@SergioBenitez
Copy link
Member

SergioBenitez commented May 18, 2021

The unit tests should now pass.

This commit adds basic support of Last-Modified and If-Modified-Since headers
in order to enable HTTP caching while serving static files.
@schrieveslaach
Copy link
Contributor Author

@SergioBenitez, thanks. It works again.

@@ -133,6 +133,12 @@ impl Options {
/// that would be served is a directory.
pub const NormalizeDirs: Options = Options(0b0100);

/// `Options` enabling caching based on the `Last-Modified` header. When
/// this is enabled, the [`StaticFiles`] handler will at the `Last-Modified`
Copy link

Choose a reason for hiding this comment

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

Suggested change
/// this is enabled, the [`StaticFiles`] handler will at the `Last-Modified`
/// this is enabled, the [`StaticFiles`] handler will add the `Last-Modified`

@@ -133,6 +133,12 @@ impl Options {
/// that would be served is a directory.
pub const NormalizeDirs: Options = Options(0b0100);

/// `Options` enabling caching based on the `Last-Modified` header. When
/// this is enabled, the [`StaticFiles`] handler will at the `Last-Modified`
/// header baesd on the modification datetime of the files in the served
Copy link

Choose a reason for hiding this comment

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

Suggested change
/// header baesd on the modification datetime of the files in the served
/// header based on the modification datetime of the files in the served

@SergioBenitez
Copy link
Member

I don't think the API or implementation here are ones that I'm in favor of. Specifically:

  • This API does not leave room for growth into headers which are not simply flags, that is, which require a value like Cache-Control.
  • This API reuses Options from StaticFiles for NamedFile when only one bitfield actually applies.
  • This implementation introduces a dependency on header for two types.
  • The constructor approach (with_last_modified_date()) results in lengthy invocations.
  • The Responder impl indexes into a vector without checking it contains a value and then unwrap()s. This can trivially lead to panics.
  • There are a many vec![], to_string() allocations. This functionality should require none.

Before revisting an implementation for this and #95 in general, I would like to agree on an API design and implementation strategy that has none of these drawbacks. Until then, I am closing this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants