parseCacheControl parses Cache-Control response directives from h. Uses Header.Values to handle multiple header lines; matches names case-insensitively per RFC 9111 §5.2.
(h http.Header)
| 68 | // Uses Header.Values to handle multiple header lines; matches names |
| 69 | // case-insensitively per RFC 9111 §5.2. |
| 70 | func parseCacheControl(h http.Header) cacheDirectives { |
| 71 | d := cacheDirectives{maxAge: -1, sMaxAge: -1} |
| 72 | for _, line := range h.Values("Cache-Control") { |
| 73 | for token := range strings.SplitSeq(line, ",") { |
| 74 | parts := strings.SplitN(strings.TrimSpace(token), "=", 2) |
| 75 | directive := strings.ToLower(strings.TrimSpace(parts[0])) |
| 76 | switch directive { |
| 77 | case "no-store": |
| 78 | d.noStore = true |
| 79 | case "no-cache": |
| 80 | d.noCache = true |
| 81 | case "private": |
| 82 | d.private = true |
| 83 | case "must-revalidate": |
| 84 | d.mustRevalidate = true |
| 85 | case "proxy-revalidate": |
| 86 | d.proxyRevalidate = true |
| 87 | case "public": |
| 88 | d.public = true |
| 89 | case "max-age": |
| 90 | if len(parts) == 2 { |
| 91 | if v, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64); err == nil { |
| 92 | d.maxAge = v |
| 93 | } |
| 94 | } |
| 95 | case "s-maxage": |
| 96 | if len(parts) == 2 { |
| 97 | if v, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64); err == nil { |
| 98 | d.sMaxAge = v |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return d |
| 105 | } |
searching dependent graphs…