github.com/gofiber/fiber/v2@v2.47.0/middleware/earlydata/earlydata.go (about) 1 package earlydata 2 3 import ( 4 "github.com/gofiber/fiber/v2" 5 ) 6 7 const ( 8 localsKeyAllowed = "earlydata_allowed" 9 ) 10 11 func IsEarly(c *fiber.Ctx) bool { 12 return c.Locals(localsKeyAllowed) != nil 13 } 14 15 // New creates a new middleware handler 16 // https://datatracker.ietf.org/doc/html/rfc8470#section-5.1 17 func New(config ...Config) fiber.Handler { 18 // Set default config 19 cfg := configDefault(config...) 20 21 // Return new handler 22 return func(c *fiber.Ctx) error { 23 // Don't execute middleware if Next returns true 24 if cfg.Next != nil && cfg.Next(c) { 25 return c.Next() 26 } 27 28 // Abort if we can't trust the early-data header 29 if !c.IsProxyTrusted() { 30 return cfg.Error 31 } 32 33 // Continue stack if request is not an early-data request 34 if !cfg.IsEarlyData(c) { 35 return c.Next() 36 } 37 38 // Continue stack if we allow early-data for this request 39 if cfg.AllowEarlyData(c) { 40 _ = c.Locals(localsKeyAllowed, true) 41 return c.Next() 42 } 43 44 // Else return our error 45 return cfg.Error 46 } 47 }