Explorar el Código

return ETag support

DarthSim hace 6 años
padre
commit
2f8d7b8ad6
Se han modificado 4 ficheros con 15 adiciones y 14 borrados
  1. 5 4
      etag.go
  2. 1 1
      process.go
  3. 1 1
      processing_options.go
  4. 8 8
      server.go

+ 5 - 4
etag.go

@@ -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)))
 }

+ 1 - 1
process.go

@@ -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 {

+ 1 - 1
processing_options.go

@@ -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)
 }

+ 8 - 8
server.go

@@ -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)