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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
package revel
import (
"compress/gzip"
"compress/zlib"
"io"
"net/http"
"strconv"
"strings"
)
var compressionTypes = [...]string{
"gzip",
"deflate",
}
var compressableMimes = [...]string{
"text/plain",
"text/html",
"text/xml",
"text/css",
"application/json",
"application/xml",
"application/xhtml+xml",
"application/rss+xml",
"application/javascript",
"application/x-javascript",
}
type WriteFlusher interface {
io.Writer
io.Closer
Flush() error
}
type CompressResponseWriter struct {
http.ResponseWriter
compressWriter WriteFlusher
compressionType string
headersWritten bool
closeNotify chan bool
parentNotify <-chan bool
closed bool
}
func CompressFilter(c *Controller, fc []Filter) {
fc[0](c, fc[1:])
if Config.BoolDefault("results.compressed", false) {
if c.Response.Status != http.StatusNoContent && c.Response.Status != http.StatusNotModified {
writer := CompressResponseWriter{c.Response.Out, nil, "", false, make(chan bool, 1), nil, false}
writer.DetectCompressionType(c.Request, c.Response)
w, ok := c.Response.Out.(http.CloseNotifier)
if ok {
writer.parentNotify = w.CloseNotify()
}
c.Response.Out = &writer
} else {
TRACE.Printf("Compression disabled for response status (%d)", c.Response.Status)
}
}
}
func (c CompressResponseWriter) CloseNotify() <-chan bool {
if c.parentNotify != nil {
return c.parentNotify
}
return c.closeNotify
}
func (c *CompressResponseWriter) prepareHeaders() {
if c.compressionType != "" {
responseMime := c.Header().Get("Content-Type")
responseMime = strings.TrimSpace(strings.SplitN(responseMime, ";", 2)[0])
shouldEncode := false
if c.Header().Get("Content-Encoding") == "" {
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) Close() error {
if c.compressionType != "" {
c.compressWriter.Close()
}
if w, ok := c.ResponseWriter.(io.Closer); ok {
w.Close()
}
// Non-blocking write to the closenotifier, if we for some reason should
// get called multiple times
select {
case c.closeNotify <- true:
default:
}
c.closed = true
return nil
}
func (c *CompressResponseWriter) Write(b []byte) (int, error) {
// Abort if parent has been closed
if c.parentNotify != nil {
select {
case <-c.parentNotify:
return 0, io.ErrClosedPipe
default:
}
}
// Abort if we ourselves have been closed
if c.closed {
return 0, io.ErrClosedPipe
}
if !c.headersWritten {
c.prepareHeaders()
c.headersWritten = true
}
if c.compressionType != "" {
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)
}
}
}
|