computeEtag determines whether the If-None-Match header should be sent to the source image server. It returns etag value to be set and boolean indicating whether the conditional headers should be sent at all.
()
| 93 | // source image server. It returns etag value to be set and boolean indicating |
| 94 | // whether the conditional headers should be sent at all. |
| 95 | func (c *Request) computeEtag() (string, bool) { |
| 96 | // If the feature is disabled or no header is present, |
| 97 | // we shouldn't send the header at all, but it should not affect other headers |
| 98 | if !c.config.ETagEnabled || len(c.ifNoneMatch) == 0 { |
| 99 | return "", false |
| 100 | } |
| 101 | |
| 102 | // If etag buster is not set, we should send the header as is if present |
| 103 | if len(c.config.ETagBuster) == 0 { |
| 104 | return c.ifNoneMatch, false |
| 105 | } |
| 106 | |
| 107 | // Unquote and remove /W |
| 108 | ifNoneMatch := httpheaders.UnquoteEtag(c.ifNoneMatch) |
| 109 | |
| 110 | // We expect that incoming ETag header has the buster |
| 111 | rest, busterFound := strings.CutPrefix(ifNoneMatch, c.config.ETagBuster+"/") |
| 112 | if !busterFound { |
| 113 | return "", true // do not send any conditional headers otherwise (???) |
| 114 | } |
| 115 | |
| 116 | // Parse the rest of the header as base64-encoded string, if it fails, |
| 117 | // we should not send any conditional headers (invalid etag) |
| 118 | etag, err := base64.RawURLEncoding.DecodeString(rest) |
| 119 | if err != nil { |
| 120 | return "", true // do not send any conditional headers otherwise |
| 121 | } |
| 122 | |
| 123 | // Quotes will be encoded into etag |
| 124 | return string(etag), false |
| 125 | } |
| 126 | |
| 127 | // injectLastModifiedHeader injects the Last-Modified header into the user response |
| 128 | func (c *Request) injectLastModifiedHeader(rw http.ResponseWriter) { |
no test coverage detected