This repository was archived by the owner on Apr 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgzip.go
More file actions
171 lines (146 loc) · 4.24 KB
/
Copy pathgzip.go
File metadata and controls
171 lines (146 loc) · 4.24 KB
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
// Copyright 2013 Martini Authors
// Copyright 2015 The Macaron Authors
// Copyright 2017 Grafana Labs
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package gziper
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"github.com/klauspost/compress/gzip"
"gopkg.in/macaron.v1"
)
const (
_HEADER_ACCEPT_ENCODING = "Accept-Encoding"
_HEADER_CONTENT_ENCODING = "Content-Encoding"
_HEADER_CONTENT_LENGTH = "Content-Length"
_HEADER_CONTENT_TYPE = "Content-Type"
_HEADER_VARY = "Vary"
)
type nopCloser struct {
io.Writer
}
func (nopCloser) Close() error { return nil }
type WriterPool struct {
pool sync.Pool
}
func NewWriterPool() *WriterPool {
return &WriterPool{pool: sync.Pool{
New: func() interface{} { return nil },
}}
}
func (wp *WriterPool) Get(rw macaron.ResponseWriter) *gzip.Writer {
ret := wp.pool.Get()
if ret == nil {
ret = gzip.NewWriter(rw)
} else {
ret.(*gzip.Writer).Reset(rw)
}
return ret.(*gzip.Writer)
}
func (wp *WriterPool) Put(w *gzip.Writer) {
wp.pool.Put(w)
}
var writerPool WriterPool
/*
Gzip middleware inspired by https://github.com/go-macaron/gzip
The main difference is that this middleware will only compress
the body if it is not already compressed (content-type header
already set to gzip).
This usecase is needed when requests are conditionally proxied
and the proxy response may or may not already be compressed.
*/
func Gziper() macaron.Handler {
return func(ctx *macaron.Context) {
if !strings.Contains(ctx.Req.Header.Get(_HEADER_ACCEPT_ENCODING), "gzip") {
return
}
grw := &gzipResponseWriter{
ResponseWriter: ctx.Resp,
}
defer grw.close()
ctx.Resp = grw
ctx.MapTo(grw, (*http.ResponseWriter)(nil))
// Check if render middleware has been registered,
// if yes, we need to modify ResponseWriter for it as well.
if _, ok := ctx.Render.(*macaron.DummyRender); !ok {
ctx.Render.SetResponseWriter(grw)
}
ctx.Next()
// delete content length after we know we have been written to
grw.Header().Del(_HEADER_CONTENT_LENGTH)
}
}
type gzipResponseWriter struct {
w io.WriteCloser
macaron.ResponseWriter
closed bool
}
func (grw *gzipResponseWriter) setup() {
if grw.w != nil {
return
}
if grw.closed || grw.Header().Get(_HEADER_CONTENT_ENCODING) == "gzip" {
// another handler has already set the content-type to gzip,
// so presumably they are going to write compressed data.
// We dont want to compress the data a second time, so lets
// have writes go directly to the normal ResponseWriter
grw.w = nopCloser{grw.ResponseWriter}
} else {
headers := grw.Header()
headers.Set(_HEADER_CONTENT_ENCODING, "gzip")
headers.Set(_HEADER_VARY, _HEADER_ACCEPT_ENCODING)
grw.w = writerPool.Get(grw.ResponseWriter)
}
}
func (grw *gzipResponseWriter) close() {
grw.closed = true
if grw.w == nil {
return
}
grw.w.Close()
if poolWriter, ok := grw.w.(*gzip.Writer); ok {
writerPool.Put(poolWriter)
}
grw.w = nil
}
func (grw *gzipResponseWriter) Write(p []byte) (int, error) {
if !grw.Written() {
// try and detect the content type. If we dont do this here,
// http.ResponseWriter will try and detect the content-type but
// will be looking at the compressed payload.
if len(grw.Header().Get(_HEADER_CONTENT_TYPE)) == 0 {
grw.Header().Set(_HEADER_CONTENT_TYPE, http.DetectContentType(p))
}
grw.WriteHeader(http.StatusOK)
}
return grw.w.Write(p)
}
func (grw *gzipResponseWriter) WriteHeader(s int) {
if !grw.Written() {
grw.setup()
}
grw.ResponseWriter.WriteHeader(s)
}
func (grw gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := grw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}