MCPcopy
hub / github.com/willnorris/imageproxy / updateCacheHeaders

Method updateCacheHeaders

imageproxy.go:151–197  ·  view source on GitHub ↗

updateCacheHeaders updates the cache-control headers in the provided headers. If the cache-control header includes the 'private' directive, then 'no-store' is added to the header to prevent caching. If p.ForceCache is set, then 'private' and 'no-store' are both ignored and removed. This method als

(hdr http.Header)

Source from the content-addressed store, hash-verified

149// duration, the expires header, and the max-age header. It also removes the
150// expires header.
151func (p *Proxy) updateCacheHeaders(hdr http.Header) {
152 cc := tphc.ParseCacheControl(hdr)
153
154 // respect 'private' and 'no-store' directives unless ForceCache is set.
155 // The httpcache package ignores the 'private' directive,
156 // since it's not intended to be used as a shared cache.
157 // imageproxy IS a shared cache, so we enforce the 'private' directive ourself
158 // by setting 'no-store', which httpcache does respect.
159 if p.ForceCache {
160 delete(cc, "private")
161 delete(cc, "no-store")
162 hdr.Set("Cache-Control", cc.String())
163 } else {
164 if _, ok := cc["private"]; ok {
165 cc["no-store"] = ""
166 hdr.Set("Cache-Control", cc.String())
167 return
168 }
169 if _, ok := cc["no-store"]; ok {
170 return
171 }
172 }
173
174 if p.MinimumCacheDuration == 0 {
175 return
176 }
177
178 var expiresDuration time.Duration
179 var maxAgeDuration time.Duration
180
181 if maxAge, ok := cc["max-age"]; ok {
182 maxAgeDuration, _ = time.ParseDuration(maxAge + "s")
183 }
184 if date, err := httpcache.Date(hdr); err == nil {
185 if expiresHeader := hdr.Get("Expires"); expiresHeader != "" {
186 if expires, err := time.Parse(time.RFC1123, expiresHeader); err == nil {
187 expiresDuration = expires.Sub(date)
188 }
189 }
190 }
191
192 maxAge := max(p.MinimumCacheDuration, expiresDuration, maxAgeDuration)
193 cc["max-age"] = fmt.Sprintf("%d", int(maxAge.Seconds()))
194
195 hdr.Set("Cache-Control", cc.String())
196 hdr.Del("Expires")
197}
198
199// ServeHTTP handles incoming requests.
200func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {

Callers 2

RoundTripMethod · 0.80

Calls 3

SetMethod · 0.65
GetMethod · 0.65
StringMethod · 0.45

Tested by 1