-
Notifications
You must be signed in to change notification settings - Fork 3
/
responsewriter.go
186 lines (161 loc) · 3.66 KB
/
responsewriter.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
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
package jsonmask
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"net/http"
"strconv"
"compress/gzip"
"compress/zlib"
)
// JSONMask
type JSONMask struct {
queryKey string
next http.Handler
}
func NewJSONMask(next http.Handler, queryKey string) (http.Handler, error) {
if next == nil {
return nil, fmt.Errorf("http.Handler required")
}
if queryKey == "" {
return nil, fmt.Errorf("queryKey required")
}
return &JSONMask{queryKey, next}, nil
}
func (h *JSONMask) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fields := req.URL.Query().Get(h.queryKey)
if fields == "" {
h.next.ServeHTTP(rw, req)
return
}
sl, err := Compile(fields)
if err != nil {
h.next.ServeHTTP(rw, req)
return
}
jrw := NewResponseWriter(rw, sl)
defer jrw.Close()
h.next.ServeHTTP(jrw, req)
}
type ResponseWriter struct {
buf bytes.Buffer
rw http.ResponseWriter
sl Selection
status JSONMaskResponseWriterStatus
}
type JSONMaskResponseWriterStatus struct {
Status int
Size int
Trimmed int
}
type ResponseWriterWithCloseNotify struct {
*ResponseWriter
}
type JSONMaskResponseWriter interface {
http.ResponseWriter
Close() error
Status() JSONMaskResponseWriterStatus
}
func NewResponseWriter(rw http.ResponseWriter, sl Selection) JSONMaskResponseWriter {
jrw := &ResponseWriter{rw: rw, sl: sl}
if _, ok := rw.(http.CloseNotifier); !ok {
return jrw
}
return &ResponseWriterWithCloseNotify{jrw}
}
func (jrw *ResponseWriterWithCloseNotify) CloseNotify() <-chan bool {
return jrw.rw.(http.CloseNotifier).CloseNotify()
}
func (jrw *ResponseWriter) Header() http.Header {
return jrw.rw.Header()
}
func (jrw *ResponseWriter) Write(b []byte) (int, error) {
size, err := jrw.buf.Write(b)
jrw.status.Size += size
return size, err
}
func (jrw *ResponseWriter) WriteHeader(s int) {
jrw.status.Status = s
}
func (jrw *ResponseWriter) Close() error {
data := jrw.buf.Bytes()
if jrw.status.Status == http.StatusOK {
encoding := jrw.rw.Header().Get("Content-Encoding")
if d, err := uncompress(encoding, data); err == nil {
if d, err = jrw.sl.Mask(d); err == nil {
if d, err = compress(encoding, d); err == nil {
data = d
jrw.rw.Header().Set("Content-Length", strconv.Itoa(len(d)))
jrw.status.Trimmed = jrw.status.Size - len(d)
}
}
}
}
jrw.rw.WriteHeader(jrw.status.Status)
_, err := jrw.rw.Write(data)
return err
}
func (jrw *ResponseWriter) Status() JSONMaskResponseWriterStatus {
return jrw.status
}
func (jrw *ResponseWriter) Flush() {
if f, ok := jrw.rw.(http.Flusher); ok {
f.Flush()
}
}
func (jrw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := jrw.rw.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, fmt.Errorf("not a hijacker: %T", jrw.rw)
}
func compress(encoding string, data []byte) ([]byte, error) {
var err error
var wc io.WriteCloser
var b bytes.Buffer
switch encoding {
case "gzip":
wc = gzip.NewWriter(&b)
case "deflate":
wc = zlib.NewWriter(&b)
}
if wc == nil {
return data, nil
}
if _, err = wc.Write(data); err != nil {
return nil, err
}
if err := wc.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func uncompress(encoding string, data []byte) ([]byte, error) {
var err error
var rc io.ReadCloser
switch encoding {
case "gzip":
rc, err = gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
case "deflate":
rc, err = zlib.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
}
if rc == nil {
return data, nil
}
var b bytes.Buffer
if _, err := io.Copy(&b, rc); err != nil {
return nil, err
}
if err := rc.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}