move router init() into non-generated router.go

experimental fix for conn hangs
This commit is contained in:
Azareal 2021-04-08 12:20:57 +10:00
parent 7733cf5cdc
commit 5bdda5c573
4 changed files with 49 additions and 58 deletions

View File

@ -154,6 +154,8 @@ type devConfig struct {
ExtraTmpls string // Experimental flag for adding compiled templates, we'll likely replace this with a better mechanism ExtraTmpls string // Experimental flag for adding compiled templates, we'll likely replace this with a better mechanism
//QuicPort int // Experimental! //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 // configHolder is purely for having a big struct to unmarshal data into

View File

@ -10,7 +10,8 @@ import (
"sync/atomic" "sync/atomic"
"errors" "errors"
"net/http" "net/http"
"time" "io"
"io/ioutil"
c "github.com/Azareal/Gosora/common" c "github.com/Azareal/Gosora/common"
co "github.com/Azareal/Gosora/common/counters" co "github.com/Azareal/Gosora/common/counters"
@ -897,34 +898,6 @@ var markToID = map[string]int{
"safari":1, "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 // HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
type HTTPSRedirect struct {} type HTTPSRedirect struct {}
@ -953,6 +926,13 @@ func (r *GenRouter) SuspiciousRequest(req *http.Request, pre string) {
// TODO: SetDefaultPath // TODO: SetDefaultPath
// TODO: GetDefaultPath // TODO: GetDefaultPath
func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { 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) { malformedRequest := func(typ int) {
w.WriteHeader(200) // 400 w.WriteHeader(200) // 400
w.Write([]byte("")) w.Write([]byte(""))

View File

@ -12,8 +12,37 @@ import (
"time" "time"
c "github.com/Azareal/Gosora/common" 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 { type WriterIntercept struct {
http.ResponseWriter http.ResponseWriter
} }

View File

@ -432,7 +432,8 @@ import (
"sync/atomic" "sync/atomic"
"errors" "errors"
"net/http" "net/http"
"time" "io"
"io/ioutil"
c "github.com/Azareal/Gosora/common" c "github.com/Azareal/Gosora/common"
co "github.com/Azareal/Gosora/common/counters" co "github.com/Azareal/Gosora/common/counters"
@ -480,34 +481,6 @@ var markToID = map[string]int{ {{range $index, $el := .AllAgentMarkNames}}
"safari":1, "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 // HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
type HTTPSRedirect struct {} type HTTPSRedirect struct {}
@ -536,6 +509,13 @@ func (r *GenRouter) SuspiciousRequest(req *http.Request, pre string) {
// TODO: SetDefaultPath // TODO: SetDefaultPath
// TODO: GetDefaultPath // TODO: GetDefaultPath
func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { 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) { malformedRequest := func(typ int) {
w.WriteHeader(200) // 400 w.WriteHeader(200) // 400
w.Write([]byte("")) w.Write([]byte(""))