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

stream decompression instead of buffering #2018

Merged
merged 4 commits into from Dec 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 24 additions & 45 deletions middleware/decompress.go
@@ -1,10 +1,8 @@
package middleware

import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"sync"

Expand Down Expand Up @@ -43,26 +41,7 @@ type DefaultGzipDecompressPool struct {
}

func (d *DefaultGzipDecompressPool) gzipDecompressPool() sync.Pool {
return sync.Pool{
New: func() interface{} {
// create with an empty reader (but with GZIP header)
w, err := gzip.NewWriterLevel(ioutil.Discard, gzip.BestSpeed)
if err != nil {
return err
}

b := new(bytes.Buffer)
w.Reset(b)
w.Flush()
w.Close()

r, err := gzip.NewReader(bytes.NewReader(b.Bytes()))
if err != nil {
return err
}
return r
},
}
return sync.Pool{New: func() interface{} { return new(gzip.Reader) }}
}

//Decompress decompresses request body based if content encoding type is set to "gzip" with default config
Expand All @@ -82,38 +61,38 @@ func DecompressWithConfig(config DecompressConfig) echo.MiddlewareFunc {

return func(next echo.HandlerFunc) echo.HandlerFunc {
pool := config.GzipDecompressPool.gzipDecompressPool()

return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
switch c.Request().Header.Get(echo.HeaderContentEncoding) {
case GZIPEncoding:
b := c.Request().Body

i := pool.Get()
gr, ok := i.(*gzip.Reader)
if !ok {
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error())
}

if err := gr.Reset(b); err != nil {
pool.Put(gr)
if err == io.EOF { //ignore if body is empty
return next(c)
}
return err
}
var buf bytes.Buffer
io.Copy(&buf, gr)
if c.Request().Header.Get(echo.HeaderContentEncoding) != GZIPEncoding {
return next(c)
}

gr.Close()
pool.Put(gr)
i := pool.Get()
gr, ok := i.(*gzip.Reader)
if !ok || gr == nil {
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you're trying reading the request body, wouldn't this be a 4xx?

Copy link
Contributor Author

@davidmdm davidmdm Nov 6, 2021

Choose a reason for hiding this comment

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

This was as is. Regardless I believe this is correct.
Since sync.Pool returns an interface{} we have no choice but to cast the value to the underlying type to work with it.
This middleware creates a sync.Pool to amortise allocation of gzip.Reader objects. If for some reason the pool is returning something else that we aren't expecting than something is wrong with the lib, not the request. Hence the 500.

An argument could be made that this might even be a good spot to panic, but I don't want to introduce unneeded changes in this PR.

}
defer pool.Put(gr)

b.Close() // http.Request.Body is closed by the Server, but because we are replacing it, it must be closed here
b := c.Request().Body
defer b.Close()

r := ioutil.NopCloser(&buf)
c.Request().Body = r
if err := gr.Reset(b); err != nil {
if err == io.EOF { //ignore if body is empty
return next(c)
}
return err
}

// only Close gzip reader if it was set to a proper gzip source otherwise it will panic on close.
defer gr.Close()

c.Request().Body = gr

return next(c)
}
}
Expand Down