wrap_writer.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package middleware
  2. // The original work was derived from Goji's middleware, source:
  3. // https://github.com/zenazn/goji/tree/master/web/middleware
  4. import (
  5. "bufio"
  6. "io"
  7. "net"
  8. "net/http"
  9. )
  10. // NewWrapResponseWriter wraps an http.ResponseWriter, returning a proxy that allows you to
  11. // hook into various parts of the response process.
  12. func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter {
  13. _, fl := w.(http.Flusher)
  14. bw := basicWriter{ResponseWriter: w}
  15. if protoMajor == 2 {
  16. _, ps := w.(http.Pusher)
  17. if fl && ps {
  18. return &http2FancyWriter{bw}
  19. }
  20. } else {
  21. _, hj := w.(http.Hijacker)
  22. _, rf := w.(io.ReaderFrom)
  23. if fl && hj && rf {
  24. return &httpFancyWriter{bw}
  25. }
  26. if fl && hj {
  27. return &flushHijackWriter{bw}
  28. }
  29. if hj {
  30. return &hijackWriter{bw}
  31. }
  32. }
  33. if fl {
  34. return &flushWriter{bw}
  35. }
  36. return &bw
  37. }
  38. // WrapResponseWriter is a proxy around an http.ResponseWriter that allows you to hook
  39. // into various parts of the response process.
  40. type WrapResponseWriter interface {
  41. http.ResponseWriter
  42. // Status returns the HTTP status of the request, or 0 if one has not
  43. // yet been sent.
  44. Status() int
  45. // BytesWritten returns the total number of bytes sent to the client.
  46. BytesWritten() int
  47. // Tee causes the response body to be written to the given io.Writer in
  48. // addition to proxying the writes through. Only one io.Writer can be
  49. // tee'd to at once: setting a second one will overwrite the first.
  50. // Writes will be sent to the proxy before being written to this
  51. // io.Writer. It is illegal for the tee'd writer to be modified
  52. // concurrently with writes.
  53. Tee(io.Writer)
  54. // Unwrap returns the original proxied target.
  55. Unwrap() http.ResponseWriter
  56. // Discard causes all writes to the original ResponseWriter be discarded,
  57. // instead writing only to the tee'd writer if it's set.
  58. // The caller is responsible for calling WriteHeader and Write on the
  59. // original ResponseWriter once the processing is done.
  60. Discard()
  61. }
  62. // basicWriter wraps a http.ResponseWriter that implements the minimal
  63. // http.ResponseWriter interface.
  64. type basicWriter struct {
  65. http.ResponseWriter
  66. tee io.Writer
  67. code int
  68. bytes int
  69. wroteHeader bool
  70. discard bool
  71. }
  72. func (b *basicWriter) WriteHeader(code int) {
  73. if code >= 100 && code <= 199 && code != http.StatusSwitchingProtocols {
  74. if !b.discard {
  75. b.ResponseWriter.WriteHeader(code)
  76. }
  77. } else if !b.wroteHeader {
  78. b.code = code
  79. b.wroteHeader = true
  80. if !b.discard {
  81. b.ResponseWriter.WriteHeader(code)
  82. }
  83. }
  84. }
  85. func (b *basicWriter) Write(buf []byte) (n int, err error) {
  86. b.maybeWriteHeader()
  87. if !b.discard {
  88. n, err = b.ResponseWriter.Write(buf)
  89. if b.tee != nil {
  90. _, err2 := b.tee.Write(buf[:n])
  91. // Prefer errors generated by the proxied writer.
  92. if err == nil {
  93. err = err2
  94. }
  95. }
  96. } else if b.tee != nil {
  97. n, err = b.tee.Write(buf)
  98. } else {
  99. n, err = io.Discard.Write(buf)
  100. }
  101. b.bytes += n
  102. return n, err
  103. }
  104. func (b *basicWriter) maybeWriteHeader() {
  105. if !b.wroteHeader {
  106. b.WriteHeader(http.StatusOK)
  107. }
  108. }
  109. func (b *basicWriter) Status() int {
  110. return b.code
  111. }
  112. func (b *basicWriter) BytesWritten() int {
  113. return b.bytes
  114. }
  115. func (b *basicWriter) Tee(w io.Writer) {
  116. b.tee = w
  117. }
  118. func (b *basicWriter) Unwrap() http.ResponseWriter {
  119. return b.ResponseWriter
  120. }
  121. func (b *basicWriter) Discard() {
  122. b.discard = true
  123. }
  124. // flushWriter ...
  125. type flushWriter struct {
  126. basicWriter
  127. }
  128. func (f *flushWriter) Flush() {
  129. f.wroteHeader = true
  130. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  131. fl.Flush()
  132. }
  133. var _ http.Flusher = &flushWriter{}
  134. // hijackWriter ...
  135. type hijackWriter struct {
  136. basicWriter
  137. }
  138. func (f *hijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  139. hj := f.basicWriter.ResponseWriter.(http.Hijacker)
  140. return hj.Hijack()
  141. }
  142. var _ http.Hijacker = &hijackWriter{}
  143. // flushHijackWriter ...
  144. type flushHijackWriter struct {
  145. basicWriter
  146. }
  147. func (f *flushHijackWriter) Flush() {
  148. f.wroteHeader = true
  149. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  150. fl.Flush()
  151. }
  152. func (f *flushHijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  153. hj := f.basicWriter.ResponseWriter.(http.Hijacker)
  154. return hj.Hijack()
  155. }
  156. var _ http.Flusher = &flushHijackWriter{}
  157. var _ http.Hijacker = &flushHijackWriter{}
  158. // httpFancyWriter is a HTTP writer that additionally satisfies
  159. // http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
  160. // of wrapping the http.ResponseWriter that package http gives you, in order to
  161. // make the proxied object support the full method set of the proxied object.
  162. type httpFancyWriter struct {
  163. basicWriter
  164. }
  165. func (f *httpFancyWriter) Flush() {
  166. f.wroteHeader = true
  167. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  168. fl.Flush()
  169. }
  170. func (f *httpFancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  171. hj := f.basicWriter.ResponseWriter.(http.Hijacker)
  172. return hj.Hijack()
  173. }
  174. func (f *http2FancyWriter) Push(target string, opts *http.PushOptions) error {
  175. return f.basicWriter.ResponseWriter.(http.Pusher).Push(target, opts)
  176. }
  177. func (f *httpFancyWriter) ReadFrom(r io.Reader) (int64, error) {
  178. if f.basicWriter.tee != nil {
  179. n, err := io.Copy(&f.basicWriter, r)
  180. f.basicWriter.bytes += int(n)
  181. return n, err
  182. }
  183. rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
  184. f.basicWriter.maybeWriteHeader()
  185. n, err := rf.ReadFrom(r)
  186. f.basicWriter.bytes += int(n)
  187. return n, err
  188. }
  189. var _ http.Flusher = &httpFancyWriter{}
  190. var _ http.Hijacker = &httpFancyWriter{}
  191. var _ http.Pusher = &http2FancyWriter{}
  192. var _ io.ReaderFrom = &httpFancyWriter{}
  193. // http2FancyWriter is a HTTP2 writer that additionally satisfies
  194. // http.Flusher, and io.ReaderFrom. It exists for the common case
  195. // of wrapping the http.ResponseWriter that package http gives you, in order to
  196. // make the proxied object support the full method set of the proxied object.
  197. type http2FancyWriter struct {
  198. basicWriter
  199. }
  200. func (f *http2FancyWriter) Flush() {
  201. f.wroteHeader = true
  202. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  203. fl.Flush()
  204. }
  205. var _ http.Flusher = &http2FancyWriter{}