package 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.3 func 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 }