From 162df6b250953276e4da745baf28267cd2df9d8a Mon Sep 17 00:00:00 2001 From: John Date: Tue, 13 Aug 2019 20:15:09 +0800 Subject: [PATCH] fix issue in number overflow from int64 to int for ghttp.Session/ghttp.Cookie/gcache --- .example/net/ghttp/server/session/session.go | 33 +++----- net/ghttp/ghttp_server_config.go | 89 ++++++++------------ net/ghttp/ghttp_server_config_cookie.go | 4 +- net/ghttp/ghttp_server_config_session.go | 4 +- net/ghttp/ghttp_server_cookie.go | 10 +-- os/gcache/gcache_mem_cache.go | 10 +-- os/gcache/gcache_mem_cache_item.go | 4 +- 7 files changed, 65 insertions(+), 89 deletions(-) diff --git a/.example/net/ghttp/server/session/session.go b/.example/net/ghttp/server/session/session.go index 1186869b8..ed25b849e 100644 --- a/.example/net/ghttp/server/session/session.go +++ b/.example/net/ghttp/server/session/session.go @@ -2,32 +2,21 @@ package main import ( "github.com/gogf/gf/frame/g" - "github.com/gogf/gf/frame/gmvc" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/os/gtime" ) -type Controller struct { - gmvc.Controller -} - -func (c *Controller) Login() { - c.Session.Id() - c.Response.Write("这个页面用户填写信息执行登录") -} - -func (c *Controller) DoLogin() { - c.Session.Set("key", "value") - //c.Response.Header().Set("Set-Cookie", "myid=1B27UGQGCIBP0P70; Path=/; Domain=127.0.0.1; Expires=Wed, 04 Mar 2020 07:12:05 GMT") - - c.Response.RedirectTo("/main") -} - -func (c *Controller) Main() { - c.Response.WriteJson(c.Session.Map()) -} - func main() { s := g.Server() - s.BindController("/", new(Controller)) + s.SetSessionMaxAge(72000000) + s.SetSessionIdName("gpadminssid") + s.BindHandler("/set", func(r *ghttp.Request) { + r.Session.Set("time", gtime.Second()) + r.Response.Write("ok") + }) + s.BindHandler("/get", func(r *ghttp.Request) { + r.Response.WriteJson(r.Session.Map()) + }) s.SetPort(8199) s.Run() } diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 3844827ec..7ebd112e9 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -36,57 +36,42 @@ type LogHandler func(r *Request, error ...interface{}) // HTTP Server 设置结构体,静态配置 type ServerConfig struct { - // 底层http对象配置 - Addr string // 监听IP和端口,监听本地所有IP使用":端口"(支持多个地址,使用","号分隔) - HTTPSAddr string // HTTPS服务监听地址(支持多个地址,使用","号分隔) - HTTPSCertPath string // HTTPS证书文件路径 - HTTPSKeyPath string // HTTPS签名文件路径 - Handler http.Handler // 默认的处理函数 - ReadTimeout time.Duration // 读取超时 - WriteTimeout time.Duration // 写入超时 - IdleTimeout time.Duration // 等待超时 - MaxHeaderBytes int // 最大的header长度 - TLSConfig tls.Config - KeepAlive bool - - // 静态文件配置 - IndexFiles []string // 默认访问的文件列表 - IndexFolder bool // 如果访问目录是否显示目录列表 - ServerAgent string // Server Agent - ServerRoot string // 服务器服务的本地目录根路径(检索优先级比StaticPaths低) - SearchPaths []string // 静态文件搜索目录(包含ServerRoot,按照优先级进行排序) - StaticPaths []staticPathItem // 静态文件目录映射(按照优先级进行排序) - FileServerEnabled bool // 是否允许静态文件服务(通过静态文件服务方法调用自动识别) - - // COOKIE - CookieMaxAge int // Cookie有效期 - CookiePath string // Cookie有效Path(注意同时也会影响SessionID) - CookieDomain string // Cookie有效Domain(注意同时也会影响SessionID) - - // SESSION - SessionMaxAge int // Session有效期 - SessionIdName string // SessionId名称 - - // IP访问控制 - DenyIps []string // 不允许访问的ip列表,支持ip前缀过滤,如: 10 将不允许10开头的ip访问 - AllowIps []string // 仅允许访问的ip列表,支持ip前缀过滤,如: 10 将仅允许10开头的ip访问 - - // 路由访问控制 - DenyRoutes []string // 不允许访问的路由规则列表 - Rewrites map[string]string // URI Rewrite重写配置 - - // 日志配置 - LogPath string // 存放日志的目录路径(默认为空,表示不写文件) - LogHandler LogHandler // 自定义日志处理回调方法(默认为空) - LogStdout bool // 是否打印日志到终端(默认开启) - ErrorLogEnabled bool // 是否开启error log(默认开启) - AccessLogEnabled bool // 是否开启access log(默认关闭) - - // 其他设置 - NameToUriType int // 服务注册时对象和方法名称转换为URI时的规则 - GzipContentTypes []string // 允许进行gzip压缩的文件类型 - DumpRouteMap bool // 是否在程序启动时默认打印路由表信息 - RouterCacheExpire int // 路由检索缓存过期时间(秒) + Addr string // 监听IP和端口,监听本地所有IP使用":端口"(支持多个地址,使用","号分隔) + HTTPSAddr string // HTTPS服务监听地址(支持多个地址,使用","号分隔) + HTTPSCertPath string // HTTPS证书文件路径 + HTTPSKeyPath string // HTTPS签名文件路径 + Handler http.Handler // 默认的处理函数 + ReadTimeout time.Duration // 读取超时 + WriteTimeout time.Duration // 写入超时 + IdleTimeout time.Duration // 等待超时 + MaxHeaderBytes int // 最大的header长度 + TLSConfig tls.Config // HTTPS证书配置 + KeepAlive bool // 是否开启长连接 + ServerAgent string // Server Agent + Rewrites map[string]string // URI Rewrite重写配置 + IndexFiles []string // Static: 默认访问的文件列表 + IndexFolder bool // Static: 如果访问目录是否显示目录列表 + ServerRoot string // Static: 服务器服务的本地目录根路径(检索优先级比StaticPaths低) + SearchPaths []string // Static: 静态文件搜索目录(包含ServerRoot,按照优先级进行排序) + StaticPaths []staticPathItem // Static: 静态文件目录映射(按照优先级进行排序) + FileServerEnabled bool // Static: 是否允许静态文件服务(通过静态文件服务方法调用自动识别) + CookieMaxAge int64 // Cookie: 有效期(秒) + CookiePath string // Cookie: 有效Path(注意同时也会影响SessionID) + CookieDomain string // Cookie: 有效Domain(注意同时也会影响SessionID) + SessionMaxAge int64 // Session: 有效期(秒) + SessionIdName string // Session: SessionId + DenyIps []string // Security: 不允许访问的ip列表,支持ip前缀过滤,如: 10 将不允许10开头的ip访问 + AllowIps []string // Security: 仅允许访问的ip列表,支持ip前缀过滤,如: 10 将仅允许10开头的ip访问 + DenyRoutes []string // Security: 不允许访问的路由规则列表 + LogPath string // Logging: 存放日志的目录路径(默认为空,表示不写文件) + LogHandler LogHandler // Logging: 日志配置: 自定义日志处理回调方法(默认为空) + LogStdout bool // Logging: 是否打印日志到终端(默认开启) + ErrorLogEnabled bool // Logging: 是否开启error log(默认开启) + AccessLogEnabled bool // Logging: 是否开启access log(默认关闭) + NameToUriType int // Mess: 服务注册时对象和方法名称转换为URI时的规则 + GzipContentTypes []string // Mess: 允许进行gzip压缩的文件类型 + DumpRouteMap bool // Mess: 是否在程序启动时默认打印路由表信息 + RouterCacheExpire int // Mess: 路由检索缓存过期时间(秒) } // 默认HTTP Server配置 @@ -101,7 +86,7 @@ var defaultServerConfig = ServerConfig{ KeepAlive: true, IndexFiles: []string{"index.html", "index.htm"}, IndexFolder: false, - ServerAgent: "gf", + ServerAgent: "gf http server", ServerRoot: "", StaticPaths: make([]staticPathItem, 0), FileServerEnabled: false, diff --git a/net/ghttp/ghttp_server_config_cookie.go b/net/ghttp/ghttp_server_config_cookie.go index a353d85f8..5c49962b7 100644 --- a/net/ghttp/ghttp_server_config_cookie.go +++ b/net/ghttp/ghttp_server_config_cookie.go @@ -11,7 +11,7 @@ import ( ) // 设置http server参数 - CookieMaxAge -func (s *Server) SetCookieMaxAge(age int) { +func (s *Server) SetCookieMaxAge(age int64) { if s.Status() == SERVER_STATUS_RUNNING { glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) return @@ -38,7 +38,7 @@ func (s *Server) SetCookieDomain(domain string) { } // 获取http server参数 - CookieMaxAge -func (s *Server) GetCookieMaxAge() int { +func (s *Server) GetCookieMaxAge() int64 { return s.config.CookieMaxAge } diff --git a/net/ghttp/ghttp_server_config_session.go b/net/ghttp/ghttp_server_config_session.go index ceecfbd3a..84f4e813a 100644 --- a/net/ghttp/ghttp_server_config_session.go +++ b/net/ghttp/ghttp_server_config_session.go @@ -9,7 +9,7 @@ package ghttp import "github.com/gogf/gf/os/glog" // 设置http server参数 - SessionMaxAge -func (s *Server) SetSessionMaxAge(age int) { +func (s *Server) SetSessionMaxAge(age int64) { if s.Status() == SERVER_STATUS_RUNNING { glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) return @@ -27,7 +27,7 @@ func (s *Server) SetSessionIdName(name string) { } // 获取http server参数 - SessionMaxAge -func (s *Server) GetSessionMaxAge() int { +func (s *Server) GetSessionMaxAge() int64 { return s.config.SessionMaxAge } diff --git a/net/ghttp/ghttp_server_cookie.go b/net/ghttp/ghttp_server_cookie.go index 69c84d701..d408651f9 100644 --- a/net/ghttp/ghttp_server_cookie.go +++ b/net/ghttp/ghttp_server_cookie.go @@ -21,7 +21,7 @@ type Cookie struct { data map[string]CookieItem // 数据项 path string // 默认的cookie path domain string // 默认的cookie domain - maxage int // 默认的cookie maxage + maxage int64 // 默认的cookie maxage server *Server // 所属Server request *Request // 所属HTTP请求对象 response *Response // 所属HTTP返回对象 @@ -32,7 +32,7 @@ type CookieItem struct { value string domain string // 有效域名 path string // 有效路径 - expire int // 过期时间 + expire int64 // 过期时间 httpOnly bool } @@ -61,7 +61,7 @@ func (c *Cookie) init() { } for _, v := range c.request.Cookies() { c.data[v.Name] = CookieItem{ - v.Value, v.Domain, v.Path, v.Expires.Second(), v.HttpOnly, + v.Value, v.Domain, v.Path, int64(v.Expires.Second()), v.HttpOnly, } } } @@ -94,14 +94,14 @@ func (c *Cookie) Set(key, value string) { } // 设置cookie,带详细cookie参数 -func (c *Cookie) SetCookie(key, value, domain, path string, maxAge int, httpOnly ...bool) { +func (c *Cookie) SetCookie(key, value, domain, path string, maxAge int64, httpOnly ...bool) { c.init() isHttpOnly := false if len(httpOnly) > 0 { isHttpOnly = httpOnly[0] } c.data[key] = CookieItem{ - value, domain, path, int(gtime.Second()) + maxAge, isHttpOnly, + value, domain, path, gtime.Second() + maxAge, isHttpOnly, } } diff --git a/os/gcache/gcache_mem_cache.go b/os/gcache/gcache_mem_cache.go index 4999161fb..f7ed0c71f 100644 --- a/os/gcache/gcache_mem_cache.go +++ b/os/gcache/gcache_mem_cache.go @@ -107,11 +107,11 @@ func (c *memCache) getOrNewExpireSet(expire int64) (expireSet *gset.Set) { // getMilliExpire converts parameter to int type in milliseconds. // // Note that there's some performance cost in type assertion here, but it's valuable. -func (c *memCache) getMilliExpire(duration interface{}) int { +func (c *memCache) getMilliExpire(duration interface{}) int64 { if d, ok := duration.(time.Duration); ok { - return int(d.Nanoseconds() / 1000000) + return d.Nanoseconds() / 1000000 } else { - return duration.(int) + return gconv.Int64(duration) } } @@ -158,9 +158,9 @@ func (c *memCache) doSetWithLockCheck(key interface{}, value interface{}, durati } // getInternalExpire returns the expire time with given expire duration in milliseconds. -func (c *memCache) getInternalExpire(expire int) int64 { +func (c *memCache) getInternalExpire(expire int64) int64 { if expire != 0 { - return gtime.Millisecond() + int64(expire) + return gtime.Millisecond() + expire } else { return gDEFAULT_MAX_EXPIRE } diff --git a/os/gcache/gcache_mem_cache_item.go b/os/gcache/gcache_mem_cache_item.go index 07601c8ff..ace5ffb13 100644 --- a/os/gcache/gcache_mem_cache_item.go +++ b/os/gcache/gcache_mem_cache_item.go @@ -6,7 +6,9 @@ package gcache -import "github.com/gogf/gf/os/gtime" +import ( + "github.com/gogf/gf/os/gtime" +) // IsExpired checks whether is expired. func (item *memCacheItem) IsExpired() bool {