SetEncoder can be used to set the implementation of a compression algorithm. The encoding should be a standardized identifier. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding For example, add the Brotli algorithm: import brotli_enc "gopkg.in/kothar/brotli-go.v0/enc
(encoding string, fn EncoderFunc)
| 133 | // return brotli_enc.NewBrotliWriter(params, w) |
| 134 | // }) |
| 135 | func (c *Compressor) SetEncoder(encoding string, fn EncoderFunc) { |
| 136 | encoding = strings.ToLower(encoding) |
| 137 | if encoding == "" { |
| 138 | panic("the encoding can not be empty") |
| 139 | } |
| 140 | if fn == nil { |
| 141 | panic("attempted to set a nil encoder function") |
| 142 | } |
| 143 | |
| 144 | // If we are adding a new encoder that is already registered, we have to |
| 145 | // clear that one out first. |
| 146 | delete(c.pooledEncoders, encoding) |
| 147 | delete(c.encoders, encoding) |
| 148 | |
| 149 | // If the encoder supports Resetting (IoReseterWriter), then it can be pooled. |
| 150 | encoder := fn(io.Discard, c.level) |
| 151 | if _, ok := encoder.(ioResetterWriter); ok { |
| 152 | pool := &sync.Pool{ |
| 153 | New: func() interface{} { |
| 154 | return fn(io.Discard, c.level) |
| 155 | }, |
| 156 | } |
| 157 | c.pooledEncoders[encoding] = pool |
| 158 | } |
| 159 | // If the encoder is not in the pooledEncoders, add it to the normal encoders. |
| 160 | if _, ok := c.pooledEncoders[encoding]; !ok { |
| 161 | c.encoders[encoding] = fn |
| 162 | } |
| 163 | |
| 164 | for i, v := range c.encodingPrecedence { |
| 165 | if v == encoding { |
| 166 | c.encodingPrecedence = append(c.encodingPrecedence[:i], c.encodingPrecedence[i+1:]...) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | c.encodingPrecedence = append([]string{encoding}, c.encodingPrecedence...) |
| 171 | } |
| 172 | |
| 173 | // ServeHTTP returns the response from the orig file system, compressed if possible. |
| 174 | func (c *Compressor) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
no outgoing calls