diff options
author | swh <swhbox@foxmail.com> | 2017-04-20 09:44:54 +0300 |
---|---|---|
committer | swh <swhbox@foxmail.com> | 2017-04-20 09:44:54 +0300 |
commit | 793a11a5b02a3dded19b557be93d2732c3aad9f0 (patch) | |
tree | 17696eced7a03f9de2a068110df0fc7c4c6520d9 /store.go | |
parent | 7ce48c6de5e67f5ec9aa46e6cad2432addb49149 (diff) |
update
Diffstat (limited to 'store.go')
-rw-r--r-- | store.go | 24 |
1 files changed, 11 insertions, 13 deletions
@@ -14,7 +14,6 @@ import ( "github.com/gorilla/securecookie" "github.com/webx-top/echo" - "github.com/webx-top/echo/engine" ) // Store is an interface for custom session stores. @@ -28,10 +27,10 @@ type Store interface { // // Note that New should never return a nil session, even in the case of // an error if using the Registry infrastructure to cache the session. - New(r engine.Request, name string) (*Session, error) + New(ctx echo.Context, name string) (*Session, error) // Save should persist session to the underlying store implementation. - Save(r engine.Request, w engine.Response, s *Session) error + Save(ctx echo.Context, s *Session) error } // CookieStore ---------------------------------------------------------------- @@ -86,13 +85,13 @@ func (s *CookieStore) Get(ctx echo.Context, name string) (*Session, error) { // The difference between New() and Get() is that calling New() twice will // decode the session data twice, while Get() registers and reuses the same // decoded session after the first call. -func (s *CookieStore) New(r engine.Request, name string) (*Session, error) { +func (s *CookieStore) New(ctx echo.Context, name string) (*Session, error) { session := NewSession(s, name) opts := *s.Options session.Options = &opts session.IsNew = true var err error - if v := r.Cookie(name); v != `` { + if v := ctx.Request().Cookie(name); len(v) > 0 { err = securecookie.DecodeMulti(name, v, &session.Values, s.Codecs...) if err == nil { @@ -103,14 +102,13 @@ func (s *CookieStore) New(r engine.Request, name string) (*Session, error) { } // Save adds a single session to the response. -func (s *CookieStore) Save(r engine.Request, w engine.Response, - session *Session) error { +func (s *CookieStore) Save(ctx echo.Context, session *Session) error { encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, s.Codecs...) if err != nil { return err } - w.SetCookie(NewCookie(session.Name(), encoded, session.Options)) + SetCookie(ctx, session.Name(), encoded, session.Options) return nil } @@ -187,13 +185,13 @@ func (s *FilesystemStore) Get(ctx echo.Context, name string) (*Session, error) { // New returns a session for the given name without adding it to the registry. // // See CookieStore.New(). -func (s *FilesystemStore) New(r engine.Request, name string) (*Session, error) { +func (s *FilesystemStore) New(ctx echo.Context, name string) (*Session, error) { session := NewSession(s, name) opts := *s.Options session.Options = &opts session.IsNew = true var err error - if v := r.Cookie(name); v != `` { + if v := ctx.Request().Cookie(name); len(v) > 0 { err = securecookie.DecodeMulti(name, v, &session.ID, s.Codecs...) if err == nil { err = s.load(session) @@ -206,9 +204,9 @@ func (s *FilesystemStore) New(r engine.Request, name string) (*Session, error) { } // Save adds a single session to the response. -func (s *FilesystemStore) Save(r engine.Request, w engine.Response, +func (s *FilesystemStore) Save(ctx echo.Context, session *Session) error { - if session.ID == "" { + if len(session.ID) == 0 { // Because the ID is used in the filename, encode it to // use alphanumeric characters only. session.ID = strings.TrimRight( @@ -223,7 +221,7 @@ func (s *FilesystemStore) Save(r engine.Request, w engine.Response, if err != nil { return err } - w.SetCookie(NewCookie(session.Name(), encoded, session.Options)) + SetCookie(ctx, session.Name(), encoded, session.Options) return nil } |