From 3218c89f17da014216136e835372f9ce28d1d141 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 23 Sep 2019 16:21:19 +0800 Subject: [PATCH] improve gfile/gstr/ghttp --- .example/other/test.go | 46 ++++++++++++++-- container/gvar/gvar.go | 52 +++++++++++++++++++ debug/gdebug/gdebug.go | 32 +++++++++++- .../{ghash_test.go => ghash_bench_test.go} | 0 encoding/gjson/gjson_z_bench_test.go | 17 ++++++ go.sum | 44 ---------------- net/ghttp/ghttp_request_post.go | 12 +++++ net/ghttp/ghttp_request_query.go | 12 +++++ net/ghttp/ghttp_request_request.go | 12 +++++ net/ghttp/ghttp_response.go | 5 ++ net/ghttp/ghttp_response_cors.go | 46 ++++++++++++---- os/gfile/gfile.go | 12 ++++- os/gfile/gfile_z_test.go | 16 +++++- os/gsession/gsession_storage.go | 5 +- text/gstr/gstr_domain.go | 38 ++++++++++++++ text/gstr/gstr_z_unit_domain_test.go | 47 +++++++++++++++++ version.go | 2 +- 17 files changed, 330 insertions(+), 68 deletions(-) rename encoding/ghash/{ghash_test.go => ghash_bench_test.go} (100%) delete mode 100644 go.sum create mode 100644 text/gstr/gstr_domain.go create mode 100644 text/gstr/gstr_z_unit_domain_test.go diff --git a/.example/other/test.go b/.example/other/test.go index 3089d5cea..077f9b5fc 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,12 +1,48 @@ package main import ( - "github.com/gogf/gf/os/gcmd" - "github.com/gogf/gf/os/glog" + "fmt" + + "github.com/gogf/gf/os/gfile" + "github.com/gogf/gf/text/gregex" + + "github.com/gogf/gf/debug/gdebug" ) func main() { - glog.SetFlags(glog.F_TIME_DATE | glog.F_TIME_TIME | glog.F_FILE_SHORT) - glog.Debug("dd") - glog.Println("timeout", gcmd.GetOpt("timeout")) + cdnUrl := "http://localhost" + content := ` + + + + + +` + s, err := gregex.ReplaceStringFuncMatch(`(href|src)=['"](.+?)['"]`, content, func(match []string) string { + link := match[2] + if len(link) == 0 { + return match[0] + } + if link[0:1] != "/" && link[0:1] != "#" { + if len(link) > 10 && link[0:10] == "javascript" { + return match[0] + } + if len(link) > 7 && link[0:7] == "mailto:" { + return match[0] + } + if len(link) > 4 && link[0:4] == "http" { + return match[0] + } + link = "/" + link + } + if link[0:1] == "/" { + switch gfile.ExtName(link) { + case "png", "jpg", "jpeg", "gif", "js", "css", "otf", "eot", "ttf", "woff", "woff2": + return fmt.Sprintf(`%s="%s%s?%s"`, match[1], cdnUrl, link, gdebug.BinVersion()) + } + } + return match[0] + }) + fmt.Println(err) + fmt.Println(s) } diff --git a/container/gvar/gvar.go b/container/gvar/gvar.go index c9f07e4d6..c22dbd292 100644 --- a/container/gvar/gvar.go +++ b/container/gvar/gvar.go @@ -220,11 +220,63 @@ func (v *Var) Map(tags ...string) map[string]interface{} { return gconv.Map(v.Val(), tags...) } +// MapStrStr converts to map[string]string. +func (v *Var) MapStrStr(tags ...string) map[string]string { + m := v.Map(tags...) + if len(m) > 0 { + vMap := make(map[string]string) + for k, v := range m { + vMap[k] = gconv.String(v) + } + return vMap + } + return nil +} + +// MapStrVar converts to map[string]*Var. +func (v *Var) MapStrVar(tags ...string) map[string]*Var { + m := v.Map(tags...) + if len(m) > 0 { + vMap := make(map[string]*Var) + for k, v := range m { + vMap[k] = New(v) + } + return vMap + } + return nil +} + // MapDeep converts to map[string]interface{} recursively. func (v *Var) MapDeep(tags ...string) map[string]interface{} { return gconv.MapDeep(v.Val(), tags...) } +// MapDeep converts to map[string]string recursively. +func (v *Var) MapStrStrDeep(tags ...string) map[string]string { + m := v.MapDeep(tags...) + if len(m) > 0 { + vMap := make(map[string]string) + for k, v := range m { + vMap[k] = gconv.String(v) + } + return vMap + } + return nil +} + +// MapStrVarDeep converts to map[string]*Var recursively. +func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var { + m := v.MapDeep(tags...) + if len(m) > 0 { + vMap := make(map[string]*Var) + for k, v := range m { + vMap[k] = New(v) + } + return vMap + } + return nil +} + // Struct maps value of to . // The parameter should be a pointer to a struct instance. // The parameter is used to specify the key-to-attribute mapping rules. diff --git a/debug/gdebug/gdebug.go b/debug/gdebug/gdebug.go index ce32f2608..ee5d8e1ab 100644 --- a/debug/gdebug/gdebug.go +++ b/debug/gdebug/gdebug.go @@ -12,7 +12,13 @@ import ( "fmt" "path/filepath" "runtime" + "strconv" "strings" + + "github.com/gogf/gf/encoding/ghash" + + "github.com/gogf/gf/crypto/gmd5" + "github.com/gogf/gf/os/gfile" ) const ( @@ -21,8 +27,9 @@ const ( ) var ( - // goRootForFilter is used for stack filtering purpose. - goRootForFilter = runtime.GOROOT() + goRootForFilter = runtime.GOROOT() // goRootForFilter is used for stack filtering purpose. + binaryVersion = "" // The version of current running binary(uint64 hex). + binaryVersionMd5 = "" // The version of current running binary(MD5). ) func init() { @@ -31,6 +38,27 @@ func init() { } } +// BinVersion returns the version of current running binary. +// It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary. +func BinVersion() string { + if binaryVersion == "" { + binaryVersion = strconv.FormatInt( + int64(ghash.BKDRHash(gfile.GetBytes(gfile.SelfPath()))), + 36, + ) + } + return binaryVersion +} + +// BinVersionMd5 returns the version of current running binary. +// It uses MD5 algorithm to calculate the unique version of the binary. +func BinVersionMd5() string { + if binaryVersionMd5 == "" { + binaryVersionMd5, _ = gmd5.EncryptFile(gfile.SelfPath()) + } + return binaryVersionMd5 +} + // PrintStack prints to standard error the stack trace returned by runtime.Stack. func PrintStack(skip ...int) { fmt.Print(Stack(skip...)) diff --git a/encoding/ghash/ghash_test.go b/encoding/ghash/ghash_bench_test.go similarity index 100% rename from encoding/ghash/ghash_test.go rename to encoding/ghash/ghash_bench_test.go diff --git a/encoding/gjson/gjson_z_bench_test.go b/encoding/gjson/gjson_z_bench_test.go index 6496fc657..f257efaa5 100644 --- a/encoding/gjson/gjson_z_bench_test.go +++ b/encoding/gjson/gjson_z_bench_test.go @@ -12,6 +12,23 @@ import ( "github.com/gogf/gf/encoding/gjson" ) +var ( + jsonStr1 = `[1,2,3]` + jsonStr2 = `{"CallbackCommand":"Group.CallbackAfterSendMsg","From_Account":"61934946","GroupId":"@TGS#2FLGX67FD","MsgBody":[{"MsgContent":{"Text":"是的"},"MsgType":"TIMTextElem"}],"MsgSeq":23,"MsgTime":1567032819,"Operator_Account":"61934946","Random":2804799576,"Type":"Public"}` +) + +func Benchmark_Validate1(b *testing.B) { + for i := 0; i < b.N; i++ { + gjson.Valid(jsonStr1) + } +} + +func Benchmark_Validate2(b *testing.B) { + for i := 0; i < b.N; i++ { + gjson.Valid(jsonStr2) + } +} + func Benchmark_Set1(b *testing.B) { for i := 0; i < b.N; i++ { p := gjson.New(map[string]string{ diff --git a/go.sum b/go.sum deleted file mode 100644 index 10b37e2ef..000000000 --- a/go.sum +++ /dev/null @@ -1,44 +0,0 @@ -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I= -github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= -github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= -github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/gf-third/mysql v1.4.2 h1:f1M5CNFUG3WkE07UOomtu4o0n/KJKeuUUf5Nc9ZFXs4= -github.com/gf-third/mysql v1.4.2/go.mod h1:+dd90V663ppI2fV5uQ6+rHk0u8KCyU6FkG8Um8Cx3ms= -github.com/gf-third/yaml/v3 v3.0.0 h1:IMLH3JWFpNraTz5d6jzaNZFvWaVYAwhP9w1lhk9SFUs= -github.com/gf-third/yaml/v3 v3.0.0/go.mod h1:En6jd9ZtAhuCiVfRnTo8NYVgbiZvg/F26r8Tj+vsUy4= -github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0= -github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= -github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= -github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556 h1:RS7ewakEriTcISIy+USQV9tE37SpWCblCiPE6QWxagk= -github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556/go.mod h1:Xk7G0nwBiIloTMbLddk4WWJOqi4i/JLhadLd0HUXO30= -github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= -github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88= -github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= -github.com/theckman/go-flock v0.7.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1cHUZgO1Ebq5r2hIjfo= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -google.golang.org/appengine v1.6.2/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/net/ghttp/ghttp_request_post.go b/net/ghttp/ghttp_request_post.go index ed9cef7e6..4a7cbb55a 100644 --- a/net/ghttp/ghttp_request_post.go +++ b/net/ghttp/ghttp_request_post.go @@ -157,6 +157,18 @@ func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]s return nil } +func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { + postMap := r.GetPostMap(kvMap...) + if len(postMap) > 0 { + m := make(map[string]*gvar.Var) + for k, v := range postMap { + m[k] = gvar.New(v) + } + return m + } + return nil +} + // 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系 func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error { r.initPost() diff --git a/net/ghttp/ghttp_request_query.go b/net/ghttp/ghttp_request_query.go index 834ec824e..c432a697c 100644 --- a/net/ghttp/ghttp_request_query.go +++ b/net/ghttp/ghttp_request_query.go @@ -129,6 +129,18 @@ func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string] return nil } +func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { + queryMap := r.GetQueryMap(kvMap...) + if len(queryMap) > 0 { + m := make(map[string]*gvar.Var) + for k, v := range queryMap { + m[k] = gvar.New(v) + } + return m + } + return nil +} + // 将所有的get参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系 func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error { r.initGet() diff --git a/net/ghttp/ghttp_request_request.go b/net/ghttp/ghttp_request_request.go index 229c99533..543cfdc57 100644 --- a/net/ghttp/ghttp_request_request.go +++ b/net/ghttp/ghttp_request_request.go @@ -130,6 +130,18 @@ func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[strin return nil } +func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { + requestMap := r.GetRequestMap(kvMap...) + if len(requestMap) > 0 { + m := make(map[string]*gvar.Var) + for k, v := range requestMap { + m[k] = gvar.New(v) + } + return m + } + return nil +} + // 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系 func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]string) error { tagMap := structs.TagMapName(pointer, paramTagPriority, true) diff --git a/net/ghttp/ghttp_response.go b/net/ghttp/ghttp_response.go index 8cfa248a9..fbc13a4f4 100644 --- a/net/ghttp/ghttp_response.go +++ b/net/ghttp/ghttp_response.go @@ -221,6 +221,11 @@ func (r *Response) Buffer() []byte { return r.buffer.Bytes() } +// 获取当前缓冲区中的数据(string) +func (r *Response) BufferString() string { + return r.buffer.String() +} + // 获取当前缓冲区中的数据大小 func (r *Response) BufferLength() int { return r.buffer.Len() diff --git a/net/ghttp/ghttp_response_cors.go b/net/ghttp/ghttp_response_cors.go index 81bd7ad29..65278bfd5 100644 --- a/net/ghttp/ghttp_response_cors.go +++ b/net/ghttp/ghttp_response_cors.go @@ -13,17 +13,18 @@ import ( ) // See https://www.w3.org/TR/cors/ . -// 服务端允许跨域请求选项 type CORSOptions struct { - AllowOrigin string // Access-Control-Allow-Origin - AllowCredentials string // Access-Control-Allow-Credentials - ExposeHeaders string // Access-Control-Expose-Headers - MaxAge int // Access-Control-Max-Age - AllowMethods string // Access-Control-Allow-Methods - AllowHeaders string // Access-Control-Allow-Headers + AllowDomain []string // Used for allowing requests from custom domains + AllowOrigin string // Access-Control-Allow-Origin + AllowCredentials string // Access-Control-Allow-Credentials + ExposeHeaders string // Access-Control-Expose-Headers + MaxAge int // Access-Control-Max-Age + AllowMethods string // Access-Control-Allow-Methods + AllowHeaders string // Access-Control-Allow-Headers } -// 默认的CORS配置 +// DefaultCORSOptions returns the default CORS options, +// which allows any cross-domain request. func (r *Response) DefaultCORSOptions() CORSOptions { options := CORSOptions{ AllowOrigin: "*", @@ -42,12 +43,34 @@ func (r *Response) DefaultCORSOptions() CORSOptions { return options } +// CORS sets custom CORS options. // See https://www.w3.org/TR/cors/ . -// 允许请求跨域访问. func (r *Response) CORS(options CORSOptions) { - if options.AllowOrigin != "" { + if options.AllowDomain != nil { + //origin := r.request.Header.Get("Origin") + //if origin == "" { + // return + //} + //parsed, err := url.Parse(origin) + //if err != nil { + // return + //} + //for k, v := range options.AllowDomain { + // if gstr.Contains(v, "*") { + // // Regular expression. + // gstr.ReplaceByArray(v, []string{ + // ".", "\\.", "*", "[^\\.]*", + // }) + // } else if len(parsed.Host) >= len(v) && parsed.Host[len(parsed.Host)-len(v):] == v { + // // Last domain. + // r.Header().Set("Access-Control-Allow-Origin", origin) + // break + // } + //} + } else if options.AllowOrigin != "" { r.Header().Set("Access-Control-Allow-Origin", options.AllowOrigin) } + if options.AllowCredentials != "" { r.Header().Set("Access-Control-Allow-Credentials", options.AllowCredentials) } @@ -65,7 +88,8 @@ func (r *Response) CORS(options CORSOptions) { } } -// 允许请求跨域访问(使用默认配置). +// CORSDefault sets CORS with default CORS options, +// which allows any cross-domain request. func (r *Response) CORSDefault() { r.CORS(r.DefaultCORSOptions()) } diff --git a/os/gfile/gfile.go b/os/gfile/gfile.go index 8942797e4..b6e8541b0 100644 --- a/os/gfile/gfile.go +++ b/os/gfile/gfile.go @@ -444,7 +444,17 @@ func IsEmpty(path string) bool { // // Note: the result contains symbol '.'. func Ext(path string) string { - return filepath.Ext(path) + ext := filepath.Ext(path) + if p := strings.IndexByte(ext, '?'); p != -1 { + ext = ext[0:p] + } + return ext +} + +// ExtName is like function Ext, which returns the file name extension used by path, +// but the result does not contains symbol '.'. +func ExtName(path string) string { + return strings.TrimLeft(Ext(path), ".") } // Home returns absolute path of current user's home directory. diff --git a/os/gfile/gfile_z_test.go b/os/gfile/gfile_z_test.go index 47478a68c..3416a4afc 100644 --- a/os/gfile/gfile_z_test.go +++ b/os/gfile/gfile_z_test.go @@ -593,7 +593,6 @@ func Test_Dir(t *testing.T) { }) } -// 获取文件名 func Test_Ext(t *testing.T) { gtest.Case(t, func() { var ( @@ -608,7 +607,22 @@ func Test_Ext(t *testing.T) { gtest.Assert(gfile.Ext(testpath()+paths1), ".txt") gtest.Assert(gfile.Ext(testpath()+dirpath1), "") + }) + gtest.Case(t, func() { + gtest.Assert(gfile.Ext("/var/www/test.js"), ".js") + gtest.Assert(gfile.Ext("/var/www/test.min.js"), ".js") + gtest.Assert(gfile.Ext("/var/www/test.js?1"), ".js") + gtest.Assert(gfile.Ext("/var/www/test.min.js?v1"), ".js") + }) +} + +func Test_ExtName(t *testing.T) { + gtest.Case(t, func() { + gtest.Assert(gfile.ExtName("/var/www/test.js"), "js") + gtest.Assert(gfile.ExtName("/var/www/test.min.js"), "js") + gtest.Assert(gfile.ExtName("/var/www/test.js?v=1"), "js") + gtest.Assert(gfile.ExtName("/var/www/test.min.js?v=1"), "js") }) } diff --git a/os/gsession/gsession_storage.go b/os/gsession/gsession_storage.go index 141be367b..8a1b10d84 100644 --- a/os/gsession/gsession_storage.go +++ b/os/gsession/gsession_storage.go @@ -27,12 +27,11 @@ type Storage interface { // RemoveAll deletes all key-value pairs from storage. RemoveAll() error - // GetSession returns the session data bytes for given session id. + // GetSession returns the session data map for given session id. // The parameter specifies the TTL for this session. // It returns nil if the TTL is exceeded. GetSession(id string, ttl time.Duration) map[string]interface{} - // SetSession updates the content for session id. - // Note that the parameter is the serialized bytes for session map. + // SetSession updates the data map for specified session id. SetSession(id string, data map[string]interface{}) error // UpdateTTL updates the TTL for specified session id. diff --git a/text/gstr/gstr_domain.go b/text/gstr/gstr_domain.go new file mode 100644 index 000000000..6be615b4f --- /dev/null +++ b/text/gstr/gstr_domain.go @@ -0,0 +1,38 @@ +// Copyright 2019 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. + +package gstr + +import "strings" + +// IsSubDomain checks whether is sub-domain of mainDomain. +// It supports '*' in . +func IsSubDomain(subDomain string, mainDomain string) bool { + subArray := strings.Split(subDomain, ".") + mainArray := strings.Split(mainDomain, ".") + subLength := len(subArray) + mainLength := len(mainArray) + // Eg: + // "s.s.goframe.org" is not sub-domain of "*.goframe.org" + // but + // "s.s.goframe.org" is not sub-domain of "goframe.org" + if mainLength > 2 && subLength > mainLength { + return false + } + minLength := subLength + if mainLength < minLength { + minLength = mainLength + } + for i := minLength; i > 0; i-- { + if mainArray[mainLength-i] == "*" { + continue + } + if mainArray[mainLength-i] != subArray[subLength-i] { + return false + } + } + return true +} diff --git a/text/gstr/gstr_z_unit_domain_test.go b/text/gstr/gstr_z_unit_domain_test.go new file mode 100644 index 000000000..c82c75bd5 --- /dev/null +++ b/text/gstr/gstr_z_unit_domain_test.go @@ -0,0 +1,47 @@ +// Copyright 2019 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. + +// go test *.go -bench=".*" + +package gstr_test + +import ( + "testing" + + "github.com/gogf/gf/test/gtest" + "github.com/gogf/gf/text/gstr" +) + +func Test_IsSubDomain(t *testing.T) { + gtest.Case(t, func() { + main := "goframe.org" + gtest.Assert(gstr.IsSubDomain("goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("johng.cn", main), false) + gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false) + gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false) + }) + gtest.Case(t, func() { + main := "*.goframe.org" + gtest.Assert(gstr.IsSubDomain("goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), false) + gtest.Assert(gstr.IsSubDomain("johng.cn", main), false) + gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false) + gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false) + }) + gtest.Case(t, func() { + main := "*.*.goframe.org" + gtest.Assert(gstr.IsSubDomain("goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), true) + gtest.Assert(gstr.IsSubDomain("s.s.s.goframe.org", main), false) + gtest.Assert(gstr.IsSubDomain("johng.cn", main), false) + gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false) + gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false) + }) +} diff --git a/version.go b/version.go index 374bba3e1..13c31448d 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package gf -const VERSION = "v1.9.1" +const VERSION = "v1.9.3" const AUTHORS = "john"