fix MicroNotFound

reduce allocs in cdn push
This commit is contained in:
Azareal 2020-03-27 22:00:29 +10:00
parent 3adee57b37
commit b53577a126
5 changed files with 18 additions and 21 deletions

View File

@ -37,8 +37,7 @@ var defaultServerPort = "80" // 8080's a good one, if you're testing and don't w
func main() { func main() {
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows // Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func() { defer func() {
r := recover() if r := recover(); r != nil {
if r != nil {
fmt.Println(r) fmt.Println(r)
debug.PrintStack() debug.PrintStack()
pressAnyKey() pressAnyKey()

View File

@ -322,6 +322,7 @@ func SecurityError(w http.ResponseWriter, r *http.Request, user *User) RouteErro
} }
func MicroNotFound(w http.ResponseWriter, r *http.Request) RouteError { func MicroNotFound(w http.ResponseWriter, r *http.Request) RouteError {
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(404) w.WriteHeader(404)
_, _ = w.Write([]byte("file not found")) _, _ = w.Write([]byte("file not found"))
return HandledRouteError() return HandledRouteError()

View File

@ -2817,21 +2817,21 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user *
r.RLock() r.RLock()
h, ok := r.extraRoutes[req.URL.Path] h, ok := r.extraRoutes[req.URL.Path]
r.RUnlock() r.RUnlock()
req.URL.Path += extraData
if ok { if ok {
req.URL.Path += extraData
// TODO: Be more specific about *which* dynamic route it is // TODO: Be more specific about *which* dynamic route it is
co.RouteViewCounter.Bump(165) co.RouteViewCounter.Bump(165)
return h(w,req,user) return h(w,req,user)
} }
co.RouteViewCounter.Bump3(172, cn)
lp := strings.ToLower(req.URL.Path) lp := strings.ToLower(req.URL.Path)
if strings.Contains(lp,"admin") || strings.Contains(lp,"sql") || strings.Contains(lp,"manage") || strings.Contains(lp,"//") || strings.Contains(lp,"\\\\") || strings.Contains(lp,"wp") || strings.Contains(lp,"wordpress") || strings.Contains(lp,"config") || strings.Contains(lp,"setup") || strings.Contains(lp,"install") || strings.Contains(lp,"update") || strings.Contains(lp,"php") || strings.Contains(lp,"pl") || strings.Contains(lp,"wget") || strings.Contains(lp,"wp-") || strings.Contains(lp,"include") || strings.Contains(lp,"vendor") || strings.Contains(lp,"bin") || strings.Contains(lp,"system") || strings.Contains(lp,"eval") || strings.Contains(lp,"config") { if strings.Contains(lp,"admin") || strings.Contains(lp,"sql") || strings.Contains(lp,"manage") || strings.Contains(lp,"//") || strings.Contains(lp,"\\\\") || strings.Contains(lp,"wp") || strings.Contains(lp,"wordpress") || strings.Contains(lp,"config") || strings.Contains(lp,"setup") || strings.Contains(lp,"install") || strings.Contains(lp,"update") || strings.Contains(lp,"php") || strings.Contains(lp,"pl") || strings.Contains(lp,"wget") || strings.Contains(lp,"wp-") || strings.Contains(lp,"include") || strings.Contains(lp,"vendor") || strings.Contains(lp,"bin") || strings.Contains(lp,"system") || strings.Contains(lp,"eval") || strings.Contains(lp,"config") {
r.SuspiciousRequest(req,"Bad Route") r.SuspiciousRequest(req,"Bad Route")
} else { return c.MicroNotFound(w,req)
r.DumpRequest(req,"Bad Route")
} }
co.RouteViewCounter.Bump3(172, cn) r.DumpRequest(req,"Bad Route")
return c.NotFound(w,req,nil) return c.NotFound(w,req,nil)
} }
return err return err

View File

@ -1007,21 +1007,21 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user *
r.RLock() r.RLock()
h, ok := r.extraRoutes[req.URL.Path] h, ok := r.extraRoutes[req.URL.Path]
r.RUnlock() r.RUnlock()
req.URL.Path += extraData
if ok { if ok {
req.URL.Path += extraData
// TODO: Be more specific about *which* dynamic route it is // TODO: Be more specific about *which* dynamic route it is
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.DynamicRoute"}}) co.RouteViewCounter.Bump({{index .AllRouteMap "routes.DynamicRoute"}})
return h(w,req,user) return h(w,req,user)
} }
co.RouteViewCounter.Bump3({{index .AllRouteMap "routes.BadRoute"}}, cn)
lp := strings.ToLower(req.URL.Path) lp := strings.ToLower(req.URL.Path)
if strings.Contains(lp,"admin") || strings.Contains(lp,"sql") || strings.Contains(lp,"manage") || strings.Contains(lp,"//") || strings.Contains(lp,"\\\\") || strings.Contains(lp,"wp") || strings.Contains(lp,"wordpress") || strings.Contains(lp,"config") || strings.Contains(lp,"setup") || strings.Contains(lp,"install") || strings.Contains(lp,"update") || strings.Contains(lp,"php") || strings.Contains(lp,"pl") || strings.Contains(lp,"wget") || strings.Contains(lp,"wp-") || strings.Contains(lp,"include") || strings.Contains(lp,"vendor") || strings.Contains(lp,"bin") || strings.Contains(lp,"system") || strings.Contains(lp,"eval") || strings.Contains(lp,"config") { if strings.Contains(lp,"admin") || strings.Contains(lp,"sql") || strings.Contains(lp,"manage") || strings.Contains(lp,"//") || strings.Contains(lp,"\\\\") || strings.Contains(lp,"wp") || strings.Contains(lp,"wordpress") || strings.Contains(lp,"config") || strings.Contains(lp,"setup") || strings.Contains(lp,"install") || strings.Contains(lp,"update") || strings.Contains(lp,"php") || strings.Contains(lp,"pl") || strings.Contains(lp,"wget") || strings.Contains(lp,"wp-") || strings.Contains(lp,"include") || strings.Contains(lp,"vendor") || strings.Contains(lp,"bin") || strings.Contains(lp,"system") || strings.Contains(lp,"eval") || strings.Contains(lp,"config") {
r.SuspiciousRequest(req,"Bad Route") r.SuspiciousRequest(req,"Bad Route")
} else { return c.MicroNotFound(w,req)
r.DumpRequest(req,"Bad Route")
} }
co.RouteViewCounter.Bump3({{index .AllRouteMap "routes.BadRoute"}}, cn) r.DumpRequest(req,"Bad Route")
return c.NotFound(w,req,nil) return c.NotFound(w,req,nil)
} }
return err return err

View File

@ -23,8 +23,8 @@ func ParseSEOURL(urlBit string) (slug string, id int, err error) {
return halves[0], tid, err return halves[0], tid, err
} }
var slen1 = len("</s/>; rel=preload; as=script,") var slen1 = len("</s/>;rel=preload;as=script,")
var slen2 = len("</s/>; rel=preload; as=style,") var slen2 = len("</s/>;rel=preload;as=style,")
func doPush(w http.ResponseWriter, header *c.Header) { func doPush(w http.ResponseWriter, header *c.Header) {
//fmt.Println("in doPush") //fmt.Println("in doPush")
@ -32,11 +32,11 @@ func doPush(w http.ResponseWriter, header *c.Header) {
// TODO: Cache these in a sync.Pool? // TODO: Cache these in a sync.Pool?
var sb strings.Builder var sb strings.Builder
push := func(in []string) { push := func(in []string) {
sb.Grow((slen1 + 5) * len(in)) sb.Grow((slen1 + 6) * len(in))
for _, path := range in { for _, path := range in {
sb.WriteString("</s/") sb.WriteString("</s/")
sb.WriteString(path) sb.WriteString(path)
sb.WriteString(">; rel=preload; as=script,") sb.WriteString(">;rel=preload;as=script,")
} }
} }
push(header.Scripts) push(header.Scripts)
@ -44,11 +44,11 @@ func doPush(w http.ResponseWriter, header *c.Header) {
push(header.ScriptsAsync) push(header.ScriptsAsync)
if len(header.Stylesheets) > 0 { if len(header.Stylesheets) > 0 {
sb.Grow((slen2 + 6) * len(header.Stylesheets)) sb.Grow((slen2 + 7) * len(header.Stylesheets))
for _, path := range header.Stylesheets { for _, path := range header.Stylesheets {
sb.WriteString("</s/") sb.WriteString("</s/")
sb.WriteString(path) sb.WriteString(path)
sb.WriteString(">; rel=preload; as=style,") sb.WriteString(">;rel=preload;as=style,")
} }
} }
// TODO: Push avatars? // TODO: Push avatars?
@ -67,6 +67,7 @@ func doPush(w http.ResponseWriter, header *c.Header) {
if !ok { if !ok {
return return
} }
//panic("has pusher")
//fmt.Println("has pusher") //fmt.Println("has pusher")
push := func(in []string) { push := func(in []string) {
@ -162,11 +163,7 @@ func renderTemplate3(tmplName, hookName string, w http.ResponseWriter, r *http.R
if c.RunPreRenderHook("pre_render_"+hookName, w, r, h.CurrentUser, pi) { if c.RunPreRenderHook("pre_render_"+hookName, w, r, h.CurrentUser, pi) {
return nil return nil
} }
err := h.Theme.RunTmpl(tmplName, pi, w) return h.Theme.RunTmpl(tmplName, pi, w)
if err != nil {
return err
}
return nil
} }
// TODO: Rename renderTemplate to RenderTemplate instead of using this hack to avoid breaking things // TODO: Rename renderTemplate to RenderTemplate instead of using this hack to avoid breaking things