response_writer.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package martini
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. )
  8. // ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about
  9. // the response. It is recommended that middleware handlers use this construct to wrap a responsewriter
  10. // if the functionality calls for it.
  11. type ResponseWriter interface {
  12. http.ResponseWriter
  13. http.Flusher
  14. http.Hijacker
  15. // Status returns the status code of the response or 0 if the response has not been written.
  16. Status() int
  17. // Written returns whether or not the ResponseWriter has been written.
  18. Written() bool
  19. // Size returns the size of the response body.
  20. Size() int
  21. // Before allows for a function to be called before the ResponseWriter has been written to. This is
  22. // useful for setting headers or any other operations that must happen before a response has been written.
  23. Before(BeforeFunc)
  24. }
  25. // BeforeFunc is a function that is called before the ResponseWriter has been written to.
  26. type BeforeFunc func(ResponseWriter)
  27. // NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
  28. func NewResponseWriter(rw http.ResponseWriter) ResponseWriter {
  29. return &responseWriter{rw, 0, 0, nil}
  30. }
  31. type responseWriter struct {
  32. http.ResponseWriter
  33. status int
  34. size int
  35. beforeFuncs []BeforeFunc
  36. }
  37. func (rw *responseWriter) WriteHeader(s int) {
  38. rw.callBefore()
  39. rw.ResponseWriter.WriteHeader(s)
  40. rw.status = s
  41. }
  42. func (rw *responseWriter) Write(b []byte) (int, error) {
  43. if !rw.Written() {
  44. // The status will be StatusOK if WriteHeader has not been called yet
  45. rw.WriteHeader(http.StatusOK)
  46. }
  47. size, err := rw.ResponseWriter.Write(b)
  48. rw.size += size
  49. return size, err
  50. }
  51. func (rw *responseWriter) Status() int {
  52. return rw.status
  53. }
  54. func (rw *responseWriter) Size() int {
  55. return rw.size
  56. }
  57. func (rw *responseWriter) Written() bool {
  58. return rw.status != 0
  59. }
  60. func (rw *responseWriter) Before(before BeforeFunc) {
  61. rw.beforeFuncs = append(rw.beforeFuncs, before)
  62. }
  63. func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  64. hijacker, ok := rw.ResponseWriter.(http.Hijacker)
  65. if !ok {
  66. return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
  67. }
  68. return hijacker.Hijack()
  69. }
  70. func (rw *responseWriter) CloseNotify() <-chan bool {
  71. return rw.ResponseWriter.(http.CloseNotifier).CloseNotify()
  72. }
  73. func (rw *responseWriter) callBefore() {
  74. for i := len(rw.beforeFuncs) - 1; i >= 0; i-- {
  75. rw.beforeFuncs[i](rw)
  76. }
  77. }
  78. func (rw *responseWriter) Flush() {
  79. flusher, ok := rw.ResponseWriter.(http.Flusher)
  80. if ok {
  81. flusher.Flush()
  82. }
  83. }