Skip to content

Commit

Permalink
ImmediateHeaderFlush when no body (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobochka authored Mar 15, 2021
1 parent 02e0722 commit 0cd7349
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
8 changes: 4 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1716,21 +1716,21 @@ func (resp *Response) writeBodyStream(w *bufio.Writer, sendBody bool) (err error
}
}
if contentLength >= 0 {
if err = resp.Header.Write(w); err == nil && sendBody {
if err = resp.Header.Write(w); err == nil {
if resp.ImmediateHeaderFlush {
err = w.Flush()
}
if err == nil {
if err == nil && sendBody {
err = writeBodyFixedSize(w, resp.bodyStream, int64(contentLength))
}
}
} else {
resp.Header.SetContentLength(-1)
if err = resp.Header.Write(w); err == nil && sendBody {
if err = resp.Header.Write(w); err == nil {
if resp.ImmediateHeaderFlush {
err = w.Flush()
}
if err == nil {
if err == nil && sendBody {
err = writeBodyChunked(w, resp.bodyStream)
}
}
Expand Down
80 changes: 80 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,7 @@ func TestRequestRawBodyCopyTo(t *testing.T) {
type testReader struct {
read chan (int)
cb chan (struct{})
onClose func() error
}

func (r *testReader) Read(b []byte) (int, error) {
Expand All @@ -2230,6 +2231,13 @@ func (r *testReader) Read(b []byte) (int, error) {
return read, nil
}

func (r *testReader) Close() error {
if r.onClose != nil {
return r.onClose()
}
return nil
}

func TestResponseImmediateHeaderFlushRegressionFixedLength(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2301,6 +2309,42 @@ func TestResponseImmediateHeaderFlushFixedLength(t *testing.T) {
<-waitForIt
}

func TestResponseImmediateHeaderFlushFixedLengthSkipBody(t *testing.T) {
t.Parallel()

var r Response

r.ImmediateHeaderFlush = true
r.SkipBody = true

ch := make(chan int)
cb := make(chan struct{})

buf := &testReader{read: ch, cb: cb}

r.SetBodyStream(buf, 0)

b := []byte{}
w := bytes.NewBuffer(b)
bb := bufio.NewWriter(w)

var headersOnClose string
buf.onClose = func() error {
headersOnClose = w.String()
return nil
}

bw := &r

if err := bw.Write(bb); err != nil {
t.Errorf("unexpected error: %s", err)
}

if !strings.Contains(headersOnClose, "Content-Length: 0") {
t.Fatalf("Expected headers to be eagerly flushed")
}
}

func TestResponseImmediateHeaderFlushChunked(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2347,6 +2391,42 @@ func TestResponseImmediateHeaderFlushChunked(t *testing.T) {
<-waitForIt
}

func TestResponseImmediateHeaderFlushChunkedNoBody(t *testing.T) {
t.Parallel()

var r Response

r.ImmediateHeaderFlush = true
r.SkipBody = true

ch := make(chan int)
cb := make(chan struct{})

buf := &testReader{read: ch, cb: cb}

r.SetBodyStream(buf, -1)

b := []byte{}
w := bytes.NewBuffer(b)
bb := bufio.NewWriter(w)

var headersOnClose string
buf.onClose = func() error {
headersOnClose = w.String()
return nil
}

bw := &r

if err := bw.Write(bb); err != nil {
t.Errorf("unexpected error: %s", err)
}

if !strings.Contains(headersOnClose, "Transfer-Encoding: chunked") {
t.Fatalf("Expected headers to be eagerly flushed")
}
}

type ErroneousBodyStream struct {
errOnRead bool
errOnClose bool
Expand Down

0 comments on commit 0cd7349

Please sign in to comment.