etag.go 802 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "context"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "hash"
  8. "sync"
  9. )
  10. var errNotModified = newError(304, "Not modified", "Not modified")
  11. type eTagCalc struct {
  12. hash hash.Hash
  13. enc *json.Encoder
  14. }
  15. var eTagCalcPool = sync.Pool{
  16. New: func() interface{} {
  17. h := sha256.New()
  18. enc := json.NewEncoder(h)
  19. enc.SetEscapeHTML(false)
  20. enc.SetIndent("", "")
  21. return &eTagCalc{h, enc}
  22. },
  23. }
  24. func calcETag(ctx context.Context) string {
  25. c := eTagCalcPool.Get().(*eTagCalc)
  26. defer eTagCalcPool.Put(c)
  27. c.hash.Reset()
  28. c.hash.Write(getImageData(ctx).Bytes())
  29. footprint := c.hash.Sum(nil)
  30. c.hash.Reset()
  31. c.hash.Write(footprint)
  32. c.hash.Write([]byte(version))
  33. c.enc.Encode(conf)
  34. c.enc.Encode(getProcessingOptions(ctx))
  35. return hex.EncodeToString(c.hash.Sum(nil))
  36. }