mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
caddyhttp: Impl ResponseWriter.Unwrap()
, prep for Go 1.20's ResponseController
(#5509)
* feat: add support for ResponseWriter.Unwrap() * cherry-pick Francis' code
This commit is contained in:
parent
2b04e09fa7
commit
1c9ea0113d
5 changed files with 30 additions and 0 deletions
|
@ -66,3 +66,9 @@ func (d *delegator) WriteHeader(code int) {
|
||||||
d.status = code
|
d.status = code
|
||||||
d.ResponseWriter.WriteHeader(code)
|
d.ResponseWriter.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap returns the underlying ResponseWriter, necessary for
|
||||||
|
// http.ResponseController to work correctly.
|
||||||
|
func (d *delegator) Unwrap() http.ResponseWriter {
|
||||||
|
return d.ResponseWriter
|
||||||
|
}
|
||||||
|
|
|
@ -299,6 +299,11 @@ func (rw *responseWriter) Close() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap returns the underlying ResponseWriter.
|
||||||
|
func (rw *responseWriter) Unwrap() http.ResponseWriter {
|
||||||
|
return rw.HTTPInterfaces
|
||||||
|
}
|
||||||
|
|
||||||
// init should be called before we write a response, if rw.buf has contents.
|
// init should be called before we write a response, if rw.buf has contents.
|
||||||
func (rw *responseWriter) init() {
|
func (rw *responseWriter) init() {
|
||||||
if rw.Header().Get("Content-Encoding") == "" && isEncodeAllowed(rw.Header()) &&
|
if rw.Header().Get("Content-Encoding") == "" && isEncodeAllowed(rw.Header()) &&
|
||||||
|
|
|
@ -636,6 +636,12 @@ func (wr statusOverrideResponseWriter) WriteHeader(int) {
|
||||||
wr.ResponseWriter.WriteHeader(wr.code)
|
wr.ResponseWriter.WriteHeader(wr.code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap returns the underlying ResponseWriter, necessary for
|
||||||
|
// http.ResponseController to work correctly.
|
||||||
|
func (wr statusOverrideResponseWriter) Unwrap() http.ResponseWriter {
|
||||||
|
return wr.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
// osFS is a simple fs.FS implementation that uses the local
|
// osFS is a simple fs.FS implementation that uses the local
|
||||||
// file system. (We do not use os.DirFS because we do our own
|
// file system. (We do not use os.DirFS because we do our own
|
||||||
// rooting or path prefixing without being constrained to a single
|
// rooting or path prefixing without being constrained to a single
|
||||||
|
|
|
@ -72,6 +72,11 @@ func (rww *ResponseWriterWrapper) ReadFrom(r io.Reader) (n int64, err error) {
|
||||||
return io.Copy(rww.ResponseWriter, r)
|
return io.Copy(rww.ResponseWriter, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap returns the underlying ResponseWriter.
|
||||||
|
func (rww *ResponseWriterWrapper) Unwrap() http.ResponseWriter {
|
||||||
|
return rww.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
// HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support.
|
// HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support.
|
||||||
type HTTPInterfaces interface {
|
type HTTPInterfaces interface {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
|
|
|
@ -95,6 +95,14 @@ func TestResponseWriterWrapperReadFrom(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResponseWriterWrapperUnwrap(t *testing.T) {
|
||||||
|
w := &ResponseWriterWrapper{&baseRespWriter{}}
|
||||||
|
|
||||||
|
if _, ok := w.Unwrap().(*baseRespWriter); !ok {
|
||||||
|
t.Errorf("Unwrap() doesn't return the underlying ResponseWriter")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestResponseRecorderReadFrom(t *testing.T) {
|
func TestResponseRecorderReadFrom(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
responseWriter responseWriterSpy
|
responseWriter responseWriterSpy
|
||||||
|
|
Loading…
Reference in a new issue