@@ -2,7 +2,9 @@ package routing
2
2
3
3
import (
4
4
"bytes"
5
+ "github.com/gopulse/pulse-router/utils"
5
6
"github.com/valyala/fasthttp"
7
+ "time"
6
8
)
7
9
8
10
type handlerFunc func (ctx * Context ) error
@@ -13,6 +15,20 @@ type Context struct {
13
15
paramValues []string
14
16
handlers []handlerFunc
15
17
handlerIdx int
18
+ Cookies * fasthttp.Cookie
19
+ }
20
+
21
+ type Cookie struct {
22
+ Name string `json:"name"`
23
+ Value string `json:"value"`
24
+ Path string `json:"path"`
25
+ Domain string `json:"domain"`
26
+ MaxAge int `json:"max_age"`
27
+ Expires time.Time `json:"expires"`
28
+ Secure bool `json:"secure"`
29
+ HTTPOnly bool `json:"http_only"`
30
+ SameSite string `json:"same_site"`
31
+ SessionOnly bool `json:"session_only"`
16
32
}
17
33
18
34
// NewContext returns a new Context.
@@ -81,3 +97,63 @@ func (c *Context) Reset() {
81
97
func (c * Context ) Abort () {
82
98
c .handlerIdx = len (c .handlers )
83
99
}
100
+
101
+ // SetCookie sets a cookie with the given name, value, and options.
102
+ func (c * Context ) SetCookie (cookie * Cookie ) {
103
+ acCookie := fasthttp .AcquireCookie ()
104
+
105
+ acCookie .SetKey (cookie .Name )
106
+ acCookie .SetValue (cookie .Value )
107
+ acCookie .SetPath (cookie .Path )
108
+ acCookie .SetDomain (cookie .Domain )
109
+ acCookie .SetSecure (cookie .Secure )
110
+ acCookie .SetHTTPOnly (cookie .HTTPOnly )
111
+ acCookie .SetSecure (cookie .Secure )
112
+ if ! cookie .SessionOnly {
113
+ acCookie .SetMaxAge (cookie .MaxAge )
114
+ acCookie .SetExpire (cookie .Expires )
115
+ }
116
+
117
+ switch utils .ToLower (cookie .SameSite ) {
118
+ case string (rune (fasthttp .CookieSameSiteStrictMode )):
119
+ acCookie .SetSameSite (fasthttp .CookieSameSiteStrictMode )
120
+ case string (rune (fasthttp .CookieSameSiteNoneMode )):
121
+ acCookie .SetSameSite (fasthttp .CookieSameSiteNoneMode )
122
+ case string (rune (fasthttp .CookieSameSiteDisabled )):
123
+ acCookie .SetSameSite (fasthttp .CookieSameSiteDisabled )
124
+ default :
125
+ acCookie .SetSameSite (fasthttp .CookieSameSiteDefaultMode )
126
+ }
127
+
128
+ c .Ctx .Response .Header .SetCookie (acCookie )
129
+ fasthttp .ReleaseCookie (acCookie )
130
+ }
131
+
132
+ // GetCookie returns the value of the cookie with the given name.
133
+ func (c * Context ) GetCookie (name string ) string {
134
+ cookie := c .Ctx .Request .Header .Cookie (name )
135
+ if cookie == nil {
136
+ return ""
137
+ }
138
+ return string (cookie )
139
+ }
140
+
141
+ // ClearCookie deletes the cookie with the given name.
142
+ func (c * Context ) ClearCookie (name string ) {
143
+ c .SetCookie (& Cookie {
144
+ Name : name ,
145
+ Value : "" ,
146
+ Path : "/" ,
147
+ MaxAge : - 1 ,
148
+ Secure : false ,
149
+ HTTPOnly : true ,
150
+ })
151
+ }
152
+
153
+ func (c * Context ) SetHeader (key , value string ) {
154
+ c .Ctx .Response .Header .Set (key , value )
155
+ }
156
+
157
+ func (c * Context ) GetHeader (key string ) string {
158
+ return string (c .Ctx .Request .Header .Peek (key ))
159
+ }
0 commit comments