From 532254e03be709965c5502657f17c043a04e7d5f Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Thu, 18 Aug 2016 00:28:03 +0300 Subject: [PATCH] Bring back AcquireCookie and ReleaseCookie usage --- context.go | 25 +++++++++++++------------ sessions.go | 5 +++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/context.go b/context.go index ddb85b0a..8dfcd8bb 100644 --- a/context.go +++ b/context.go @@ -790,22 +790,22 @@ func (ctx *Context) SetCookie(cookie *fasthttp.Cookie) { // SetCookieKV adds a cookie, receives just a key(string) and a value(string) func (ctx *Context) SetCookieKV(key, value string) { - //c := fasthttp.AcquireCookie() - c := &fasthttp.Cookie{} + c := fasthttp.AcquireCookie() + // c := &fasthttp.Cookie{} c.SetKey(key) c.SetValue(value) c.SetHTTPOnly(true) c.SetExpire(time.Now().Add(time.Duration(120) * time.Minute)) ctx.SetCookie(c) - //fasthttp.ReleaseCookie(c) + fasthttp.ReleaseCookie(c) } // RemoveCookie deletes a cookie by it's name/key func (ctx *Context) RemoveCookie(name string) { ctx.Response.Header.DelCookie(name) - // cookie := fasthttp.AcquireCookie() - cookie := &fasthttp.Cookie{} + cookie := fasthttp.AcquireCookie() + //cookie := &fasthttp.Cookie{} cookie.SetKey(name) cookie.SetValue("") cookie.SetPath("/") @@ -813,7 +813,7 @@ func (ctx *Context) RemoveCookie(name string) { exp := time.Now().Add(-time.Duration(1) * time.Minute) //RFC says 1 second, but let's do it 1 minute to make sure is working... cookie.SetExpire(exp) ctx.SetCookie(cookie) - //fasthttp.ReleaseCookie(cookie) + fasthttp.ReleaseCookie(cookie) // delete request's cookie also, which is temporarly available ctx.Request.Header.DelCookie(name) } @@ -911,25 +911,26 @@ func (ctx *Context) GetFlash(key string) (string, error) { func (ctx *Context) SetFlash(key string, value string) { cKey := flashMessageCookiePrefix + key cValue := base64.URLEncoding.EncodeToString([]byte(value)) - /* see https://github.com/kataras/iris/issues/351 - c := fasthttp.AcquireCookie() this occurs strange behavior if called inside a handler which ctx.Session() is already called for the first time + + c := fasthttp.AcquireCookie() c.SetKey(cKey) c.SetValue(cValue) c.SetPath("/") c.SetHTTPOnly(true) ctx.RequestCtx.Response.Header.SetCookie(c) fasthttp.ReleaseCookie(c) - */ - // but this works, and the above: + + // if any bug on the future: this works, and the above: //ctx.RequestCtx.Request.Header.SetCookie(cKey, cValue) //ctx.RequestCtx.Response.Header.Add("Set-Cookie", cKey+"="+cValue+"; Path:/; HttpOnly") // - c := &fasthttp.Cookie{} + + /*c := &fasthttp.Cookie{} c.SetKey(cKey) c.SetValue(cValue) c.SetPath("/") c.SetHTTPOnly(true) - ctx.SetCookie(c) + ctx.SetCookie(c)*/ } diff --git a/sessions.go b/sessions.go index 8268db09..343c6d34 100644 --- a/sessions.go +++ b/sessions.go @@ -332,8 +332,8 @@ func (m *sessionsManager) start(ctx *Context) *session { if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie sid := m.generateSessionID() session = m.provider.init(sid) - //cookie := fasthttp.AcquireCookie() strange errors when c.SetFlash (old) - cookie := &fasthttp.Cookie{} + cookie := fasthttp.AcquireCookie() + //cookie := &fasthttp.Cookie{} // The RFC makes no mention of encoding url value, so here I think to encode both sessionid key and the value using the safe(to put and to use as cookie) url-encoding cookie.SetKey(m.config.Cookie) cookie.SetValue(sid) @@ -377,6 +377,7 @@ func (m *sessionsManager) start(ctx *Context) *session { } // if it's -1 then the cookie is deleted when the browser closes ctx.SetCookie(cookie) + fasthttp.ReleaseCookie(cookie) } else { session = m.provider.read(cookieValue) }