Skip to content

Commit

Permalink
feat: add end chunk for broken reader (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever authored Aug 24, 2023
1 parent a3770c3 commit e2872f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/protocol/http1/ext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
errs "github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/network"
"github.com/cloudwego/hertz/pkg/protocol"
Expand Down Expand Up @@ -117,12 +118,17 @@ func WriteBodyChunked(w network.Writer, r io.Reader) error {
if err == nil {
panic("BUG: io.Reader returned 0, nil")
}
if err == io.EOF {
if err = WriteChunk(w, buf[:0], true); err != nil {
break
}
err = nil

if !errors.Is(err, io.EOF) {
hlog.SystemLogger().Warnf("writing chunked response body encountered an error from the reader, "+
"this may cause the short of the content in response body, error: %s", err.Error())
}

if err = WriteChunk(w, buf[:0], true); err != nil {
break
}

err = nil
break
}
if err = WriteChunk(w, buf[:n], true); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/protocol/http1/ext/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

errs "github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/test/assert"
"github.com/cloudwego/hertz/pkg/common/test/mock"
"github.com/cloudwego/hertz/pkg/protocol"
Expand Down Expand Up @@ -123,6 +124,9 @@ func TestReadRawHeaders(t *testing.T) {
}

func TestBodyChunked(t *testing.T) {
var log bytes.Buffer
hlog.SetOutput(&log)

body := "foobar baz aaa bbb ccc"
chunk := "16\r\nfoobar baz aaa bbb ccc\r\n0\r\n"
b := bytes.NewBufferString(body)
Expand All @@ -137,6 +141,22 @@ func TestBodyChunked(t *testing.T) {
rb, err := ReadBody(zr, -1, 0, nil)
assert.Nil(t, err)
assert.DeepEqual(t, body, string(rb))

assert.DeepEqual(t, 0, log.Len())
}

func TestBrokenBodyChunked(t *testing.T) {
brokenReader := mock.NewBrokenConn("")
var log bytes.Buffer
hlog.SetOutput(&log)

var w bytes.Buffer
zw := netpoll.NewWriter(&w)
err := WriteBodyChunked(zw, brokenReader)
assert.Nil(t, err)

assert.DeepEqual(t, []byte("0\r\n"), w.Bytes())
assert.True(t, bytes.Contains(log.Bytes(), []byte("writing chunked response body encountered an error from the reader")))
}

func TestBodyFixedSize(t *testing.T) {
Expand Down

0 comments on commit e2872f9

Please sign in to comment.