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

fileserver: Read Etags from precomputed files #6222

Merged
merged 1 commit into from
Apr 13, 2024

Conversation

armadi1809
Copy link
Contributor

Initial pass at closing #5734

@armadi1809 armadi1809 changed the title Implemented reading static files' etags from precomputed files File Server: read etags from precomputed files Apr 5, 2024
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks for the great enhancement! This looks pretty good but I just have a few comments.

modules/caddyhttp/fileserver/staticfiles.go Outdated Show resolved Hide resolved
modules/caddyhttp/fileserver/staticfiles.go Outdated Show resolved Hide resolved
modules/caddyhttp/fileserver/staticfiles.go Outdated Show resolved Hide resolved
@francislavoie francislavoie changed the title File Server: read etags from precomputed files fileserver: Read Etags from precomputed files Apr 11, 2024
@francislavoie francislavoie added the feature ⚙️ New feature or request label Apr 11, 2024
@francislavoie francislavoie added this to the v2.8.0 milestone Apr 11, 2024
@armadi1809
Copy link
Contributor Author

Thanks for taking a look at this Matt. All the feedback discussions should now be resolved.

@mholt mholt merged commit 567d96c into caddyserver:master Apr 13, 2024
25 checks passed
@mholt
Copy link
Member

mholt commented Apr 13, 2024

Awesome, thanks for working on this!

@hanikesn
Copy link

Nice! I wish there were docs for this. I stumbled upon this while looking in the source code to check why my custom etag headers weren't used to serve 304s.

@hanikesn
Copy link

hanikesn commented Jun 10, 2024

This only seems to work for precompressed files though, any particular reason for that? I'd be quite useful for this to work on any files. Sorry for the noise I misread the code and had another issue.

@mholt
Copy link
Member

mholt commented Jun 10, 2024

Docs are being updated with the new features shortly :)

@hanikesn
Copy link

hanikesn commented Jun 10, 2024

Docs are being updated with the new features shortly :)

Thanks, right now this is quite easy to misuse. I.e. the etag files must not have newlines and must include quotes "". Otherwise http.serveContent won't send 304s for requests including If-None-Match requests.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match is specific for that as well.

Here's a short cmd to generate them in the correct format:

find . -type f -not -name "*.sha256" -exec sh -c 'sha256sum "$0" | cut -d " " -f 1 | tr -d "\n" | xargs -0 printf "\"%s\"" > "$0.sha256"' '{}' \;

I'm wondering whether this should create a warning in the logs or Caddy can just do the "right thing" and trim and quote the value.

@mholt
Copy link
Member

mholt commented Jun 10, 2024

If the etag is weak, though, the quotes won't be around the whole thing.

We could probably trim newlines if that's a common occurrence, but it's not something I've encountered in my (limited) experience.

@hanikesn
Copy link

We could probably trim newlines if that's a common occurrence, but it's not something I've encountered in my (limited) experience.

As long as it ends up in the docs I'm happy 🙂 It's easy to waste time with this digging into the source code of http.serveContent(), but also pre-computing etags is already a quite advanced use case. With reproducible builds on the advance though it'll become more common.

@mholt
Copy link
Member

mholt commented Jun 10, 2024

I'd welcome a PR at least for the newline patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants