diff options
author | Tw <[email protected]> | 2017-05-02 10:49:50 +0800 |
---|---|---|
committer | Tw <[email protected]> | 2017-05-02 10:53:16 +0800 |
commit | f58653bc131264756e2bd14463b5d58faea9f67c (patch) | |
tree | c5c2be2cc385ecc761fdd6257f31213c63967960 | |
parent | e0ed7093970cf08f63cf04f663c6841a7fdc4499 (diff) | |
download | caddy-f58653bc131264756e2bd14463b5d58faea9f67c.tar.gz caddy-f58653bc131264756e2bd14463b5d58faea9f67c.zip |
internal: inherit original ResponseWriter's interfaces
Signed-off-by: Tw <[email protected]>
-rw-r--r-- | caddyhttp/internalsrv/internal.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/caddyhttp/internalsrv/internal.go b/caddyhttp/internalsrv/internal.go index 089aa0021..d811ff3bf 100644 --- a/caddyhttp/internalsrv/internal.go +++ b/caddyhttp/internalsrv/internal.go @@ -7,6 +7,8 @@ package internalsrv import ( + "bufio" + "net" "net/http" "github.com/mholt/caddy/caddyhttp/httpserver" @@ -94,3 +96,51 @@ func (w internalResponseWriter) Write(b []byte) (int, error) { } return w.ResponseWriter.Write(b) } + +// Hijack implements http.Hijacker. It simply wraps the underlying +// ResponseWriter's Hijack method if there is one, or returns an error. +func (w internalResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + if hj, ok := w.ResponseWriter.(http.Hijacker); ok { + return hj.Hijack() + } + return nil, nil, httpserver.NonHijackerError{Underlying: w.ResponseWriter} +} + +// Flush implements http.Flusher. It simply wraps the underlying +// ResponseWriter's Flush method if there is one, or panics. +func (w internalResponseWriter) Flush() { + if f, ok := w.ResponseWriter.(http.Flusher); ok { + f.Flush() + } else { + panic(httpserver.NonFlusherError{Underlying: w.ResponseWriter}) + } +} + +// CloseNotify implements http.CloseNotifier. +// It just inherits the underlying ResponseWriter's CloseNotify method. +// It panics if the underlying ResponseWriter is not a CloseNotifier. +func (w internalResponseWriter) CloseNotify() <-chan bool { + if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok { + return cn.CloseNotify() + } + panic(httpserver.NonCloseNotifierError{Underlying: w.ResponseWriter}) +} + +// Push implements http.Pusher. +// It just inherits the underlying ResponseWriter's Push method. +// It panics if the underlying ResponseWriter is not a Pusher. +func (w internalResponseWriter) Push(target string, opts *http.PushOptions) error { + if pusher, hasPusher := w.ResponseWriter.(http.Pusher); hasPusher { + return pusher.Push(target, opts) + } + + return httpserver.NonPusherError{Underlying: w.ResponseWriter} +} + +// Interface guards +var ( + _ http.Pusher = internalResponseWriter{} + _ http.Flusher = internalResponseWriter{} + _ http.CloseNotifier = internalResponseWriter{} + _ http.Hijacker = internalResponseWriter{} +) |