diff --git a/cache/cache_strategy.go b/cache/cache_strategy.go index 4e4f3c4..566ed88 100644 --- a/cache/cache_strategy.go +++ b/cache/cache_strategy.go @@ -5,8 +5,8 @@ import ( "encoding/hex" "github.com/pquerna/cachecontrol/cacheobject" "github.com/tarent/lib-compose/logging" + "github.com/tarent/lib-compose/util" "net/http" - "strings" ) const ( @@ -93,7 +93,7 @@ func (tcs *CacheStrategy) HashWithParameters(method string, url string, requestH } for _, c := range includeCookies { - if value, found := readCookieValue(requestHeader, c); found { + if value, found := util.ReadCookieValue(requestHeader, c); found { hasher.Write([]byte(c)) hasher.Write([]byte(value)) } @@ -130,35 +130,3 @@ func (tcs *CacheStrategy) isReasonIgnorable(reason cacheobject.Reason) bool { } return false } - -// taken and adapted from net/http -func readCookieValue(h http.Header, filterName string) (string, bool) { - lines, ok := h["Cookie"] - if !ok { - return "", false - } - - for _, line := range lines { - parts := strings.Split(strings.TrimSpace(line), ";") - if len(parts) == 1 && parts[0] == "" { - continue - } - for i := 0; i < len(parts); i++ { - parts[i] = strings.TrimSpace(parts[i]) - if len(parts[i]) == 0 { - continue - } - name, val := parts[i], "" - if j := strings.Index(name, "="); j >= 0 { - name, val = name[:j], name[j+1:] - } - if filterName == name { - if len(val) > 1 && val[0] == '"' && val[len(val)-1] == '"' { - val = val[1 : len(val)-1] - } - return val, true - } - } - } - return "", false -} diff --git a/cache/cache_strategy_test.go b/cache/cache_strategy_test.go index facd07b..fe45e6c 100644 --- a/cache/cache_strategy_test.go +++ b/cache/cache_strategy_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "net/http" "testing" + "github.com/tarent/lib-compose/util" ) type hashCall struct { @@ -198,21 +199,21 @@ func Test_CacheStrategy_IsCachable(t *testing.T) { func Test_CacheStrategy_readCookieValue(t *testing.T) { a := assert.New(t) - v, found := readCookieValue(http.Header{"Cookie": {"foo=bar"}}, "foo") + v, found := util.ReadCookieValue(http.Header{"Cookie": {"foo=bar"}}, "foo") a.True(found) a.Equal("bar", v) - v, found = readCookieValue(http.Header{"Cookie": {`foo="bar"`}}, "foo") + v, found = util.ReadCookieValue(http.Header{"Cookie": {`foo="bar"`}}, "foo") a.True(found) a.Equal("bar", v) - v, found = readCookieValue(http.Header{"Cookie": {"foo"}}, "foo") + v, found = util.ReadCookieValue(http.Header{"Cookie": {"foo"}}, "foo") a.True(found) a.Equal("", v) - v, found = readCookieValue(http.Header{"Cookie": {";"}}, "foo") + v, found = util.ReadCookieValue(http.Header{"Cookie": {";"}}, "foo") a.False(found) - v, found = readCookieValue(http.Header{"Cookie": {""}}, "foo") + v, found = util.ReadCookieValue(http.Header{"Cookie": {""}}, "foo") a.False(found) }