Skip to content

Commit

Permalink
Use http.Header methods instead of being clever
Browse files Browse the repository at this point in the history
golang/go@1e814df tried to avoid
unnecessary calls to CanonicalHeaderKey in the standard library, but it
is not clear that there is any meaningful performance gain, nor is it
used consistently. This change mostly reverts the manual manipulation
of the http.Header map by using the provided methods instead.
  • Loading branch information
tmthrgd committed Dec 3, 2017
1 parent 6229177 commit 1e9627c
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ func (w *responseWriter) startGzip() (err error) {
h := w.Header()

// Set the GZIP header.
h["Content-Encoding"] = []string{"gzip"}
h.Set("Content-Encoding", "gzip")

// if the Content-Length is already set, then calls
// to Write on gzip will fail to set the
// Content-Length header since its already set
// See: https://github.com/golang/go/issues/14975.
delete(h, "Content-Length")
h.Del("Content-Length")

// Write the header to gzip response.
w.ResponseWriter.WriteHeader(w.code)
Expand Down Expand Up @@ -218,7 +218,7 @@ func (w *responseWriter) inferContentType(b []byte) {
}

// It infer it from the uncompressed body.
h["Content-Type"] = []string{http.DetectContentType(b)}
h.Set("Content-Type", http.DetectContentType(b))
}

func (w *responseWriter) shouldPassThrough() bool {
Expand Down Expand Up @@ -324,8 +324,7 @@ func (h *handler) pool() *sync.Pool {
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hdr := w.Header()
hdr["Vary"] = append(hdr["Vary"], "Accept-Encoding")
w.Header().Add("Vary", "Accept-Encoding")

var acceptsGzip bool
for _, spec := range header.ParseAccept(r.Header, "Accept-Encoding") {
Expand Down

0 comments on commit 1e9627c

Please sign in to comment.