cookie.go 828 B

123456789101112131415161718192021222324252627282930313233343536
  1. package binder
  2. import (
  3. "github.com/gofiber/utils/v2"
  4. "github.com/valyala/fasthttp"
  5. )
  6. // CookieBinding is the cookie binder for cookie request body.
  7. type CookieBinding struct {
  8. EnableSplitting bool
  9. }
  10. // Name returns the binding name.
  11. func (*CookieBinding) Name() string {
  12. return "cookie"
  13. }
  14. // Bind parses the request cookie and returns the result.
  15. func (b *CookieBinding) Bind(req *fasthttp.Request, out any) error {
  16. data := make(map[string][]string)
  17. for key, val := range req.Header.Cookies() {
  18. k := utils.UnsafeString(key)
  19. v := utils.UnsafeString(val)
  20. if err := formatBindData(b.Name(), out, data, k, v, b.EnableSplitting, false); err != nil {
  21. return err
  22. }
  23. }
  24. return parse(b.Name(), out, data)
  25. }
  26. // Reset resets the CookieBinding binder.
  27. func (b *CookieBinding) Reset() {
  28. b.EnableSplitting = false
  29. }