Commit 5e54f6b5 authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Remove: useless package

parent 4df678e8
MIT License
Copyright (c) 2018 Victor Springer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
/*
MIT License
Copyright (c) 2018 Victor Springer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package cache
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"hash/fnv"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/nicolaspernoud/vestibule/pkg/glob"
)
// Response is the cached response data structure.
type Response struct {
// Value is the cached response value.
Value []byte
// Header is the cached response header.
Header http.Header
// Expiration is the cached response expiration date.
Expiration time.Time
// LastAccess is the last date a cached response was accessed.
// Used by LRU and MRU algorithms.
LastAccess time.Time
// Frequency is the count of times a cached response is accessed.
// Used for LFU and MFU algorithms.
Frequency int
}
// Client data structure for HTTP cache middleware.
type Client struct {
adapter Adapter
ttl time.Duration
refreshKey string
methods []string
}
// ClientOption is used to set Client settings.
type ClientOption func(c *Client) error
// Adapter interface for HTTP cache middleware client.
type Adapter interface {
// Get retrieves the cached response by a given key. It also
// returns true or false, whether it exists or not.
Get(key uint64) ([]byte, bool)
// Set caches a response for a given key until an expiration date.
Set(key uint64, response []byte, expiration time.Time)
// Release frees cache for a given key.
Release(key uint64)
}
// Middleware is the HTTP cache middleware handler.
func (c *Client) Middleware(next http.Handler, patterns []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check pattern matching
var patternMatched bool
for _, p := range patterns {
if glob.Glob(p, r.URL.Path) {
patternMatched = true
break
}
}
if c.cacheableMethod(r.Method) && patternMatched {
sortURLParams(r.URL)
key := generateKey(r.URL.String())
if r.Method == http.MethodPost && r.Body != nil {
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
next.ServeHTTP(w, r)
return
}
reader := ioutil.NopCloser(bytes.NewBuffer(body))
key = generateKeyWithBody(r.URL.String(), body)
r.Body = reader
}
params := r.URL.Query()
if _, ok := params[c.refreshKey]; ok {
delete(params, c.refreshKey)
r.URL.RawQuery = params.Encode()
key = generateKey(r.URL.String())
c.adapter.Release(key)
} else {
b, ok := c.adapter.Get(key)
response := BytesToResponse(b)
if ok {
if response.Expiration.After(time.Now()) {
response.LastAccess = time.Now()
response.Frequency++
c.adapter.Set(key, response.Bytes(), response.Expiration)
//w.WriteHeader(http.StatusNotModified)
for k, v := range response.Header {
w.Header().Set(k, strings.Join(v, ","))
}
w.Write(response.Value)
return
}
c.adapter.Release(key)
}
}
rec := httptest.NewRecorder()
next.ServeHTTP(rec, r)
result := rec.Result()
statusCode := result.StatusCode
value := rec.Body.Bytes()
if statusCode < 300 {
now := time.Now()
response := Response{
Value: value,
Header: result.Header,
Expiration: now.Add(c.ttl),
LastAccess: now,
Frequency: 1,
}
c.adapter.Set(key, response.Bytes(), response.Expiration)
}
for k, v := range result.Header {
w.Header().Set(k, strings.Join(v, ","))
}
w.WriteHeader(statusCode)
w.Write(value)
return
}
next.ServeHTTP(w, r)
})
}
func (c *Client) cacheableMethod(method string) bool {
for _, m := range c.methods {
if method == m {
return true
}
}
return false
}
// BytesToResponse converts bytes array into Response data structure.
func BytesToResponse(b []byte) Response {
var r Response
dec := gob.NewDecoder(bytes.NewReader(b))
dec.Decode(&r)
return r
}
// Bytes converts Response data structure into bytes array.
func (r Response) Bytes() []byte {
var b bytes.Buffer
enc := gob.NewEncoder(&b)
enc.Encode(&r)
return b.Bytes()
}
func sortURLParams(URL *url.URL) {
params := URL.Query()
for _, param := range params {
sort.Slice(param, func(i, j int) bool {
return param[i] < param[j]
})
}
URL.RawQuery = params.Encode()
}
// KeyAsString can be used by adapters to convert the cache key from uint64 to string.
func KeyAsString(key uint64) string {
return strconv.FormatUint(key, 36)
}
func generateKey(URL string) uint64 {
hash := fnv.New64a()
hash.Write([]byte(URL))
return hash.Sum64()
}
func generateKeyWithBody(URL string, body []byte) uint64 {
hash := fnv.New64a()
body = append([]byte(URL), body...)
hash.Write(body)
return hash.Sum64()
}
// NewClient initializes the cache HTTP middleware client with the given
// options.
func NewClient(opts ...ClientOption) (*Client, error) {
c := &Client{}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
if c.adapter == nil {
return nil, errors.New("cache client adapter is not set")
}
if int64(c.ttl) < 1 {
return nil, errors.New("cache client ttl is not set")
}
if c.methods == nil {
c.methods = []string{http.MethodGet}
}
return c, nil
}
// ClientWithAdapter sets the adapter type for the HTTP cache
// middleware client.
func ClientWithAdapter(a Adapter) ClientOption {
return func(c *Client) error {
c.adapter = a
return nil
}
}
// ClientWithTTL sets how long each response is going to be cached.
func ClientWithTTL(ttl time.Duration) ClientOption {
return func(c *Client) error {
if int64(ttl) < 1 {
return fmt.Errorf("cache client ttl %v is invalid", ttl)
}
c.ttl = ttl
return nil
}
}
// ClientWithRefreshKey sets the parameter key used to free a request
// cached response. Optional setting.
func ClientWithRefreshKey(refreshKey string) ClientOption {
return func(c *Client) error {
c.refreshKey = refreshKey
return nil
}
}
// ClientWithMethods sets the acceptable HTTP methods to be cached.
// Optional setting. If not set, default is "GET".
func ClientWithMethods(methods []string) ClientOption {
return func(c *Client) error {
for _, method := range methods {
if method != http.MethodGet && method != http.MethodPost {
return fmt.Errorf("invalid method %s", method)
}
}
c.methods = methods
return nil
}
}
package cache
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"sync"
"testing"
"time"
)
type adapterMock struct {
sync.Mutex
store map[uint64][]byte
}
type errReader int
func (a *adapterMock) Get(key uint64) ([]byte, bool) {
a.Lock()
defer a.Unlock()
if _, ok := a.store[key]; ok {
return a.store[key], true
}
return nil, false
}
func (a *adapterMock) Set(key uint64, response []byte, expiration time.Time) {
a.Lock()
defer a.Unlock()
a.store[key] = response
}
func (a *adapterMock) Release(key uint64) {
a.Lock()
defer a.Unlock()
delete(a.store, key)
}
func (errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("readAll error")
}
func TestMiddleware(t *testing.T) {
counter := 0
httpTestHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("new value %v", counter)))
})
adapter := &adapterMock{
store: map[uint64][]byte{
14974843192121052621: Response{
Value: []byte("value 1"),
Expiration: time.Now().Add(1 * time.Minute),
}.Bytes(),
14974839893586167988: Response{
Value: []byte("value 2"),
Expiration: time.Now().Add(1 * time.Minute),
}.Bytes(),
14974840993097796199: Response{
Value: []byte("value 3"),
Expiration: time.Now().Add(-1 * time.Minute),
}.Bytes(),
10956846073361780255: Response{
Value: []byte("value 4"),
Expiration: time.Now().Add(-1 * time.Minute),
}.Bytes(),
},
}
client, _ := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
ClientWithRefreshKey("rk"),
ClientWithMethods([]string{http.MethodGet, http.MethodPost}),
)
handler := client.Middleware(httpTestHandler, []string{"*"})
tests := []struct {
name string
url string
method string
body []byte
wantBody string
wantCode int
}{
{
"returns cached response",
"http://foo.bar/test-1",
"GET",
nil,
"value 1",
200,
},
{
"returns new response",
"http://foo.bar/test-2",
"PUT",
nil,
"new value 2",
200,
},
{
"returns cached response",
"http://foo.bar/test-2",
"GET",
nil,
"value 2",
200,
},
{
"returns new response",
"http://foo.bar/test-3?zaz=baz&baz=zaz",
"GET",
nil,
"new value 4",
200,
},
{
"returns cached response",
"http://foo.bar/test-3?baz=zaz&zaz=baz",
"GET",
nil,
"new value 4",
200,
},
{
"cache expired",
"http://foo.bar/test-3",
"GET",
nil,
"new value 6",
200,
},
{
"releases cached response and returns new response",
"http://foo.bar/test-2?rk=true",
"GET",
nil,
"new value 7",
200,
},
{
"returns new cached response",
"http://foo.bar/test-2",
"GET",
nil,
"new value 7",
200,
},
{
"returns new cached response",
"http://foo.bar/test-2",
"POST",
[]byte(`{"foo": "bar"}`),
"new value 9",
200,
},
{
"returns new cached response",
"http://foo.bar/test-2",
"POST",
[]byte(`{"foo": "bar"}`),
"new value 9",
200,
},
{
"ignores request body",
"http://foo.bar/test-2",
"GET",
[]byte(`{"foo": "bar"}`),
"new value 7",
200,
},
{
"returns new response",
"http://foo.bar/test-2",
"POST",
[]byte(`{"foo": "bar"}`),
"new value 12",
200,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
counter++
var r *http.Request
var err error
if counter != 12 {
reader := bytes.NewReader(tt.body)
r, err = http.NewRequest(tt.method, tt.url, reader)
if err != nil {
t.Error(err)
return
}
} else {
r, err = http.NewRequest(tt.method, tt.url, errReader(0))
if err != nil {
t.Error(err)
return
}
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if !reflect.DeepEqual(w.Code, tt.wantCode) {
t.Errorf("*Client.Middleware() = %v, want %v", w.Code, tt.wantCode)
return
}
if !reflect.DeepEqual(w.Body.String(), tt.wantBody) {
t.Errorf("*Client.Middleware() = %v, want %v", w.Body.String(), tt.wantBody)
}
})
}
}
func TestBytesToResponse(t *testing.T) {
r := Response{
Value: []byte("value 1"),
Expiration: time.Time{},
Frequency: 0,
LastAccess: time.Time{},
}
tests := []struct {
name string
b []byte
wantValue string
}{
{
"convert bytes array to response",
r.Bytes(),
"value 1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := BytesToResponse(tt.b)
if string(got.Value) != tt.wantValue {
t.Errorf("BytesToResponse() Value = %v, want %v", got, tt.wantValue)
return
}
})
}
}
func TestResponseToBytes(t *testing.T) {
r := Response{
Value: nil,
Expiration: time.Time{},
Frequency: 0,
LastAccess: time.Time{},
}
tests := []struct {
name string
response Response
}{
{
"convert response to bytes array",
r,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := tt.response.Bytes()
if b == nil || len(b) == 0 {
t.Error("Bytes() failed to convert")
return
}
})
}
}
func TestSortURLParams(t *testing.T) {
u, _ := url.Parse("http://test.com?zaz=bar&foo=zaz&boo=foo&boo=baz")
tests := []struct {
name string
URL *url.URL
want string
}{
{
"returns url with ordered querystring params",
u,
"http://test.com?boo=baz&boo=foo&foo=zaz&zaz=bar",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sortURLParams(tt.URL)
got := tt.URL.String()
if got != tt.want {
t.Errorf("sortURLParams() = %v, want %v", got, tt.want)
}
})
}
}
func TestGenerateKeyString(t *testing.T) {
urls := []string{