Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding: Implement http.Hijacker in gzipWriter through delegation #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions encoding/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ Package encoding contains Content-Encoding related filters.
package encoding

import (
"bufio"
"compress/gzip"
"errors"
"io"
"net"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -58,6 +61,13 @@ func (w *gzipWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
}

func (w *gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := w.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, errors.New("not a Hijacker")
}

func (w *gzipWriter) init() error {
w.Lock()
defer w.Unlock()
Expand Down
17 changes: 17 additions & 0 deletions encoding/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,20 @@ func TestGzipper_WriteHeader(t *testing.T) {
}
}
}

func TestGzipper_Hijacker(t *testing.T) {
srv := httptest.NewServer(Gzipper(gzip.DefaultCompression)(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, ok := w.(http.Hijacker); !ok {
t.Error("underlying http.ResponseWriter should implement http.Hijacker")
}
}),
))
defer srv.Close()

resp, err := http.Get(srv.URL)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}