master
Bel LaPointe 2021-09-17 08:07:35 -06:00
parent 2e227088e2
commit 94b595af26
2 changed files with 17 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module local/gziphttp
go 1.16

View File

@ -3,6 +3,7 @@ package gziphttp
import ( import (
"compress/gzip" "compress/gzip"
"net/http" "net/http"
"strings"
) )
type GZipResponseWriter struct { type GZipResponseWriter struct {
@ -10,6 +11,19 @@ type GZipResponseWriter struct {
gz *gzip.Writer gz *gzip.Writer
} }
func Can(r *http.Request) bool {
encodings, _ := r.Header["Accept-Encoding"]
for _, encoding := range encodings {
items := strings.Split(encoding, ",")
for _, item := range items {
if strings.TrimSpace(item) == "gzip" {
return true
}
}
}
return false
}
func New(w http.ResponseWriter) *GZipResponseWriter { func New(w http.ResponseWriter) *GZipResponseWriter {
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
w.Header().Add("Content-Encoding", "gzip") w.Header().Add("Content-Encoding", "gzip")