forked from robfig/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
158 lines (137 loc) · 3.37 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package revel
import (
"compress/gzip"
"compress/zlib"
"net/http"
"strconv"
"strings"
)
var compressionTypes = [...]string{
"gzip",
"deflate",
}
var compressableMimes = [...]string{
"text/plain",
"text/html",
"text/xml",
"text/css",
"application/xml",
"application/xhtml+xml",
"application/rss+xml",
"application/javascript",
"application/x-javascript",
}
type WriteFlusher interface {
Write([]byte) (int, error)
Flush() error
}
type CompressResponseWriter struct {
http.ResponseWriter
compressWriter WriteFlusher
compressionType string
headersWritten bool
}
func CompressFilter(c *Controller, fc []Filter) {
writer := CompressResponseWriter{c.Response.Out, nil, "", false}
writer.DetectCompressionType(c.Request, c.Response)
c.Response.Out = &writer
fc[0](c, fc[1:])
}
func (c *CompressResponseWriter) prepareHeaders() {
if c.compressionType != "" {
responseMime := c.Header().Get("Content-Type")
responseMime = strings.TrimSpace(strings.SplitN(responseMime, ";", 2)[0])
shouldEncode := false
for _, compressableMime := range compressableMimes {
if responseMime == compressableMime {
shouldEncode = true
c.Header().Set("Content-Encoding", c.compressionType)
c.Header().Del("Content-Length")
break
}
}
if !shouldEncode {
c.compressWriter = nil
c.compressionType = ""
}
}
}
func (c *CompressResponseWriter) WriteHeader(status int) {
c.headersWritten = true
c.prepareHeaders()
c.ResponseWriter.WriteHeader(status)
}
func (c *CompressResponseWriter) Write(b []byte) (int, error) {
if !c.headersWritten {
c.prepareHeaders()
c.headersWritten = true
}
if c.compressionType != "" {
defer c.compressWriter.Flush()
return c.compressWriter.Write(b)
} else {
return c.ResponseWriter.Write(b)
}
}
func (c *CompressResponseWriter) DetectCompressionType(req *Request, resp *Response) {
if Config.BoolDefault("results.compressed", false) {
acceptedEncodings := strings.Split(req.Request.Header.Get("Accept-Encoding"), ",")
largestQ := 0.0
chosenEncoding := len(compressionTypes)
for _, encoding := range acceptedEncodings {
encoding = strings.TrimSpace(encoding)
encodingParts := strings.SplitN(encoding, ";", 2)
// If we are the format "gzip;q=0.8"
if len(encodingParts) > 1 {
// Strip off the q=
num, err := strconv.ParseFloat(strings.TrimSpace(encodingParts[1])[2:], 32)
if err != nil {
continue
}
if num >= largestQ && num > 0 {
if encodingParts[0] == "*" {
chosenEncoding = 0
largestQ = num
continue
}
for i, encoding := range compressionTypes {
if encoding == encodingParts[0] {
if i < chosenEncoding {
largestQ = num
chosenEncoding = i
}
break
}
}
}
} else {
// If we can accept anything, chose our preferred method.
if encodingParts[0] == "*" {
chosenEncoding = 0
largestQ = 1
break
}
// This is for just plain "gzip"
for i, encoding := range compressionTypes {
if encoding == encodingParts[0] {
if i < chosenEncoding {
largestQ = 1.0
chosenEncoding = i
}
break
}
}
}
}
if largestQ == 0 {
return
}
c.compressionType = compressionTypes[chosenEncoding]
switch c.compressionType {
case "gzip":
c.compressWriter = gzip.NewWriter(resp.Out)
case "deflate":
c.compressWriter = zlib.NewWriter(resp.Out)
}
}
}