response_writer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2013 Martini Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package macaron
  15. import (
  16. "bufio"
  17. "fmt"
  18. "net"
  19. "net/http"
  20. )
  21. // ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about
  22. // the response. It is recommended that middleware handlers use this construct to wrap a responsewriter
  23. // if the functionality calls for it.
  24. type ResponseWriter interface {
  25. http.ResponseWriter
  26. http.Flusher
  27. // Status returns the status code of the response or 0 if the response has not been written.
  28. Status() int
  29. // Written returns whether or not the ResponseWriter has been written.
  30. Written() bool
  31. // Size returns the size of the response body.
  32. Size() int
  33. // Before allows for a function to be called before the ResponseWriter has been written to. This is
  34. // useful for setting headers or any other operations that must happen before a response has been written.
  35. Before(BeforeFunc)
  36. }
  37. // BeforeFunc is a function that is called before the ResponseWriter has been written to.
  38. type BeforeFunc func(ResponseWriter)
  39. // NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
  40. func NewResponseWriter(rw http.ResponseWriter) ResponseWriter {
  41. return &responseWriter{rw, 0, 0, nil}
  42. }
  43. type responseWriter struct {
  44. http.ResponseWriter
  45. status int
  46. size int
  47. beforeFuncs []BeforeFunc
  48. }
  49. func (rw *responseWriter) WriteHeader(s int) {
  50. rw.callBefore()
  51. rw.ResponseWriter.WriteHeader(s)
  52. rw.status = s
  53. }
  54. func (rw *responseWriter) Write(b []byte) (int, error) {
  55. if !rw.Written() {
  56. // The status will be StatusOK if WriteHeader has not been called yet
  57. rw.WriteHeader(http.StatusOK)
  58. }
  59. size, err := rw.ResponseWriter.Write(b)
  60. rw.size += size
  61. return size, err
  62. }
  63. func (rw *responseWriter) Status() int {
  64. return rw.status
  65. }
  66. func (rw *responseWriter) Size() int {
  67. return rw.size
  68. }
  69. func (rw *responseWriter) Written() bool {
  70. return rw.status != 0
  71. }
  72. func (rw *responseWriter) Before(before BeforeFunc) {
  73. rw.beforeFuncs = append(rw.beforeFuncs, before)
  74. }
  75. func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  76. hijacker, ok := rw.ResponseWriter.(http.Hijacker)
  77. if !ok {
  78. return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
  79. }
  80. return hijacker.Hijack()
  81. }
  82. func (rw *responseWriter) CloseNotify() <-chan bool {
  83. return rw.ResponseWriter.(http.CloseNotifier).CloseNotify()
  84. }
  85. func (rw *responseWriter) callBefore() {
  86. for i := len(rw.beforeFuncs) - 1; i >= 0; i-- {
  87. rw.beforeFuncs[i](rw)
  88. }
  89. }
  90. func (rw *responseWriter) Flush() {
  91. flusher, ok := rw.ResponseWriter.(http.Flusher)
  92. if ok {
  93. flusher.Flush()
  94. }
  95. }