1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-02 11:34:20 +02:00

return ETag support

This commit is contained in:
DarthSim 2018-10-05 22:20:29 +06:00
parent 77d86d0e2d
commit 2f8d7b8ad6
4 changed files with 15 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
@ -8,14 +9,14 @@ import (
var notModifiedErr = newError(304, "Not modified", "Not modified")
func calcETag(b []byte, po *processingOptions) string {
footprint := sha256.Sum256(b)
func calcETag(ctx context.Context) []byte {
footprint := sha256.Sum256(getImageData(ctx).Bytes())
hash := sha256.New()
hash.Write(footprint[:])
hash.Write([]byte(version))
binary.Write(hash, binary.LittleEndian, conf)
binary.Write(hash, binary.LittleEndian, *po)
binary.Write(hash, binary.LittleEndian, *getProcessingOptions(ctx))
return fmt.Sprintf("%x", hash.Sum(nil))
return []byte(fmt.Sprintf("%x", hash.Sum(nil)))
}

View File

@ -208,7 +208,7 @@ func processImage(ctx context.Context) ([]byte, error) {
defer C.vips_cleanup()
data := getImageData(ctx).Bytes()
po := getprocessingOptions(ctx)
po := getProcessingOptions(ctx)
imgtype := getImageType(ctx)
if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {

View File

@ -666,6 +666,6 @@ func getImageURL(ctx context.Context) string {
return ctx.Value(imageURLCtxKey).(string)
}
func getprocessingOptions(ctx context.Context) *processingOptions {
func getProcessingOptions(ctx context.Context) *processingOptions {
return ctx.Value(processingOptionsCtxKey).(*processingOptions)
}

View File

@ -83,7 +83,7 @@ func writeCORS(rctx *fasthttp.RequestCtx) {
func respondWithImage(ctx context.Context, reqID string, rctx *fasthttp.RequestCtx, data []byte) {
rctx.SetStatusCode(200)
po := getprocessingOptions(ctx)
po := getProcessingOptions(ctx)
rctx.SetContentType(mimes[po.Format])
rctx.Response.Header.Set("Cache-Control", fmt.Sprintf("max-age=%d, public", conf.TTL))
@ -185,14 +185,14 @@ func serveHTTP(rctx *fasthttp.RequestCtx) {
checkTimeout(ctx)
// if conf.ETagEnabled {
// eTag := calcETag(b, &procOpt)
// rw.Header().Set("ETag", eTag)
if conf.ETagEnabled {
eTag := calcETag(ctx)
rctx.Response.Header.SetBytesV("ETag", eTag)
// if eTag == r.Header.Get("If-None-Match") {
// panic(notModifiedErr)
// }
// }
if bytes.Equal(eTag, rctx.Request.Header.Peek("If-None-Match")) {
panic(notModifiedErr)
}
}
checkTimeout(ctx)