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

feat: implements remaining http cache conditionals #407

Merged
merged 2 commits into from
Jun 23, 2024
Merged

feat: implements remaining http cache conditionals #407

merged 2 commits into from
Jun 23, 2024

Conversation

TinyTinni
Copy link
Contributor

@TinyTinni TinyTinni commented Jun 22, 2024

Hi,

this PR fixes/implements some of the HTTP conditionals like "if-modified":

  • if-modified-since: was implemented
  • if-unmodified-since: was not implemented
  • if-none-match + etag: was implemented
  • if-match + etag: was not implemented

This PR implements/fixes those conditionals and also adds some testcases for all 4.

I hope you like it.

For reference, MDN Documentation: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since

edit: clarified "if-modified-since". I misread the code.

@@ -1648,11 +1646,11 @@ async fn zip_dir<W: AsyncWrite + Unpin>(
Ok(())
}

fn extract_cache_headers(meta: &Metadata) -> Option<(Option<ETag>, LastModified)> {
fn extract_cache_headers(meta: &Metadata) -> Option<(ETag, LastModified)> {
Copy link
Owner

Choose a reason for hiding this comment

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

ETag and Last-Modified can be used separately.

We should refactor the extract_cache_headers function as follows:

fn extract_cache_headers(meta: &Metadata) -> (Option<ETag>, Option<LastModified>)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the second commit yesterday, I removed this code change again. There is actually no reason to separate Etag and LastModified as those both are computed by the existing file meta data information.

ETag can fail, if the file's timestamp isn't present. In this case, LastModified fails too.
Etag can also fail, if the .parse method fails when the string is ill-formated [1]. But the string is almost static except when there is no valid timestamp, in this case we fail before. (size doesn't seem to have a failure mode at seems to be guaranteed)

That was my reasoning to revert the change on this function, as it seemed unnecessary.
But it might be safer to have separate checks.

I am unsure which path to take. Going with the rolled back changes or do you want the 2 options and checks if etag could be parsed?

[1] headers crate etag parse: https://doc.rust-lang.org/nightly/core/str/trait.FromStr.html#tymethod.from_str

Copy link
Owner

Choose a reason for hiding this comment

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

fn extract_cache_headers(meta: &Metadata) -> Option<(ETag, LastModified)> is ok.

@sigoden sigoden changed the title Implements remaining http conditionals feat: implements remaining http cache conditionals Jun 23, 2024
@sigoden sigoden merged commit 632f7a4 into sigoden:main Jun 23, 2024
3 checks passed
@TinyTinni TinyTinni deleted the caching-conditions branch June 23, 2024 12:27
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.

2 participants