github.com/gofiber/fiber/v2@v2.47.0/middleware/earlydata/config.go (about) 1 package earlydata 2 3 import ( 4 "github.com/gofiber/fiber/v2" 5 ) 6 7 const ( 8 DefaultHeaderName = "Early-Data" 9 DefaultHeaderTrueValue = "1" 10 ) 11 12 // Config defines the config for middleware. 13 type Config struct { 14 // Next defines a function to skip this middleware when returned true. 15 // 16 // Optional. Default: nil 17 Next func(c *fiber.Ctx) bool 18 19 // IsEarlyData returns whether the request is an early-data request. 20 // 21 // Optional. Default: a function which checks if the "Early-Data" request header equals "1". 22 IsEarlyData func(c *fiber.Ctx) bool 23 24 // AllowEarlyData returns whether the early-data request should be allowed or rejected. 25 // 26 // Optional. Default: a function which rejects the request on unsafe and allows the request on safe HTTP request methods. 27 AllowEarlyData func(c *fiber.Ctx) bool 28 29 // Error is returned in case an early-data request is rejected. 30 // 31 // Optional. Default: fiber.ErrTooEarly. 32 Error error 33 } 34 35 // ConfigDefault is the default config 36 var ConfigDefault = Config{ 37 IsEarlyData: func(c *fiber.Ctx) bool { 38 return c.Get(DefaultHeaderName) == DefaultHeaderTrueValue 39 }, 40 41 AllowEarlyData: func(c *fiber.Ctx) bool { 42 return fiber.IsMethodSafe(c.Method()) 43 }, 44 45 Error: fiber.ErrTooEarly, 46 } 47 48 // Helper function to set default values 49 func configDefault(config ...Config) Config { 50 // Return default config if nothing provided 51 if len(config) < 1 { 52 return ConfigDefault 53 } 54 55 // Override default config 56 cfg := config[0] 57 58 // Set default values 59 60 if cfg.IsEarlyData == nil { 61 cfg.IsEarlyData = ConfigDefault.IsEarlyData 62 } 63 64 if cfg.AllowEarlyData == nil { 65 cfg.AllowEarlyData = ConfigDefault.AllowEarlyData 66 } 67 68 if cfg.Error == nil { 69 cfg.Error = ConfigDefault.Error 70 } 71 72 return cfg 73 }