Skip to content

Commit b70ce54

Browse files
committed
Add HTTPCompressorWithLevel
Also use the new flate package for gzip compression for HTTP, and set the default HTTP compression level to 4, which is currently the best efficiency in V2.
1 parent f8935d5 commit b70ce54

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

http.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package brotli
22

33
import (
4-
"compress/gzip"
54
"io"
65
"net/http"
76
"strings"
7+
8+
"github.com/andybalholm/brotli/flate"
89
)
910

1011
// HTTPCompressor chooses a compression method (brotli, gzip, or none) based on
1112
// the Accept-Encoding header, sets the Content-Encoding header, and returns a
1213
// WriteCloser that implements that compression. The Close method must be called
1314
// before the current HTTP handler returns.
1415
func HTTPCompressor(w http.ResponseWriter, r *http.Request) io.WriteCloser {
16+
return HTTPCompressorWithLevel(w, r, 4)
17+
}
18+
19+
func HTTPCompressorWithLevel(w http.ResponseWriter, r *http.Request, level int) io.WriteCloser {
1520
if w.Header().Get("Vary") == "" {
1621
w.Header().Set("Vary", "Accept-Encoding")
1722
}
@@ -20,10 +25,10 @@ func HTTPCompressor(w http.ResponseWriter, r *http.Request) io.WriteCloser {
2025
switch encoding {
2126
case "br":
2227
w.Header().Set("Content-Encoding", "br")
23-
return NewWriterV2(w, DefaultCompression)
28+
return NewWriterV2(w, level)
2429
case "gzip":
2530
w.Header().Set("Content-Encoding", "gzip")
26-
return gzip.NewWriter(w)
31+
return flate.NewGZIPWriter(w, level)
2732
}
2833
return nopCloser{w}
2934
}

0 commit comments

Comments
 (0)