mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
完成cookie和session功能调试
This commit is contained in:
@ -4,6 +4,10 @@ import (
|
||||
"gitee.com/johng/gf/g/net/gsession"
|
||||
)
|
||||
|
||||
const (
|
||||
gDEFAULT_SESSION_ID_NAME = "gfsessionid"
|
||||
)
|
||||
|
||||
// 控制器基类
|
||||
type ControllerBase struct {
|
||||
Request *ClientRequest
|
||||
@ -17,7 +21,7 @@ func (c *ControllerBase) Init(r *ClientRequest, w *ServerResponse) {
|
||||
c.Request = r
|
||||
c.Response = w
|
||||
c.Cookie = NewCookie(c.Request, c.Response)
|
||||
if r := c.Cookie.Get("gfsessionid"); r != "" {
|
||||
if r := c.Cookie.Get(gDEFAULT_SESSION_ID_NAME); r != "" {
|
||||
c.Session = gsession.Get(r)
|
||||
} else {
|
||||
c.Session = gsession.Get(gsession.Id())
|
||||
@ -26,6 +30,9 @@ func (c *ControllerBase) Init(r *ClientRequest, w *ServerResponse) {
|
||||
|
||||
// 控制器结束请求
|
||||
func (c *ControllerBase) Shut() {
|
||||
if c.Cookie.Get(gDEFAULT_SESSION_ID_NAME) == "" {
|
||||
c.Cookie.Set(gDEFAULT_SESSION_ID_NAME, c.Session.Id())
|
||||
}
|
||||
c.Cookie.Output()
|
||||
c.Response.Output()
|
||||
}
|
||||
|
||||
@ -2,18 +2,22 @@ package ghttp
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"net/http"
|
||||
"strings"
|
||||
"net/http"
|
||||
"time"
|
||||
"gitee.com/johng/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
const (
|
||||
gDEFAULT_MAX_AGE = 86400
|
||||
gDEFAULT_PATH = "/" // 默认path
|
||||
gDEFAULT_MAX_AGE = 86400 // 默认cookie有效期
|
||||
)
|
||||
|
||||
// cookie对象
|
||||
type Cookie struct {
|
||||
mu sync.RWMutex // 并发安全互斥锁
|
||||
data map[string]CookieItem // 数据项
|
||||
domain string // 默认的cookie域名
|
||||
request *ClientRequest // 所属HTTP请求对象
|
||||
response *ServerResponse // 所属HTTP返回对象
|
||||
}
|
||||
@ -23,16 +27,24 @@ type CookieItem struct {
|
||||
value string
|
||||
domain string
|
||||
path string
|
||||
maxage int
|
||||
expire int //过期时间
|
||||
}
|
||||
|
||||
// 初始化cookie对象
|
||||
func NewCookie(r *ClientRequest, w *ServerResponse) *Cookie {
|
||||
return &Cookie{
|
||||
c := &Cookie{
|
||||
data : make(map[string]CookieItem),
|
||||
domain : defaultDomain(r),
|
||||
request : r,
|
||||
response : w,
|
||||
}
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
// 获取默认的domain参数
|
||||
func defaultDomain(r *ClientRequest) string {
|
||||
return strings.Split(r.Host, ":")[0]
|
||||
}
|
||||
|
||||
// 从请求流中初始化
|
||||
@ -41,14 +53,14 @@ func (c *Cookie) init() {
|
||||
defer c.mu.Unlock()
|
||||
for _, v := range c.request.Cookies() {
|
||||
c.data[v.Name] = CookieItem {
|
||||
v.Value, v.Domain, v.Path, v.MaxAge,
|
||||
v.Value, v.Domain, v.Path, v.Expires.Second(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置cookie,使用默认参数
|
||||
func (c *Cookie) Set(key, value string) {
|
||||
c.SetCookie(key, value, strings.Split(c.request.Host, ":")[0], "/", gDEFAULT_MAX_AGE)
|
||||
c.SetCookie(key, value, c.domain, gDEFAULT_PATH, gDEFAULT_MAX_AGE)
|
||||
}
|
||||
|
||||
// 设置cookie,带详细cookie参数
|
||||
@ -56,23 +68,27 @@ func (c *Cookie) SetCookie(key, value, domain, path string, maxage int) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.data[key] = CookieItem {
|
||||
value, domain, path, maxage,
|
||||
value, domain, path, int(gtime.Second()) + maxage,
|
||||
}
|
||||
}
|
||||
|
||||
// 查询cookie
|
||||
func (c *Cookie) Get(key string) string {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
if r, ok := c.data[key]; ok {
|
||||
return r.value
|
||||
if r.expire >= 0 {
|
||||
return r.value
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 删除cookie的重点是需要通知浏览器客户端cookie已过期
|
||||
func (c *Cookie) Remove(key string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
delete(c.data, key)
|
||||
c.SetCookie(key, "", c.domain, gDEFAULT_PATH, -86400)
|
||||
}
|
||||
|
||||
// 输出到客户端
|
||||
@ -80,6 +96,9 @@ func (c *Cookie) Output() {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
for k, v := range c.data {
|
||||
http.SetCookie(c.response.ResponseWriter, &http.Cookie{Name: k, Value: v.value, Domain: v.domain, Path: v.path, MaxAge: v.maxage})
|
||||
if v.expire == 0 {
|
||||
continue
|
||||
}
|
||||
http.SetCookie(c.response.ResponseWriter, &http.Cookie{Name: k, Value: v.value, Domain: v.domain, Path: v.path, Expires: time.Unix(int64(v.expire), 0)})
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,7 @@ func Get(sessionid string) *Session {
|
||||
return r.(*Session)
|
||||
}
|
||||
s := &Session{
|
||||
id : Id(),
|
||||
data : gmap.NewStringInterfaceMap(),
|
||||
expire : DEFAULT_EXPIRE_TIME,
|
||||
}
|
||||
@ -77,6 +78,15 @@ func (s *Session) Get (k string) interface{} {
|
||||
return s.data.Get(k)
|
||||
}
|
||||
|
||||
// 获取session(字符串)
|
||||
func (s *Session) GetString (k string) string {
|
||||
go s.updateExpire()
|
||||
if r := s.data.Get(k); r != nil {
|
||||
return r.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 删除session
|
||||
func (s *Session) Remove (k string) {
|
||||
go s.updateExpire()
|
||||
|
||||
@ -22,7 +22,8 @@ func init() {
|
||||
|
||||
// 定义操作逻辑
|
||||
func (c *ControllerUser) Info() {
|
||||
c.Cookie.Set("name", "john2")
|
||||
//c.Session.Set("name", "john")
|
||||
c.Response.WriteString("session:" + c.Session.GetString("name"))
|
||||
c.Response.Write([]byte("user information page"))
|
||||
//t, err := template.New("test").Funcs(template.FuncMap{"add": Add}).Parse(gfile.GetContents("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view/user/info.tpl"))
|
||||
//if err != nil {
|
||||
|
||||
@ -1,20 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
"gitee.com/johng/gf/g/os/gtime"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type B struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func(b *B) Test() {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := &B{}
|
||||
t := reflect.ValueOf(b).Elem().Type()
|
||||
//n := reflect.New(t)
|
||||
fmt.Println(t.NumMethod())
|
||||
s := gtime.Second()
|
||||
t := time.Unix(s, 0)
|
||||
fmt.Println(t.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
Reference in New Issue
Block a user