Skip to content

Commit

Permalink
fix: server will ignore last data when Read return n,EOF (#15)
Browse files Browse the repository at this point in the history
* fix: server will ignore last data when Read return n,EOF

* update error message
  • Loading branch information
wzekin authored May 5, 2023
1 parent 08fdf08 commit 24c7527
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
8 changes: 6 additions & 2 deletions factory/http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func getStream(data []byte) io.Reader {

go func() {
time.Sleep(100 * time.Millisecond)
writer.Write(data)
if len(data) != 0 {
writer.Write(data)
}
writer.Close()
}()

Expand All @@ -156,8 +158,10 @@ func getBadStream(data []byte) io.Reader {
reader, writer := io.Pipe()

go func() {
writer.Write(data)
time.Sleep(100 * time.Millisecond)
if len(data) != 0 {
writer.Write(data)
}
writer.CloseWithError(errors.New("test error"))
}()

Expand Down
23 changes: 13 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1829,26 +1829,29 @@ func (sc *serverConn) newWriterAndRequestNoBody(st *stream) (*responseWriter, er
return rw, nil
}

func writeResponseBody(rw *responseWriter, reqCtx *app.RequestContext) error {
func writeResponseBody(rw *responseWriter, reqCtx *app.RequestContext) (err error) {
if reqCtx.Response.IsBodyStream() {
var n int
vbuf := utils.CopyBufPool.Get()
buf := vbuf.([]byte)
for {
n, err := reqCtx.Response.BodyStream().Read(buf)
if err == io.EOF {
n, err = reqCtx.Response.BodyStream().Read(buf)
if n == 0 {
if err == nil {
return errors.New("response bodyStream().Read(buf) returns 0, nil")
}
if err == io.EOF {
err = nil
}
break
}
if err != nil {
return err
}
_, err = rw.Write(buf[:n])
if err != nil {
return err
if _, err = rw.Write(buf[:n]); err != nil {
break
}
}
utils.CopyBufPool.Put(vbuf)

return nil
return err
} else {
// reqCtx.Response.Body can be no error
// will split at FrameWriteRequest's Consume function
Expand Down
2 changes: 2 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func newHertzServerTester(t testing.TB, handler app.HandlerFunc, opts ...interfa
}
st.hpackEnc = hpack.NewEncoder(&st.headerBuf)
st.hpackDec = hpack.NewDecoder(initialHeaderTableSize, st.onHeaderField)
st.addLogFilter("Transport readFrame error")

testHookGetServerConn = func(v *serverConn) {
st.scMu.Lock()
Expand All @@ -228,6 +229,7 @@ func newHertzServerTester(t testing.TB, handler app.HandlerFunc, opts ...interfa
st.fr.logWrites = true
}
}

return st
}

Expand Down

0 comments on commit 24c7527

Please sign in to comment.