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
io/fs, net/http: define interface for automatic ETag serving #60940
Comments
I have a hacky implementation available here: |
Thinking out loud (sorry for the noise), it seems even better to add an optional method to
Updated proposal:First, in io/fs, define // A FileHashesInfo provides the file hashes in constant time.
type FileHashesInfo interface {
fs.FileInfo
// FileHashes returns content hashes of the file that uniquely
// identifies the file contents.
// The returned hashes should be of the form algorithm-base64,
// and implementations are encouraged to use sha256, sha384, or sha512
// as the algorithms and RawStdEncoding as base64 encoding,
// for interoperability with other systems.
//
// FileHashes must NOT compute any hash of the file during the call.
// That is, it must run in time O(1) not O(length of file).
// If no content hash is already available, FileHashes should
// return nil rather than take the time to compute one.
FileHashes() []string
} Second, in net/http, when serving a File (in func setEtag(w ResponseWriter, fi fs.FileInfo) {
if ch, ok := fi.(FileHashesInfo); ok {
if w.Header().Get("Etag") != "" {
return
}
for _, h := range ch.FileHashes() {
// TODO: skip the hash if unsuitable (define "suitable")
// TODO: should the etag be weak or strong?
w.Header().Set("Etag", `W/"`+h+`"`)
break
}
}
} Third (probably out of scope for this proposal), add the |
This proposal has been added to the active column of the proposals project |
To summarize the proposal above:
I think we can probably improve on a few of these decisions.
|
Thanks for the feedback!
Logically, I would put the Trying to be more concrete, I was able to find 3 implementations of fs.FS in the stdlib:
So the first case dos not really influence the decision. For cases outside of the stdlib, I looked up S3 implementations and found that the hashes were returned when GETting the file or requesting the HEAD (so adding it to the FileInfo would mirror both ways of accessing the hashes, while attaching to
I would drop the
I really like your
My example code only sets the ETAg once. I think this should be sufficient. However to work fine, the implementer should:
PS: do you think that dropping a comment in the previous proposal would be a good idea, to gather more feedback? |
Updated proposal, taking into accounts the comments above: // ContentHashesInfo provides pre-computed hashes of the file contents.
type ContentHashesInfo interface {
FileInfo
// ContentHashes returns pre-computed hashes of the file contents.
//
// ContentHashes must NOT compute any hash of the file during the call.
// That is, it must run in time O(1) not O(length of file).
// If no content hash is already available, ContentHashes should
// return nil rather than take the time to compute one.
//
// The order of the returned hash must be stable.
ContentHashes() []Hash
}
// Hash indicates the hash of a given content.
type Hash struct {
// Algorithm indicates the algorithm used. Implementations are encouraged
// to use package-like name for interoperability with other systems
// (lowercase, without dash: e.g. sha256, sha1, crc32)
Algorithm string
// Sum is the result of the hash, it should not be modified.
Sum []byte
} I have created a new |
I'm still on the fence about FileInfo vs File, but I'm willing to try FileInfo and see how it goes. It seems like we are at:
The remaining question in my reply above is (4), namely what does HTTP do when Hash returns multiple hashes? As far as I can tell it makes no sense to send back multiple ETag headers. |
I would suggest to use the first suitable hash. For instance taking the first one with at least 32 bits (and truncating it to 512 bits): if w.Header().Get("Etag") != "" {
return
}
const minLen, maxLen = 4, 64
for _, h := range ch.ContentHashes() {
buf := h.Sum
if len(buf) < minLen {
// hash should have at least 32 bits
continue
}
if len(buf) > maxLen {
buf = buf[:maxLen]
}
// Strong etag: any encoding middleware should set it to weak.
w.Header().Set("Etag", `"`+base64.RawStdEncoding.EncodeToString(buf)+`"`)
break
} |
Nit: Hash() returns more than one Hash. Hashes()? |
It seems fine for Hash to return []Hash. It doesn't have to be Hashes. Have all remaining concerns about this proposal been addressed? |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 The proposal details are as follows. In io/fs, we add:
Then, in net/http.serveFile, serveFile calls Stat, and if the result implements HashFileInfo, it calls info.Hash. If that returns >=1 hashes, serveFile uses hash[0] as the Etag header, formatting it using Alg+":"+base64(Sum). In package embed, the file type would add a Hash method and an assertion that it implements HashFileInfo. It would return a single hash with Algorithm “sha256”. |
@oliverpool If you're interested in working on this, feel free to send a patch |
Renewal of #43223
Here is a proposal which tries to address the concerns raised in #43223.
Updated proposal (fs.FileInfo)
First, in io/fs, define
Second, in net/http, when serving a File (in
serveFile
, right beforeserveContent
for instance), if its FileInfo implements FileHashesInfo and the FileHashes method succeeds and is alphanumeric (no spaces, no Unicode, no symbols, to avoid any kind of header problems), use that result as the default ETag.Third (probably out of scope for this proposal), add the
FileHashes
method onembed.FS.*file
(which implements theFileInfo
interface).This proposal fixes the following objections:
The caller will simply get all available implementations and can filter them out.
The implementers are encouraged to indicate the algorithm used for each hash.
This one does.
This implementation cannot return an error (the implementer choose to panic. Returning
nil
seems better suited).It is currently very cumbersome, since the middleware would need to open the file as well (which means having the exact same logic regarding URL cleanup as the http.FileServer). Here is my attempt: https://git.sr.ht/~oliverpool/exp/tree/main/item/httpetag/fileserver.go (even uglier, since I have use
reflect
to retrieve the underlyingfs.File
from thehttp.File
).Could a "github-collaborator" post a message in #43223 to notify the people who engaged in previous proposal of this updated proposal?
Original proposal (fs.File)
First, in io/fs, define
Second, in net/http, when serving a File (in
serveFile
, right beforeserveContent
for instance), if it implements ContentHashFile and the ContentHash method succeeds and is alphanumeric (no spaces, no Unicode, no symbols, to avoid any kind of header problems), use that result as the default ETag.Third, add the
ContentHash
method onhttp.ioFile
file (as a proxy to thefs.File
ContentHash
method).Fourth (probably out of scope for this proposal), add the
ContentHash
method onembed.FS
files.The text was updated successfully, but these errors were encountered: