Merge branch 'master' into develop

This commit is contained in:
John
2019-02-26 22:23:24 +08:00
12 changed files with 391 additions and 78 deletions

View File

@ -24,11 +24,13 @@ import (
// http客户端
type Client struct {
http.Client // 底层http client对象
header map[string]string // HEADER信息Map
prefix string // 设置请求的URL前缀
authUser string // HTTP基本权限设置名称
authPass string // HTTP基本权限设置密码
http.Client // 底层http client对象
header map[string]string // HEADER信息Map
cookies map[string]string // 自定义COOKIE
prefix string // 设置请求的URL前缀
authUser string // HTTP基本权限设置名称
authPass string // HTTP基本权限设置密码
browserMode bool // 是否模拟浏览器模式(自动保存提交COOKIE)
}
// http客户端对象指针
@ -39,10 +41,16 @@ func NewClient() (*Client) {
DisableKeepAlives: true,
},
},
header : make(map[string]string),
header : make(map[string]string),
cookies : make(map[string]string),
}
}
// 是否模拟浏览器模式(自动保存提交COOKIE)
func (c *Client) SetBrowserMode(enabled bool) {
c.browserMode = enabled
}
// 设置HTTP Header
func (c *Client) SetHeader(key, value string) {
c.header[key] = value
@ -58,6 +66,18 @@ func (c *Client) SetHeaderRaw(header string) {
}
}
// 设置COOKIE
func (c *Client) SetCookie(key, value string) {
c.cookies[key] = value
}
// 使用Map设置COOKIE
func (c *Client) SetCookieMap(cookieMap map[string]string) {
for k, v := range cookieMap {
c.cookies[k] = v
}
}
// 设置请求的URL前缀
func (c *Client) SetPrefix(prefix string) {
c.prefix = prefix
@ -143,6 +163,19 @@ func (c *Client) Post(url string, data...string) (*ClientResponse, error) {
req.Header.Set(k, v)
}
}
// COOKIE
if len(c.cookies) > 0 {
headerCookie := ""
for k, v := range c.cookies {
if len(headerCookie) > 0 {
headerCookie += ";"
}
headerCookie += k + "=" + v
}
if len(headerCookie) > 0 {
req.Header.Set("Cookie", headerCookie)
}
}
// HTTP账号密码
if len(c.authUser) > 0 {
req.SetBasicAuth(c.authUser, c.authPass)
@ -152,8 +185,10 @@ func (c *Client) Post(url string, data...string) (*ClientResponse, error) {
if err != nil {
return nil, err
}
r := &ClientResponse{}
r.Response = *resp
r := &ClientResponse{
cookies : make(map[string]string),
}
r.Response = resp
return r, nil
}
@ -254,13 +289,40 @@ func (c *Client) DoRequest(method, url string, data...string) (*ClientResponse,
req.Header.Set(k, v)
}
}
// COOKIE
if len(c.cookies) > 0 {
headerCookie := ""
for k, v := range c.cookies {
if len(headerCookie) > 0 {
headerCookie += ";"
}
headerCookie += k + "=" + v
}
if len(headerCookie) > 0 {
req.Header.Set("Cookie", headerCookie)
}
}
// 执行请求
resp, err := c.Do(req)
if err != nil {
return nil, err
}
r := &ClientResponse{}
r.Response = *resp
r := &ClientResponse{
cookies : make(map[string]string),
}
r.Response = resp
// 浏览器模式
if c.browserMode {
now := time.Now()
for _, v := range r.Cookies() {
if v.Expires.UnixNano() < now.UnixNano() {
delete(c.cookies, v.Name)
} else {
c.cookies[v.Name] = v.Value
}
}
}
//fmt.Println(url, c.cookies)
return r, nil
}

View File

@ -10,11 +10,27 @@ package ghttp
import (
"io/ioutil"
"net/http"
"time"
)
// 客户端请求结果对象
type ClientResponse struct {
http.Response
*http.Response
cookies map[string]string
}
// 获得返回的指定COOKIE值
func (r *ClientResponse) GetCookie(key string) string {
if r.cookies == nil {
now := time.Now()
for _, v := range r.Cookies() {
if v.Expires.UnixNano() < now.UnixNano() {
continue
}
r.cookies[v.Name] = v.Value
}
}
return r.cookies[key]
}
// 获取返回的数据(二进制).

View File

@ -42,6 +42,7 @@ func GetCookie(r *Request) *Cookie {
}
return &Cookie {
request : r,
server : r.Server,
}
}
@ -52,7 +53,6 @@ func (c *Cookie) init() {
c.path = c.request.Server.GetCookiePath()
c.domain = c.request.Server.GetCookieDomain()
c.maxage = c.request.Server.GetCookieMaxAge()
c.server = c.request.Server
c.response = c.request.Response
// 如果没有设置COOKIE有效域名那么设置HOST为默认有效域名
if c.domain == "" {
@ -115,6 +115,11 @@ func (c *Cookie) SetCookie(key, value, domain, path string, maxAge int, httpOnly
}
}
// 获得客户端提交的SessionId
func (c *Cookie) GetSessionId() string {
return c.Get(c.server.GetSessionIdName())
}
// 设置SessionId
func (c *Cookie) SetSessionId(id string) {
c.Set(c.server.GetSessionIdName(), id)
@ -133,9 +138,14 @@ func (c *Cookie) Get(key string) string {
return ""
}
// 删除COOKIE使用默认的domain&path
func (c *Cookie) Remove(key string) {
c.SetCookie(key, "", c.domain, c.path, -86400)
}
// 标记该cookie在对应的域名和路径失效
// 删除cookie的重点是需要通知浏览器客户端cookie已过期
func (c *Cookie) Remove(key, domain, path string) {
func (c *Cookie) RemoveCookie(key, domain, path string) {
c.SetCookie(key, "", domain, path, -86400)
}

View File

@ -64,15 +64,11 @@ func (s *Server)BindController(pattern string, c Controller, methods...string) e
faddr : nil,
}
// 如果方法中带有Index方法那么额外自动增加一个路由规则匹配主URI
// 例如: pattern为/user, 那么会同时注册/user及/user/index,
// 这里处理新增/user路由绑定
if strings.EqualFold(mname, "Index") {
p := key
if strings.EqualFold(p[len(p) - 6:], "/index") {
p = p[0 : len(p) - 6]
if len(p) == 0 {
p = "/"
}
}
m[p] = &handlerItem {
p := gstr.PosR(key, "/index")
m[key[0 : p] + key[p + 6 : ]] = &handlerItem {
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, mname),
rtype : gROUTE_REGISTER_CONTROLLER,
ctype : v.Elem().Type(),

View File

@ -74,14 +74,8 @@ func (s *Server)BindObject(pattern string, obj interface{}, methods...string) er
}
// 如果方法中带有Index方法那么额外自动增加一个路由规则匹配主URI
if strings.EqualFold(mname, "Index") {
p := key
if strings.EqualFold(p[len(p) - 6:], "/index") {
p = p[0 : len(p) - 6]
if len(p) == 0 {
p = "/"
}
}
m[p] = &handlerItem {
p := gstr.PosR(key, "/index")
m[key[0 : p] + key[p + 6 : ]] = &handlerItem {
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, mname),
rtype : gROUTE_REGISTER_OBJECT,
ctype : nil,

View File

@ -52,7 +52,7 @@ func (s *Session) init() {
}
}
// 获取SessionId
// 获取/创建SessionId
func (s *Session) Id() string {
s.init()
return s.id
@ -60,8 +60,11 @@ func (s *Session) Id() string {
// 获取当前session所有数据
func (s *Session) Data() map[string]interface{} {
s.init()
return s.data.Map()
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
s.init()
return s.data.Map()
}
return nil
}
// 设置session
@ -84,70 +87,158 @@ func (s *Session) BatchSet(m map[string]interface{}) {
// 判断键名是否存在
func (s *Session) Contains (key string) bool {
s.init()
return s.data.Contains(key)
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
s.init()
return s.data.Contains(key)
}
return false
}
// 获取SESSION
func (s *Session) Get (key string) interface{} {
s.init()
return s.data.Get(key)
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
s.init()
return s.data.Get(key)
}
return nil
}
// 获取SESSION建议都用该方法获取参数
func (s *Session) GetVar(key string) gvar.VarRead {
s.init()
return gvar.NewRead(s.data.Get(key), true)
}
func (s *Session) GetString (key string) string { return gconv.String(s.Get(key)) }
func (s *Session) GetBool(key string) bool { return gconv.Bool(s.Get(key)) }
func (s *Session) GetInt(key string) int { return gconv.Int(s.Get(key)) }
func (s *Session) GetInt8(key string) int8 { return gconv.Int8(s.Get(key)) }
func (s *Session) GetInt16(key string) int16 { return gconv.Int16(s.Get(key)) }
func (s *Session) GetInt32(key string) int32 { return gconv.Int32(s.Get(key)) }
func (s *Session) GetInt64(key string) int64 { return gconv.Int64(s.Get(key)) }
func (s *Session) GetUint(key string) uint { return gconv.Uint(s.Get(key)) }
func (s *Session) GetUint8(key string) uint8 { return gconv.Uint8(s.Get(key)) }
func (s *Session) GetUint16(key string) uint16 { return gconv.Uint16(s.Get(key)) }
func (s *Session) GetUint32(key string) uint32 { return gconv.Uint32(s.Get(key)) }
func (s *Session) GetUint64(key string) uint64 { return gconv.Uint64(s.Get(key)) }
func (s *Session) GetFloat32 (key string) float32 { return gconv.Float32(s.Get(key)) }
func (s *Session) GetFloat64 (key string) float64 { return gconv.Float64(s.Get(key)) }
func (s *Session) GetBytes(key string) []byte { return gconv.Bytes(s.Get(key)) }
func (s *Session) GetInts(key string) []int { return gconv.Ints(s.Get(key)) }
func (s *Session) GetFloats(key string) []float64 { return gconv.Floats(s.Get(key)) }
func (s *Session) GetStrings(key string) []string { return gconv.Strings(s.Get(key)) }
func (s *Session) GetInterfaces(key string) []interface{} { return gconv.Interfaces(s.Get(key)) }
func (s *Session) GetTime(key string, format...string) time.Time { return gconv.Time(s.Get(key), format...) }
func (s *Session) GetTimeDuration(key string) time.Duration { return gconv.TimeDuration(s.Get(key)) }
// 将变量转换为对象,注意 objPointer 参数必须为struct指针
func (s *Session) GetStruct(key string, objPointer interface{}, attrMapping...map[string]string) error {
return gconv.Struct(s.Get(key), objPointer, attrMapping...)
return gvar.NewRead(s.Get(key), true)
}
// 删除session
func (s *Session) Remove(key string) {
s.init()
s.data.Remove(key)
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
s.init()
s.data.Remove(key)
}
}
// 清空session
func (s *Session) Clear() {
s.init()
s.data.Clear()
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
s.init()
s.data.Clear()
}
}
// 更新过期时间(如果用在守护进程中长期使用,需要手动调用进行更新,防止超时被清除)
func (s *Session) UpdateExpire() {
if len(s.id) > 0 {
if len(s.id) > 0 && s.data.Size() > 0 {
s.server.sessions.Set(s.id, s.data, s.server.GetSessionMaxAge()*1000)
}
}
}
// Deprecated, use GetVar instead.
func (s *Session) GetString(key string) string {
return gconv.String(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetBool(key string) bool {
return gconv.Bool(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetInt(key string) int {
return gconv.Int(s.Get(key)) }
// Deprecated, use GetVar instead.
func (s *Session) GetInt8(key string) int8 {
return gconv.Int8(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetInt16(key string) int16 {
return gconv.Int16(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetInt32(key string) int32 {
return gconv.Int32(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetInt64(key string) int64 {
return gconv.Int64(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetUint(key string) uint {
return gconv.Uint(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetUint8(key string) uint8 {
return gconv.Uint8(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetUint16(key string) uint16 {
return gconv.Uint16(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetUint32(key string) uint32 {
return gconv.Uint32(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetUint64(key string) uint64 {
return gconv.Uint64(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetFloat32(key string) float32 {
return gconv.Float32(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetFloat64(key string) float64 {
return gconv.Float64(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetBytes(key string) []byte {
return gconv.Bytes(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetInts(key string) []int {
return gconv.Ints(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetFloats(key string) []float64 {
return gconv.Floats(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetStrings(key string) []string {
return gconv.Strings(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetInterfaces(key string) []interface{} {
return gconv.Interfaces(s.Get(key))
}
// Deprecated, use GetVar instead.
func (s *Session) GetTime(key string, format...string) time.Time {
return gconv.Time(s.Get(key), format...)
}
// Deprecated, use GetVar instead.
func (s *Session) GetTimeDuration(key string) time.Duration {
return gconv.TimeDuration(s.Get(key))
}
// Deprecated, use GetVar instead.
// (已废弃, 请使用GetVar) 将变量转换为对象,注意 objPointer 参数必须为struct指针
func (s *Session) GetStruct(key string, objPointer interface{}, attrMapping...map[string]string) error {
return gconv.Struct(s.Get(key), objPointer, attrMapping...)
}

View File

@ -0,0 +1,62 @@
// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
// COOKIE测试
package ghttp_test
import (
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/net/ghttp"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func Test_Cookie(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("/set", func(r *ghttp.Request){
r.Cookie.Set(r.Get("k"), r.Get("v"))
})
s.BindHandler("/get", func(r *ghttp.Request){
//fmt.Println(r.Cookie.Map())
r.Response.Write(r.Cookie.Get(r.Get("k")))
})
s.BindHandler("/remove", func(r *ghttp.Request){
r.Cookie.Remove(r.Get("k"))
})
s.SetPort(8500)
s.SetDumpRouteMap(false)
go s.Run()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)
client.SetPrefix("http://127.0.0.1:8500")
r1, e1 := client.Get("/set?k=key1&v=100")
if r1 != nil {
defer r1.Close()
}
gtest.Assert(e1, nil)
gtest.Assert(r1.ReadAllString(), "")
gtest.Assert(client.GetContent("/set?k=key2&v=200"), "")
gtest.Assert(client.GetContent("/get?k=key1"), "100")
gtest.Assert(client.GetContent("/get?k=key2"), "200")
gtest.Assert(client.GetContent("/get?k=key3"), "")
gtest.Assert(client.GetContent("/remove?k=key1"), "")
gtest.Assert(client.GetContent("/remove?k=key3"), "")
gtest.Assert(client.GetContent("/remove?k=key4"), "")
gtest.Assert(client.GetContent("/get?k=key1"), "")
gtest.Assert(client.GetContent("/get?k=key2"), "200")
})
}

View File

@ -20,6 +20,10 @@ import (
// 执行对象
type Object struct {}
func (o *Object) Index(r *ghttp.Request) {
r.Response.Write("Object Index")
}
func (o *Object) Show(r *ghttp.Request) {
r.Response.Write("Object Show")
}
@ -33,6 +37,10 @@ type Controller struct {
gmvc.Controller
}
func (c *Controller) Index() {
c.Response.Write("Controller Index")
}
func (c *Controller) Show() {
c.Response.Write("Controller Show")
}
@ -72,11 +80,17 @@ func Test_Router_Group1(t *testing.T) {
gtest.Assert(client.GetContent ("/api/handler"), "Handler")
gtest.Assert(client.GetContent ("/api/ctl"), "Controller Index")
gtest.Assert(client.GetContent ("/api/ctl/"), "Controller Index")
gtest.Assert(client.GetContent ("/api/ctl/index"), "Controller Index")
gtest.Assert(client.GetContent ("/api/ctl/my-show"), "Controller Show")
gtest.Assert(client.GetContent ("/api/ctl/post"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/ctl/show"), "Controller Show")
gtest.Assert(client.PostContent("/api/ctl/rest"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/obj"), "Object Index")
gtest.Assert(client.GetContent ("/api/obj/"), "Object Index")
gtest.Assert(client.GetContent ("/api/obj/index"), "Object Index")
gtest.Assert(client.GetContent ("/api/obj/delete"), "Object REST Delete")
gtest.Assert(client.GetContent ("/api/obj/my-show"), "Object Show")
gtest.Assert(client.GetContent ("/api/obj/show"), "Object Show")

View File

@ -0,0 +1,66 @@
// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
// SESSION测试
package ghttp_test
import (
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/net/ghttp"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func Test_Session(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("/set", func(r *ghttp.Request){
r.Session.Set(r.Get("k"), r.Get("v"))
})
s.BindHandler("/get", func(r *ghttp.Request){
r.Response.Write(r.Session.Get(r.Get("k")))
})
s.BindHandler("/remove", func(r *ghttp.Request){
r.Session.Remove(r.Get("k"))
})
s.BindHandler("/clear", func(r *ghttp.Request){
r.Session.Clear()
})
s.SetPort(8600)
s.SetDumpRouteMap(false)
go s.Run()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)
client.SetPrefix("http://127.0.0.1:8600")
r1, e1 := client.Get("/set?k=key1&v=100")
if r1 != nil {
defer r1.Close()
}
gtest.Assert(e1, nil)
gtest.Assert(r1.ReadAllString(), "")
gtest.Assert(client.GetContent("/set?k=key2&v=200"), "")
gtest.Assert(client.GetContent("/get?k=key1"), "100")
gtest.Assert(client.GetContent("/get?k=key2"), "200")
gtest.Assert(client.GetContent("/get?k=key3"), "")
gtest.Assert(client.GetContent("/remove?k=key1"), "")
gtest.Assert(client.GetContent("/remove?k=key3"), "")
gtest.Assert(client.GetContent("/remove?k=key4"), "")
gtest.Assert(client.GetContent("/get?k=key1"), "")
gtest.Assert(client.GetContent("/get?k=key2"), "200")
gtest.Assert(client.GetContent("/clear"), "")
gtest.Assert(client.GetContent("/get?k=key2"), "")
})
}

View File

@ -130,7 +130,9 @@ func (p *Pool) File() (*File, error) {
return nil, err
}
}
if !p.inited.Val() || p.inited.Set(true) == false {
// !p.inited.Val() 使用原子读取操作判断,保证该操作判断的效率;
// p.inited.Set(true) == false 使用原子写入操作,保证该操作的原子性;
if !p.inited.Val() && p.inited.Set(true) == false {
gfsnotify.Add(f.path, func(event *gfsnotify.Event) {
// 如果文件被删除或者重命名,立即重建指针池
if event.IsRemove() || event.IsRename() {