Skip to content

Commit

Permalink
⚰️ chore: removed deprecated ETag
Browse files Browse the repository at this point in the history
  • Loading branch information
balcieren committed Sep 5, 2022
1 parent 61b07f1 commit 853aeaa
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 199 deletions.
12 changes: 0 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ type Config struct {
// Default: false
UnescapePath bool `json:"unescape_path"`

// Enable or disable ETag header generation, since both weak and strong etags are generated
// using the same hashing method (CRC-32). Weak ETags are the default when enabled.
//
// Default: false
ETag bool `json:"etag"`

// Max body size that the server accepts.
// -1 will decline any body size
//
Expand Down Expand Up @@ -481,12 +475,6 @@ func New(config ...Config) *App {
app.config = config[0]
}

if app.config.ETag {
if !IsChild() {
fmt.Println("[Warning] Config.ETag is deprecated since v2.0.6, please use 'middleware/etag'.")
}
}

// Override default values
if app.config.BodyLimit == 0 {
app.config.BodyLimit = DefaultBodyLimit
Expand Down
24 changes: 0 additions & 24 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,30 +985,6 @@ func Benchmark_AcquireCtx(b *testing.B) {
}
}

// go test -v -run=^$ -bench=Benchmark_App_ETag -benchmem -count=4
func Benchmark_App_ETag(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.Send([]byte("Hello, World!"))
for n := 0; n < b.N; n++ {
setETag(c, false)
}
utils.AssertEqual(b, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
}

// go test -v -run=^$ -bench=Benchmark_App_ETag_Weak -benchmem -count=4
func Benchmark_App_ETag_Weak(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.Send([]byte("Hello, World!"))
for n := 0; n < b.N; n++ {
setETag(c, true)
}
utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
}

// go test -run Test_NewError
func Test_NewError(t *testing.T) {
err := NewError(StatusForbidden, "permission denied")
Expand Down
6 changes: 1 addition & 5 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,12 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
}

// Find match in stack
match, err := app.next(c)
_, err := app.next(c)
if err != nil {
if catch := c.app.ErrorHandler(c, err); catch != nil {
_ = c.SendStatus(StatusInternalServerError)
}
}
// Generate ETag if enabled
if match && app.config.ETag {
setETag(c, false)
}

// Release Ctx
app.ReleaseCtx(c)
Expand Down
15 changes: 0 additions & 15 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,6 @@ func Test_Ensure_Router_Interface_Implementation(t *testing.T) {
utils.AssertEqual(t, true, ok)
}

func Test_Router_Handler_SetETag(t *testing.T) {
app := New()
app.config.ETag = true

app.Get("/", func(c *Ctx) error {
return c.SendString("Hello, World!")
})

c := &fasthttp.RequestCtx{}

app.Handler()(c)

utils.AssertEqual(t, `"13-1831710635"`, string(c.Response.Header.Peek(HeaderETag)))
}

func Test_Router_Handler_Catch_Error(t *testing.T) {
app := New()
app.config.ErrorHandler = func(ctx *Ctx, err error) error {
Expand Down
48 changes: 0 additions & 48 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package fiber
import (
"bytes"
"crypto/tls"
"fmt"
"hash/crc32"
"io"
"log"
"net"
Expand Down Expand Up @@ -175,52 +173,6 @@ func defaultString(value string, defaultValue []string) string {

const normalizedHeaderETag = "Etag"

// Generate and set ETag header to response
func setETag(c *Ctx, weak bool) {
// Don't generate ETags for invalid responses
if c.fasthttp.Response.StatusCode() != StatusOK {
return
}
body := c.fasthttp.Response.Body()
// Skips ETag if no response body is present
if len(body) == 0 {
return
}
// Get ETag header from request
clientEtag := c.Get(HeaderIfNoneMatch)

// Generate ETag for response
crc32q := crc32.MakeTable(0xD5828281)
etag := fmt.Sprintf("\"%d-%v\"", len(body), crc32.Checksum(body, crc32q))

// Enable weak tag
if weak {
etag = "W/" + etag
}

// Check if client's ETag is weak
if strings.HasPrefix(clientEtag, "W/") {
// Check if server's ETag is weak
if clientEtag[2:] == etag || clientEtag[2:] == etag[2:] {
// W/1 == 1 || W/1 == W/1
_ = c.SendStatus(StatusNotModified)
c.fasthttp.ResetBody()
return
}
// W/1 != W/2 || W/1 != 2
c.setCanonical(normalizedHeaderETag, etag)
return
}
if strings.Contains(clientEtag, etag) {
// 1 == 1
_ = c.SendStatus(StatusNotModified)
c.fasthttp.ResetBody()
return
}
// 1 != 2
c.setCanonical(normalizedHeaderETag, etag)
}

func getGroupPath(prefix, path string) string {
if len(path) == 0 || path == "/" {
return prefix
Expand Down
95 changes: 0 additions & 95 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,89 +16,6 @@ import (
"github.com/valyala/fasthttp"
)

// go test -v -run=Test_Utils_ -count=3
func Test_Utils_ETag(t *testing.T) {
app := New()
t.Run("Not Status OK", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Status(201)
setETag(c, false)
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
})

t.Run("No Body", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
setETag(c, false)
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
})

t.Run("Has HeaderIfNoneMatch", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Request().Header.Set(HeaderIfNoneMatch, `"13-1831710635"`)
setETag(c, false)
utils.AssertEqual(t, 304, c.Response().StatusCode())
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
utils.AssertEqual(t, "", string(c.Response().Body()))
})

t.Run("No HeaderIfNoneMatch", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
setETag(c, false)
utils.AssertEqual(t, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
})
}

// go test -v -run=^$ -bench=Benchmark_App_ETag -benchmem -count=4
func Benchmark_Utils_ETag(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
for n := 0; n < b.N; n++ {
setETag(c, false)
}
utils.AssertEqual(b, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
}

// go test -v -run=Test_Utils_ETag_Weak -count=1
func Test_Utils_ETag_Weak(t *testing.T) {
app := New()
t.Run("Set Weak", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
setETag(c, true)
utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
})

t.Run("Match Weak ETag", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Request().Header.Set(HeaderIfNoneMatch, `W/"13-1831710635"`)
setETag(c, true)
utils.AssertEqual(t, 304, c.Response().StatusCode())
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
utils.AssertEqual(t, "", string(c.Response().Body()))
})

t.Run("Not Match Weak ETag", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Request().Header.Set(HeaderIfNoneMatch, `W/"13-1831710635xx"`)
setETag(c, true)
utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
})
}

func Test_Utils_UniqueRouteStack(t *testing.T) {
route1 := &Route{}
route2 := &Route{}
Expand Down Expand Up @@ -127,18 +44,6 @@ func Test_Utils_UniqueRouteStack(t *testing.T) {
)
}

// go test -v -run=^$ -bench=Benchmark_App_ETag_Weak -benchmem -count=4
func Benchmark_Utils_ETag_Weak(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
for n := 0; n < b.N; n++ {
setETag(c, true)
}
utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
}

func Test_Utils_getGroupPath(t *testing.T) {
t.Parallel()
res := getGroupPath("/v1", "/")
Expand Down

1 comment on commit 853aeaa

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 853aeaa Previous: 81d89ee Ratio
Benchmark_Ctx_Protocol 20.96 ns/op 0 B/op 0 allocs/op 2.751 ns/op 0 B/op 0 allocs/op 7.62

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.