1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00

Compress HTML responses to Gzip/Deflate if supported by browser

This commit is contained in:
Frédéric Guillot 2018-07-06 20:39:28 -07:00
parent 53deb0b8cd
commit 34a3fe426b
40 changed files with 82 additions and 58 deletions

View file

@ -5,7 +5,10 @@
package response
import (
"compress/flate"
"compress/gzip"
"net/http"
"strings"
"time"
)
@ -32,3 +35,23 @@ func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, conten
w.Write(content)
}
}
// Compress the response sent to the browser.
func Compress(w http.ResponseWriter, r *http.Request, data []byte) {
acceptEncoding := r.Header.Get("Accept-Encoding")
switch {
case strings.Contains(acceptEncoding, "gzip"):
w.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close()
gzipWriter.Write(data)
case strings.Contains(acceptEncoding, "deflate"):
w.Header().Set("Content-Encoding", "deflate")
flateWriter, _ := flate.NewWriter(w, -1)
defer flateWriter.Close()
flateWriter.Write(data)
default:
w.Write(data)
}
}