compress.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package restful
  2. // Copyright 2013 Ernest Micklei. All rights reserved.
  3. // Use of this source code is governed by a license
  4. // that can be found in the LICENSE file.
  5. import (
  6. "bufio"
  7. "compress/gzip"
  8. "compress/zlib"
  9. "errors"
  10. "io"
  11. "net"
  12. "net/http"
  13. "strings"
  14. )
  15. // OBSOLETE : use restful.DefaultContainer.EnableContentEncoding(true) to change this setting.
  16. var EnableContentEncoding = false
  17. // CompressingResponseWriter is a http.ResponseWriter that can perform content encoding (gzip and zlib)
  18. type CompressingResponseWriter struct {
  19. writer http.ResponseWriter
  20. compressor io.WriteCloser
  21. encoding string
  22. }
  23. // Header is part of http.ResponseWriter interface
  24. func (c *CompressingResponseWriter) Header() http.Header {
  25. return c.writer.Header()
  26. }
  27. // WriteHeader is part of http.ResponseWriter interface
  28. func (c *CompressingResponseWriter) WriteHeader(status int) {
  29. c.writer.WriteHeader(status)
  30. }
  31. // Write is part of http.ResponseWriter interface
  32. // It is passed through the compressor
  33. func (c *CompressingResponseWriter) Write(bytes []byte) (int, error) {
  34. if c.isCompressorClosed() {
  35. return -1, errors.New("Compressing error: tried to write data using closed compressor")
  36. }
  37. return c.compressor.Write(bytes)
  38. }
  39. // CloseNotify is part of http.CloseNotifier interface
  40. func (c *CompressingResponseWriter) CloseNotify() <-chan bool {
  41. return c.writer.(http.CloseNotifier).CloseNotify()
  42. }
  43. // Close the underlying compressor
  44. func (c *CompressingResponseWriter) Close() error {
  45. if c.isCompressorClosed() {
  46. return errors.New("Compressing error: tried to close already closed compressor")
  47. }
  48. c.compressor.Close()
  49. if ENCODING_GZIP == c.encoding {
  50. currentCompressorProvider.ReleaseGzipWriter(c.compressor.(*gzip.Writer))
  51. }
  52. if ENCODING_DEFLATE == c.encoding {
  53. currentCompressorProvider.ReleaseZlibWriter(c.compressor.(*zlib.Writer))
  54. }
  55. // gc hint needed?
  56. c.compressor = nil
  57. return nil
  58. }
  59. func (c *CompressingResponseWriter) isCompressorClosed() bool {
  60. return nil == c.compressor
  61. }
  62. // Hijack implements the Hijacker interface
  63. // This is especially useful when combining Container.EnabledContentEncoding
  64. // in combination with websockets (for instance gorilla/websocket)
  65. func (c *CompressingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  66. hijacker, ok := c.writer.(http.Hijacker)
  67. if !ok {
  68. return nil, nil, errors.New("ResponseWriter doesn't support Hijacker interface")
  69. }
  70. return hijacker.Hijack()
  71. }
  72. // WantsCompressedResponse reads the Accept-Encoding header to see if and which encoding is requested.
  73. // It also inspects the httpWriter whether its content-encoding is already set (non-empty).
  74. func wantsCompressedResponse(httpRequest *http.Request, httpWriter http.ResponseWriter) (bool, string) {
  75. if contentEncoding := httpWriter.Header().Get(HEADER_ContentEncoding); contentEncoding != "" {
  76. return false, ""
  77. }
  78. header := httpRequest.Header.Get(HEADER_AcceptEncoding)
  79. gi := strings.Index(header, ENCODING_GZIP)
  80. zi := strings.Index(header, ENCODING_DEFLATE)
  81. // use in order of appearance
  82. if gi == -1 {
  83. return zi != -1, ENCODING_DEFLATE
  84. } else if zi == -1 {
  85. return gi != -1, ENCODING_GZIP
  86. } else {
  87. if gi < zi {
  88. return true, ENCODING_GZIP
  89. }
  90. return true, ENCODING_DEFLATE
  91. }
  92. }
  93. // NewCompressingResponseWriter create a CompressingResponseWriter for a known encoding = {gzip,deflate}
  94. func NewCompressingResponseWriter(httpWriter http.ResponseWriter, encoding string) (*CompressingResponseWriter, error) {
  95. httpWriter.Header().Set(HEADER_ContentEncoding, encoding)
  96. c := new(CompressingResponseWriter)
  97. c.writer = httpWriter
  98. var err error
  99. if ENCODING_GZIP == encoding {
  100. w := currentCompressorProvider.AcquireGzipWriter()
  101. w.Reset(httpWriter)
  102. c.compressor = w
  103. c.encoding = ENCODING_GZIP
  104. } else if ENCODING_DEFLATE == encoding {
  105. w := currentCompressorProvider.AcquireZlibWriter()
  106. w.Reset(httpWriter)
  107. c.compressor = w
  108. c.encoding = ENCODING_DEFLATE
  109. } else {
  110. return nil, errors.New("Unknown encoding:" + encoding)
  111. }
  112. return c, err
  113. }