Fix up page handling.
All checks were successful
continuous-integration/drone/push Build is passing

Remove timeout support.
Add identification middleware.
This commit is contained in:
Captain ALM 2022-07-15 10:46:45 +01:00
parent a61c28bc5b
commit 98dac85215
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
9 changed files with 53 additions and 88 deletions

View File

@ -80,11 +80,7 @@ func main() {
var fcgiListen net.Listener var fcgiListen net.Listener
switch strings.ToLower(configYml.Listen.WebMethod) { switch strings.ToLower(configYml.Listen.WebMethod) {
case "http": case "http":
webServer = &http.Server{ webServer = &http.Server{Handler: pageHandler.GetRouter(configYml)}
Handler: pageHandler.GetRouter(configYml),
ReadTimeout: configYml.Listen.ReadTimeout,
ReadHeaderTimeout: configYml.Listen.WriteTimeout,
}
go runBackgroundHttp(webServer, getListener(configYml, cwdDir), false) go runBackgroundHttp(webServer, getListener(configYml, cwdDir), false)
case "fcgi": case "fcgi":
fcgiListen = getListener(configYml, cwdDir) fcgiListen = getListener(configYml, cwdDir)

View File

@ -1,49 +0,0 @@
package main
import (
"net"
"time"
)
type tListener struct {
net.Listener
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func (l *tListener) Accept() (net.Conn, error) {
c, err := l.Listener.Accept()
if err != nil {
return nil, err
}
tc := &tConn{
Conn: c,
ReadTimeout: l.ReadTimeout,
WriteTimeout: l.WriteTimeout,
}
return tc, nil
}
type tConn struct {
net.Conn
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func (c *tConn) Read(b []byte) (n int, err error) {
err = c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
if err != nil {
return 0, err
}
n, err = c.Conn.Read(b)
return
}
func (c *tConn) Write(b []byte) (n int, err error) {
err = c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
if err != nil {
return
}
n, err = c.Conn.Write(b)
return
}

View File

@ -31,13 +31,6 @@ func getListener(config conf.ConfigYaml, cwd string) net.Listener {
switch split[0] { switch split[0] {
case "tcp", "tcp4", "tcp6": case "tcp", "tcp4", "tcp6":
theListener, theError = net.Listen(strings.ToLower(config.Listen.WebNetwork), config.Listen.Web) theListener, theError = net.Listen(strings.ToLower(config.Listen.WebNetwork), config.Listen.Web)
if theError == nil && strings.ToLower(config.Listen.WebMethod) == "fcgi" {
theListener = &tListener{
Listener: theListener,
ReadTimeout: config.Listen.ReadTimeout,
WriteTimeout: config.Listen.WriteTimeout,
}
}
case "unix", "unixgram", "unixpacket": case "unix", "unixgram", "unixpacket":
socketPath := config.Listen.Web socketPath := config.Listen.Web
if !filepath.IsAbs(socketPath) { if !filepath.IsAbs(socketPath) {

View File

@ -1,28 +1,8 @@
package conf package conf
import "time"
type ListenYaml struct { type ListenYaml struct {
Web string `yaml:"web"` Web string `yaml:"web"`
WebMethod string `yaml:"webMethod"` WebMethod string `yaml:"webMethod"`
WebNetwork string `yaml:"webNetwork"` WebNetwork string `yaml:"webNetwork"`
ReadTimeout time.Duration `yaml:"readTimeout"`
WriteTimeout time.Duration `yaml:"writeTimeout"`
Identify bool `yaml:"identify"` Identify bool `yaml:"identify"`
} }
func (ly ListenYaml) GetReadTimeout() time.Duration {
if ly.ReadTimeout.Seconds() < 1 {
return 1 * time.Second
} else {
return ly.ReadTimeout
}
}
func (ly ListenYaml) GetWriteTimeout() time.Duration {
if ly.WriteTimeout.Seconds() < 1 {
return 1 * time.Second
} else {
return ly.WriteTimeout
}
}

View File

@ -1,6 +1,7 @@
package conf package conf
type ServeYaml struct { type ServeYaml struct {
DataStorage string `yaml:"dataStorage"`
Domains []string `yaml:"domains"` Domains []string `yaml:"domains"`
RangeSupported bool `yaml:"rangeSupported"` RangeSupported bool `yaml:"rangeSupported"`
CacheSettings CacheSettingsYaml `yaml:"cacheSettings"` CacheSettings CacheSettingsYaml `yaml:"cacheSettings"`

View File

@ -24,6 +24,9 @@ func GetRouter(config conf.ConfigYaml) http.Handler {
} }
theRouter.PathPrefix("/").HandlerFunc(domainNotAllowed) theRouter.PathPrefix("/").HandlerFunc(domainNotAllowed)
} }
if config.Listen.Identify {
theRouter.Use(headerMiddleware)
}
} }
return theRouter return theRouter
} }
@ -40,3 +43,12 @@ func domainNotAllowed(rw http.ResponseWriter, req *http.Request) {
} }
} }
} }
func headerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "Clerie Gilbert")
w.Header().Set("X-Powered-By", "Love")
w.Header().Set("X-Friendly", "True")
next.ServeHTTP(w, r)
})
}

View File

@ -3,6 +3,7 @@ package pageHandler
import ( import (
"golang.captainalm.com/cityuni-webserver/conf" "golang.captainalm.com/cityuni-webserver/conf"
"net/http" "net/http"
"net/url"
"strings" "strings"
"sync" "sync"
) )
@ -24,7 +25,7 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
} }
return &PageHandler{ return &PageHandler{
PageContentsCache: thePCCMap, PageContentsCache: thePCCMap,
PageProviders: GetProviders(config.CacheSettings.EnableTemplateCaching), PageProviders: GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage),
pageContentsCacheRWMutex: theMutex, pageContentsCacheRWMutex: theMutex,
RangeSupported: config.RangeSupported, RangeSupported: config.RangeSupported,
CacheSettings: config.CacheSettings, CacheSettings: config.CacheSettings,
@ -35,6 +36,34 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque
//Provide processing for requests using providers //Provide processing for requests using providers
} }
func (ph *PageHandler) GetCleanQuery(request *http.Request) url.Values {
toClean := request.URL.Query()
provider := ph.PageProviders[request.URL.Path]
if provider == nil {
return make(url.Values)
}
supportedKeys := provider.GetSupportedURLParameters()
toDelete := make([]string, len(toClean))
theSize := 0
for s := range toClean {
noExist := true
for _, key := range supportedKeys {
if s == key {
noExist = false
break
}
}
if noExist {
toDelete[theSize] = s
theSize++
}
}
for i := 0; i < theSize; i++ {
delete(toClean, toDelete[i])
}
return toClean
}
func (ph *PageHandler) PurgeContentsCache(path string, query string) { func (ph *PageHandler) PurgeContentsCache(path string, query string) {
if ph.CacheSettings.EnableContentsCaching { if ph.CacheSettings.EnableContentsCaching {
if path == "" { if path == "" {

View File

@ -1,7 +1,10 @@
package pageHandler package pageHandler
import "net/url"
type PageProvider interface { type PageProvider interface {
GetPath() string GetPath() string
GetContents(urlParameters map[string]string) (contentType string, contents []byte) GetSupportedURLParameters() []string
GetContents(urlParameters url.Values) (contentType string, contents []byte)
PurgeTemplate() PurgeTemplate()
} }

View File

@ -2,7 +2,7 @@ package pageHandler
var providers map[string]PageProvider var providers map[string]PageProvider
func GetProviders(cacheTemplates bool) map[string]PageProvider { func GetProviders(cacheTemplates bool, dataStorage string) map[string]PageProvider {
if providers == nil { if providers == nil {
providers = make(map[string]PageProvider) providers = make(map[string]PageProvider)
//Add the providers in the pages sub package //Add the providers in the pages sub package