From 9555743a4710b88ecc2ebbd68874eb595e00c51f Mon Sep 17 00:00:00 2001 From: Alex Guerra Date: Wed, 8 Jun 2016 17:06:55 -0500 Subject: [PATCH] Implement io.ReaderFrom on responseWriter This interface is implemented on the standard net/http response object in order to unlock some optimizations around sendfile. In the same vein as the other optional interfaces, calling ReadFrom will result in a runtime panic if the underling http.ResponseWriter doesn't implement it. --- response_writer.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/response_writer.go b/response_writer.go index fcbe230d..1ad5b191 100644 --- a/response_writer.go +++ b/response_writer.go @@ -22,6 +22,7 @@ type ( http.Hijacker http.Flusher http.CloseNotifier + io.ReaderFrom // Returns the HTTP response status code of the current request. Status() int @@ -114,3 +115,13 @@ func (w *responseWriter) CloseNotify() <-chan bool { func (w *responseWriter) Flush() { w.ResponseWriter.(http.Flusher).Flush() } + +// Implements the io.ReaderFrom interface +func (w *responseWriter) ReadFrom(r io.Reader) (n int64, err error) { + if w.size < 0 { + w.size = 0 + } + n, err = w.ResponseWriter.(io.ReaderFrom).ReadFrom(r) + w.size += n + return +}