Skip to content

Commit

Permalink
Support more interfaces for ResponseStatusRecorder
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Jan 21, 2022
1 parent 412598e commit 857a1e8
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion response_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@

package web

import "net/http"
import (
"bufio"
"errors"
"io"
"net"
"net/http"
)

// ResponseStatusRecorder implements http.ResponseWriter that keeps tack of HTTP
// response status code and written body size in bytes.
Expand Down Expand Up @@ -56,3 +62,35 @@ func (r *ResponseStatusRecorder) Status() int {
func (r *ResponseStatusRecorder) ResponseBodySize() int {
return r.size
}

func (r *ResponseStatusRecorder) Flush() {
f, ok := r.ResponseWriter.(http.Flusher)
if !ok {
return
}
f.Flush()
}

func (r *ResponseStatusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := r.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("response writer does not implement http.Hijacker")
}
return h.Hijack()
}

func (r *ResponseStatusRecorder) ReadFrom(src io.Reader) (int64, error) {
rf, ok := r.ResponseWriter.(io.ReaderFrom)
if !ok {
return 0, errors.New("response writer does not implement io.ReaderFrom")
}
return rf.ReadFrom(src)
}

func (r *ResponseStatusRecorder) Push(target string, opts *http.PushOptions) error {
p, ok := r.ResponseWriter.(http.Pusher)
if !ok {
return errors.New("response writer does not implement http.Pusher")
}
return p.Push(target, opts)
}

0 comments on commit 857a1e8

Please sign in to comment.