MCPcopy
hub / github.com/caddyserver/caddy / calculateEtag

Function calculateEtag

modules/caddyhttp/fileserver/staticfiles.go:763–774  ·  view source on GitHub ↗

calculateEtag computes an entity tag using a strong validator without consuming the contents of the file. It requires the file info contain the correct size and modification time. It strives to implement the semantics regarding ETags as defined by RFC 9110 section 8.8.3 and 8.8.1. See https://www.rf

(d os.FileInfo)

Source from the content-addressed store, hash-verified

761// of Caddy deployments, stores mod times with millisecond precision,
762// which we consider precise enough to qualify as a strong validator.
763func calculateEtag(d os.FileInfo) string {
764 mtime := d.ModTime()
765 if !usefulModTime(mtime) {
766 return ""
767 }
768 var sb strings.Builder
769 sb.WriteRune('"')
770 sb.WriteString(strconv.FormatInt(mtime.UnixNano(), 36))
771 sb.WriteString(strconv.FormatInt(d.Size(), 36))
772 sb.WriteRune('"')
773 return sb.String()
774}
775
776// Finds the first corresponding etag file for a given file in the file system and return its content
777func (fsrv *FileServer) getEtagFromFile(fileSystem fs.FS, filename string) (string, error) {

Callers 1

ServeHTTPMethod · 0.85

Calls 3

usefulModTimeFunction · 0.85
SizeMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected