@@ -123,7 +123,7 @@ func Middleware(options ...Options) macaron.Handler {
123
123
// OK we should proxy the response writer
124
124
// We are still not necessarily going to compress...
125
125
proxyWriter := & ProxyResponseWriter {
126
- ResponseWriter : ctx .Resp ,
126
+ internal : ctx .Resp ,
127
127
}
128
128
defer proxyWriter .Close ()
129
129
@@ -137,19 +137,52 @@ func Middleware(options ...Options) macaron.Handler {
137
137
}
138
138
139
139
ctx .Next ()
140
+ ctx .Resp = proxyWriter .internal
140
141
}
141
142
}
142
143
143
144
// ProxyResponseWriter is a wrapped macaron ResponseWriter that may compress its contents
144
145
type ProxyResponseWriter struct {
145
- writer io.WriteCloser
146
- macaron.ResponseWriter
147
- stopped bool
146
+ writer io.WriteCloser
147
+ internal macaron.ResponseWriter
148
+ stopped bool
148
149
149
150
code int
150
151
buf []byte
151
152
}
152
153
154
+ // Header returns the header map
155
+ func (proxy * ProxyResponseWriter ) Header () http.Header {
156
+ return proxy .internal .Header ()
157
+ }
158
+
159
+ // Status returns the status code of the response or 0 if the response has not been written.
160
+ func (proxy * ProxyResponseWriter ) Status () int {
161
+ if proxy .code != 0 {
162
+ return proxy .code
163
+ }
164
+ return proxy .internal .Status ()
165
+ }
166
+
167
+ // Written returns whether or not the ResponseWriter has been written.
168
+ func (proxy * ProxyResponseWriter ) Written () bool {
169
+ if proxy .code != 0 {
170
+ return true
171
+ }
172
+ return proxy .internal .Written ()
173
+ }
174
+
175
+ // Size returns the size of the response body.
176
+ func (proxy * ProxyResponseWriter ) Size () int {
177
+ return proxy .internal .Size ()
178
+ }
179
+
180
+ // Before allows for a function to be called before the ResponseWriter has been written to. This is
181
+ // useful for setting headers or any other operations that must happen before a response has been written.
182
+ func (proxy * ProxyResponseWriter ) Before (before macaron.BeforeFunc ) {
183
+ proxy .internal .Before (before )
184
+ }
185
+
153
186
// Write appends data to the proxied gzip writer.
154
187
func (proxy * ProxyResponseWriter ) Write (b []byte ) (int , error ) {
155
188
// if writer is initialized, use the writer
@@ -210,7 +243,7 @@ func (proxy *ProxyResponseWriter) startGzip() error {
210
243
211
244
// Write the header to gzip response.
212
245
if proxy .code != 0 {
213
- proxy .ResponseWriter .WriteHeader (proxy .code )
246
+ proxy .internal .WriteHeader (proxy .code )
214
247
// Ensure that no other WriteHeader's happen
215
248
proxy .code = 0
216
249
}
@@ -220,7 +253,7 @@ func (proxy *ProxyResponseWriter) startGzip() error {
220
253
// write the gzip header even if nothing was ever written.
221
254
if len (proxy .buf ) > 0 {
222
255
// Initialize the GZIP response.
223
- proxy .writer = writerPool .Get (proxy .ResponseWriter )
256
+ proxy .writer = writerPool .Get (proxy .internal )
224
257
225
258
return proxy .writeBuf ()
226
259
}
@@ -229,11 +262,11 @@ func (proxy *ProxyResponseWriter) startGzip() error {
229
262
230
263
func (proxy * ProxyResponseWriter ) startPlain () error {
231
264
if proxy .code != 0 {
232
- proxy .ResponseWriter .WriteHeader (proxy .code )
265
+ proxy .internal .WriteHeader (proxy .code )
233
266
proxy .code = 0
234
267
}
235
268
proxy .stopped = true
236
- proxy .writer = noopCloser {proxy .ResponseWriter }
269
+ proxy .writer = noopCloser {proxy .internal }
237
270
return proxy .writeBuf ()
238
271
}
239
272
@@ -295,13 +328,13 @@ func (proxy *ProxyResponseWriter) Flush() {
295
328
gw .Flush ()
296
329
}
297
330
298
- proxy .ResponseWriter .Flush ()
331
+ proxy .internal .Flush ()
299
332
}
300
333
301
334
// Hijack implements http.Hijacker. If the underlying ResponseWriter is a
302
335
// Hijacker, its Hijack method is returned. Otherwise an error is returned.
303
336
func (proxy * ProxyResponseWriter ) Hijack () (net.Conn , * bufio.ReadWriter , error ) {
304
- hijacker , ok := proxy .ResponseWriter .(http.Hijacker )
337
+ hijacker , ok := proxy .internal .(http.Hijacker )
305
338
if ! ok {
306
339
return nil , nil , fmt .Errorf ("the ResponseWriter doesn't support the Hijacker interface" )
307
340
}
0 commit comments