-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Milestone
Description
In the discussion of io/fs and embed, a few people asked for automatic serving of ETag headers for static content, using content hashes. We ran out of time for that in Go 1.16, in part because it's not entirely clear what we should do.
Here is a proposal that may be full of holes.
First, in io/fs, define
package fs
// A ContentHashFile is a file that can return its own content hash efficiently.
type ContentHashFile interface {
File
// ContentHash returns a content hash of the file that uniquely
// identifies the file contents, suitable for use as a cache key.
// The returned hash should be text, such as hexadecimal or base64.
//
// ContentHash must NOT compute the hash of the file during the call.
// That is, it must run in time O(1) not O(length of file).
// If a content hash is not already available, ContentHash should
// return an error rather than take the time to compute one.
ContentHash() (string, error)
}
Second, in net/http, when serving a File, 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.
thomasf, Merovius, OneOfOne, jimmyfrasche, Oppodelldog and 8 more