3 // Copyright 2013 Ernest Micklei. All rights reserved.
4 // Use of this source code is governed by a license
5 // that can be found in the LICENSE file.
18 // OBSOLETE : use restful.DefaultContainer.EnableContentEncoding(true) to change this setting.
19 var EnableContentEncoding = false
21 // CompressingResponseWriter is a http.ResponseWriter that can perform content encoding (gzip and zlib)
22 type CompressingResponseWriter struct {
23 writer http.ResponseWriter
24 compressor io.WriteCloser
28 // Header is part of http.ResponseWriter interface
29 func (c *CompressingResponseWriter) Header() http.Header {
30 return c.writer.Header()
33 // WriteHeader is part of http.ResponseWriter interface
34 func (c *CompressingResponseWriter) WriteHeader(status int) {
35 c.writer.WriteHeader(status)
38 // Write is part of http.ResponseWriter interface
39 // It is passed through the compressor
40 func (c *CompressingResponseWriter) Write(bytes []byte) (int, error) {
41 if c.isCompressorClosed() {
42 return -1, errors.New("Compressing error: tried to write data using closed compressor")
44 return c.compressor.Write(bytes)
47 // CloseNotify is part of http.CloseNotifier interface
48 func (c *CompressingResponseWriter) CloseNotify() <-chan bool {
49 return c.writer.(http.CloseNotifier).CloseNotify()
52 // Close the underlying compressor
53 func (c *CompressingResponseWriter) Close() error {
54 if c.isCompressorClosed() {
55 return errors.New("Compressing error: tried to close already closed compressor")
59 if ENCODING_GZIP == c.encoding {
60 currentCompressorProvider.ReleaseGzipWriter(c.compressor.(*gzip.Writer))
62 if ENCODING_DEFLATE == c.encoding {
63 currentCompressorProvider.ReleaseZlibWriter(c.compressor.(*zlib.Writer))
70 func (c *CompressingResponseWriter) isCompressorClosed() bool {
71 return nil == c.compressor
74 // Hijack implements the Hijacker interface
75 // This is especially useful when combining Container.EnabledContentEncoding
76 // in combination with websockets (for instance gorilla/websocket)
77 func (c *CompressingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
78 hijacker, ok := c.writer.(http.Hijacker)
80 return nil, nil, errors.New("ResponseWriter doesn't support Hijacker interface")
82 return hijacker.Hijack()
85 // WantsCompressedResponse reads the Accept-Encoding header to see if and which encoding is requested.
86 func wantsCompressedResponse(httpRequest *http.Request) (bool, string) {
87 header := httpRequest.Header.Get(HEADER_AcceptEncoding)
88 gi := strings.Index(header, ENCODING_GZIP)
89 zi := strings.Index(header, ENCODING_DEFLATE)
90 // use in order of appearance
92 return zi != -1, ENCODING_DEFLATE
94 return gi != -1, ENCODING_GZIP
97 return true, ENCODING_GZIP
99 return true, ENCODING_DEFLATE
103 // NewCompressingResponseWriter create a CompressingResponseWriter for a known encoding = {gzip,deflate}
104 func NewCompressingResponseWriter(httpWriter http.ResponseWriter, encoding string) (*CompressingResponseWriter, error) {
105 httpWriter.Header().Set(HEADER_ContentEncoding, encoding)
106 c := new(CompressingResponseWriter)
107 c.writer = httpWriter
109 if ENCODING_GZIP == encoding {
110 w := currentCompressorProvider.AcquireGzipWriter()
113 c.encoding = ENCODING_GZIP
114 } else if ENCODING_DEFLATE == encoding {
115 w := currentCompressorProvider.AcquireZlibWriter()
118 c.encoding = ENCODING_DEFLATE
120 return nil, errors.New("Unknown encoding:" + encoding)