From b2cd309dc711ad238e63785012080a37ccd6314c Mon Sep 17 00:00:00 2001 From: Jeff Prestes Date: Fri, 27 Mar 2020 13:36:03 -0300 Subject: [PATCH] response_writer: support http/2 push (#188) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add http/2 push support * Update method return suggested by unknown at response_writer.go Co-Authored-By: ᴜɴᴋɴᴡᴏɴ * usage of errors.New * Update response_writer.go Co-authored-by: ᴜɴᴋɴᴡᴏɴ --- response_writer.go | 12 +++++++++++- response_writer_test.go | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/response_writer.go b/response_writer.go index 9133948..59485d4 100644 --- a/response_writer.go +++ b/response_writer.go @@ -16,6 +16,7 @@ package macaron import ( "bufio" + "errors" "fmt" "net" "net/http" @@ -27,6 +28,7 @@ import ( type ResponseWriter interface { http.ResponseWriter http.Flusher + http.Pusher // Status returns the status code of the response or 0 if the response has not been written. Status() int // Written returns whether or not the ResponseWriter has been written. @@ -91,7 +93,7 @@ func (rw *responseWriter) Before(before BeforeFunc) { func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { hijacker, ok := rw.ResponseWriter.(http.Hijacker) if !ok { - return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface") + return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface") } return hijacker.Hijack() } @@ -112,3 +114,11 @@ func (rw *responseWriter) Flush() { flusher.Flush() } } + +func (rw *responseWriter) Push(target string, opts *http.PushOptions) error { + pusher, ok := rw.ResponseWriter.(http.Pusher) + if !ok { + return errors.New("the ResponseWriter doesn't support the Pusher interface") + } + return pusher.Push(target, opts) +} diff --git a/response_writer_test.go b/response_writer_test.go index 082b697..4ce2d11 100644 --- a/response_writer_test.go +++ b/response_writer_test.go @@ -185,4 +185,11 @@ func Test_ResponseWriter(t *testing.T) { So(resp.Code, ShouldEqual, http.StatusOK) So(resp.Body.String(), ShouldEqual, "data: Hello\n\ndata: Hello\n\n") }) + + Convey("Response writer with http/2 push", t, func() { + resp := httptest.NewRecorder() + rw := NewResponseWriter("GET", resp) + _, ok := rw.(http.Pusher) + So(ok, ShouldBeTrue) + }) }