/
writer.go
200 lines (164 loc) · 4.11 KB
/
writer.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package intercept
import (
"bufio"
"bytes"
"io"
"log"
"net"
"net/http"
"github.com/felixge/httpsnoop"
"go.sancus.dev/web"
"go.sancus.dev/web/errors"
)
type WriteInterceptor struct {
buffer bytes.Buffer
code int
mute bool
capture bool
headersWritten bool
rw http.ResponseWriter // ResponseWriter wrapper
header http.Header // Working copy of Headers
original http.Header // Original Headers table
}
func (m *WriteInterceptor) Writer() http.ResponseWriter {
return m.rw
}
func (m *WriteInterceptor) Error() web.Error {
if !m.headersWritten {
return &errors.HandlerError{
Code: http.StatusNoContent,
Header: m.header,
}
}
return errors.NewWebError(m.code, m.header, m.buffer.Bytes())
}
func (m *WriteInterceptor) readFrom(original httpsnoop.ReadFromFunc, src io.Reader) (int64, error) {
if !m.headersWritten {
m.rw.WriteHeader(http.StatusOK)
}
if m.capture {
// buffer
return m.buffer.ReadFrom(src)
} else if m.mute {
// blackhole
var dummy [DefaultReadBufferSize]byte
n, err := src.Read(dummy[:])
return int64(n), err
} else {
// real
return original(src)
}
}
func (m *WriteInterceptor) write(original httpsnoop.WriteFunc, b []byte) (int, error) {
if !m.headersWritten {
m.rw.WriteHeader(http.StatusOK)
}
if m.capture {
// buffer
return m.buffer.Write(b)
} else if m.mute {
// fake
return len(b), nil
} else {
// real
return original(b)
}
}
func (m *WriteInterceptor) writeHeader(original httpsnoop.WriteHeaderFunc, code int) {
if m.headersWritten {
log.Fatal(errors.New("%+n(%v): %s", errors.Here(), code, "Invalid Call"))
}
m.headersWritten = true
m.code = code
if code >= http.StatusContinue && code < http.StatusMultipleChoices {
// good, copy headers and write them
if code == http.StatusNoContent {
m.mute = true
}
for k := range m.original {
if w, ok := m.header[k]; !ok {
// delete deleted headers
m.original.Del(k)
} else {
// replace value of those that remain
m.original[k] = w
}
}
for k, v := range m.header {
if _, ok := m.original[k]; !ok {
// add new headers
m.original[k] = v
}
}
original(code)
} else {
// capture writes for later review
m.capture = true
}
}
func (m *WriteInterceptor) hijack(original httpsnoop.HijackFunc) (net.Conn, *bufio.ReadWriter, error) {
if m.headersWritten {
log.Fatal(errors.New("%+n(%s): %s", errors.Here(), "Hijack", "Invalid Call"))
}
m.headersWritten = true
m.mute = false
m.capture = false
return original()
}
func NewWriter(w http.ResponseWriter, method string) *WriteInterceptor {
var mute bool
if method == "HEAD" || method == "OPTIONS" {
mute = true
}
h := w.Header()
m := &WriteInterceptor{
original: h,
header: h.Clone(),
mute: mute,
}
hooks := httpsnoop.Hooks{
Header: func(original httpsnoop.HeaderFunc) httpsnoop.HeaderFunc {
return func() http.Header {
return m.header
}
},
WriteHeader: func(original httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
return func(code int) {
m.writeHeader(original, code)
}
},
Write: func(original httpsnoop.WriteFunc) httpsnoop.WriteFunc {
return func(b []byte) (int, error) {
return m.write(original, b)
}
},
Flush: func(original httpsnoop.FlushFunc) httpsnoop.FlushFunc {
return func() {
err := errors.ErrNotImplemented("%T.%s", m, "Flush")
log.Fatal(err)
}
},
//CloseNotify: func(original httpsnoop.CloseNotifyFunc) httpsnoop.CloseNotifyFunc {
// return original
//},
Hijack: func(original httpsnoop.HijackFunc) httpsnoop.HijackFunc {
return func() (net.Conn, *bufio.ReadWriter, error) {
return m.hijack(original)
}
},
ReadFrom: func(original httpsnoop.ReadFromFunc) httpsnoop.ReadFromFunc {
return func(src io.Reader) (int64, error) {
return m.readFrom(original, src)
}
},
Push: func(original httpsnoop.PushFunc) httpsnoop.PushFunc {
return func(target string, opts *http.PushOptions) error {
err := errors.ErrNotImplemented("%T.%s", m, "Push")
log.Fatal(err)
return err
}
},
}
m.rw = httpsnoop.Wrap(w, hooks)
return m
}