Skip to content

Commit

Permalink
Do not calculate favicon len on each handler call
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Mar 4, 2024
1 parent dac9a75 commit d7fce2d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
42 changes: 22 additions & 20 deletions middleware/favicon/favicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,34 @@ func New(config ...Config) fiber.Handler {
}
}

// Load icon if provided
// Load iconData if provided
var (
err error
icon []byte
iconLen string
err error
iconData []byte
iconLenHeader string
iconLen int
)
if cfg.Data != nil {
// use the provided favicon data
icon = cfg.Data
iconLen = strconv.Itoa(len(cfg.Data))
iconData = cfg.Data
iconLenHeader = strconv.Itoa(len(cfg.Data))
iconLen = len(cfg.Data)
} else if cfg.File != "" {
// read from configured filesystem if present
if cfg.FileSystem != nil {
f, err := cfg.FileSystem.Open(cfg.File)
if err != nil {
panic(err)
}
if icon, err = io.ReadAll(f); err != nil {
if iconData, err = io.ReadAll(f); err != nil {
panic(err)
}
} else if icon, err = os.ReadFile(cfg.File); err != nil {
} else if iconData, err = os.ReadFile(cfg.File); err != nil {
panic(err)
}

iconLen = strconv.Itoa(len(icon))
iconLenHeader = strconv.Itoa(len(iconData))
iconLen = len(iconData)
}

// Return new handler
Expand All @@ -121,24 +124,23 @@ func New(config ...Config) fiber.Handler {
return c.Next()
}

// Only allow GET, HEAD and OPTIONS requests
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead {
if c.Method() != fiber.MethodOptions {
c.Status(fiber.StatusMethodNotAllowed)
} else {
c.Status(fiber.StatusOK)
}
// Only allow GET, HEAD, and OPTIONS requests
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead && c.Method() != fiber.MethodOptions {
c.Status(fiber.StatusMethodNotAllowed)
c.Set(fiber.HeaderAllow, hAllow)
c.Set(fiber.HeaderContentLength, hZero)
return nil
}

if c.Method() == fiber.MethodOptions {
return c.SendStatus(fiber.StatusOK)
}

// Serve cached favicon
if len(icon) > 0 {
c.Set(fiber.HeaderContentLength, iconLen)
if iconLen > 0 {
c.Set(fiber.HeaderContentLength, iconLenHeader)
c.Set(fiber.HeaderContentType, hType)
c.Set(fiber.HeaderCacheControl, cfg.CacheControl)
return c.Status(fiber.StatusOK).Send(icon)
return c.Status(fiber.StatusOK).Send(iconData)
}

return c.SendStatus(fiber.StatusNoContent)
Expand Down
4 changes: 1 addition & 3 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,14 @@ func Test_Proxy_DoDeadline_RestoreOriginalURL(t *testing.T) {

// go test -race -run Test_Proxy_DoDeadline_PastDeadline
func Test_Proxy_DoDeadline_PastDeadline(t *testing.T) {
t.Parallel()

_, addr := createProxyTestServer(t, func(c fiber.Ctx) error {
time.Sleep(time.Second * 5)
return c.SendString("proxied")
})

app := fiber.New()
app.Get("/test", func(c fiber.Ctx) error {
return DoDeadline(c, "http://"+addr, time.Now().Add(2*time.Second))
return DoDeadline(c, "http://"+addr, time.Now().Add(time.Second))
})

_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), int((1*time.Second)/time.Millisecond))
Expand Down

0 comments on commit d7fce2d

Please sign in to comment.