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(pgs): initial support for conditional requests #178

Merged
merged 4 commits into from
Dec 31, 2024

Conversation

mac-chaffee
Copy link
Contributor

@mac-chaffee mac-chaffee commented Dec 21, 2024

This PR provides initial support for conditional requests.

I copied the important RFC-compliant bits from https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/net/http/fs.go and used that to parse and set the right headers and status codes for conditional requests.

The alternative would be using http.ServeContent, but that requires an io.ReadSeeker which wouldn't work with files obtained from imgproxy since that returns an io.ReadCloser.

Ref: #177

@mac-chaffee mac-chaffee marked this pull request as ready for review December 21, 2024 15:31
@mac-chaffee
Copy link
Contributor Author

To test, follow the new instructions in dev.md to sign up and upload a file. Then:

# This example hits pgs/web directly since the file isn't cached

$ curl -iH "Host: picouser-test.pgs.dev.pico.sh" -H "If-Modified-Since: Sat, 21 Dec 2026 03:03:03 GMT" -H 'If-None-Match: "a9a83095955b3d3efacc7f833a103cb2-1"'  localhost:3000/file.txt
HTTP/1.1 304 Not Modified
Cache-Control: max-age=600
Cache-Status: Souin; fwd=uri-miss; key=GET-http-picouser-test.pgs.dev.pico.sh-/file.txt; detail=UNCACHEABLE-STATUS-CODE
Etag: "a9a83095955b3d3efacc7f833a103cb2-1"
Surrogate-Key: picouser-test
Date: Sat, 21 Dec 2024 15:33:53 GMT

# Cache the file
$ curl -iH "Host: picouser-test.pgs.dev.pico.sh"  localhost:3000/file.txt
HTTP/1.1 200 OK
Cache-Control: max-age=600
Cache-Status: Souin; fwd=uri-miss; stored; key=GET-http-picouser-test.pgs.dev.pico.sh-/file.txt
Content-Length: 14
Content-Type: text/plain
Etag: "a9a83095955b3d3efacc7f833a103cb2-1"
Last-Modified: Sat, 21 Dec 2024 03:03:10 GMT
Surrogate-Key: picouser-test
Date: Sat, 21 Dec 2024 15:34:31 GMT

Hello, World!

# Now test conditional requests coming from souin
$ curl -iH "Host: picouser-test.pgs.dev.pico.sh" -H "If-Modified-Since: Sat, 21 Dec 2026 03:03:03 GMT" -H 'If-None-Match: "a9a83095955b3d3efacc7f833a103cb2-1"'  localhost:3000/file.txt
HTTP/1.1 304 Not Modified
Age: 63
Cache-Control: max-age=600
Cache-Status: Souin; hit; ttl=537; key=GET-http-picouser-test.pgs.dev.pico.sh-/file.txt; detail=OTTER
Date: Sat, 21 Dec 2024 15:34:31 GMT
Etag: "a9a83095955b3d3efacc7f833a103cb2-1"
Last-Modified: Sat, 21 Dec 2024 03:03:10 GMT
Surrogate-Key: picouser-test

@neurosnap
Copy link
Member

neurosnap commented Dec 30, 2024

Thanks so much for your contribution! I think we might want to take a stab at getting http.ServeContent to work with pobj. I know this can get tricky so I can try to see how feasible it is.

I'd really like align pobj and pgs with what golang's stdlib expects.

Thoughts?

@neurosnap
Copy link
Member

Alright, I quickly looked into it, I think we should just continue with your approach because converting io.ReadCloser to io.ReadSeeker might be prohibitive.

@mac-chaffee
Copy link
Contributor Author

mac-chaffee commented Dec 30, 2024

Right if you update pobj to always return a ReadSeeker, we still couldn't use http.ServeContent in pgs because the file returned from imgproxy doesn't come through pobj; it comes from http.Get.

Copy link
Member

@neurosnap neurosnap left a comment

Choose a reason for hiding this comment

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

Overall looks good since most of the changes are from fs.go.

How hard would it be to add a test to web_test.go that ensures things are working as intended?

pgs/fs.go Outdated Show resolved Hide resolved
@mac-chaffee
Copy link
Contributor Author

Sure I can work on a test, thanks for reviewing!

@mac-chaffee
Copy link
Contributor Author

Tests added!

Copy link
Member

@neurosnap neurosnap left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@neurosnap neurosnap merged commit b3098e1 into picosh:main Dec 31, 2024
1 check passed
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