Skip to content

Commit d71b118

Browse files
committed
Implement cookies in context, handle set/get headers
1 parent 27c9db2 commit d71b118

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

context.go

+76
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package routing
22

33
import (
44
"bytes"
5+
"github.com/gopulse/pulse-router/utils"
56
"github.com/valyala/fasthttp"
7+
"time"
68
)
79

810
type handlerFunc func(ctx *Context) error
@@ -13,6 +15,20 @@ type Context struct {
1315
paramValues []string
1416
handlers []handlerFunc
1517
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"`
1632
}
1733

1834
// NewContext returns a new Context.
@@ -81,3 +97,63 @@ func (c *Context) Reset() {
8197
func (c *Context) Abort() {
8298
c.handlerIdx = len(c.handlers)
8399
}
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

Comments
 (0)