From 1becc4932c0d5ef37ec4244f550a880372567307 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 14 Nov 2019 15:41:28 +0800 Subject: [PATCH] add more functions for gtcp; remove default domain set for cookie feature of ghttp.Server --- .example/net/ghttp/server/session/session.go | 20 ++++++------ .example/net/gtcp/gtcp_func.go | 7 ++--- .example/os/gcfg/basic/config.toml | 2 ++ .example/os/gcfg/basic/gcfg4.go | 2 +- .example/os/gcfg/basic/gcfg_error.go | 3 +- net/ghttp/ghttp_client_config.go | 10 ++++-- net/ghttp/ghttp_server_config.go | 4 +++ net/ghttp/ghttp_server_cookie.go | 8 ++--- net/gtcp/gtcp_conn.go | 32 ++++++++++++++++++-- net/gtcp/gtcp_pool.go | 12 ++++++++ 10 files changed, 75 insertions(+), 25 deletions(-) diff --git a/.example/net/ghttp/server/session/session.go b/.example/net/ghttp/server/session/session.go index 7f1e0691a..241a99db2 100644 --- a/.example/net/ghttp/server/session/session.go +++ b/.example/net/ghttp/server/session/session.go @@ -8,15 +8,17 @@ import ( func main() { s := g.Server() - 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.BindHandler("/clear", func(r *ghttp.Request) { - r.Session.Clear() + s.Group("/", func(g *ghttp.RouterGroup) { + g.GET("/set", func(r *ghttp.Request) { + r.Session.Set("time", gtime.Second()) + r.Response.Write("ok") + }) + g.GET("/get", func(r *ghttp.Request) { + r.Response.WriteJson(r.Session.Map()) + }) + g.GET("/clear", func(r *ghttp.Request) { + r.Session.Clear() + }) }) s.SetPort(8199) s.Run() diff --git a/.example/net/gtcp/gtcp_func.go b/.example/net/gtcp/gtcp_func.go index 7bc083718..bca7df226 100644 --- a/.example/net/gtcp/gtcp_func.go +++ b/.example/net/gtcp/gtcp_func.go @@ -8,11 +8,10 @@ import ( ) func main() { - data, err := gtcp.SendRecv("www.baidu.com:80", []byte("HEAD / HTTP/1.1\n\n"), -1) - if len(data) > 0 { - fmt.Println(string(data)) - } + dstConn, err := gtcp.NewPoolConn("www.medlinker.com:80") + _, err = dstConn.Write([]byte("HEAD / HTTP/1.1\n\n")) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err.Error()) } + fmt.Println(dstConn.RecvLine()) } diff --git a/.example/os/gcfg/basic/config.toml b/.example/os/gcfg/basic/config.toml index 7d211ff13..384011b2f 100644 --- a/.example/os/gcfg/basic/config.toml +++ b/.example/os/gcfg/basic/config.toml @@ -1,3 +1,5 @@ + + # redis配置 [[redis-cache]] db = 0 diff --git a/.example/os/gcfg/basic/gcfg4.go b/.example/os/gcfg/basic/gcfg4.go index b81b398c1..321ed03a7 100644 --- a/.example/os/gcfg/basic/gcfg4.go +++ b/.example/os/gcfg/basic/gcfg4.go @@ -8,5 +8,5 @@ import ( // 使用g.Config方法获取配置管理对象,并指定默认的配置文件名称 func main() { - fmt.Println(g.Config("config.json").Get("viewpath")) + fmt.Println(g.Config().Get("viewpath")) } diff --git a/.example/os/gcfg/basic/gcfg_error.go b/.example/os/gcfg/basic/gcfg_error.go index 9a4852c3c..b0bc88f72 100644 --- a/.example/os/gcfg/basic/gcfg_error.go +++ b/.example/os/gcfg/basic/gcfg_error.go @@ -6,7 +6,6 @@ import ( "github.com/gogf/gf/frame/g" ) -// 演示在找不到配置文件时的错误提示 func main() { - fmt.Println(g.Config("none-exist-config.toml").Get("none")) + fmt.Println(g.Config().Get("none")) } diff --git a/net/ghttp/ghttp_client_config.go b/net/ghttp/ghttp_client_config.go index 82bcaa671..08baaf8aa 100644 --- a/net/ghttp/ghttp_client_config.go +++ b/net/ghttp/ghttp_client_config.go @@ -25,6 +25,12 @@ func (c *Client) SetHeader(key, value string) { c.header[key] = value } +func (c *Client) SetHeaderMap(m map[string]string) { + for k, v := range m { + c.header[k] = v + } +} + // 通过字符串设置HTTP Header func (c *Client) SetHeaderRaw(header string) { for _, line := range strings.Split(strings.TrimSpace(header), "\n") { @@ -41,8 +47,8 @@ func (c *Client) SetCookie(key, value string) { } // 使用Map设置COOKIE -func (c *Client) SetCookieMap(cookieMap map[string]string) { - for k, v := range cookieMap { +func (c *Client) SetCookieMap(m map[string]string) { + for k, v := range m { c.cookies[k] = v } } diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 024780b7a..3abde8d6c 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -137,6 +137,10 @@ func (s *Server) SetConfig(c ServerConfig) error { c.Handler = http.HandlerFunc(s.defaultHttpHandle) } s.config = c + // Static. + if c.ServerRoot != "" { + s.SetServerRoot(c.ServerRoot) + } // HTTPS. if c.TLSConfig == nil && c.HTTPSCertPath != "" { s.EnableHTTPS(c.HTTPSCertPath, c.HTTPSKeyPath) diff --git a/net/ghttp/ghttp_server_cookie.go b/net/ghttp/ghttp_server_cookie.go index 4783acb49..33e4876b0 100644 --- a/net/ghttp/ghttp_server_cookie.go +++ b/net/ghttp/ghttp_server_cookie.go @@ -55,10 +55,10 @@ func (c *Cookie) init() { c.domain = c.request.Server.GetCookieDomain() c.maxage = c.request.Server.GetCookieMaxAge() c.response = c.request.Response - // 如果没有设置COOKIE有效域名,那么设置HOST为默认有效域名 - if c.domain == "" { - c.domain = c.request.GetHost() - } + // DO NOT ADD ANY DEFAULT COOKIE DOMAIN! + //if c.domain == "" { + // c.domain = c.request.GetHost() + //} for _, v := range c.request.Cookies() { c.data[v.Name] = CookieItem{ v.Value, v.Domain, v.Path, int64(v.Expires.Second()), v.HttpOnly, diff --git a/net/gtcp/gtcp_conn.go b/net/gtcp/gtcp_conn.go index 28fe9d9fb..53dc1dadd 100644 --- a/net/gtcp/gtcp_conn.go +++ b/net/gtcp/gtcp_conn.go @@ -186,17 +186,43 @@ func (c *Conn) RecvLine(retry ...Retry) ([]byte, error) { for { buffer, err = c.Recv(1, retry...) if len(buffer) > 0 { - data = append(data, buffer...) if buffer[0] == '\n' { + data = append(data, buffer[:len(buffer)-1]...) break + } else { + data = append(data, buffer...) } } if err != nil { break } } - if len(data) > 0 { - data = bytes.TrimRight(data, "\n\r") + return data, err +} + +// RecvTil reads data from the connection until reads bytes . +// Note that the returned result contains the last bytes . +func (c *Conn) RecvTil(til []byte, retry ...Retry) ([]byte, error) { + var err error + var buffer []byte + data := make([]byte, 0) + length := len(til) + for { + buffer, err = c.Recv(1, retry...) + if len(buffer) > 0 { + if length > 0 && + len(data) >= length-1 && + buffer[0] == til[length-1] && + bytes.EqualFold(data[len(data)-length+1:], til[:length-1]) { + data = append(data, buffer...) + break + } else { + data = append(data, buffer...) + } + } + if err != nil { + break + } } return data, err } diff --git a/net/gtcp/gtcp_pool.go b/net/gtcp/gtcp_pool.go index 89c1ee77c..a681fe60e 100644 --- a/net/gtcp/gtcp_pool.go +++ b/net/gtcp/gtcp_pool.go @@ -118,6 +118,18 @@ func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error) { return data, err } +// RecvTil reads data from the connection until reads bytes . +// Note that the returned result contains the last bytes . +func (c *PoolConn) RecvTil(til []byte, retry ...Retry) ([]byte, error) { + data, err := c.Conn.RecvTil(til, retry...) + if err != nil { + c.status = gCONN_STATUS_ERROR + } else { + c.status = gCONN_STATUS_ACTIVE + } + return data, err +} + // RecvWithTimeout reads data from the connection with timeout. func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) { if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil {