From 351dfac653aa2beb01d4c0ecc28aa914ad0b9902 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 21 Dec 2015 22:51:18 +0800 Subject: [PATCH] context.Response should implement Hijack/Flush/CloseNotify --- context/context.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/context/context.go b/context/context.go index f3dffdbd..db790ff2 100644 --- a/context/context.go +++ b/context/context.go @@ -23,10 +23,13 @@ package context import ( + "bufio" "crypto/hmac" "crypto/sha1" "encoding/base64" + "errors" "fmt" + "net" "net/http" "strconv" "strings" @@ -188,3 +191,27 @@ func (w *Response) WriteHeader(code int) { w.Started = true w.ResponseWriter.WriteHeader(code) } + +// Hijack hijacker for http +func (w *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) { + hj, ok := w.ResponseWriter.(http.Hijacker) + if !ok { + return nil, nil, errors.New("webserver doesn't support hijacking") + } + return hj.Hijack() +} + +// Flush http.Flusher +func (w *Response) Flush() { + if f, ok := w.ResponseWriter.(http.Flusher); ok { + f.Flush() + } +} + +// CloseNotify http.CloseNotifier +func (w *Response) CloseNotify() <-chan bool { + if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok { + return cn.CloseNotify() + } + return nil +}