-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
40 lines (30 loc) · 756 Bytes
/
Copy pathhash.go
File metadata and controls
40 lines (30 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package httpcache
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"slices"
"strings"
)
type RequestHashFn func(req *http.Request) string
func simpleRequestHash(req *http.Request) string {
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 = "|"
func hash(headers http.Header) string {
keys := make([]string, 0, len(headers))
for key := range headers {
keys = append(keys, key)
}
slices.Sort(keys)
var sb strings.Builder
for _, key := range keys {
fmt.Fprintf(&sb, "%s:%s%s", key, headers.Get(key), delimiter)
}
return sha256str([]byte(sb.String()))
}