diff options
author | Steve <nilslice@gmail.com> | 2017-01-25 11:18:36 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-25 11:18:36 -0800 |
commit | 3dea30f62a274db3e0cc95441338b2b71a751198 (patch) | |
tree | 522c4232685eb834d559698e0d249c44b0753363 | |
parent | 2a979e80aeb2d1960a51925e96314de2e49f5fac (diff) |
[core] implement http.Pusher on our composite gzip/response writer (#45)
-rw-r--r-- | system/api/handlers.go | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/system/api/handlers.go b/system/api/handlers.go index 9292e15..2e70fdc 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -129,7 +129,7 @@ func contentHandler(res http.ResponseWriter, req *http.Request) { return } - defer push(res, req, pt, post) + push(res, req, pt, post) j, err := fmtJSON(json.RawMessage(post)) if err != nil { @@ -166,7 +166,7 @@ func contentHandlerBySlug(res http.ResponseWriter, req *http.Request) { return } - defer push(res, req, it, post) + push(res, req, it, post) j, err := fmtJSON(json.RawMessage(post)) if err != nil { @@ -332,7 +332,12 @@ func Gzip(next http.HandlerFunc) http.HandlerFunc { if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { // gzip response data res.Header().Set("Content-Encoding", "gzip") - gzres := gzipResponseWriter{res, gzip.NewWriter(res)} + var gzres gzipResponseWriter + if pusher, ok := res.(http.Pusher); ok { + gzres = gzipResponseWriter{res, pusher, gzip.NewWriter(res)} + } else { + gzres = gzipResponseWriter{res, nil, gzip.NewWriter(res)} + } next.ServeHTTP(gzres, req) return @@ -344,6 +349,8 @@ func Gzip(next http.HandlerFunc) http.HandlerFunc { type gzipResponseWriter struct { http.ResponseWriter + pusher http.Pusher + gw *gzip.Writer } @@ -351,3 +358,15 @@ func (gzw gzipResponseWriter) Write(p []byte) (int, error) { defer gzw.gw.Close() return gzw.gw.Write(p) } + +func (gzw gzipResponseWriter) Push(target string, opts *http.PushOptions) error { + if opts == nil { + opts = &http.PushOptions{ + Header: make(http.Header), + } + } + + opts.Header.Set("Accept-Encoding", "gzip") + + return gzw.pusher.Push(target, opts) +} |