Skip to content

Commit

Permalink
response_writer: support http/2 push (#188)
Browse files Browse the repository at this point in the history
* add http/2 push support

* Update method return suggested by unknown at response_writer.go

Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>

* usage of errors.New

* Update response_writer.go

Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
  • Loading branch information
jeffprestes and unknwon committed Mar 27, 2020
1 parent 1874886 commit b2cd309
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion response_writer.go
Expand Up @@ -16,6 +16,7 @@ package macaron

import (
"bufio"
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -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.
Expand Down Expand Up @@ -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()
}
Expand All @@ -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)
}
7 changes: 7 additions & 0 deletions response_writer_test.go
Expand Up @@ -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)
})
}

0 comments on commit b2cd309

Please sign in to comment.