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

Fix compression of *os.Files. #197

Merged
merged 1 commit into from
Aug 31, 2020
Merged
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
7 changes: 7 additions & 0 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (cw *compressResponseWriter) Write(b []byte) (int, error) {
return cw.compressor.Write(b)
}

func (cw *compressResponseWriter) ReadFrom(r io.Reader) (int64, error) {
return io.Copy(cw.compressor, r)
}

type flusher interface {
Flush() error
}
Expand Down Expand Up @@ -129,6 +133,9 @@ func CompressHandlerLevel(h http.Handler, level int) http.Handler {
Flush: func(httpsnoop.FlushFunc) httpsnoop.FlushFunc {
return cw.Flush
},
ReadFrom: func(rff httpsnoop.ReadFromFunc) httpsnoop.ReadFromFunc {
return cw.ReadFrom
},
})

h.ServeHTTP(w, r)
Expand Down
54 changes: 54 additions & 0 deletions compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ package handlers

import (
"bufio"
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strconv"
"testing"
)
Expand Down Expand Up @@ -158,6 +164,54 @@ func TestCompressHandlerGzipDeflate(t *testing.T) {
}
}

// Make sure we can compress and serve an *os.File properly. We need
// to use a real http server to trigger the net/http sendfile special
// case.
func TestCompressFile(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏻👏🏻👏🏻

dir, err := ioutil.TempDir("", "gorilla_compress")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

err = ioutil.WriteFile(filepath.Join(dir, "hello.txt"), []byte("hello"), 0644)
if err != nil {
t.Fatal(err)
}

s := httptest.NewServer(CompressHandler(http.FileServer(http.Dir(dir))))
defer s.Close()

url := &url.URL{Scheme: "http", Host: s.Listener.Addr().String(), Path: "/hello.txt"}
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set(acceptEncoding, "gzip")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("expected OK, got %q", res.Status)
}

var got bytes.Buffer
gr, err := gzip.NewReader(res.Body)
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(&got, gr)
if err != nil {
t.Fatal(err)
}

if got.String() != "hello" {
t.Errorf("expected hello, got %q", got.String())
}
}

type fullyFeaturedResponseWriter struct{}

// Header/Write/WriteHeader implement the http.ResponseWriter interface.
Expand Down