github.com/cheikhshift/buffalo@v0.9.5/response.go (about) 1 package buffalo 2 3 import ( 4 "bufio" 5 "encoding/binary" 6 "net" 7 "net/http" 8 9 "github.com/pkg/errors" 10 ) 11 12 // Response implements the http.ResponseWriter interface and allows 13 // for the capture of the response status and size to be used for things 14 // like logging requests. 15 type Response struct { 16 Status int 17 Size int 18 http.ResponseWriter 19 } 20 21 // WriteHeader sets the status code for a response 22 func (w *Response) WriteHeader(i int) { 23 w.Status = i 24 w.ResponseWriter.WriteHeader(i) 25 } 26 27 // Write the body of the response 28 func (w *Response) Write(b []byte) (int, error) { 29 w.Size = binary.Size(b) 30 return w.ResponseWriter.Write(b) 31 } 32 33 // Hijack implements the http.Hijacker interface to allow for things like websockets. 34 func (w *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) { 35 if hj, ok := w.ResponseWriter.(http.Hijacker); ok { 36 return hj.Hijack() 37 } 38 return nil, nil, errors.WithStack(errors.New("does not implement http.Hijack")) 39 } 40 41 // Flush the response 42 func (w *Response) Flush() { 43 if f, ok := w.ResponseWriter.(http.Flusher); ok { 44 f.Flush() 45 } 46 } 47 48 // CloseNotify implements the http.CloseNotifier interface 49 func (w *Response) CloseNotify() <-chan bool { 50 if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok { 51 return cn.CloseNotify() 52 } 53 return nil 54 }