Skip to content

Commit

Permalink
pkg/earlydata: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
leonklingele committed Jan 26, 2023
1 parent 55f5f3c commit 4e79e32
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion middleware/earlydata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import (
"github.com/gofiber/fiber/v3"
)

const (
DefaultHeaderName = "Early-Data"
DefaultHeaderTrueValue = "1"
)

// 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

// IsEarlyData returns whether the request is an early-data request.
//
// Optional. Default: a function which checks if the "Early-Data" request header equals "1".
Expand All @@ -25,7 +35,7 @@ type Config struct {
// ConfigDefault is the default config
var ConfigDefault = Config{
IsEarlyData: func(c fiber.Ctx) bool {
return c.Get("Early-Data") == "1"
return c.Get(DefaultHeaderName) == DefaultHeaderTrueValue
},

AllowEarlyData: func(c fiber.Ctx) bool {
Expand Down
5 changes: 5 additions & 0 deletions middleware/earlydata/earlydata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func New(config ...Config) fiber.Handler {

// 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()
}

// Abort if we can't trust the early-data header
if !c.IsProxyTrusted() {
return cfg.Error
Expand Down

0 comments on commit 4e79e32

Please sign in to comment.