2018-02-26 10:45:52 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-05 18:20:29 +02:00
|
|
|
"context"
|
2018-09-06 15:02:21 +02:00
|
|
|
"crypto/sha256"
|
2018-10-05 21:45:14 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"hash"
|
|
|
|
"sync"
|
2018-02-26 10:45:52 +02:00
|
|
|
)
|
|
|
|
|
2018-10-05 22:29:55 +02:00
|
|
|
var errNotModified = newError(304, "Not modified", "Not modified")
|
2018-02-26 10:45:52 +02:00
|
|
|
|
2018-10-05 21:45:14 +02:00
|
|
|
type eTagCalc struct {
|
|
|
|
hash hash.Hash
|
|
|
|
enc *json.Encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
var eTagCalcPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
h := sha256.New()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(h)
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
enc.SetIndent("", "")
|
|
|
|
|
|
|
|
return &eTagCalc{h, enc}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-05 18:20:29 +02:00
|
|
|
func calcETag(ctx context.Context) []byte {
|
2018-10-05 21:45:14 +02:00
|
|
|
c := eTagCalcPool.Get().(*eTagCalc)
|
|
|
|
defer eTagCalcPool.Put(c)
|
|
|
|
|
|
|
|
c.hash.Reset()
|
|
|
|
c.hash.Write(getImageData(ctx).Bytes())
|
|
|
|
footprint := c.hash.Sum(nil)
|
|
|
|
|
|
|
|
c.hash.Reset()
|
|
|
|
c.hash.Write(footprint)
|
|
|
|
c.hash.Write([]byte(version))
|
|
|
|
c.enc.Encode(conf)
|
|
|
|
c.enc.Encode(getProcessingOptions(ctx))
|
2018-02-26 10:45:52 +02:00
|
|
|
|
2018-10-05 21:45:14 +02:00
|
|
|
etag := make([]byte, 64)
|
|
|
|
hex.Encode(etag, c.hash.Sum(nil))
|
2018-02-26 10:45:52 +02:00
|
|
|
|
2018-10-05 21:45:14 +02:00
|
|
|
return etag
|
2018-02-26 10:45:52 +02:00
|
|
|
}
|