Moved /uploads back into the main router switch.

Added the NoGzip method to RouteImpl.
Added the LitBeforeMultiline method to RouteImpl.
This commit is contained in:
Azareal 2018-08-22 12:13:17 +10:00
parent 8f7cda9378
commit f90fe99fb3
4 changed files with 72 additions and 40 deletions

View File

@ -888,19 +888,7 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
// Disable Gzip when SSL is disabled for security reasons? // 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 if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
// 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") {
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
gz := gzip.NewWriter(w) gz := gzip.NewWriter(w)
@ -956,6 +944,12 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u
return return
} }
gzw, ok := w.(gzipResponseWriter)
if ok {
w = gzw.ResponseWriter
w.Header().Del("Content-Type")
w.Header().Del("Content-Encoding")
}
counters.RouteViewCounter.Bump(5) counters.RouteViewCounter.Bump(5)
err = routes.ShowAttachment(w,req,user,extraData) err = routes.ShowAttachment(w,req,user,extraData)
if err != nil { 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)) err = common.HandleUploadRoute(w,req,user,int(common.Config.MaxRequestSize))
if err != nil { if err != nil {
router.handleError(err,w,req,user) router.handleError(err,w,req,user)
return return
} }
err = common.NoUploadSessionMismatch(w,req,user) err = common.NoUploadSessionMismatch(w,req,user)
if err != nil { if err != nil {
router.handleError(err,w,req,user) 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)) err = common.HandleUploadRoute(w,req,user,int(common.Config.MaxRequestSize))
if err != nil { if err != nil {
router.handleError(err,w,req,user) router.handleError(err,w,req,user)
return return
} }
err = common.NoUploadSessionMismatch(w,req,user) err = common.NoUploadSessionMismatch(w,req,user)
if err != nil { if err != nil {
router.handleError(err,w,req,user) 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)) err = common.HandleUploadRoute(w,req,user,int(common.Config.MaxRequestSize))
if err != nil { if err != nil {
router.handleError(err,w,req,user) router.handleError(err,w,req,user)
return return
} }
err = common.NoUploadSessionMismatch(w,req,user) err = common.NoUploadSessionMismatch(w,req,user)
if err != nil { if err != nil {
router.handleError(err,w,req,user) router.handleError(err,w,req,user)
@ -2063,6 +2057,21 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u
if err != nil { if err != nil {
router.handleError(err,w,req,user) 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 "": case "":
// Stop the favicons, robots.txt file, etc. resolving to the topics list // Stop the favicons, robots.txt file, etc. resolving to the topics list
// TODO: Add support for favicons and robots.txt files // TODO: Add support for favicons and robots.txt files

View File

@ -665,19 +665,7 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
// Disable Gzip when SSL is disabled for security reasons? // 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 if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
// 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") {
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
gz := gzip.NewWriter(w) gz := gzip.NewWriter(w)
@ -696,6 +684,21 @@ func (router *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, u
if err != nil { if err != nil {
router.handleError(err,w,req,user) 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 "": case "":
// Stop the favicons, robots.txt file, etc. resolving to the topics list // Stop the favicons, robots.txt file, etc. resolving to the topics list
// TODO: Add support for favicons and robots.txt files // TODO: Add support for favicons and robots.txt files

View File

@ -1,5 +1,7 @@
package main package main
import "strings"
type RouteImpl struct { type RouteImpl struct {
Name string Name string
Path string Path string
@ -32,6 +34,15 @@ func (route *RouteImpl) LitBefore(items ...string) *RouteImpl {
return route 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 { func (route *RouteImpl) hasBefore(items ...string) bool {
for _, item := range items { for _, item := range items {
if route.hasBeforeItem(item) { if route.hasBeforeItem(item) {
@ -50,6 +61,15 @@ func (route *RouteImpl) hasBeforeItem(item string) bool {
return false 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) { func addRouteGroup(routeGroup *RouteGroup) {
routeGroups = append(routeGroups, 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 { 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 { if err != nil {
router.handleError(err,w,req,user) router.handleError(err,w,req,user)
return return

View File

@ -8,7 +8,7 @@ func routes() {
addRoute(View("routes.ViewForum", "/forum/", "extraData")) addRoute(View("routes.ViewForum", "/forum/", "extraData"))
addRoute(AnonAction("routes.ChangeTheme", "/theme/")) addRoute(AnonAction("routes.ChangeTheme", "/theme/"))
addRoute( addRoute(
View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm"), View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm").NoGzip(),
) )
apiGroup := newRouteGroup("/api/", apiGroup := newRouteGroup("/api/",