Skip to content

Return 304 only when the file is unchanged since If-Modified-Since - #23

Open
webpatser wants to merge 1 commit into
amphp:2.xfrom
webpatser:fix/if-modified-since
Open

webpatser wants to merge 1 commit into
amphp:2.xfrom
webpatser:fix/if-modified-since

Conversation

@webpatser

Copy link
Copy Markdown

Closes #18

DocumentRoot::checkPreconditions() compared the timestamps the wrong way round: it answered 304 Not Modified when the file's mtime was newer than If-Modified-Since, i.e. exactly when the file had changed. Clients with a stale cache got an empty 304 instead of the new content, and clients with a fresh cache got a full 200.

The fix flips the comparison, so a 304 is sent only when mtime <= If-Modified-Since.

While in there I also made If-Modified-Since be ignored whenever If-None-Match is present, as RFC 9110 section 13.1.3 requires. A matching If-None-Match still returns 304 early; a non-matching one no longer falls through to the date check. It is a one-line change, but happy to split it out if you prefer. If-Unmodified-Since is untouched.

Tests (in test/DocumentRootTest.php):

  • If-Modified-Since older than the mtime returns 200 with the body
  • equal to the mtime returns 304
  • newer than the mtime returns 304
  • a non-matching If-None-Match plus a newer If-Modified-Since returns 200

testPreconditionNotModified asserted the buggy behaviour (an If-Modified-Since of 1970 giving 304), so it now sends the file's own mtime instead. The new tests fail on current 2.x and pass with this change; the full suite is green.

Heads up: the repo's CI currently fails at setup because of actions/cache@v2, as noted on #22, so the checks here will not show green for that reason alone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Issue with If-Modified-Since HTTP header

1 participant