| 1 | package compresshttp |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log" |
| 6 | "net/http" |
| 7 | ) |
| 8 | |
| 9 | func Middleware(next http.Handler) http.Handler { |
| 10 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 11 | w.Header().Set(acceptEncoding, AcceptedEncodings) |
| 12 | // decompress request |
| 13 | if err := DecompressRequest(r); err == ErrUnacceptableEncoding { |
| 14 | http.Error(w, "invalid content-encoding", http.StatusUnsupportedMediaType) |
| 15 | return |
| 16 | } else if err != nil { |
| 17 | log.Printf("error: decoding request from %s: %+v", r.RemoteAddr, err) |
| 18 | http.Error(w, "failed to decompress request", http.StatusBadRequest) |
| 19 | return |
| 20 | } |
| 21 | // choose response encoding |
| 22 | encoding := selectEncoding(r.Header.Get(acceptEncoding)) |
| 23 | if encoding == "" || encoding == EncodingIdentity { |
| 24 | // shortcut if no encoding is possible |
| 25 | next.ServeHTTP(w, r) |
| 26 | return |
| 27 | } |
| 28 | w.Header().Del("Content-Length") |
| 29 | // wrap writer in compression and call handler |
| 30 | wrapped := &responseCompressor{ |
| 31 | rw: w, |
| 32 | encoding: encoding, |
| 33 | } |
| 34 | next.ServeHTTP(wrapped, r) |
| 35 | // flush |
| 36 | if err := wrapped.Close(); err != nil { |
| 37 | log.Printf("error: flushing response to %s: %+v", r.RemoteAddr, err) |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | type responseCompressor struct { |
| 43 | rw http.ResponseWriter |
| 44 | wc io.WriteCloser |
| 45 | |
| 46 | encoding string |
| 47 | wroteHeader bool |
| 48 | } |
| 49 | |
| 50 | func (w *responseCompressor) WriteHeader(status int) { |
| 51 | if !w.wroteHeader { |
| 52 | if status >= 300 { |
| 53 | // don't compress errors |
| 54 | w.encoding = "" |
| 55 | } else if w.encoding != "" && w.encoding != EncodingIdentity { |
| 56 | w.Header().Set(contentEncoding, w.encoding) |
| 57 | } |
| 58 | w.wroteHeader = true |
| 59 | } |
| 60 | w.rw.WriteHeader(status) |
| 61 | } |
| 62 | |
| 63 | func (w *responseCompressor) Write(d []byte) (int, error) { |
| 64 | // wait until the first byte to start compressing so that it can be |
| 65 | // selectively disabled in the case of errors |
| 66 | if w.wc == nil { |
| 67 | if !w.wroteHeader { |
| 68 | w.Header().Set(contentEncoding, w.encoding) |
| 69 | w.rw.WriteHeader(http.StatusOK) |
| 70 | w.wroteHeader = true |
| 71 | } |
| 72 | var err error |
| 73 | w.wc, err = setupCompression(w.encoding, w.rw) |
| 74 | if err != nil { |
| 75 | return 0, err |
| 76 | } |
| 77 | } |
| 78 | return w.wc.Write(d) |
| 79 | } |
| 80 | |
| 81 | func (w *responseCompressor) Header() http.Header { |
| 82 | return w.rw.Header() |
| 83 | } |
| 84 | |
| 85 | func (w *responseCompressor) Flush() { |
| 86 | // flush compressor |
| 87 | if flusher, ok := w.wc.(flusher); ok { |
| 88 | if err := flusher.Flush(); err != nil { |
| 89 | log.Println("warning: flushing compressor:", err) |
| 90 | } |
| 91 | } |
| 92 | // flush response |
| 93 | if flusher, ok := w.rw.(http.Flusher); ok { |
| 94 | flusher.Flush() |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func (w *responseCompressor) Close() error { |
| 99 | if w.wc == nil { |
| 100 | return nil |
| 101 | } |
| 102 | return w.wc.Close() |
| 103 | } |
| 104 | |
| 105 | type flusher interface { |
| 106 | Flush() error |
| 107 | } |