github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/earlydata.md (about)

     1  ---
     2  id: earlydata
     3  title: EarlyData
     4  ---
     5  
     6  The Early Data middleware for [Fiber](https://github.com/gofiber/fiber) adds support for TLS 1.3's early data ("0-RTT") feature.
     7  Citing [RFC 8446](https://datatracker.ietf.org/doc/html/rfc8446#section-2-3), when a client and server share a PSK, TLS 1.3 allows clients to send data on the first flight ("early data") to speed up the request, effectively reducing the regular 1-RTT request to a 0-RTT request.
     8  
     9  Make sure to enable fiber's `EnableTrustedProxyCheck` config option before using this middleware in order to not trust bogus HTTP request headers of the client.
    10  
    11  Also be aware that enabling support for early data in your reverse proxy (e.g. nginx, as done with a simple `ssl_early_data on;`) makes requests replayable. Refer to the following documents before continuing:
    12  
    13  - https://datatracker.ietf.org/doc/html/rfc8446#section-8
    14  - https://blog.trailofbits.com/2019/03/25/what-application-developers-need-to-know-about-tls-early-data-0rtt/
    15  
    16  By default, this middleware allows early data requests on safe HTTP request methods only and rejects the request otherwise, i.e. aborts the request before executing your handler. This behavior can be controlled by the `AllowEarlyData` config option.
    17  Safe HTTP methods — `GET`, `HEAD`, `OPTIONS` and `TRACE` — should not modify a state on the server.
    18  
    19  ## Signatures
    20  
    21  ```go
    22  func New(config ...Config) fiber.Handler
    23  ```
    24  
    25  ## Examples
    26  
    27  Import the middleware package that is part of the Fiber web framework
    28  
    29  ```go
    30  import (
    31  	"github.com/gofiber/fiber/v2"
    32  	"github.com/gofiber/fiber/v2/middleware/earlydata"
    33  )
    34  ```
    35  
    36  After you initiate your Fiber app, you can use the following possibilities:
    37  
    38  ```go
    39  // Initialize default config
    40  app.Use(earlydata.New())
    41  
    42  // Or extend your config for customization
    43  app.Use(earlydata.New(earlydata.Config{
    44  	Error: fiber.ErrTooEarly,
    45  	// ...
    46  }))
    47  ```
    48  
    49  ## Config
    50  
    51  ```go
    52  // Config defines the config for middleware.
    53  type Config struct {
    54  	// Next defines a function to skip this middleware when returned true.
    55  	//
    56  	// Optional. Default: nil
    57  	Next func(c *fiber.Ctx) bool
    58  
    59  	// IsEarlyData returns whether the request is an early-data request.
    60  	//
    61  	// Optional. Default: a function which checks if the "Early-Data" request header equals "1".
    62  	IsEarlyData func(c *fiber.Ctx) bool
    63  
    64  	// AllowEarlyData returns whether the early-data request should be allowed or rejected.
    65  	//
    66  	// Optional. Default: a function which rejects the request on unsafe and allows the request on safe HTTP request methods.
    67  	AllowEarlyData func(c *fiber.Ctx) bool
    68  
    69  	// Error is returned in case an early-data request is rejected.
    70  	//
    71  	// Optional. Default: fiber.ErrTooEarly.
    72  	Error error
    73  }
    74  ```
    75  
    76  ## Default Config
    77  
    78  ```go
    79  var ConfigDefault = Config{
    80  	IsEarlyData: func(c *fiber.Ctx) bool {
    81  		return c.Get(DefaultHeaderName) == DefaultHeaderTrueValue
    82  	},
    83  
    84  	AllowEarlyData: func(c *fiber.Ctx) bool {
    85  		return fiber.IsMethodSafe(c.Method())
    86  	},
    87  
    88  	Error: fiber.ErrTooEarly,
    89  }
    90  ```
    91  
    92  ## Constants
    93  
    94  ```go
    95  const (
    96  	DefaultHeaderName      = "Early-Data"
    97  	DefaultHeaderTrueValue = "1"
    98  )
    99  ```