Skip to content

Commit 4faf8a8

Browse files
committed
net/http, doc/go1.13.html: revert TimeoutHandler.Flush
Also added a test to ensure that any interactions between TimeoutHandler and Flusher result in the correct status code and body, but also that we don't get superfluous logs from stray writes as was seen in the bug report. Fixes #34439. Change-Id: I4af62db256742326f9353f98a2fcb5f71d2a5fd9 Reviewed-on: https://go-review.googlesource.com/c/go/+/197659 Run-TryBot: Emmanuel Odeke <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 616c39f commit 4faf8a8

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

doc/go1.13.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
821821
<p><!-- CL 154383 -->
822822
<a href="/pkg/net/http/#TimeoutHandler"><code>TimeoutHandler</code></a>'s
823823
<a href="/pkg/net/http/#ResponseWriter"><code>ResponseWriter</code></a> now implements the
824-
<a href="/pkg/net/http/#Pusher"><code>Pusher</code></a> and <a href="/pkg/net/http/#Flusher"><code>Flusher</code></a> interfaces.
824+
<a href="/pkg/net/http/#Pusher"><code>Pusher</code></a> interface.
825825
</p>
826826

827827
<p><!-- CL 157339 -->

src/net/http/serve_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6161,6 +6161,54 @@ func TestUnsupportedTransferEncodingsReturn501(t *testing.T) {
61616161
}
61626162
}
61636163

6164+
// Issue 34439: ensure that TimeoutHandler doesn't implement Flusher
6165+
// and that any interaction with Flusher won't affect TimeoutHandler's behavior.
6166+
func TestTimeoutHandlerAndFlusher(t *testing.T) {
6167+
timeout := 50 * time.Millisecond
6168+
6169+
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
6170+
w.WriteHeader(StatusTeapot)
6171+
w.Write([]byte("line1\n"))
6172+
fl, ok := w.(Flusher)
6173+
if ok {
6174+
fl.Flush()
6175+
}
6176+
time.Sleep(timeout * 2)
6177+
w.Write([]byte("line2\n"))
6178+
})
6179+
6180+
cst := httptest.NewUnstartedServer(TimeoutHandler(handler, timeout, "TIMED OUT\n"))
6181+
// Provide a logger that will report an error on any superfluous log.
6182+
cst.Config.ErrorLog = log.New(&errorOnWrite{t: t}, "", 0)
6183+
cst.Start()
6184+
defer cst.Close()
6185+
6186+
res, err := cst.Client().Get(cst.URL)
6187+
if err != nil {
6188+
t.Fatal(err)
6189+
}
6190+
defer res.Body.Close()
6191+
6192+
if g, w := res.StatusCode, StatusServiceUnavailable; g != w {
6193+
t.Errorf("Status code mismatch\ngot: %d\nwant: %d", g, w)
6194+
}
6195+
6196+
slurp, _ := ioutil.ReadAll(res.Body)
6197+
if g, w := string(slurp), "TIMED OUT\n"; g != w {
6198+
t.Fatalf("Body mismatch\ngot: %q\nwant: %q", g, w)
6199+
}
6200+
}
6201+
6202+
// errorOnWrite will invoke t.Error on any attempted write.
6203+
type errorOnWrite struct {
6204+
t *testing.T
6205+
}
6206+
6207+
func (ew *errorOnWrite) Write(b []byte) (int, error) {
6208+
ew.t.Errorf("Unexpected write: %s\n", b)
6209+
return len(b), nil
6210+
}
6211+
61646212
// fetchWireResponse is a helper for dialing to host,
61656213
// sending http1ReqBody as the payload and retrieving
61666214
// the response as it was sent on the wire.

src/net/http/server.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,7 +3272,6 @@ type timeoutWriter struct {
32723272
}
32733273

32743274
var _ Pusher = (*timeoutWriter)(nil)
3275-
var _ Flusher = (*timeoutWriter)(nil)
32763275

32773276
// Push implements the Pusher interface.
32783277
func (tw *timeoutWriter) Push(target string, opts *PushOptions) error {
@@ -3282,14 +3281,6 @@ func (tw *timeoutWriter) Push(target string, opts *PushOptions) error {
32823281
return ErrNotSupported
32833282
}
32843283

3285-
// Flush implements the Flusher interface.
3286-
func (tw *timeoutWriter) Flush() {
3287-
f, ok := tw.w.(Flusher)
3288-
if ok {
3289-
f.Flush()
3290-
}
3291-
}
3292-
32933284
func (tw *timeoutWriter) Header() Header { return tw.h }
32943285

32953286
func (tw *timeoutWriter) Write(p []byte) (int, error) {

0 commit comments

Comments
 (0)