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 bug that Content-Encoding won't be written to final response header for gzip and deflate #1

Merged
merged 2 commits into from
Jun 23, 2023
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
6 changes: 5 additions & 1 deletion bodyWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
type respWriter struct {
gin.ResponseWriter
threshold int
encoding string
algo algorithm
buf *bytes.Buffer
bytesWritten int
compressor io.WriteCloser
}

func newResponseWriter(c *gin.Context, swapSize int, algo algorithm) *respWriter {
func newResponseWriter(c *gin.Context, swapSize int, encoding string, algo algorithm) *respWriter {
return &respWriter{
c.Writer,
swapSize,
encoding,
algo,
bytes.NewBuffer(nil),
0,
Expand All @@ -45,6 +47,8 @@ func (rw *respWriter) Write(b []byte) (int, error) {
rw.Header().Del("Content-Length")

if !rw.Swapped() && rw.buf.Len()+len(b) >= rw.threshold {
rw.ResponseWriter.Header().Set("Content-Encoding", rw.encoding)
rw.ResponseWriter.Header().Set("Vary", "Accept-Encoding")
rw.compressor = rw.algo.getWriter(rw.ResponseWriter)
if copied, err := io.Copy(rw.compressor, rw.buf); err != nil {
return int(copied), err
Expand Down
2 changes: 1 addition & 1 deletion compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"testing"

"github.com/aurowora/compress"
compress "github.com/lf4096/gin-compress"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion decompress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"github.com/andybalholm/brotli"
"github.com/aurowora/compress"
compress "github.com/lf4096/gin-compress"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zlib"
"github.com/klauspost/compress/zstd"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/aurowora/compress
module github.com/lf4096/gin-compress

go 1.16

Expand Down
7 changes: 1 addition & 6 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,10 @@ func (cm *compressMiddleware) Handler(c *gin.Context) {
return
}

rw := newResponseWriter(c, cm.cfg.minCompressBytes, algorithms[algo])
rw := newResponseWriter(c, cm.cfg.minCompressBytes, algo, algorithms[algo])
c.Writer = rw
c.Next()

if rw.Swapped() {
rw.ResponseWriter.Header().Set("Content-Encoding", algo)
rw.ResponseWriter.Header().Set("Vary", "Accept-Encoding")
}

_ = rw.Close()
}

Expand Down