mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-27 12:06:31 -08:00
tsweb: implement FlushError on loggingResponseWriter
http.ResponseController.Flush prefers a FlushError method at each level of the ResponseWriter wrapper chain and falls back to Flusher.Flush, which can't report an error. loggingResponseWriter only had Flush, so handlers using ResponseController to flush never saw the underlying writer's flush errors, such as a stream reset by an expired write deadline. Add FlushError that delegates via ResponseController so errors from the wrapped writer propagate. Updates tailscale/corp#48531 Signed-off-by: Brad Fitzpatrick <[email protected]> Change-Id: I259c5de234435f632387261165fe15dcaf34ff45
This commit is contained in:
1 parent
3f3e56f412
commit
523b626a8e
2 files changed
+24
No files matched your search
@@ -736,6 +736,13 @@ func (lg loggingResponseWriter) Flush() {
|
||||
f.Flush()
|
||||
}
|
||||
|
||||
// FlushError is the error-returning form of Flush that
|
||||
// [http.ResponseController.Flush] prefers, so handlers can see write
|
||||
// errors (such as an expired write deadline) that Flush would swallow.
|
||||
func (lg *loggingResponseWriter) FlushError() error {
|
||||
return http.NewResponseController(lg.ResponseWriter).Flush()
|
||||
}
|
||||
|
||||
func (lg *loggingResponseWriter) Unwrap() http.ResponseWriter {
|
||||
return lg.ResponseWriter
|
||||
}
|
||||
|
||||
@@ -1365,3 +1365,20 @@ func ExampleMiddlewareStack() {
|
||||
// B mw3
|
||||
// C mw2.4
|
||||
}
|
||||
|
||||
// flushErrWriter is a ResponseWriter whose flush fails with err.
|
||||
type flushErrWriter struct {
|
||||
http.ResponseWriter
|
||||
err error
|
||||
}
|
||||
|
||||
func (w flushErrWriter) FlushError() error { return w.err }
|
||||
|
||||
func TestLoggingResponseWriter_FlushError(t *testing.T) {
|
||||
want := errors.New("flush failed")
|
||||
inner := flushErrWriter{ResponseWriter: httptest.NewRecorder(), err: want}
|
||||
lw := newLogResponseWriter(t.Logf, inner, httptest.NewRequest("GET", "/", nil))
|
||||
if err := http.NewResponseController(lw).Flush(); !errors.Is(err, want) {
|
||||
t.Errorf("Flush error = %v, want %v", err, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user