From 05957b4965413bccac8a7e24dc8f5314f15a278b Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 24 Feb 2016 12:23:15 -0700 Subject: [PATCH] gzip: Implement http.Hijacker (fixes #635) --- middleware/gzip/gzip.go | 11 +++++++++++ middleware/recorder.go | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/middleware/gzip/gzip.go b/middleware/gzip/gzip.go index d7ed835d..9d75b351 100644 --- a/middleware/gzip/gzip.go +++ b/middleware/gzip/gzip.go @@ -3,10 +3,12 @@ package gzip import ( + "bufio" "compress/gzip" "fmt" "io" "io/ioutil" + "net" "net/http" "strings" @@ -130,3 +132,12 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) { n, err := w.Writer.Write(b) return n, err } + +// Hijack implements http.Hijacker. It simply wraps the underlying +// ResponseWriter's Hijack method if there is one, or returns an error. +func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + if hj, ok := w.ResponseWriter.(http.Hijacker); ok { + return hj.Hijack() + } + return nil, nil, fmt.Errorf("not a Hijacker") +} diff --git a/middleware/recorder.go b/middleware/recorder.go index 481b60df..d3e65dc6 100644 --- a/middleware/recorder.go +++ b/middleware/recorder.go @@ -62,11 +62,11 @@ func (r *ResponseRecorder) Status() int { return r.status } -// Hijacker is a wrapper of http.Hijacker underearth if any, -// otherwise it just returns an error. +// Hijack implements http.Hijacker. It simply wraps the underlying +// ResponseWriter's Hijack method if there is one, or returns an error. func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) { if hj, ok := r.ResponseWriter.(http.Hijacker); ok { return hj.Hijack() } - return nil, nil, errors.New("I'm not a Hijacker") + return nil, nil, errors.New("not a Hijacker") }