diff --git a/.example/net/ghttp/server/session/basic/session.go b/.example/net/ghttp/server/session/basic/session.go index c7a21a684..d7ae0e1ec 100644 --- a/.example/net/ghttp/server/session/basic/session.go +++ b/.example/net/ghttp/server/session/basic/session.go @@ -8,6 +8,7 @@ import ( func main() { s := g.Server() + s.SetSessionCookieMaxAge(0) s.Group("/", func(group *ghttp.RouterGroup) { group.GET("/set", func(r *ghttp.Request) { r.Session.Set("time", gtime.Timestamp()) diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 9a8a7aa9b..715ab0890 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -152,14 +152,11 @@ type ServerConfig struct { // Session. // ================================== - // SessionMaxAge specifies max TTL for session items. - SessionMaxAge time.Duration - // SessionIdName specifies the session id name. SessionIdName string - // SessionCookieOutput specifies whether automatic outputting session id to cookie. - SessionCookieOutput bool + // SessionMaxAge specifies max TTL for session items. + SessionMaxAge time.Duration // SessionPath specifies the session storage directory path for storing session files. // It only makes sense if the session storage is type of file storage. @@ -168,6 +165,13 @@ type ServerConfig struct { // SessionStorage specifies the session storage. SessionStorage gsession.Storage + // SessionCookieMaxAge specifies the cookie ttl for session id. + // It it is set 0, it means it expires along with browser session. + SessionCookieMaxAge time.Duration + + // SessionCookieOutput specifies whether automatic outputting session id to cookie. + SessionCookieOutput bool + // ================================== // Logging. // ================================== @@ -243,10 +247,11 @@ func NewConfig() ServerConfig { CookieMaxAge: time.Hour * 24 * 365, CookiePath: "/", CookieDomain: "", - SessionMaxAge: time.Hour * 24, SessionIdName: "gfsessionid", SessionPath: gsession.DefaultStorageFilePath, + SessionMaxAge: time.Hour * 24, SessionCookieOutput: true, + SessionCookieMaxAge: time.Hour * 24, Logger: glog.New(), LogLevel: "all", LogStdout: true, diff --git a/net/ghttp/ghttp_server_config_session.go b/net/ghttp/ghttp_server_config_session.go index e9f712629..205eae613 100644 --- a/net/ghttp/ghttp_server_config_session.go +++ b/net/ghttp/ghttp_server_config_session.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -32,6 +32,11 @@ func (s *Server) SetSessionCookieOutput(enabled bool) { s.config.SessionCookieOutput = enabled } +// SetSessionCookieMaxAge sets the SessionCookieMaxAge for server. +func (s *Server) SetSessionCookieMaxAge(maxAge time.Duration) { + s.config.SessionCookieMaxAge = maxAge +} + // GetSessionMaxAge returns the SessionMaxAge of server. func (s *Server) GetSessionMaxAge() time.Duration { return s.config.SessionMaxAge @@ -41,3 +46,8 @@ func (s *Server) GetSessionMaxAge() time.Duration { func (s *Server) GetSessionIdName() string { return s.config.SessionIdName } + +// GetSessionCookieMaxAge returns the SessionCookieMaxAge of server. +func (s *Server) GetSessionCookieMaxAge() time.Duration { + return s.config.SessionCookieMaxAge +} diff --git a/net/ghttp/ghttp_server_cookie.go b/net/ghttp/ghttp_server_cookie.go index af7f7473c..47fbfb918 100644 --- a/net/ghttp/ghttp_server_cookie.go +++ b/net/ghttp/ghttp_server_cookie.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -14,9 +14,6 @@ import ( // Cookie for HTTP COOKIE management. type Cookie struct { data map[string]*cookieItem // Underlying cookie items. - path string // The default cookie path. - domain string // The default cookie domain - maxAge time.Duration // The default cookie max age. server *Server // Belonged HTTP server request *Request // Belonged HTTP request. response *Response // Belonged HTTP response. @@ -47,13 +44,10 @@ func (c *Cookie) init() { return } c.data = make(map[string]*cookieItem) - c.path = c.request.Server.GetCookiePath() - c.domain = c.request.Server.GetCookieDomain() - c.maxAge = c.request.Server.GetCookieMaxAge() c.response = c.request.Response // DO NOT ADD ANY DEFAULT COOKIE DOMAIN! - //if c.domain == "" { - // c.domain = c.request.GetHost() + //if c.request.Server.GetCookieDomain() == "" { + // c.request.Server.GetCookieDomain() = c.request.GetHost() //} for _, v := range c.request.Cookies() { c.data[v.Name] = &cookieItem{ @@ -86,7 +80,13 @@ func (c *Cookie) Contains(key string) bool { // Set sets cookie item with default domain, path and expiration age. func (c *Cookie) Set(key, value string) { - c.SetCookie(key, value, c.domain, c.path, c.maxAge) + c.SetCookie( + key, + value, + c.request.Server.GetCookieDomain(), + c.request.Server.GetCookiePath(), + c.request.Server.GetCookieMaxAge(), + ) } // SetCookie sets cookie item given given domain, path and expiration age. @@ -128,7 +128,13 @@ func (c *Cookie) GetSessionId() string { // SetSessionId sets session id in the cookie. func (c *Cookie) SetSessionId(id string) { - c.Set(c.server.GetSessionIdName(), id) + c.SetCookie( + c.server.GetSessionIdName(), + id, + c.request.Server.GetCookieDomain(), + c.request.Server.GetCookiePath(), + c.server.GetSessionCookieMaxAge(), + ) } // Get retrieves and returns the value with specified key. @@ -149,7 +155,13 @@ func (c *Cookie) Get(key string, def ...string) string { // Remove deletes specified key and its value from cookie using default domain and path. // It actually tells the http client that the cookie is expired, do not send it to server next time. func (c *Cookie) Remove(key string) { - c.SetCookie(key, "", c.domain, c.path, -86400) + c.SetCookie( + key, + "", + c.request.Server.GetCookieDomain(), + c.request.Server.GetCookiePath(), + -86400, + ) } // RemoveCookie deletes specified key and its value from cookie using given domain and path.