Skip to content

Commit

Permalink
add SessionCookieMaxAge configuration for ghttp.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Jan 11, 2021
1 parent 6f5b0c3 commit 8ee3793
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions .example/net/ghttp/server/session/basic/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
17 changes: 11 additions & 6 deletions net/ghttp/ghttp_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
// ==================================
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion net/ghttp/ghttp_server_config_session.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
36 changes: 24 additions & 12 deletions net/ghttp/ghttp_server_cookie.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 8ee3793

Please sign in to comment.