| 1 | package etagscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.20 bytes/" |
| 5 | "hash/crc32scip-go gomod github.com/golang/go/src go1.20 `hash/crc32`/" |
| 6 | |
| 7 | "github.com/gofiber/fiber/v2scip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/" |
| 8 | |
| 9 | "github.com/valyala/bytebufferpoolscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/" |
| 10 | ) |
| 11 | |
| 12 | // New creates a new middleware handler |
| 13 | func Newscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/New().(config ...Configscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/Config#) fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.Handlerscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Handler# { |
| 14 | // Set default config |
| 15 | cfg := configDefaultscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/configDefault().(config...) |
| 16 | |
| 17 | var ( |
| 18 | normalizedHeaderETag = []byte("Etag") |
| 19 | weakPrefix = []byte("W/") |
| 20 | ) |
| 21 | |
| 22 | const crcPol = 0xD5828281 |
| 23 | crc32q := crc32scip-go gomod github.com/golang/go/src go1.20 `hash/crc32`/.MakeTablescip-go gomod github.com/golang/go/src go1.20 `hash/crc32`/MakeTable().(crcPol) |
| 24 | |
| 25 | // Return new handler |
| 26 | return func(c *fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.Ctxscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#) error { |
| 27 | // Don't execute middleware if Next returns true |
| 28 | if cfg.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/Config#Next. != nil && cfg.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/Config#Next.(c) { |
| 29 | return c.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Next().() |
| 30 | } |
| 31 | |
| 32 | // Return err if next handler returns one |
| 33 | if err := c.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Next().(); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
| 37 | // Don't generate ETags for invalid responses |
| 38 | if c.Responsescip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Response().().StatusCodescip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/Response#StatusCode().() != fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.StatusOKscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/StatusOK. { |
| 39 | return nil |
| 40 | } |
| 41 | body := c.Responsescip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Response().().Bodyscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/Response#Body().() |
| 42 | // Skips ETag if no response body is present |
| 43 | if len(body) == 0 { |
| 44 | return nil |
| 45 | } |
| 46 | // Skip ETag if header is already present |
| 47 | if c.Responsescip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Response().().Headerscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/Response#Header..PeekBytesscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/ResponseHeader#PeekBytes().(normalizedHeaderETag) != nil { |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // Generate ETag for response |
| 52 | bb := bytebufferpoolscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/.Getscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/Get().() |
| 53 | defer bytebufferpoolscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/.Putscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/Put().(bb) |
| 54 | |
| 55 | // Enable weak tag |
| 56 | if cfg.Weakscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/Config#Weak. { |
| 57 | _, _ = bb.Writescip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#Write().(weakPrefix) |
| 58 | } |
| 59 | |
| 60 | _ = bb.WriteBytescip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#WriteByte().('"') |
| 61 | bb.Bscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#B. = appendUintscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/appendUint().(bb.Bytesscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#Bytes().(), uint32(len(body))) |
| 62 | _ = bb.WriteBytescip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#WriteByte().('-') |
| 63 | bb.Bscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#B. = appendUintscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/appendUint().(bb.Bytesscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#Bytes().(), crc32scip-go gomod github.com/golang/go/src go1.20 `hash/crc32`/.Checksumscip-go gomod github.com/golang/go/src go1.20 `hash/crc32`/Checksum().(body, crc32q)) |
| 64 | _ = bb.WriteBytescip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#WriteByte().('"') |
| 65 | |
| 66 | etag := bb.Bytesscip-go gomod github.com/valyala/bytebufferpool v1.0.0 `github.com/valyala/bytebufferpool`/ByteBuffer#Bytes().() |
| 67 | |
| 68 | // Get ETag header from request |
| 69 | clientEtag := c.Requestscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Request().().Headerscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/Request#Header..Peekscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/RequestHeader#Peek().(fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.HeaderIfNoneMatchscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/HeaderIfNoneMatch.) |
| 70 | |
| 71 | // Check if client's ETag is weak |
| 72 | if bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 bytes/HasPrefix().(clientEtag, weakPrefix) { |
| 73 | // Check if server's ETag is weak |
| 74 | if bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Equalscip-go gomod github.com/golang/go/src go1.20 bytes/Equal().(clientEtag[2:], etag) || bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Equalscip-go gomod github.com/golang/go/src go1.20 bytes/Equal().(clientEtag[2:], etag[2:]) { |
| 75 | // W/1 == 1 || W/1 == W/1 |
| 76 | c.Contextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Context().().ResetBodyscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/RequestCtx#ResetBody().() |
| 77 | |
| 78 | return c.SendStatusscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#SendStatus().(fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.StatusNotModifiedscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/StatusNotModified.) |
| 79 | } |
| 80 | // W/1 != W/2 || W/1 != 2 |
| 81 | c.Responsescip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Response().().Headerscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/Response#Header..SetCanonicalscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/ResponseHeader#SetCanonical().(normalizedHeaderETag, etag) |
| 82 | |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | if bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Containsscip-go gomod github.com/golang/go/src go1.20 bytes/Contains().(clientEtag, etag) { |
| 87 | // 1 == 1 |
| 88 | c.Contextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Context().().ResetBodyscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/RequestCtx#ResetBody().() |
| 89 | |
| 90 | return c.SendStatusscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#SendStatus().(fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.StatusNotModifiedscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/StatusNotModified.) |
| 91 | } |
| 92 | // 1 != 2 |
| 93 | c.Responsescip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Response().().Headerscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/Response#Header..SetCanonicalscip-go gomod github.com/valyala/fasthttp v1.51.0 `github.com/valyala/fasthttp`/ResponseHeader#SetCanonical().(normalizedHeaderETag, etag) |
| 94 | |
| 95 | return nil |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // appendUint appends n to dst and returns the extended dst. |
| 100 | func appendUintscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/etag`/appendUint().(dst []byte, n uint32) []byte { |
| 101 | var b [20]byte |
| 102 | buf := b[:] |
| 103 | i := len(buf) |
| 104 | var q uint32 |
| 105 | for n >= 10 { |
| 106 | i-- |
| 107 | q = n / 10 |
| 108 | buf[i] = '0' + byte(n-q*10) |
| 109 | n = q |
| 110 | } |
| 111 | i-- |
| 112 | buf[i] = '0' + byte(n) |
| 113 | |
| 114 | dst = append(dst, buf[i:]...) |
| 115 | return dst |
| 116 | } |