From 5bdda5c573d7605670bceb31d567295833936cab Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 8 Apr 2021 12:20:57 +1000 Subject: [PATCH] move router init() into non-generated router.go experimental fix for conn hangs --- common/site.go | 2 ++ gen_router.go | 38 +++++++++----------------------------- router.go | 29 +++++++++++++++++++++++++++++ router_gen/main.go | 38 +++++++++----------------------------- 4 files changed, 49 insertions(+), 58 deletions(-) diff --git a/common/site.go b/common/site.go index 411602b0..bda719b1 100644 --- a/common/site.go +++ b/common/site.go @@ -154,6 +154,8 @@ type devConfig struct { ExtraTmpls string // Experimental flag for adding compiled templates, we'll likely replace this with a better mechanism //QuicPort int // Experimental! + + ExpFix1 bool // unlisted setting, experimental fix for http/1.1 conn hangs } // configHolder is purely for having a big struct to unmarshal data into diff --git a/gen_router.go b/gen_router.go index dd0123f4..261a3071 100644 --- a/gen_router.go +++ b/gen_router.go @@ -10,7 +10,8 @@ import ( "sync/atomic" "errors" "net/http" - "time" + "io" + "io/ioutil" c "github.com/Azareal/Gosora/common" co "github.com/Azareal/Gosora/common/counters" @@ -897,34 +898,6 @@ var markToID = map[string]int{ "safari":1, }*/ -// TODO: Stop spilling these into the package scope? -func init() { - _ = time.Now() - co.SetRouteMapEnum(routeMapEnum) - co.SetReverseRouteMapEnum(reverseRouteMapEnum) - co.SetAgentMapEnum(agentMapEnum) - co.SetReverseAgentMapEnum(reverseAgentMapEnum) - co.SetOSMapEnum(osMapEnum) - co.SetReverseOSMapEnum(reverseOSMapEnum) - - g := func(n string) int { - a, ok := agentMapEnum[n] - if !ok { - panic("name not found in agentMapEnum") - } - return a - } - c.Chrome = g("chrome") - c.Firefox = g("firefox") - c.SimpleBots = []int{ - g("semrush"), - g("ahrefs"), - g("python"), - //g("go"), - g("curl"), - } -} - // HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS type HTTPSRedirect struct {} @@ -953,6 +926,13 @@ func (r *GenRouter) SuspiciousRequest(req *http.Request, pre string) { // TODO: SetDefaultPath // TODO: GetDefaultPath func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { + // HTTP/1.1 hanging conn fix + if req.ProtoMajor == 1 && c.Dev.ExpFix1 { + defer func() { + io.Copy(ioutil.Discard, req.Body) + req.Body.Close() + }() + } malformedRequest := func(typ int) { w.WriteHeader(200) // 400 w.Write([]byte("")) diff --git a/router.go b/router.go index c0628a47..f65643a8 100644 --- a/router.go +++ b/router.go @@ -12,8 +12,37 @@ import ( "time" c "github.com/Azareal/Gosora/common" + co "github.com/Azareal/Gosora/common/counters" ) +// TODO: Stop spilling these into the package scope? +func init() { + _ = time.Now() + co.SetRouteMapEnum(routeMapEnum) + co.SetReverseRouteMapEnum(reverseRouteMapEnum) + co.SetAgentMapEnum(agentMapEnum) + co.SetReverseAgentMapEnum(reverseAgentMapEnum) + co.SetOSMapEnum(osMapEnum) + co.SetReverseOSMapEnum(reverseOSMapEnum) + + g := func(n string) int { + a, ok := agentMapEnum[n] + if !ok { + panic("name not found in agentMapEnum") + } + return a + } + c.Chrome = g("chrome") + c.Firefox = g("firefox") + c.SimpleBots = []int{ + g("semrush"), + g("ahrefs"), + g("python"), + //g("go"), + g("curl"), + } +} + type WriterIntercept struct { http.ResponseWriter } diff --git a/router_gen/main.go b/router_gen/main.go index 308a2a95..a11813f5 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -432,7 +432,8 @@ import ( "sync/atomic" "errors" "net/http" - "time" + "io" + "io/ioutil" c "github.com/Azareal/Gosora/common" co "github.com/Azareal/Gosora/common/counters" @@ -480,34 +481,6 @@ var markToID = map[string]int{ {{range $index, $el := .AllAgentMarkNames}} "safari":1, }*/ -// TODO: Stop spilling these into the package scope? -func init() { - _ = time.Now() - co.SetRouteMapEnum(routeMapEnum) - co.SetReverseRouteMapEnum(reverseRouteMapEnum) - co.SetAgentMapEnum(agentMapEnum) - co.SetReverseAgentMapEnum(reverseAgentMapEnum) - co.SetOSMapEnum(osMapEnum) - co.SetReverseOSMapEnum(reverseOSMapEnum) - - g := func(n string) int { - a, ok := agentMapEnum[n] - if !ok { - panic("name not found in agentMapEnum") - } - return a - } - c.Chrome = g("chrome") - c.Firefox = g("firefox") - c.SimpleBots = []int{ - g("semrush"), - g("ahrefs"), - g("python"), - //g("go"), - g("curl"), - } -} - // HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS type HTTPSRedirect struct {} @@ -536,6 +509,13 @@ func (r *GenRouter) SuspiciousRequest(req *http.Request, pre string) { // TODO: SetDefaultPath // TODO: GetDefaultPath func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { + // HTTP/1.1 hanging conn fix + if req.ProtoMajor == 1 && c.Dev.ExpFix1 { + defer func() { + io.Copy(ioutil.Discard, req.Body) + req.Body.Close() + }() + } malformedRequest := func(typ int) { w.WriteHeader(200) // 400 w.Write([]byte(""))