Skip to content

Commit

Permalink
Use github.com/NYTimes/gziphandler
Browse files Browse the repository at this point in the history
  • Loading branch information
iketheadore committed May 12, 2018
1 parent ef4bd5c commit c857085
Show file tree
Hide file tree
Showing 12 changed files with 850 additions and 30 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -42,3 +42,7 @@
[[constraint]]
name = "github.com/cenkalti/backoff"
version = "v1.1.0"

[[constraint]]
name = "github.com/NYTimes/gziphandler"
version = "1.0.1"
3 changes: 2 additions & 1 deletion src/gui/http.go
Expand Up @@ -11,6 +11,7 @@ import (
"time"
"unicode"

"github.com/NYTimes/gziphandler"
"github.com/skycoin/skycoin/src/api/webrpc"
"github.com/skycoin/skycoin/src/cipher"
"github.com/skycoin/skycoin/src/daemon"
Expand Down Expand Up @@ -199,7 +200,7 @@ func newServerMux(c muxConfig, gateway Gatewayer, csrfStore *CSRFStore, rpc *web
handler = wh.ElapsedHandler(logger, handler)
handler = CSRFCheck(csrfStore, handler)
handler = headerCheck(c.host, handler)
handler = wh.GzipHandler(handler)
handler = gziphandler.GzipHandler(handler)
mux.Handle(endpoint, handler)
}

Expand Down
28 changes: 0 additions & 28 deletions src/util/http/handler.go
@@ -1,9 +1,7 @@
package httphelper

import (
"compress/gzip"
"fmt"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -48,29 +46,3 @@ func HostCheck(logger *logging.Logger, host string, handler http.Handler) http.H
handler.ServeHTTP(w, r)
})
}

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (grw gzipResponseWriter) Write(b []byte) (int, error) {
return grw.Writer.Write(b)
}

// GzipHandler compresses response with gzip if request's header of 'Accept-Encoding' contains 'gzip'.
func GzipHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
handler.ServeHTTP(w, r)
return
}

w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()

gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
handler.ServeHTTP(gzr, r)
})
}
1 change: 1 addition & 0 deletions vendor/github.com/NYTimes/gziphandler/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/NYTimes/gziphandler/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions vendor/github.com/NYTimes/gziphandler/CODE_OF_CONDUCT.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/NYTimes/gziphandler/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c857085

Please sign in to comment.