Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server will ignore last data when Read return n,EOF #15

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1834,26 +1834,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