response_writer.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "bufio"
  7. "io"
  8. "net"
  9. "net/http"
  10. )
  11. const (
  12. noWritten = -1
  13. defaultStatus = 200
  14. )
  15. type (
  16. ResponseWriter interface {
  17. http.ResponseWriter
  18. http.Hijacker
  19. http.Flusher
  20. http.CloseNotifier
  21. // Returns the HTTP response status code of the current request.
  22. Status() int
  23. // Returns the number of bytes already written into the response http body.
  24. // See Written()
  25. Size() int
  26. // Writes the string into the response body.
  27. WriteString(string) (int, error)
  28. // Returns true if the response body was already written.
  29. Written() bool
  30. // Forces to write the http header (status code + headers).
  31. WriteHeaderNow()
  32. }
  33. responseWriter struct {
  34. http.ResponseWriter
  35. size int
  36. status int
  37. }
  38. )
  39. var _ ResponseWriter = &responseWriter{}
  40. func (w *responseWriter) reset(writer http.ResponseWriter) {
  41. w.ResponseWriter = writer
  42. w.size = noWritten
  43. w.status = defaultStatus
  44. }
  45. func (w *responseWriter) WriteHeader(code int) {
  46. if code > 0 && w.status != code {
  47. if w.Written() {
  48. debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)
  49. }
  50. w.status = code
  51. }
  52. }
  53. func (w *responseWriter) WriteHeaderNow() {
  54. if !w.Written() {
  55. w.size = 0
  56. w.ResponseWriter.WriteHeader(w.status)
  57. }
  58. }
  59. func (w *responseWriter) Write(data []byte) (n int, err error) {
  60. w.WriteHeaderNow()
  61. n, err = w.ResponseWriter.Write(data)
  62. w.size += n
  63. return
  64. }
  65. func (w *responseWriter) WriteString(s string) (n int, err error) {
  66. w.WriteHeaderNow()
  67. n, err = io.WriteString(w.ResponseWriter, s)
  68. w.size += n
  69. return
  70. }
  71. func (w *responseWriter) Status() int {
  72. return w.status
  73. }
  74. func (w *responseWriter) Size() int {
  75. return w.size
  76. }
  77. func (w *responseWriter) Written() bool {
  78. return w.size != noWritten
  79. }
  80. // Implements the http.Hijacker interface
  81. func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  82. if w.size < 0 {
  83. w.size = 0
  84. }
  85. return w.ResponseWriter.(http.Hijacker).Hijack()
  86. }
  87. // Implements the http.CloseNotify interface
  88. func (w *responseWriter) CloseNotify() <-chan bool {
  89. return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
  90. }
  91. // Implements the http.Flush interface
  92. func (w *responseWriter) Flush() {
  93. w.ResponseWriter.(http.Flusher).Flush()
  94. }