From 146aa3aaadfd88eaf4ca69311e948f9672fbdf2c Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 22 Aug 2018 17:10:29 +1000 Subject: [PATCH] Fixed a bug where gzip spits out garbage bytes when it's disabled. Disabled Gzip on the Control Panel Backup Page. --- gen_router.go | 12 +++++++++++- router_gen/main.go | 6 +++++- router_gen/routes.go | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/gen_router.go b/gen_router.go index f8244d0e..8ad99391 100644 --- a/gen_router.go +++ b/gen_router.go @@ -892,7 +892,11 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Type", "text/html") gz := gzip.NewWriter(w) - defer gz.Close() + defer func() { + if w.Header().Get("Content-Encoding") == "gzip" { + gz.Close() + } + }() w = gzipResponseWriter{Writer: gz, ResponseWriter: w} } router.routeSwitch(w, req, user, prefix, extraData) @@ -1421,6 +1425,12 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u return } + gzw, ok := w.(gzipResponseWriter) + if ok { + w = gzw.ResponseWriter + w.Header().Del("Content-Type") + w.Header().Del("Content-Encoding") + } counters.RouteViewCounter.Bump(73) err = panel.Backups(w,req,user,extraData) case "/panel/logs/regs/": diff --git a/router_gen/main.go b/router_gen/main.go index 1347fcf0..0eb3660b 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -663,7 +663,11 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Type", "text/html") gz := gzip.NewWriter(w) - defer gz.Close() + defer func() { + if w.Header().Get("Content-Encoding") == "gzip" { + gz.Close() + } + }() w = gzipResponseWriter{Writer: gz, ResponseWriter: w} } router.routeSwitch(w, req, user, prefix, extraData) diff --git a/router_gen/routes.go b/router_gen/routes.go index ef16b908..b1e38b33 100644 --- a/router_gen/routes.go +++ b/router_gen/routes.go @@ -210,7 +210,7 @@ func buildPanelRoutes() { Action("routePanelGroupsEditPermsSubmit", "/panel/groups/edit/perms/submit/", "extraData"), Action("routePanelGroupsCreateSubmit", "/panel/groups/create/"), - View("panel.Backups", "/panel/backups/", "extraData").Before("SuperAdminOnly"), // TODO: Tests for this + View("panel.Backups", "/panel/backups/", "extraData").Before("SuperAdminOnly").NoGzip(), // TODO: Tests for this View("panel.LogsRegs", "/panel/logs/regs/"), View("panel.LogsMod", "/panel/logs/mod/"), View("panel.Debug", "/panel/debug/").Before("AdminOnly"),