2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). 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,
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
2017-12-30 23:49:55 +08:00
|
|
|
|
//
|
|
|
|
|
// HTTP Cookie管理对象,
|
|
|
|
|
// 由于Cookie是和HTTP请求挂钩的,因此被包含到 ghttp 包中进行管理
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-12-12 17:25:50 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
2017-12-30 23:49:55 +08:00
|
|
|
|
"time"
|
2017-12-13 11:36:29 +08:00
|
|
|
|
"strings"
|
2017-12-13 14:13:36 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
"gitee.com/johng/gf/g/os/gtime"
|
2017-12-12 17:25:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// cookie对象
|
|
|
|
|
type Cookie struct {
|
|
|
|
|
mu sync.RWMutex // 并发安全互斥锁
|
|
|
|
|
data map[string]CookieItem // 数据项
|
2017-12-13 14:13:36 +08:00
|
|
|
|
domain string // 默认的cookie域名
|
2018-04-10 10:32:37 +08:00
|
|
|
|
server *Server // 所属Server
|
2018-01-02 16:35:13 +08:00
|
|
|
|
request *Request // 所属HTTP请求对象
|
|
|
|
|
response *Response // 所属HTTP返回对象
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cookie项
|
|
|
|
|
type CookieItem struct {
|
2018-04-18 09:02:12 +08:00
|
|
|
|
value string
|
|
|
|
|
domain string // 有效域名
|
|
|
|
|
path string // 有效路径
|
|
|
|
|
expire int // 过期时间
|
|
|
|
|
httpOnly bool
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-10 10:32:37 +08:00
|
|
|
|
// 获取或者创建一个cookie对象,与传入的请求对应
|
|
|
|
|
func GetCookie(r *Request) *Cookie {
|
|
|
|
|
if v := r.Server.cookies.Get(r.Id); v != nil {
|
|
|
|
|
return v.(*Cookie)
|
2017-12-30 23:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
c := &Cookie {
|
2017-12-12 17:25:50 +08:00
|
|
|
|
data : make(map[string]CookieItem),
|
2018-04-10 10:32:37 +08:00
|
|
|
|
domain : strings.Split(r.Host, ":")[0],
|
|
|
|
|
server : r.Server,
|
2017-12-12 17:25:50 +08:00
|
|
|
|
request : r,
|
2018-01-02 15:52:32 +08:00
|
|
|
|
response : r.Response,
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
2017-12-13 14:13:36 +08:00
|
|
|
|
c.init()
|
2018-04-10 10:32:37 +08:00
|
|
|
|
c.server.cookies.Set(r.Id, c)
|
2017-12-13 14:13:36 +08:00
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 17:25:50 +08:00
|
|
|
|
// 从请求流中初始化
|
|
|
|
|
func (c *Cookie) init() {
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
for _, v := range c.request.Cookies() {
|
|
|
|
|
c.data[v.Name] = CookieItem {
|
2018-04-18 09:02:12 +08:00
|
|
|
|
v.Value, v.Domain, v.Path, v.Expires.Second(), v.HttpOnly,
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-10 10:32:37 +08:00
|
|
|
|
c.mu.Unlock()
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-30 23:49:55 +08:00
|
|
|
|
// 获取SessionId
|
|
|
|
|
func (c *Cookie) SessionId() string {
|
2018-04-10 10:32:37 +08:00
|
|
|
|
v := c.Get(c.server.GetSessionIdName())
|
2017-12-31 12:15:04 +08:00
|
|
|
|
if v == "" {
|
|
|
|
|
v = makeSessionId()
|
|
|
|
|
c.SetSessionId(v)
|
|
|
|
|
}
|
|
|
|
|
return v
|
2017-12-30 23:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置SessionId
|
|
|
|
|
func (c *Cookie) SetSessionId(sessionid string) {
|
2018-04-10 10:32:37 +08:00
|
|
|
|
c.Set(c.server.GetSessionIdName(), sessionid)
|
2017-12-30 23:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 11:36:29 +08:00
|
|
|
|
// 设置cookie,使用默认参数
|
|
|
|
|
func (c *Cookie) Set(key, value string) {
|
2018-04-10 10:32:37 +08:00
|
|
|
|
c.SetCookie(key, value, c.domain, gDEFAULT_COOKIE_PATH, c.server.GetCookieMaxAge())
|
2017-12-13 11:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置cookie,带详细cookie参数
|
2018-04-18 09:02:12 +08:00
|
|
|
|
func (c *Cookie) SetCookie(key, value, domain, path string, maxAge int, httpOnly ... bool) {
|
2017-12-12 17:25:50 +08:00
|
|
|
|
c.mu.Lock()
|
2018-04-18 09:02:12 +08:00
|
|
|
|
isHttpOnly := false
|
|
|
|
|
if len(httpOnly) > 0 {
|
|
|
|
|
isHttpOnly = httpOnly[0]
|
|
|
|
|
}
|
2017-12-12 17:25:50 +08:00
|
|
|
|
c.data[key] = CookieItem {
|
2018-04-18 09:02:12 +08:00
|
|
|
|
value, domain, path, int(gtime.Second()) + maxAge, isHttpOnly,
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
2018-04-18 09:02:12 +08:00
|
|
|
|
c.mu.Unlock()
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 14:13:36 +08:00
|
|
|
|
// 查询cookie
|
2017-12-12 17:25:50 +08:00
|
|
|
|
func (c *Cookie) Get(key string) string {
|
|
|
|
|
c.mu.RLock()
|
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
if r, ok := c.data[key]; ok {
|
2017-12-13 14:13:36 +08:00
|
|
|
|
if r.expire >= 0 {
|
|
|
|
|
return r.value
|
|
|
|
|
} else {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-30 23:49:55 +08:00
|
|
|
|
// 标记该cookie在对应的域名和路径失效
|
2017-12-13 14:13:36 +08:00
|
|
|
|
// 删除cookie的重点是需要通知浏览器客户端cookie已过期
|
2017-12-30 23:49:55 +08:00
|
|
|
|
func (c *Cookie) Remove(key, domain, path string) {
|
|
|
|
|
c.SetCookie(key, "", domain, path, -86400)
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 12:15:04 +08:00
|
|
|
|
// 请求完毕后删除已经存在的Cookie对象
|
|
|
|
|
func (c *Cookie) Close() {
|
2018-04-10 10:32:37 +08:00
|
|
|
|
c.server.cookies.Remove(c.request.Id)
|
2017-12-31 12:15:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 17:25:50 +08:00
|
|
|
|
// 输出到客户端
|
|
|
|
|
func (c *Cookie) Output() {
|
|
|
|
|
c.mu.RLock()
|
|
|
|
|
for k, v := range c.data {
|
2017-12-13 14:13:36 +08:00
|
|
|
|
if v.expire == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-12-30 23:49:55 +08:00
|
|
|
|
http.SetCookie(
|
2018-04-30 22:14:14 +08:00
|
|
|
|
c.response.Writer,
|
2017-12-30 23:49:55 +08:00
|
|
|
|
&http.Cookie {
|
2018-04-18 09:02:12 +08:00
|
|
|
|
Name : k,
|
|
|
|
|
Value : v.value,
|
|
|
|
|
Domain : v.domain,
|
|
|
|
|
Path : v.path,
|
|
|
|
|
Expires : time.Unix(int64(v.expire), 0),
|
|
|
|
|
HttpOnly : v.httpOnly,
|
2017-12-30 23:49:55 +08:00
|
|
|
|
},
|
|
|
|
|
)
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
2018-04-10 10:32:37 +08:00
|
|
|
|
c.mu.RUnlock()
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|