CookieEncoding accepts a value which implements `Encode` and `Decode` methods. It calls its `Encode` on `Context.SetCookie, UpsertCookie, and SetCookieKV` methods. And on `Context.GetCookie` method it calls its `Decode`. If "cookieNames" slice is not empty then only cookies with that `Name` will be
(encoding SecureCookie, cookieNames ...string)
| 5777 | // |
| 5778 | // Example: https://github.com/kataras/iris/tree/main/_examples/cookies/securecookie |
| 5779 | func CookieEncoding(encoding SecureCookie, cookieNames ...string) CookieOption { |
| 5780 | if encoding == nil { |
| 5781 | return func(_ *Context, _ *http.Cookie, _ uint8) {} |
| 5782 | } |
| 5783 | |
| 5784 | return func(ctx *Context, c *http.Cookie, op uint8) { |
| 5785 | if op == OpCookieDel { |
| 5786 | return |
| 5787 | } |
| 5788 | |
| 5789 | if !CookieIncluded(c, cookieNames) { |
| 5790 | return |
| 5791 | } |
| 5792 | |
| 5793 | switch op { |
| 5794 | case OpCookieSet: |
| 5795 | // Should encode, it's a write to the client operation. |
| 5796 | newVal, err := encoding.Encode(c.Name, c.Value) |
| 5797 | if err != nil { |
| 5798 | ctx.Application().Logger().Error(err) |
| 5799 | c.Value = "" |
| 5800 | } else { |
| 5801 | c.Value = newVal |
| 5802 | } |
| 5803 | |
| 5804 | return |
| 5805 | case OpCookieGet: |
| 5806 | // Should decode, it's a read from the client operation. |
| 5807 | if err := encoding.Decode(c.Name, c.Value, &c.Value); err != nil { |
| 5808 | c.Value = "" |
| 5809 | } |
| 5810 | } |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | const cookieOptionsContextKey = "iris.cookie.options" |
| 5815 |
no test coverage detected
searching dependent graphs…