From f90fe99fb318d7ca04a6b850aed8cbb78329751c Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 22 Aug 2018 12:13:17 +1000 Subject: [PATCH] Moved /uploads back into the main router switch. Added the NoGzip method to RouteImpl. Added the LitBeforeMultiline method to RouteImpl. --- gen_router.go | 59 +++++++++++++++++++++++----------------- router_gen/main.go | 29 +++++++++++--------- router_gen/route_impl.go | 22 ++++++++++++++- router_gen/routes.go | 2 +- 4 files changed, 72 insertions(+), 40 deletions(-) diff --git a/gen_router.go b/gen_router.go index 2767bc89..31b3afc3 100644 --- a/gen_router.go +++ b/gen_router.go @@ -888,19 +888,7 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } // Disable Gzip when SSL is disabled for security reasons? - // We don't want to double-compress things which are already compressed, so we're moving /uploads here - // TODO: Find a better way of doing this - if prefix == "/uploads" { - if extraData == "" { - common.NotFound(w,req,nil) - return - } - counters.RouteViewCounter.Bump(121) - req.URL.Path += extraData - // TODO: Find a way to propagate errors up from this? - router.UploadHandler(w,req) // TODO: Count these views - return - } else if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { + if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Type", "text/html") gz := gzip.NewWriter(w) @@ -956,6 +944,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(5) err = routes.ShowAttachment(w,req,user,extraData) if err != nil { @@ -1524,10 +1518,10 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u } err = common.HandleUploadRoute(w,req,user,int(common.Config.MaxRequestSize)) - if err != nil { - router.handleError(err,w,req,user) - return - } + if err != nil { + router.handleError(err,w,req,user) + return + } err = common.NoUploadSessionMismatch(w,req,user) if err != nil { router.handleError(err,w,req,user) @@ -1701,10 +1695,10 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u } err = common.HandleUploadRoute(w,req,user,int(common.Config.MaxRequestSize)) - if err != nil { - router.handleError(err,w,req,user) - return - } + if err != nil { + router.handleError(err,w,req,user) + return + } err = common.NoUploadSessionMismatch(w,req,user) if err != nil { router.handleError(err,w,req,user) @@ -1858,10 +1852,10 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u } err = common.HandleUploadRoute(w,req,user,int(common.Config.MaxRequestSize)) - if err != nil { - router.handleError(err,w,req,user) - return - } + if err != nil { + router.handleError(err,w,req,user) + return + } err = common.NoUploadSessionMismatch(w,req,user) if err != nil { router.handleError(err,w,req,user) @@ -2063,6 +2057,21 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u if err != nil { router.handleError(err,w,req,user) }*/ + case "/uploads": + if extraData == "" { + common.NotFound(w,req,nil) + return + } + gzw, ok := w.(gzipResponseWriter) + if ok { + w = gzw.ResponseWriter + w.Header().Del("Content-Type") + w.Header().Del("Content-Encoding") + } + counters.RouteViewCounter.Bump(121) + req.URL.Path += extraData + // TODO: Find a way to propagate errors up from this? + router.UploadHandler(w,req) // TODO: Count these views case "": // Stop the favicons, robots.txt file, etc. resolving to the topics list // TODO: Add support for favicons and robots.txt files diff --git a/router_gen/main.go b/router_gen/main.go index c7aa09c6..44755de7 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -665,19 +665,7 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } // Disable Gzip when SSL is disabled for security reasons? - // We don't want to double-compress things which are already compressed, so we're moving /uploads here - // TODO: Find a better way of doing this - if prefix == "/uploads" { - if extraData == "" { - common.NotFound(w,req,nil) - return - } - counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.UploadedFile" }}) - req.URL.Path += extraData - // TODO: Find a way to propagate errors up from this? - router.UploadHandler(w,req) // TODO: Count these views - return - } else if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { + if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Type", "text/html") gz := gzip.NewWriter(w) @@ -696,6 +684,21 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u if err != nil { router.handleError(err,w,req,user) }*/ + case "/uploads": + if extraData == "" { + common.NotFound(w,req,nil) + return + } + gzw, ok := w.(gzipResponseWriter) + if ok { + w = gzw.ResponseWriter + w.Header().Del("Content-Type") + w.Header().Del("Content-Encoding") + } + counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.UploadedFile" }}) + req.URL.Path += extraData + // TODO: Find a way to propagate errors up from this? + router.UploadHandler(w,req) // TODO: Count these views case "": // Stop the favicons, robots.txt file, etc. resolving to the topics list // TODO: Add support for favicons and robots.txt files diff --git a/router_gen/route_impl.go b/router_gen/route_impl.go index fb3f8067..cc73a11a 100644 --- a/router_gen/route_impl.go +++ b/router_gen/route_impl.go @@ -1,5 +1,7 @@ package main +import "strings" + type RouteImpl struct { Name string Path string @@ -32,6 +34,15 @@ func (route *RouteImpl) LitBefore(items ...string) *RouteImpl { return route } +func (route *RouteImpl) LitBeforeMultiline(items ...string) *RouteImpl { + for _, item := range items { + for _, line := range strings.Split(item, "\n") { + route.LitBefore(strings.TrimSpace(line)) + } + } + return route +} + func (route *RouteImpl) hasBefore(items ...string) bool { for _, item := range items { if route.hasBeforeItem(item) { @@ -50,6 +61,15 @@ func (route *RouteImpl) hasBeforeItem(item string) bool { return false } +func (route *RouteImpl) NoGzip() *RouteImpl { + return route.LitBeforeMultiline(`gzw, ok := w.(gzipResponseWriter) + if ok { + w = gzw.ResponseWriter + w.Header().Del("Content-Type") + w.Header().Del("Content-Encoding") + }`) +} + func addRouteGroup(routeGroup *RouteGroup) { routeGroups = append(routeGroups, routeGroup) } @@ -113,7 +133,7 @@ func UploadAction(fname string, path string, args ...string) *uploadAction { } func (action *uploadAction) MaxSizeVar(varName string) *RouteImpl { - action.Route.LitBefore(`err = common.HandleUploadRoute(w,req,user,` + varName + `) + action.Route.LitBeforeMultiline(`err = common.HandleUploadRoute(w,req,user,` + varName + `) if err != nil { router.handleError(err,w,req,user) return diff --git a/router_gen/routes.go b/router_gen/routes.go index 11ab54a6..ef16b908 100644 --- a/router_gen/routes.go +++ b/router_gen/routes.go @@ -8,7 +8,7 @@ func routes() { addRoute(View("routes.ViewForum", "/forum/", "extraData")) addRoute(AnonAction("routes.ChangeTheme", "/theme/")) addRoute( - View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm"), + View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm").NoGzip(), ) apiGroup := newRouteGroup("/api/",