Adding upstream version 2.52.6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
a960158181
commit
6d002e9543
441 changed files with 95392 additions and 0 deletions
146
middleware/favicon/favicon.go
Normal file
146
middleware/favicon/favicon.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package favicon
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// Config defines the config for middleware.
|
||||
type Config struct {
|
||||
// Next defines a function to skip this middleware when returned true.
|
||||
//
|
||||
// Optional. Default: nil
|
||||
Next func(c *fiber.Ctx) bool
|
||||
|
||||
// Raw data of the favicon file
|
||||
//
|
||||
// Optional. Default: nil
|
||||
Data []byte `json:"-"`
|
||||
|
||||
// File holds the path to an actual favicon that will be cached
|
||||
//
|
||||
// Optional. Default: ""
|
||||
File string `json:"file"`
|
||||
|
||||
// URL for favicon handler
|
||||
//
|
||||
// Optional. Default: "/favicon.ico"
|
||||
URL string `json:"url"`
|
||||
|
||||
// FileSystem is an optional alternate filesystem to search for the favicon in.
|
||||
// An example of this could be an embedded or network filesystem
|
||||
//
|
||||
// Optional. Default: nil
|
||||
FileSystem http.FileSystem `json:"-"`
|
||||
|
||||
// CacheControl defines how the Cache-Control header in the response should be set
|
||||
//
|
||||
// Optional. Default: "public, max-age=31536000"
|
||||
CacheControl string `json:"cache_control"`
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
File: "",
|
||||
URL: fPath,
|
||||
CacheControl: "public, max-age=31536000",
|
||||
}
|
||||
|
||||
const (
|
||||
fPath = "/favicon.ico"
|
||||
hType = "image/x-icon"
|
||||
hAllow = "GET, HEAD, OPTIONS"
|
||||
hZero = "0"
|
||||
)
|
||||
|
||||
// New creates a new middleware handler
|
||||
func New(config ...Config) fiber.Handler {
|
||||
// Set default config
|
||||
cfg := ConfigDefault
|
||||
|
||||
// Override config if provided
|
||||
if len(config) > 0 {
|
||||
cfg = config[0]
|
||||
|
||||
// Set default values
|
||||
if cfg.Next == nil {
|
||||
cfg.Next = ConfigDefault.Next
|
||||
}
|
||||
if cfg.URL == "" {
|
||||
cfg.URL = ConfigDefault.URL
|
||||
}
|
||||
if cfg.File == "" {
|
||||
cfg.File = ConfigDefault.File
|
||||
}
|
||||
if cfg.CacheControl == "" {
|
||||
cfg.CacheControl = ConfigDefault.CacheControl
|
||||
}
|
||||
}
|
||||
|
||||
// Load icon if provided
|
||||
var (
|
||||
err error
|
||||
icon []byte
|
||||
iconLen string
|
||||
)
|
||||
if cfg.Data != nil {
|
||||
// use the provided favicon data
|
||||
icon = cfg.Data
|
||||
iconLen = strconv.Itoa(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 {
|
||||
panic(err)
|
||||
}
|
||||
} else if icon, err = os.ReadFile(cfg.File); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
iconLen = strconv.Itoa(len(icon))
|
||||
}
|
||||
|
||||
// Return new handler
|
||||
return func(c *fiber.Ctx) error {
|
||||
// Don't execute middleware if Next returns true
|
||||
if cfg.Next != nil && cfg.Next(c) {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// Only respond to favicon requests
|
||||
if c.Path() != cfg.URL {
|
||||
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)
|
||||
}
|
||||
c.Set(fiber.HeaderAllow, hAllow)
|
||||
c.Set(fiber.HeaderContentLength, hZero)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Serve cached favicon
|
||||
if len(icon) > 0 {
|
||||
c.Set(fiber.HeaderContentLength, iconLen)
|
||||
c.Set(fiber.HeaderContentType, hType)
|
||||
c.Set(fiber.HeaderCacheControl, cfg.CacheControl)
|
||||
return c.Status(fiber.StatusOK).Send(icon)
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
}
|
208
middleware/favicon/favicon_test.go
Normal file
208
middleware/favicon/favicon_test.go
Normal file
|
@ -0,0 +1,208 @@
|
|||
//nolint:bodyclose // Much easier to just ignore memory leaks in tests
|
||||
package favicon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// go test -run Test_Middleware_Favicon
|
||||
func Test_Middleware_Favicon(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New())
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
// Skip Favicon middleware
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusNoContent, resp.StatusCode, "Status code")
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest(fiber.MethodOptions, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest(fiber.MethodPut, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusMethodNotAllowed, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, strings.Join([]string{fiber.MethodGet, fiber.MethodHead, fiber.MethodOptions}, ", "), resp.Header.Get(fiber.HeaderAllow))
|
||||
}
|
||||
|
||||
// go test -run Test_Middleware_Favicon_Not_Found
|
||||
func Test_Middleware_Favicon_Not_Found(t *testing.T) {
|
||||
t.Parallel()
|
||||
defer func() {
|
||||
if err := recover(); err == nil {
|
||||
t.Fatal("should cache panic")
|
||||
}
|
||||
}()
|
||||
|
||||
fiber.New().Use(New(Config{
|
||||
File: "non-exist.ico",
|
||||
}))
|
||||
}
|
||||
|
||||
// go test -run Test_Middleware_Favicon_Found
|
||||
func Test_Middleware_Favicon_Found(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
File: "../../.github/testdata/favicon.ico",
|
||||
}))
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
||||
utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
||||
}
|
||||
|
||||
// go test -run Test_Custom_Favicon_Url
|
||||
func Test_Custom_Favicon_Url(t *testing.T) {
|
||||
app := fiber.New()
|
||||
const customURL = "/favicon.svg"
|
||||
app.Use(New(Config{
|
||||
File: "../../.github/testdata/favicon.ico",
|
||||
URL: customURL,
|
||||
}))
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(http.MethodGet, customURL, nil))
|
||||
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
||||
}
|
||||
|
||||
// go test -run Test_Custom_Favicon_Data
|
||||
func Test_Custom_Favicon_Data(t *testing.T) {
|
||||
data, err := os.ReadFile("../../.github/testdata/favicon.ico")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Data: data,
|
||||
}))
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
||||
utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
||||
}
|
||||
|
||||
// mockFS wraps local filesystem for the purposes of
|
||||
// Test_Middleware_Favicon_FileSystem located below
|
||||
// TODO use os.Dir if fiber upgrades to 1.16
|
||||
type mockFS struct{}
|
||||
|
||||
func (mockFS) Open(name string) (http.File, error) {
|
||||
if name == "/" {
|
||||
name = "."
|
||||
} else {
|
||||
name = strings.TrimPrefix(name, "/")
|
||||
}
|
||||
file, err := os.Open(name) //nolint:gosec // We're in a test func, so this is fine
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open: %w", err)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// go test -run Test_Middleware_Favicon_FileSystem
|
||||
func Test_Middleware_Favicon_FileSystem(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
File: "../../.github/testdata/favicon.ico",
|
||||
FileSystem: mockFS{},
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
||||
utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
||||
}
|
||||
|
||||
// go test -run Test_Middleware_Favicon_CacheControl
|
||||
func Test_Middleware_Favicon_CacheControl(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
CacheControl: "public, max-age=100",
|
||||
File: "../../.github/testdata/favicon.ico",
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
||||
utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_Middleware_Favicon -benchmem -count=4
|
||||
func Benchmark_Middleware_Favicon(b *testing.B) {
|
||||
app := fiber.New()
|
||||
app.Use(New())
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
})
|
||||
handler := app.Handler()
|
||||
|
||||
c := &fasthttp.RequestCtx{}
|
||||
c.Request.SetRequestURI("/")
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
handler(c)
|
||||
}
|
||||
}
|
||||
|
||||
// go test -run Test_Favicon_Next
|
||||
func Test_Favicon_Next(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
app.Use(New(Config{
|
||||
Next: func(_ *fiber.Ctx) bool {
|
||||
return true
|
||||
},
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue