Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (
type RequestHashFn func(req *http.Request) string

func simpleRequestHash(req *http.Request) string {
return fmt.Sprintf("%s:%s:%s", req.Method, req.URL.String(), hash(req.Header))
return fmt.Sprintf("%s:%s:%s", req.Method, sha256str([]byte(req.URL.String())), hash(req.Header))
}

func sha256str(key []byte) string {
hash := sha256.Sum256(key)
return hex.EncodeToString(hash[:])
}

const delimiter = "|"
Expand All @@ -31,6 +36,5 @@ func hash(headers http.Header) string {
sb.WriteString(fmt.Sprintf("%s:%s%s", key, headers.Get(key), delimiter))
}

hash := sha256.Sum256([]byte(sb.String()))
return hex.EncodeToString(hash[:])
return sha256str([]byte(sb.String()))
}
29 changes: 29 additions & 0 deletions hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package httpcache

import (
"net/http"
"net/url"
"testing"
)

func mustParse(t *testing.T, urlstr string) *url.URL {
t.Helper()
parsed, err := url.ParseRequestURI(urlstr)
if err != nil {
t.Fatalf("unable to parse url string: %v", err)
}

return parsed
}

func TestSimpleRequestHash(t *testing.T) {
req := &http.Request{
Method: http.MethodGet,
URL: mustParse(t, "http://www.google.com"),
Header: http.Header{"key": {"value"}},
}

if got, want := simpleRequestHash(req), "GET:253d142703041dd25197550a0fc11d6ac03befc1e64a1320009f1edf400c39ad:7cb0ba540850f2f8b7f62da704748662704cfa62c97c36bf251f26d656610656"; got != want {
t.Fatalf("expected %s; got %s", want, got)
}
}
16 changes: 8 additions & 8 deletions postgres/store.go → postgres/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)

type store struct{ queries *postgres.Queries }
type cache struct{ queries *postgres.Queries }

func (s *store) CacheResponse(ctx context.Context, arg httpcache.Params) (httpcache.Response, error) {
return wrapPostgresResponse(s.queries.CacheResponse(ctx, postgres.CacheResponseParams{
func (c *cache) CacheResponse(ctx context.Context, arg httpcache.Params) (httpcache.Response, error) {
return wrapPostgresResponse(c.queries.CacheResponse(ctx, postgres.CacheResponseParams{
ReqHash: arg.ReqHash,
Body: pgtype.Text{String: arg.Body, Valid: true},
Headers: pgtype.Text{String: arg.Headers, Valid: true},
StatusCode: pgtype.Int4{Int32: int32(arg.StatusCode), Valid: true},
}))
}

func (s *store) DeleteAllResponses(ctx context.Context) error {
return s.queries.DeleteAllResponses(ctx)
func (c *cache) DeleteAllResponses(ctx context.Context) error {
return c.queries.DeleteAllResponses(ctx)
}

func (s *store) GetResponse(ctx context.Context, reqHash string) (httpcache.Response, error) {
return wrapPostgresResponse(s.queries.GetResponse(ctx, reqHash))
func (c *cache) GetResponse(ctx context.Context, reqHash string) (httpcache.Response, error) {
return wrapPostgresResponse(c.queries.GetResponse(ctx, reqHash))
}

func wrapPostgresResponse(res postgres.Response, err error) (httpcache.Response, error) {
Expand All @@ -45,5 +45,5 @@ func (c Connection) Init(ctx context.Context) (httpcache.ResponseCacher, error)
return nil, err
}

return &store{queries: postgres.New(c.Conn)}, nil
return &cache{queries: postgres.New(c.Conn)}, nil
}
File renamed without changes.