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

DisableGzip middleware implementation #10

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
27 changes: 24 additions & 3 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func Gziper(options ...Options) macaron.Handler {
if err != nil {
panic(err.Error())
}
defer gz.Close()

defer func() {
if _, ok := ctx.Resp.(gzipResponseWriter); ok {
gz.Close()
}
}()

gzw := gzipResponseWriter{gz, ctx.Resp}
ctx.Resp = gzw
Expand All @@ -94,8 +99,10 @@ func Gziper(options ...Options) macaron.Handler {

ctx.Next()

// delete content length after we know we have been written to
gzw.Header().Del("Content-Length")
if _, ok := ctx.Resp.(gzipResponseWriter); ok {
// delete content length after we know we have been written to
gzw.Header().Del("Content-Length")
}
}
}

Expand All @@ -118,3 +125,17 @@ func (grw gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
}
return hijacker.Hijack()
}

// DisableGzip rolls back all the changes made by gzipResponseWriter, that way the content won't be gzipped
func DisableGzip(ctx *macaron.Context) {
if grw, ok := ctx.Resp.(gzipResponseWriter); ok {
origrw := grw.ResponseWriter
ctx.MapTo(origrw, (*http.ResponseWriter)(nil))
if _, ok := ctx.Render.(*macaron.DummyRender); !ok {
ctx.Render.SetResponseWriter(origrw)
}
ctx.Resp = origrw
ctx.Resp.Header().Del(_HEADER_CONTENT_ENCODING)
ctx.Resp.Header().Del(_HEADER_VARY)
}
}
33 changes: 33 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,36 @@ func Test_ResponseWriter_Hijack(t *testing.T) {
So(hijackable.Hijacked, ShouldBeTrue)
})
}

func Test_DisableGzip(t *testing.T) {
Convey("Disable compression for a request with a middleware", t, func() {
m := macaron.New()
m.Use(Gziper())
data := "aaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb"
m.Get("/compressed", func() string { return data })
m.Get("/uncompressed", DisableGzip, func() string { return data })

// Test compressed
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/compressed", nil)
req.Header.Set(_HEADER_ACCEPT_ENCODING, "gzip")
So(err, ShouldBeNil)
m.ServeHTTP(resp, req)

ce := resp.Header().Get(_HEADER_CONTENT_ENCODING)
So(strings.EqualFold(ce, "gzip"), ShouldBeTrue)
So(strings.EqualFold(resp.Body.String(), data), ShouldBeFalse)

// Test uncompressed
resp = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/uncompressed", nil)
req.Header.Set(_HEADER_ACCEPT_ENCODING, "gzip")
So(err, ShouldBeNil)
m.ServeHTTP(resp, req)

ce = resp.Header().Get(_HEADER_CONTENT_ENCODING)
So(strings.EqualFold(ce, "gzip"), ShouldBeFalse)
So(strings.EqualFold(resp.Body.String(), data), ShouldBeTrue)

})
}