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

Set the LastModified header for raw files #18356

Merged
merged 12 commits into from
May 9, 2022

Conversation

zeripath
Copy link
Contributor

Although the use of LastModified dates for caching of git objects should be
discouraged (as it is not native to git - and there are a LOT of ways this
could be incorrect) - LastModified dates can be a helpful somewhat more human
way of caching for simple cases.

This PR adds this header and handles the If-Modified-Since header to the /raw/
routes for branch, tag and commit.

Fix #18354

Signed-off-by: Andrew Thornton art27@cantab.net

Although the use of LastModified dates for caching of git objects should be
discouraged (as it is not native to git - and there are a LOT of ways this
could be incorrect) - LastModified dates can be a helpful somewhat more human
way of caching for simple cases.

This PR adds this header and handles the If-Modified-Since header to the /raw/
routes.

Fix go-gitea#18354

Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath added the type/enhancement An improvement of existing functionality label Jan 22, 2022
@zeripath zeripath added this to the 1.17.0 milestone Jan 22, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2022

Codecov Report

❗ No coverage uploaded for pull request base (main@ab7f701). Click here to learn what that means.
The diff coverage is 46.05%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #18356   +/-   ##
=======================================
  Coverage        ?   46.03%           
=======================================
  Files           ?      840           
  Lines           ?    92931           
  Branches        ?        0           
=======================================
  Hits            ?    42779           
  Misses          ?    43354           
  Partials        ?     6798           
Impacted Files Coverage Δ
routers/web/repo/wiki.go 39.55% <0.00%> (ø)
routers/web/repo/download.go 33.70% <38.70%> (ø)
routers/api/v1/repo/file.go 65.21% <45.00%> (ø)
routers/common/repo.go 72.22% <50.00%> (ø)
modules/httpcache/httpcache.go 77.96% <59.09%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ab7f701...ff236a5. Read the comment docs.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 22, 2022
Copy link
Contributor

@Gusted Gusted left a comment

Choose a reason for hiding this comment

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

Hmm, if this is going to cause major problems, we should revert this PR. But from the code it's looking good, it should send a new copy of the file when CommitterDate is changed, which should be sufficient to have good caching.

routers/web/repo/download.go Outdated Show resolved Hide resolved
routers/web/repo/download.go Show resolved Hide resolved
Signed-off-by: Andrew Thornton <art27@cantab.net>
…ader-raw' into fix-18354-set-last-modified-header-raw
@zeripath
Copy link
Contributor Author

It's a very poor cache and could lead to things being cached when they shouldn't be - for example if there has been a force-push.

However if you want to use LastModified instead of the etag then this is the risk you take.

I guess we need to ask what @silverwind thinks - my only worry is that Browsers start sending both and the IfModifiedSince incorrectly detects no change when there is one.

@Gusted
Copy link
Contributor

Gusted commented Jan 23, 2022

my only worry is that Browsers start sending both and the IfModifiedSince incorrectly detects no change when there is one.

What do you mean sending both?

@zeripath
Copy link
Contributor Author

zeripath commented Jan 23, 2022

I mean an HTTP Request sends both If-Modified-Since and If-None-Match.

@zeripath
Copy link
Contributor Author

Actually I guess this just means we should deal with both headers at the same time.

@Gusted
Copy link
Contributor

Gusted commented Jan 23, 2022

I mean an HTTP Request sends both If-Modified-Since and If-None-Match.

When used in combination with If-Modified-Since, If-None-Match has precedence (if the server supports it).

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match

Official ref: https://httpwg.org/specs/rfc7232.html#precedence

@silverwind
Copy link
Member

Browsers by default send none of the headers. If they encounter Etag, they will send If-None-Match on subsequent requests, and if they see Last-Modified, they will send If-Modified-Since, assuming no other directives like no-store prevent caching.

@zeripath
Copy link
Contributor Author

So if we send both Last-Modified and Etag they will send both If-Modified-Since and If-None-Match?

If so this code is likely incorrect and we need to make it so that we only pay attention to If-Modified-Since iff If-None-Match

Signed-off-by: Andrew Thornton <art27@cantab.net>
Copy link
Contributor

@Gusted Gusted left a comment

Choose a reason for hiding this comment

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

LGTM

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jan 25, 2022
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 4, 2022
@wxiaoguang
Copy link
Contributor

How about work on #18448 ? IIRC http.ServeContent can handle these headers correctly.

@6543
Copy link
Member

6543 commented May 9, 2022

🚀

@6543 6543 merged commit 9f5ddca into go-gitea:main May 9, 2022
@zeripath zeripath deleted the fix-18354-set-last-modified-header-raw branch May 9, 2022 19:33
zjjhot added a commit to zjjhot/gitea that referenced this pull request May 10, 2022
* giteaofficial/main:
  Use better message for consistency check (go-gitea#19672)
  Fix new release from tags list UI (go-gitea#19670)
  Update go deps (go-gitea#19665)
  [doctor] Add check/fix for bogus action rows (go-gitea#19656)
  [skip ci] Updated translations via Crowdin
  Add tooltip to pending PR comments (go-gitea#19662)
  Add Webfinger endpoint (go-gitea#19462)
  Update documentation to disable duration settings with -1 instead of 0 (go-gitea#19647)
  Set the LastModified header for raw files (go-gitea#18356)
  Don't select join table's columns (go-gitea#19660)
AbdulrhmnGhanem pushed a commit to kitspace/gitea that referenced this pull request Aug 24, 2022
Although the use of LastModified dates for caching of git objects should be
discouraged (as it is not native to git - and there are a LOT of ways this
could be incorrect) - LastModified dates can be a helpful somewhat more human
way of caching for simple cases.

This PR adds this header and handles the If-Modified-Since header to the /raw/
routes.

Fix go-gitea#18354

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Last-Modified Header equal to the lastcommitdate of a file.
8 participants