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

wrap_writer.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  57. // basicWriter wraps a http.ResponseWriter that implements the minimal
  58. // http.ResponseWriter interface.
  59. type basicWriter struct {
  60. http.ResponseWriter
  61. wroteHeader bool
  62. code int
  63. bytes int
  64. tee io.Writer
  65. }
  66. func (b *basicWriter) WriteHeader(code int) {
  67. if !b.wroteHeader {
  68. b.code = code
  69. b.wroteHeader = true
  70. b.ResponseWriter.WriteHeader(code)
  71. }
  72. }
  73. func (b *basicWriter) Write(buf []byte) (int, error) {
  74. b.maybeWriteHeader()
  75. n, err := b.ResponseWriter.Write(buf)
  76. if b.tee != nil {
  77. _, err2 := b.tee.Write(buf[:n])
  78. // Prefer errors generated by the proxied writer.
  79. if err == nil {
  80. err = err2
  81. }
  82. }
  83. b.bytes += n
  84. return n, err
  85. }
  86. func (b *basicWriter) maybeWriteHeader() {
  87. if !b.wroteHeader {
  88. b.WriteHeader(http.StatusOK)
  89. }
  90. }
  91. func (b *basicWriter) Status() int {
  92. return b.code
  93. }
  94. func (b *basicWriter) BytesWritten() int {
  95. return b.bytes
  96. }
  97. func (b *basicWriter) Tee(w io.Writer) {
  98. b.tee = w
  99. }
  100. func (b *basicWriter) Unwrap() http.ResponseWriter {
  101. return b.ResponseWriter
  102. }
  103. // flushWriter ...
  104. type flushWriter struct {
  105. basicWriter
  106. }
  107. func (f *flushWriter) Flush() {
  108. f.wroteHeader = true
  109. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  110. fl.Flush()
  111. }
  112. var _ http.Flusher = &flushWriter{}
  113. // hijackWriter ...
  114. type hijackWriter struct {
  115. basicWriter
  116. }
  117. func (f *hijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  118. hj := f.basicWriter.ResponseWriter.(http.Hijacker)
  119. return hj.Hijack()
  120. }
  121. var _ http.Hijacker = &hijackWriter{}
  122. // flushHijackWriter ...
  123. type flushHijackWriter struct {
  124. basicWriter
  125. }
  126. func (f *flushHijackWriter) Flush() {
  127. f.wroteHeader = true
  128. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  129. fl.Flush()
  130. }
  131. func (f *flushHijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  132. hj := f.basicWriter.ResponseWriter.(http.Hijacker)
  133. return hj.Hijack()
  134. }
  135. var _ http.Flusher = &flushHijackWriter{}
  136. var _ http.Hijacker = &flushHijackWriter{}
  137. // httpFancyWriter is a HTTP writer that additionally satisfies
  138. // http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
  139. // of wrapping the http.ResponseWriter that package http gives you, in order to
  140. // make the proxied object support the full method set of the proxied object.
  141. type httpFancyWriter struct {
  142. basicWriter
  143. }
  144. func (f *httpFancyWriter) Flush() {
  145. f.wroteHeader = true
  146. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  147. fl.Flush()
  148. }
  149. func (f *httpFancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  150. hj := f.basicWriter.ResponseWriter.(http.Hijacker)
  151. return hj.Hijack()
  152. }
  153. func (f *http2FancyWriter) Push(target string, opts *http.PushOptions) error {
  154. return f.basicWriter.ResponseWriter.(http.Pusher).Push(target, opts)
  155. }
  156. func (f *httpFancyWriter) ReadFrom(r io.Reader) (int64, error) {
  157. if f.basicWriter.tee != nil {
  158. n, err := io.Copy(&f.basicWriter, r)
  159. f.basicWriter.bytes += int(n)
  160. return n, err
  161. }
  162. rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
  163. f.basicWriter.maybeWriteHeader()
  164. n, err := rf.ReadFrom(r)
  165. f.basicWriter.bytes += int(n)
  166. return n, err
  167. }
  168. var _ http.Flusher = &httpFancyWriter{}
  169. var _ http.Hijacker = &httpFancyWriter{}
  170. var _ http.Pusher = &http2FancyWriter{}
  171. var _ io.ReaderFrom = &httpFancyWriter{}
  172. // http2FancyWriter is a HTTP2 writer that additionally satisfies
  173. // http.Flusher, and io.ReaderFrom. It exists for the common case
  174. // of wrapping the http.ResponseWriter that package http gives you, in order to
  175. // make the proxied object support the full method set of the proxied object.
  176. type http2FancyWriter struct {
  177. basicWriter
  178. }
  179. func (f *http2FancyWriter) Flush() {
  180. f.wroteHeader = true
  181. fl := f.basicWriter.ResponseWriter.(http.Flusher)
  182. fl.Flush()
  183. }
  184. var _ http.Flusher = &http2FancyWriter{}