add more functions for gtcp; remove default domain set for cookie feature of ghttp.Server

This commit is contained in:
John
2019-11-14 15:41:28 +08:00
parent e36dd06f22
commit 1becc4932c
10 changed files with 75 additions and 25 deletions

View File

@ -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()

View File

@ -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())
}

View File

@ -1,3 +1,5 @@
# redis配置
[[redis-cache]]
db = 0

View File

@ -8,5 +8,5 @@ import (
// 使用g.Config方法获取配置管理对象并指定默认的配置文件名称
func main() {
fmt.Println(g.Config("config.json").Get("viewpath"))
fmt.Println(g.Config().Get("viewpath"))
}

View File

@ -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"))
}

View File

@ -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
}
}

View File

@ -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)

View File

@ -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,

View File

@ -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 <til>.
// Note that the returned result contains the last bytes <til>.
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
}

View File

@ -118,6 +118,18 @@ func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error) {
return data, err
}
// RecvTil reads data from the connection until reads bytes <til>.
// Note that the returned result contains the last bytes <til>.
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 {