| 49 | } |
| 50 | |
| 51 | func makeBuildVaryKeyFunc(hexBufPool *sync.Pool) func([]string, *fasthttp.RequestHeader) string { |
| 52 | return func(names []string, hdr *fasthttp.RequestHeader) string { |
| 53 | sum := sha256.New() |
| 54 | for _, name := range names { |
| 55 | _, _ = sum.Write(utils.UnsafeBytes(name)) //nolint:errcheck // hash.Hash.Write for std hashes never errors |
| 56 | _, _ = sum.Write([]byte{0}) //nolint:errcheck // hash.Hash.Write for std hashes never errors |
| 57 | _, _ = sum.Write(hdr.Peek(name)) //nolint:errcheck // hash.Hash.Write for std hashes never errors |
| 58 | _, _ = sum.Write([]byte{0}) //nolint:errcheck // hash.Hash.Write for std hashes never errors |
| 59 | } |
| 60 | |
| 61 | var hashBytes [sha256.Size]byte |
| 62 | sum.Sum(hashBytes[:0]) |
| 63 | |
| 64 | v := hexBufPool.Get() |
| 65 | bufPtr, ok := v.(*[]byte) |
| 66 | if !ok || bufPtr == nil { |
| 67 | b := make([]byte, hexLen) |
| 68 | bufPtr = &b |
| 69 | } |
| 70 | |
| 71 | buf := *bufPtr |
| 72 | // Defensive in case someone changed Pool.New or Put a different sized buffer. |
| 73 | if cap(buf) < hexLen { |
| 74 | buf = make([]byte, hexLen) |
| 75 | } else { |
| 76 | buf = buf[:hexLen] |
| 77 | } |
| 78 | *bufPtr = buf |
| 79 | |
| 80 | hex.Encode(buf, hashBytes[:]) |
| 81 | result := "|vary|" + string(buf) |
| 82 | |
| 83 | hexBufPool.Put(bufPtr) |
| 84 | return result |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func storeVaryManifest(ctx context.Context, manager *manager, manifestKey string, names []string, exp time.Duration) error { |
| 89 | if len(names) == 0 { |