Use Cache-Control

This commit is contained in:
Frank Denis 2018-02-04 13:48:51 +01:00
parent 6b49470b95
commit 6f546b4c21

View file

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/pquerna/cachecontrol/cacheobject"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
) )
@ -334,23 +335,16 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
func ttlFromHTTPResponse(proxy *Proxy, resp *http.Response) *uint32 { func ttlFromHTTPResponse(proxy *Proxy, resp *http.Response) *uint32 {
expiresStr, dateStr := resp.Header.Get("Expires"), resp.Header.Get("Date") cacheControlStr := resp.Header.Get("Cache-Control")
if len(expiresStr) == 0 || len(dateStr) == 0 { if len(cacheControlStr) == 0 {
return nil return nil
} }
expires, err := http.ParseTime(expiresStr) cacheControl, err := cacheobject.ParseResponseCacheControl(cacheControlStr)
if err != nil { if err != nil {
return nil return nil
} }
date, err := http.ParseTime(dateStr) foundTTL := uint32(cacheControl.MaxAge)
if err != nil { if foundTTL < proxy.cacheMinTTL {
return nil
}
if !expires.After(date) {
return nil
}
foundTTL := uint32(expires.Sub(date).Seconds())
if foundTTL < proxy.cacheMaxTTL {
foundTTL = proxy.cacheMinTTL foundTTL = proxy.cacheMinTTL
} }
if foundTTL > proxy.cacheMaxTTL { if foundTTL > proxy.cacheMaxTTL {