You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

response_writer.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "errors"
  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. http.Pusher
  28. // Status returns the status code of the response or 0 if the response has not been written.
  29. Status() int
  30. // Written returns whether or not the ResponseWriter has been written.
  31. Written() bool
  32. // Size returns the size of the response body.
  33. Size() int
  34. // Before allows for a function to be called before the ResponseWriter has been written to. This is
  35. // useful for setting headers or any other operations that must happen before a response has been written.
  36. Before(BeforeFunc)
  37. }
  38. // BeforeFunc is a function that is called before the ResponseWriter has been written to.
  39. type BeforeFunc func(ResponseWriter)
  40. // NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
  41. func NewResponseWriter(method string, rw http.ResponseWriter) ResponseWriter {
  42. return &responseWriter{method, rw, 0, 0, nil}
  43. }
  44. type responseWriter struct {
  45. method string
  46. http.ResponseWriter
  47. status int
  48. size int
  49. beforeFuncs []BeforeFunc
  50. }
  51. func (rw *responseWriter) WriteHeader(s int) {
  52. rw.callBefore()
  53. rw.ResponseWriter.WriteHeader(s)
  54. rw.status = s
  55. }
  56. func (rw *responseWriter) Write(b []byte) (size int, err error) {
  57. if !rw.Written() {
  58. // The status will be StatusOK if WriteHeader has not been called yet
  59. rw.WriteHeader(http.StatusOK)
  60. }
  61. if rw.method != "HEAD" {
  62. size, err = rw.ResponseWriter.Write(b)
  63. rw.size += size
  64. }
  65. return size, err
  66. }
  67. func (rw *responseWriter) Status() int {
  68. return rw.status
  69. }
  70. func (rw *responseWriter) Size() int {
  71. return rw.size
  72. }
  73. func (rw *responseWriter) Written() bool {
  74. return rw.status != 0
  75. }
  76. func (rw *responseWriter) Before(before BeforeFunc) {
  77. rw.beforeFuncs = append(rw.beforeFuncs, before)
  78. }
  79. func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  80. hijacker, ok := rw.ResponseWriter.(http.Hijacker)
  81. if !ok {
  82. return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
  83. }
  84. return hijacker.Hijack()
  85. }
  86. //nolint
  87. func (rw *responseWriter) CloseNotify() <-chan bool {
  88. return rw.ResponseWriter.(http.CloseNotifier).CloseNotify()
  89. }
  90. func (rw *responseWriter) callBefore() {
  91. for i := len(rw.beforeFuncs) - 1; i >= 0; i-- {
  92. rw.beforeFuncs[i](rw)
  93. }
  94. }
  95. func (rw *responseWriter) Flush() {
  96. flusher, ok := rw.ResponseWriter.(http.Flusher)
  97. if ok {
  98. flusher.Flush()
  99. }
  100. }
  101. func (rw *responseWriter) Push(target string, opts *http.PushOptions) error {
  102. pusher, ok := rw.ResponseWriter.(http.Pusher)
  103. if !ok {
  104. return errors.New("the ResponseWriter doesn't support the Pusher interface")
  105. }
  106. return pusher.Push(target, opts)
  107. }