Skip to content

Commit

Permalink
Fix a bug about http pipeline in gnet
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Feb 20, 2020
1 parent e18b177 commit e19f553
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
22 changes: 12 additions & 10 deletions README.md
Expand Up @@ -440,7 +440,6 @@ import (
)

var res string
var resBytes []byte

type request struct {
proto, method string
Expand All @@ -462,25 +461,30 @@ type httpCodec struct {

func (hc *httpCodec) Encode(c gnet.Conn, buf []byte) (out []byte, err error) {
if c.Context() == nil {
return appendHandle(out, res), nil
return buf, nil
}
return appendResp(out, "500 Error", "", errMsg+"\n"), nil
}

func (hc *httpCodec) Decode(c gnet.Conn) ([]byte, error) {
func (hc *httpCodec) Decode(c gnet.Conn) (out []byte, err error) {
buf := c.Read()
c.ResetBuffer()

// process the pipeline
leftover, err := parseReq(buf, &hc.req)
var leftover []byte
pipeline:
leftover, err = parseReq(buf, &hc.req)
// bad thing happened
if err != nil {
c.SetContext(err)
return nil, err
} else if len(leftover) == len(buf) {
// request not ready, yet
return nil, nil
return
}
c.ResetBuffer()
return buf, nil
out = appendHandle(out, res)
buf = leftover
goto pipeline
}

func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
Expand All @@ -490,15 +494,14 @@ func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
}

func (hs *httpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// process the pipeline
if c.Context() != nil {
// bad thing happened
out = errMsgBytes
action = gnet.Close
return
}
// handle the request
out = resBytes
out = frame
return
}

Expand All @@ -512,7 +515,6 @@ func main() {
flag.Parse()

res = "Hello World!\r\n"
resBytes = []byte(res)

http := new(httpServer)
hc := new(httpCodec)
Expand Down
22 changes: 12 additions & 10 deletions README_ZH.md
Expand Up @@ -438,7 +438,6 @@ import (
)

var res string
var resBytes []byte

type request struct {
proto, method string
Expand All @@ -460,25 +459,30 @@ type httpCodec struct {

func (hc *httpCodec) Encode(c gnet.Conn, buf []byte) (out []byte, err error) {
if c.Context() == nil {
return appendHandle(out, res), nil
return buf, nil
}
return appendResp(out, "500 Error", "", errMsg+"\n"), nil
}

func (hc *httpCodec) Decode(c gnet.Conn) ([]byte, error) {
func (hc *httpCodec) Decode(c gnet.Conn) (out []byte, err error) {
buf := c.Read()
c.ResetBuffer()

// process the pipeline
leftover, err := parseReq(buf, &hc.req)
var leftover []byte
pipeline:
leftover, err = parseReq(buf, &hc.req)
// bad thing happened
if err != nil {
c.SetContext(err)
return nil, err
} else if len(leftover) == len(buf) {
// request not ready, yet
return nil, nil
return
}
c.ResetBuffer()
return buf, nil
out = appendHandle(out, res)
buf = leftover
goto pipeline
}

func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
Expand All @@ -488,15 +492,14 @@ func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
}

func (hs *httpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// process the pipeline
if c.Context() != nil {
// bad thing happened
out = errMsgBytes
action = gnet.Close
return
}
// handle the request
out = resBytes
out = frame
return
}

Expand All @@ -510,7 +513,6 @@ func main() {
flag.Parse()

res = "Hello World!\r\n"
resBytes = []byte(res)

http := new(httpServer)
hc := new(httpCodec)
Expand Down
22 changes: 12 additions & 10 deletions examples/http/http.go
Expand Up @@ -17,7 +17,6 @@ import (
)

var res string
var resBytes []byte

type request struct {
proto, method string
Expand All @@ -39,25 +38,30 @@ type httpCodec struct {

func (hc *httpCodec) Encode(c gnet.Conn, buf []byte) (out []byte, err error) {
if c.Context() == nil {
return appendHandle(out, res), nil
return buf, nil
}
return appendResp(out, "500 Error", "", errMsg+"\n"), nil
}

func (hc *httpCodec) Decode(c gnet.Conn) ([]byte, error) {
func (hc *httpCodec) Decode(c gnet.Conn) (out []byte, err error) {
buf := c.Read()
c.ResetBuffer()

// process the pipeline
leftover, err := parseReq(buf, &hc.req)
var leftover []byte
pipeline:
leftover, err = parseReq(buf, &hc.req)
// bad thing happened
if err != nil {
c.SetContext(err)
return nil, err
} else if len(leftover) == len(buf) {
// request not ready, yet
return nil, nil
return
}
c.ResetBuffer()
return buf, nil
out = appendHandle(out, res)
buf = leftover
goto pipeline
}

func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
Expand All @@ -67,15 +71,14 @@ func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
}

func (hs *httpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// process the pipeline
if c.Context() != nil {
// bad thing happened
out = errMsgBytes
action = gnet.Close
return
}
// handle the request
out = resBytes
out = frame
return
}

Expand All @@ -89,7 +92,6 @@ func main() {
flag.Parse()

res = "Hello World!\r\n"
resBytes = []byte(res)

http := new(httpServer)
hc := new(httpCodec)
Expand Down

0 comments on commit e19f553

Please sign in to comment.