Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 04:15 UTC
caddyhttp: Impl `ResponseWriter.Unwrap()`, prep for Go 1.20's `ResponseController` (#5509)
* feat: add support for ResponseWriter.Unwrap() * cherry-pick Francis' code
Kévin Dunglas committed
Apr 27, 2023 at 01:44 UTC
1c9ea0113d007d57bb8f793ebe7a64a3dcad7bc7
5 files changed
+30
metrics.go
+6
@@ -66,3 +66,9 @@ func (d *delegator) WriteHeader(code int) {
66
d.status = code
67
d.ResponseWriter.WriteHeader(code)
68
}
69
+
70
+// Unwrap returns the underlying ResponseWriter, necessary for
71
+// http.ResponseController to work correctly.
72
+func (d *delegator) Unwrap() http.ResponseWriter {
73
+ return d.ResponseWriter
74
+}
modules/caddyhttp/encode/encode.go
+5
@@ -299,6 +299,11 @@ func (rw *responseWriter) Close() error {
299
return err
300
}
301
302
+// Unwrap returns the underlying ResponseWriter.
303
+func (rw *responseWriter) Unwrap() http.ResponseWriter {
304
+ return rw.HTTPInterfaces
305
+}
306
+
307
// init should be called before we write a response, if rw.buf has contents.
308
func (rw *responseWriter) init() {
309
if rw.Header().Get("Content-Encoding") == "" && isEncodeAllowed(rw.Header()) &&
modules/caddyhttp/fileserver/staticfiles.go
+6
@@ -636,6 +636,12 @@ func (wr statusOverrideResponseWriter) WriteHeader(int) {
636
wr.ResponseWriter.WriteHeader(wr.code)
637
}
638
639
+// Unwrap returns the underlying ResponseWriter, necessary for
640
+// http.ResponseController to work correctly.
641
+func (wr statusOverrideResponseWriter) Unwrap() http.ResponseWriter {
642
+ return wr.ResponseWriter
643
+}
644
+
645
// osFS is a simple fs.FS implementation that uses the local
646
// file system. (We do not use os.DirFS because we do our own
647
// rooting or path prefixing without being constrained to a single
modules/caddyhttp/responsewriter.go
+5
@@ -72,6 +72,11 @@ func (rww *ResponseWriterWrapper) ReadFrom(r io.Reader) (n int64, err error) {
72
return io.Copy(rww.ResponseWriter, r)
73
}
74
75
+// Unwrap returns the underlying ResponseWriter.
76
+func (rww *ResponseWriterWrapper) Unwrap() http.ResponseWriter {
77
+ return rww.ResponseWriter
78
+}
79
+
80
// HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support.
81
type HTTPInterfaces interface {
82
http.ResponseWriter
modules/caddyhttp/responsewriter_test.go
+8
@@ -95,6 +95,14 @@ func TestResponseWriterWrapperReadFrom(t *testing.T) {
95
}
96
}
97
98
+func TestResponseWriterWrapperUnwrap(t *testing.T) {
99
+ w := &ResponseWriterWrapper{&baseRespWriter{}}
100
+
101
+ if _, ok := w.Unwrap().(*baseRespWriter); !ok {
102
+ t.Errorf("Unwrap() doesn't return the underlying ResponseWriter")
103
+ }
104
+}
105
+
106
func TestResponseRecorderReadFrom(t *testing.T) {
107
tests := map[string]struct {
108
responseWriter responseWriterSpy