-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompress.go
88 lines (82 loc) · 2.28 KB
/
compress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2019 Christopher E. Miller
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package smallprox
import (
"compress/flate"
"compress/gzip"
"io"
"net/http"
"strings"
"github.com/andybalholm/brotli"
)
type CompressResponder struct {
toggle
}
func (er *CompressResponder) Response(req *http.Request, resp *http.Response) *http.Response {
if !er.Enabled() {
return resp
}
acceptEncs := req.Header["Accept-Encoding"]
canBrotli := HasAnyHeaderValuePart(acceptEncs, "br")
canGzip := HasAnyHeaderValuePart(acceptEncs, "gzip")
canDeflate := HasAnyHeaderValuePart(acceptEncs, "deflate")
if canBrotli || canGzip || canDeflate {
respContentType := resp.Header.Get("Content-Type")
respMIMEType := respContentType
{
isem := strings.IndexByte(respMIMEType, ';')
if isem != -1 {
respMIMEType = respMIMEType[:isem]
}
}
if strings.HasPrefix(respMIMEType, "text/") ||
respMIMEType == "application/javascript" ||
respMIMEType == "application/json" ||
strings.HasSuffix(respMIMEType, "+xml") ||
strings.HasSuffix(respMIMEType, "+json") {
if compressCheck(resp) {
outbuf := &Mutable{}
var dest io.WriteCloser
var enc string
if canBrotli {
dest = brotli.NewWriterLevel(outbuf, brotli.DefaultCompression)
enc = "br"
} else if canGzip {
// https://blog.klauspost.com/go-gzipdeflate-benchmarks/
dest, _ = gzip.NewWriterLevel(outbuf, 5)
enc = "gzip"
} else if canDeflate {
// https://blog.klauspost.com/go-gzipdeflate-benchmarks/
dest, _ = flate.NewWriter(outbuf, 5)
enc = "deflate"
}
io.Copy(dest, resp.Body)
dest.Close() // Finish the compression.
resp.Body.Close()
resp.Body = outbuf
resp.Header.Set("Content-Encoding", enc)
// TODO: Content-Length ....
}
}
}
return resp
}
func compressCheck(resp *http.Response) bool {
var pr peekCloser
if x, ok := resp.Body.(peekCloser); ok {
pr = x
} else {
pr = newPeekCloser(resp.Body)
resp.Body = pr
}
const largeEnough = 256
peekbuf, _ := pr.Peek(largeEnough)
if len(peekbuf) < largeEnough {
// Don't bother compressing smaller files.
return false
}
return true
}