Switch to released tag v0.1.0 of github.com/pquerna/cachecontrol

Signed-off-by: Davanum Srinivas <davanum@gmail.com>
This commit is contained in:
Davanum Srinivas 2022-06-10 15:52:44 -04:00
parent 7e66aa3cfb
commit 9e1bbfdc65
No known key found for this signature in database
GPG Key ID: 80D83A796103BF59
11 changed files with 123 additions and 68 deletions

4
go.mod
View File

@ -211,7 +211,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect github.com/prometheus/procfs v0.7.3 // indirect
github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021 // indirect github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021 // indirect
github.com/russross/blackfriday v1.5.2 // indirect github.com/russross/blackfriday v1.5.2 // indirect
@ -464,7 +464,7 @@ replace (
github.com/pkg/errors => github.com/pkg/errors v0.9.1 github.com/pkg/errors => github.com/pkg/errors v0.9.1
github.com/pkg/sftp => github.com/pkg/sftp v1.10.1 github.com/pkg/sftp => github.com/pkg/sftp v1.10.1
github.com/pmezard/go-difflib => github.com/pmezard/go-difflib v1.0.0 github.com/pmezard/go-difflib => github.com/pmezard/go-difflib v1.0.0
github.com/pquerna/cachecontrol => github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 github.com/pquerna/cachecontrol => github.com/pquerna/cachecontrol v0.1.0
github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.12.1 github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model => github.com/prometheus/client_model v0.2.0 github.com/prometheus/client_model => github.com/prometheus/client_model v0.2.0
github.com/prometheus/common => github.com/prometheus/common v0.32.1 github.com/prometheus/common => github.com/prometheus/common v0.32.1

4
go.sum
View File

@ -351,8 +351,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 h1:0XM1XL/OFFJjXsYXlG30spTkV/E9+gmd5GD1w2HE8xM= github.com/pquerna/cachecontrol v0.1.0 h1:yJMy84ti9h/+OEWa752kBTKv4XC30OtVVHYv/8cTqKc=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= github.com/pquerna/cachecontrol v0.1.0/go.mod h1:NrUG3Z7Rdu85UNR3vm7SOsl1nFIeSiQnrHV5K9mBcUI=
github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk= github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk=
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=

View File

@ -84,7 +84,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/common v0.32.1 // indirect

View File

@ -364,8 +364,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 h1:0XM1XL/OFFJjXsYXlG30spTkV/E9+gmd5GD1w2HE8xM= github.com/pquerna/cachecontrol v0.1.0 h1:yJMy84ti9h/+OEWa752kBTKv4XC30OtVVHYv/8cTqKc=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= github.com/pquerna/cachecontrol v0.1.0/go.mod h1:NrUG3Z7Rdu85UNR3vm7SOsl1nFIeSiQnrHV5K9mBcUI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=

View File

@ -1,10 +1,8 @@
arch:
- amd64
- ppc64le
language: go language: go
install:
- go get -d -v ./...
- go get -u github.com/stretchr/testify/require
go: go:
- 1.7 - "1.15"
- 1.8 - "1.16"
- tip

View File

@ -1,6 +1,6 @@
# cachecontrol: HTTP Caching Parser and Interpretation # cachecontrol: HTTP Caching Parser and Interpretation
[![GoDoc](https://godoc.org/github.com/pquerna/cachecontrol?status.svg)](https://godoc.org/github.com/pquerna/cachecontrol)[![Build Status](https://travis-ci.org/pquerna/cachecontrol.svg?branch=master)](https://travis-ci.org/pquerna/cachecontrol) [![PkgGoDev](https://pkg.go.dev/badge/github.com/pquerna/cachecontrol?tab=doc)](https://pkg.go.dev/github.com/pquerna/cachecontrol?tab=doc)[![Build Status](https://travis-ci.org/pquerna/cachecontrol.svg?branch=master)](https://travis-ci.org/pquerna/cachecontrol)

View File

@ -25,7 +25,7 @@ import (
) )
type Options struct { type Options struct {
// Set to True for a prviate cache, which is not shared amoung users (eg, in a browser) // Set to True for a private cache, which is not shared among users (eg, in a browser)
// Set to False for a "shared" cache, which is more common in a server context. // Set to False for a "shared" cache, which is more common in a server context.
PrivateCache bool PrivateCache bool
} }

View File

@ -32,7 +32,7 @@ var (
ErrQuoteMismatch = errors.New("Missing closing quote") ErrQuoteMismatch = errors.New("Missing closing quote")
ErrMaxAgeDeltaSeconds = errors.New("Failed to parse delta-seconds in `max-age`") ErrMaxAgeDeltaSeconds = errors.New("Failed to parse delta-seconds in `max-age`")
ErrSMaxAgeDeltaSeconds = errors.New("Failed to parse delta-seconds in `s-maxage`") ErrSMaxAgeDeltaSeconds = errors.New("Failed to parse delta-seconds in `s-maxage`")
ErrMaxStaleDeltaSeconds = errors.New("Failed to parse delta-seconds in `min-fresh`") ErrMaxStaleDeltaSeconds = errors.New("Failed to parse delta-seconds in `max-stale`")
ErrMinFreshDeltaSeconds = errors.New("Failed to parse delta-seconds in `min-fresh`") ErrMinFreshDeltaSeconds = errors.New("Failed to parse delta-seconds in `min-fresh`")
ErrNoCacheNoArgs = errors.New("Unexpected argument to `no-cache`") ErrNoCacheNoArgs = errors.New("Unexpected argument to `no-cache`")
ErrNoStoreNoArgs = errors.New("Unexpected argument to `no-store`") ErrNoStoreNoArgs = errors.New("Unexpected argument to `no-store`")
@ -41,6 +41,10 @@ var (
ErrMustRevalidateNoArgs = errors.New("Unexpected argument to `must-revalidate`") ErrMustRevalidateNoArgs = errors.New("Unexpected argument to `must-revalidate`")
ErrPublicNoArgs = errors.New("Unexpected argument to `public`") ErrPublicNoArgs = errors.New("Unexpected argument to `public`")
ErrProxyRevalidateNoArgs = errors.New("Unexpected argument to `proxy-revalidate`") ErrProxyRevalidateNoArgs = errors.New("Unexpected argument to `proxy-revalidate`")
// Experimental
ErrImmutableNoArgs = errors.New("Unexpected argument to `immutable`")
ErrStaleIfErrorDeltaSeconds = errors.New("Failed to parse delta-seconds in `stale-if-error`")
ErrStaleWhileRevalidateDeltaSeconds = errors.New("Failed to parse delta-seconds in `stale-while-revalidate`")
) )
func whitespace(b byte) bool { func whitespace(b byte) bool {
@ -160,7 +164,7 @@ type cacheDirective interface {
addPair(s string, v string) error addPair(s string, v string) error
} }
// LOW LEVEL API: Repersentation of possible request directives in a `Cache-Control` header: http://tools.ietf.org/html/rfc7234#section-5.2.1 // LOW LEVEL API: Representation of possible request directives in a `Cache-Control` header: http://tools.ietf.org/html/rfc7234#section-5.2.1
// //
// Note: Many fields will be `nil` in practice. // Note: Many fields will be `nil` in practice.
// //
@ -185,6 +189,7 @@ type RequestCacheDirectives struct {
// assigned to max-stale, then the client is willing to accept a stale // assigned to max-stale, then the client is willing to accept a stale
// response of any age. // response of any age.
MaxStale DeltaSeconds MaxStale DeltaSeconds
MaxStaleSet bool
// min-fresh(delta seconds): http://tools.ietf.org/html/rfc7234#section-5.2.1.3 // min-fresh(delta seconds): http://tools.ietf.org/html/rfc7234#section-5.2.1.3
// //
@ -236,10 +241,10 @@ func (cd *RequestCacheDirectives) addToken(token string) error {
switch token { switch token {
case "max-age": case "max-age":
err = ErrMaxAgeDeltaSeconds err = ErrMaxAgeDeltaSeconds
case "max-stale":
err = ErrMaxStaleDeltaSeconds
case "min-fresh": case "min-fresh":
err = ErrMinFreshDeltaSeconds err = ErrMinFreshDeltaSeconds
case "max-stale":
cd.MaxStaleSet = true
case "no-cache": case "no-cache":
cd.NoCache = true cd.NoCache = true
case "no-store": case "no-store":
@ -403,6 +408,21 @@ type ResponseCacheDirectives struct {
// proxy-revalidate response directive. // proxy-revalidate response directive.
SMaxAge DeltaSeconds SMaxAge DeltaSeconds
////
// Experimental features
// - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Extension_Cache-Control_directives
// - https://www.fastly.com/blog/stale-while-revalidate-stale-if-error-available-today
////
// immutable(cast-to-bool): experimental feature
Immutable bool
// stale-if-error(delta seconds): experimental feature
StaleIfError DeltaSeconds
// stale-while-revalidate(delta seconds): experimental feature
StaleWhileRevalidate DeltaSeconds
// Extensions: http://tools.ietf.org/html/rfc7234#section-5.2.3 // Extensions: http://tools.ietf.org/html/rfc7234#section-5.2.3
// //
// The Cache-Control header field can be extended through the use of one // The Cache-Control header field can be extended through the use of one
@ -416,6 +436,9 @@ func ParseResponseCacheControl(value string) (*ResponseCacheDirectives, error) {
cd := &ResponseCacheDirectives{ cd := &ResponseCacheDirectives{
MaxAge: -1, MaxAge: -1,
SMaxAge: -1, SMaxAge: -1,
// Exerimantal stale timeouts
StaleIfError: -1,
StaleWhileRevalidate: -1,
} }
err := parse(value, cd) err := parse(value, cd)
@ -446,6 +469,13 @@ func (cd *ResponseCacheDirectives) addToken(token string) error {
err = ErrMaxAgeDeltaSeconds err = ErrMaxAgeDeltaSeconds
case "s-maxage": case "s-maxage":
err = ErrSMaxAgeDeltaSeconds err = ErrSMaxAgeDeltaSeconds
// Experimental
case "immutable":
cd.Immutable = true
case "stale-if-error":
err = ErrMaxAgeDeltaSeconds
case "stale-while-revalidate":
err = ErrMaxAgeDeltaSeconds
default: default:
cd.Extensions = append(cd.Extensions, token) cd.Extensions = append(cd.Extensions, token)
} }
@ -500,6 +530,13 @@ func (cd *ResponseCacheDirectives) addPair(token string, v string) error {
cd.MaxAge, err = parseDeltaSeconds(v) cd.MaxAge, err = parseDeltaSeconds(v)
case "s-maxage": case "s-maxage":
cd.SMaxAge, err = parseDeltaSeconds(v) cd.SMaxAge, err = parseDeltaSeconds(v)
// Experimental
case "immutable":
err = ErrImmutableNoArgs
case "stale-if-error":
cd.StaleIfError, err = parseDeltaSeconds(v)
case "stale-while-revalidate":
cd.StaleWhileRevalidate, err = parseDeltaSeconds(v)
default: default:
// TODO(pquerna): this sucks, making user re-parse, and its technically not 'quoted' like the original, // TODO(pquerna): this sucks, making user re-parse, and its technically not 'quoted' like the original,
// but this is still easier, just a SplitN on "=" // but this is still easier, just a SplitN on "="

View File

@ -22,7 +22,7 @@ import (
"time" "time"
) )
// LOW LEVEL API: Repersents a potentially cachable HTTP object. // LOW LEVEL API: Represents a potentially cachable HTTP object.
// //
// This struct is designed to be serialized efficiently, so in a high // This struct is designed to be serialized efficiently, so in a high
// performance caching server, things like Date-Strings don't need to be // performance caching server, things like Date-Strings don't need to be
@ -44,7 +44,7 @@ type Object struct {
NowUTC time.Time NowUTC time.Time
} }
// LOW LEVEL API: Repersents the results of examinig an Object with // LOW LEVEL API: Represents the results of examining an Object with
// CachableObject and ExpirationObject. // CachableObject and ExpirationObject.
// //
// TODO(pquerna): decide if this is a good idea or bad // TODO(pquerna): decide if this is a good idea or bad
@ -55,33 +55,17 @@ type ObjectResults struct {
OutErr error OutErr error
} }
// LOW LEVEL API: Check if a object is cachable. // LOW LEVEL API: Check if a request is cacheable.
func CachableObject(obj *Object, rv *ObjectResults) { // This function doesn't reset the passed ObjectResults.
rv.OutReasons = nil func CachableRequestObject(obj *Object, rv *ObjectResults) {
rv.OutWarnings = nil
rv.OutErr = nil
switch obj.ReqMethod { switch obj.ReqMethod {
case "GET": case "GET":
break break
case "HEAD": case "HEAD":
break break
case "POST": case "POST":
/** // Responses to POST requests can be cacheable if they include explicit freshness information
POST: http://tools.ietf.org/html/rfc7231#section-4.3.3 break
Responses to POST requests are only cacheable when they include
explicit freshness information (see Section 4.2.1 of [RFC7234]).
However, POST caching is not widely implemented. For cases where an
origin server wishes the client to be able to cache the result of a
POST in a way that can be reused by a later GET, the origin server
MAY send a 200 (OK) response containing the result and a
Content-Location header field that has the same value as the POST's
effective request URI (Section 3.1.4.2).
*/
if !hasFreshness(obj.ReqDirectives, obj.RespDirectives, obj.RespHeaders, obj.RespExpiresHeader, obj.CacheIsPrivate) {
rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodPOST)
}
case "PUT": case "PUT":
rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodPUT) rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodPUT)
@ -103,16 +87,35 @@ func CachableObject(obj *Object, rv *ObjectResults) {
// To my knowledge, none of them are cachable. Please open a ticket if this is not the case! // To my knowledge, none of them are cachable. Please open a ticket if this is not the case!
// //
default: default:
rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodUnkown) rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodUnknown)
} }
if obj.ReqDirectives.NoStore { if obj.ReqDirectives != nil && obj.ReqDirectives.NoStore {
rv.OutReasons = append(rv.OutReasons, ReasonRequestNoStore) rv.OutReasons = append(rv.OutReasons, ReasonRequestNoStore)
} }
}
// LOW LEVEL API: Check if a response is cacheable.
// This function doesn't reset the passed ObjectResults.
func CachableResponseObject(obj *Object, rv *ObjectResults) {
/**
POST: http://tools.ietf.org/html/rfc7231#section-4.3.3
Responses to POST requests are only cacheable when they include
explicit freshness information (see Section 4.2.1 of [RFC7234]).
However, POST caching is not widely implemented. For cases where an
origin server wishes the client to be able to cache the result of a
POST in a way that can be reused by a later GET, the origin server
MAY send a 200 (OK) response containing the result and a
Content-Location header field that has the same value as the POST's
effective request URI (Section 3.1.4.2).
*/
if obj.ReqMethod == http.MethodPost && !hasFreshness(obj.RespDirectives, obj.RespHeaders, obj.RespExpiresHeader, obj.CacheIsPrivate) {
rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodPOST)
}
// Storing Responses to Authenticated Requests: http://tools.ietf.org/html/rfc7234#section-3.2 // Storing Responses to Authenticated Requests: http://tools.ietf.org/html/rfc7234#section-3.2
authz := obj.ReqHeaders.Get("Authorization") if obj.ReqHeaders.Get("Authorization") != "" {
if authz != "" {
if obj.RespDirectives.MustRevalidate || if obj.RespDirectives.MustRevalidate ||
obj.RespDirectives.Public || obj.RespDirectives.Public ||
obj.RespDirectives.SMaxAge != -1 { obj.RespDirectives.SMaxAge != -1 {
@ -149,18 +152,26 @@ func CachableObject(obj *Object, rv *ObjectResults) {
* contains a public response directive (see Section 5.2.2.5). * contains a public response directive (see Section 5.2.2.5).
*/ */
expires := obj.RespHeaders.Get("Expires") != "" if obj.RespHeaders.Get("Expires") != "" ||
statusCachable := cachableStatusCode(obj.RespStatusCode)
if expires ||
obj.RespDirectives.MaxAge != -1 || obj.RespDirectives.MaxAge != -1 ||
(obj.RespDirectives.SMaxAge != -1 && !obj.CacheIsPrivate) || (obj.RespDirectives.SMaxAge != -1 && !obj.CacheIsPrivate) ||
statusCachable || cachableStatusCode(obj.RespStatusCode) ||
obj.RespDirectives.Public { obj.RespDirectives.Public {
/* cachable by default, at least one of the above conditions was true */ /* cachable by default, at least one of the above conditions was true */
} else { return
}
rv.OutReasons = append(rv.OutReasons, ReasonResponseUncachableByDefault) rv.OutReasons = append(rv.OutReasons, ReasonResponseUncachableByDefault)
} }
// LOW LEVEL API: Check if a object is cachable.
func CachableObject(obj *Object, rv *ObjectResults) {
rv.OutReasons = nil
rv.OutWarnings = nil
rv.OutErr = nil
CachableRequestObject(obj, rv)
CachableResponseObject(obj, rv)
} }
var twentyFourHours = time.Duration(24 * time.Hour) var twentyFourHours = time.Duration(24 * time.Hour)
@ -232,7 +243,7 @@ func ExpirationObject(obj *Object, rv *ObjectResults) {
println("Expiration: ", expiresTime.String()) println("Expiration: ", expiresTime.String())
} }
} else { } else {
// TODO(pquerna): what should the default behavoir be for expiration time? // TODO(pquerna): what should the default behavior be for expiration time?
} }
rv.OutExpirationTime = expiresTime rv.OutExpirationTime = expiresTime
@ -243,20 +254,29 @@ func UsingRequestResponse(req *http.Request,
statusCode int, statusCode int,
respHeaders http.Header, respHeaders http.Header,
privateCache bool) ([]Reason, time.Time, error) { privateCache bool) ([]Reason, time.Time, error) {
reasons, time, _, _, err := UsingRequestResponseWithObject(req, statusCode, respHeaders, privateCache)
return reasons, time, err
}
// Evaluate cachability based on an HTTP request, and parts of the response.
// Returns the parsed Object as well.
func UsingRequestResponseWithObject(req *http.Request,
statusCode int,
respHeaders http.Header,
privateCache bool) ([]Reason, time.Time, []Warning, *Object, error) {
var reqHeaders http.Header var reqHeaders http.Header
var reqMethod string var reqMethod string
var reqDir *RequestCacheDirectives = nil var reqDir *RequestCacheDirectives = nil
respDir, err := ParseResponseCacheControl(respHeaders.Get("Cache-Control")) respDir, err := ParseResponseCacheControl(respHeaders.Get("Cache-Control"))
if err != nil { if err != nil {
return nil, time.Time{}, err return nil, time.Time{}, nil, nil, err
} }
if req != nil { if req != nil {
reqDir, err = ParseRequestCacheControl(req.Header.Get("Cache-Control")) reqDir, err = ParseRequestCacheControl(req.Header.Get("Cache-Control"))
if err != nil { if err != nil {
return nil, time.Time{}, err return nil, time.Time{}, nil, nil, err
} }
reqHeaders = req.Header reqHeaders = req.Header
reqMethod = req.Method reqMethod = req.Method
@ -279,7 +299,7 @@ func UsingRequestResponse(req *http.Request,
if respHeaders.Get("Date") != "" { if respHeaders.Get("Date") != "" {
dateHeader, err = http.ParseTime(respHeaders.Get("Date")) dateHeader, err = http.ParseTime(respHeaders.Get("Date"))
if err != nil { if err != nil {
return nil, time.Time{}, err return nil, time.Time{}, nil, nil, err
} }
dateHeader = dateHeader.UTC() dateHeader = dateHeader.UTC()
} }
@ -287,7 +307,7 @@ func UsingRequestResponse(req *http.Request,
if respHeaders.Get("Last-Modified") != "" { if respHeaders.Get("Last-Modified") != "" {
lastModifiedHeader, err = http.ParseTime(respHeaders.Get("Last-Modified")) lastModifiedHeader, err = http.ParseTime(respHeaders.Get("Last-Modified"))
if err != nil { if err != nil {
return nil, time.Time{}, err return nil, time.Time{}, nil, nil, err
} }
lastModifiedHeader = lastModifiedHeader.UTC() lastModifiedHeader = lastModifiedHeader.UTC()
} }
@ -312,19 +332,19 @@ func UsingRequestResponse(req *http.Request,
CachableObject(&obj, &rv) CachableObject(&obj, &rv)
if rv.OutErr != nil { if rv.OutErr != nil {
return nil, time.Time{}, rv.OutErr return nil, time.Time{}, nil, nil, rv.OutErr
} }
ExpirationObject(&obj, &rv) ExpirationObject(&obj, &rv)
if rv.OutErr != nil { if rv.OutErr != nil {
return nil, time.Time{}, rv.OutErr return nil, time.Time{}, nil, nil, rv.OutErr
} }
return rv.OutReasons, rv.OutExpirationTime, nil return rv.OutReasons, rv.OutExpirationTime, rv.OutWarnings, &obj, nil
} }
// calculate if a freshness directive is present: http://tools.ietf.org/html/rfc7234#section-4.2.1 // calculate if a freshness directive is present: http://tools.ietf.org/html/rfc7234#section-4.2.1
func hasFreshness(reqDir *RequestCacheDirectives, respDir *ResponseCacheDirectives, respHeaders http.Header, respExpires time.Time, privateCache bool) bool { func hasFreshness(respDir *ResponseCacheDirectives, respHeaders http.Header, respExpires time.Time, privateCache bool) bool {
if !privateCache && respDir.SMaxAge != -1 { if !privateCache && respDir.SMaxAge != -1 {
return true return true
} }

View File

@ -45,7 +45,7 @@ const (
ReasonRequestMethodTRACE ReasonRequestMethodTRACE
// The request method was not recognized by cachecontrol, and should not be cached. // The request method was not recognized by cachecontrol, and should not be cached.
ReasonRequestMethodUnkown ReasonRequestMethodUnknown
// The request included an Cache-Control: no-store header // The request included an Cache-Control: no-store header
ReasonRequestNoStore ReasonRequestNoStore
@ -77,7 +77,7 @@ func (r Reason) String() string {
return "ReasonRequestMethodOPTIONS" return "ReasonRequestMethodOPTIONS"
case ReasonRequestMethodTRACE: case ReasonRequestMethodTRACE:
return "ReasonRequestMethodTRACE" return "ReasonRequestMethodTRACE"
case ReasonRequestMethodUnkown: case ReasonRequestMethodUnknown:
return "ReasonRequestMethodUnkown" return "ReasonRequestMethodUnkown"
case ReasonRequestNoStore: case ReasonRequestNoStore:
return "ReasonRequestNoStore" return "ReasonRequestNoStore"

6
vendor/modules.txt vendored
View File

@ -713,8 +713,8 @@ github.com/pkg/errors
# github.com/pmezard/go-difflib v1.0.0 => github.com/pmezard/go-difflib v1.0.0 # github.com/pmezard/go-difflib v1.0.0 => github.com/pmezard/go-difflib v1.0.0
## explicit ## explicit
github.com/pmezard/go-difflib/difflib github.com/pmezard/go-difflib/difflib
# github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 => github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 # github.com/pquerna/cachecontrol v0.1.0 => github.com/pquerna/cachecontrol v0.1.0
## explicit ## explicit; go 1.16
github.com/pquerna/cachecontrol github.com/pquerna/cachecontrol
github.com/pquerna/cachecontrol/cacheobject github.com/pquerna/cachecontrol/cacheobject
# github.com/prometheus/client_golang v1.12.1 => github.com/prometheus/client_golang v1.12.1 # github.com/prometheus/client_golang v1.12.1 => github.com/prometheus/client_golang v1.12.1
@ -2747,7 +2747,7 @@ sigs.k8s.io/yaml
# github.com/pkg/errors => github.com/pkg/errors v0.9.1 # github.com/pkg/errors => github.com/pkg/errors v0.9.1
# github.com/pkg/sftp => github.com/pkg/sftp v1.10.1 # github.com/pkg/sftp => github.com/pkg/sftp v1.10.1
# github.com/pmezard/go-difflib => github.com/pmezard/go-difflib v1.0.0 # github.com/pmezard/go-difflib => github.com/pmezard/go-difflib v1.0.0
# github.com/pquerna/cachecontrol => github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 # github.com/pquerna/cachecontrol => github.com/pquerna/cachecontrol v0.1.0
# github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.12.1 # github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.12.1
# github.com/prometheus/client_model => github.com/prometheus/client_model v0.2.0 # github.com/prometheus/client_model => github.com/prometheus/client_model v0.2.0
# github.com/prometheus/common => github.com/prometheus/common v0.32.1 # github.com/prometheus/common => github.com/prometheus/common v0.32.1