Skip to content

Commit

Permalink
Merge pull request #42 from tarent/TOOM-2580-disable-caching-for-typo…
Browse files Browse the repository at this point in the history
…3-be-users

tOOM-2580: removed duplicate function readCookieValue
  • Loading branch information
domano committed Jan 31, 2017
2 parents e2c6fb3 + a64325a commit 358f7d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 39 deletions.
36 changes: 2 additions & 34 deletions cache/cache_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
}
11 changes: 6 additions & 5 deletions cache/cache_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"github.com/tarent/lib-compose/util"
)

type hashCall struct {
Expand Down Expand Up @@ -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)
}

0 comments on commit 358f7d2

Please sign in to comment.