Something went wrong. Try again.
ETag middleware for Go pkg.go.dev/tangled.org/akimo.dev/etag
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219package etag
import "strings"
// entityTag is a parsed entity-tag as defined in RFC 9110 Section 13.1.1.// opaque includes the surrounding DQUOTEs, e.g. `"foo"`.type entityTag struct { weak bool opaque string}
// isETagC reports whether c is an etagc//// etagc = %x21 / %x23-7E / obs-text, where obs-text = %x80-FF//// https://www.rfc-editor.org/info/rfc9110/#section-5.5// https://www.rfc-editor.org/info/rfc9110/#section-8.8.3func isETagC(c byte) bool { return c == 0x21 || (c >= 0x23 && c <= 0x7E) || c >= 0x80}
func skipOWS(s string, i int) int { for i < len(s) && (s[i] == ' ' || s[i] == '\t') { i++ }
return i}
func skipToComma(s string, i int) int { for i < len(s) && s[i] != ',' { i++ }
return i}
// parseSingleEntityTag parses s as a single entity-tag with optional OWS around it.func parseSingleEntityTag(s string) (entityTag, bool) { i := skipOWS(s, 0) n := len(s)
var tag entityTag
if strings.HasPrefix(s[i:], weakPrefix) { tag.weak = true i += len(weakPrefix) }
if i >= n || s[i] != '"' { return entityTag{}, false }
opaqueStart := i i++
for i < n { c := s[i]
if c == '"' { tag.opaque = s[opaqueStart : i+1] i++
i = skipOWS(s, i)
if i != n { return entityTag{}, false }
return tag, true }
if !isETagC(c) { return entityTag{}, false }
i++ }
return entityTag{}, false}
// parseEntityTagList parses an #entity-tag list, skipping invalid elements.// Commas inside opaque-tags do not act as delimiters.func parseEntityTagList(header string) []entityTag { var tags []entityTag
n := len(header) i := 0
for { i = skipOWS(header, i)
if i >= n { return tags }
if header[i] == ',' { i++
continue }
weak := false pos := i
if strings.HasPrefix(header[pos:], weakPrefix) { weak = true pos += len(weakPrefix) }
if pos >= n || header[pos] != '"' { i = skipToComma(header, pos)
continue }
opaqueStart := pos pos++
closed := false valid := true
for pos < n { c := header[pos]
if c == '"' { closed = true pos++
break }
if !isETagC(c) { valid = false
break }
pos++ }
if !closed { if !valid { i = skipToComma(header, pos)
continue }
return tags }
if !valid { i = skipToComma(header, pos)
continue }
opaque := header[opaqueStart:pos]
end := skipOWS(header, pos)
if end < n && header[end] != ',' { i = skipToComma(header, pos)
continue }
tags = append(tags, entityTag{weak: weak, opaque: opaque}) i = end }}
// WeakMatch checks if the provided ETag matches any of the ETags in the If-None-Match header using weak comparison rules.func WeakMatch(header, etag string) bool { if strings.TrimSpace(header) == "*" { return true }
target, ok := parseSingleEntityTag(etag)
if !ok { return false }
for _, tag := range parseEntityTagList(header) { if tag.opaque == target.opaque { return true } }
return false}
// StrongMatch checks if the provided ETag matches any of the ETags in the If-None-Match header using strong comparison rules.func StrongMatch(header, etag string) bool { if strings.TrimSpace(header) == "*" { return true }
target, ok := parseSingleEntityTag(etag)
if !ok || target.weak { return false }
for _, tag := range parseEntityTagList(header) { if tag.weak { continue }
if tag.opaque == target.opaque { return true } }
return false}