From 94b595af26d9553365f7c83e43a20c8b1d3db5c7 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 17 Sep 2021 08:07:35 -0600 Subject: [PATCH] gomod --- go.mod | 3 +++ gzipresponsewriter.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e87d397 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module local/gziphttp + +go 1.16 diff --git a/gzipresponsewriter.go b/gzipresponsewriter.go index 7ac071d..05467be 100755 --- a/gzipresponsewriter.go +++ b/gzipresponsewriter.go @@ -3,6 +3,7 @@ package gziphttp import ( "compress/gzip" "net/http" + "strings" ) type GZipResponseWriter struct { @@ -10,6 +11,19 @@ type GZipResponseWriter struct { 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 { w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Encoding", "gzip")