diff options
Diffstat (limited to 'src/dict2rest/gzip.go')
| -rw-r--r-- | src/dict2rest/gzip.go | 37 |
1 files changed, 0 insertions, 37 deletions
diff --git a/src/dict2rest/gzip.go b/src/dict2rest/gzip.go deleted file mode 100644 index c3bb5ed..0000000 --- a/src/dict2rest/gzip.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "compress/gzip" - "io" - "net/http" - "strings" - "github.com/alexedwards/stack" -) - -// Gzip Compression -type gzipResponseWriter struct { - io.Writer - http.ResponseWriter -} - -func (w gzipResponseWriter) Write(b []byte) (int, error) { - return w.Writer.Write(b) -} - -func Gzip(ctx *stack.Context, next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") || - ctx.Get("handled").(bool) == true { - // move on to next handler in the chain - next.ServeHTTP(w, req) - return - } - w.Header().Set("Content-Encoding", "gzip") - gz := gzip.NewWriter(w) - defer gz.Close() - gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w} - ctx.Put("handled", true) - defer ctx.Delete("handled") - next.ServeHTTP(gzw, req) - }) -} |
