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
switch strings.ToLower(configYml.Listen.WebMethod) {
case "http":
webServer = &http.Server{
Handler: pageHandler.GetRouter(configYml),
ReadTimeout: configYml.Listen.ReadTimeout,
ReadHeaderTimeout: configYml.Listen.WriteTimeout,
}
webServer = &http.Server{Handler: pageHandler.GetRouter(configYml)}
go runBackgroundHttp(webServer, getListener(configYml, cwdDir), false)
case "fcgi":
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] {
case "tcp", "tcp4", "tcp6":
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":
socketPath := config.Listen.Web
if !filepath.IsAbs(socketPath) {

View File

@ -1,28 +1,8 @@
package conf
import "time"
type ListenYaml struct {
Web string `yaml:"web"`
WebMethod string `yaml:"webMethod"`
WebNetwork string `yaml:"webNetwork"`
ReadTimeout time.Duration `yaml:"readTimeout"`
WriteTimeout time.Duration `yaml:"writeTimeout"`
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
type ServeYaml struct {
DataStorage string `yaml:"dataStorage"`
Domains []string `yaml:"domains"`
RangeSupported bool `yaml:"rangeSupported"`
CacheSettings CacheSettingsYaml `yaml:"cacheSettings"`

View File

@ -24,6 +24,9 @@ func GetRouter(config conf.ConfigYaml) http.Handler {
}
theRouter.PathPrefix("/").HandlerFunc(domainNotAllowed)
}
if config.Listen.Identify {
theRouter.Use(headerMiddleware)
}
}
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 (
"golang.captainalm.com/cityuni-webserver/conf"
"net/http"
"net/url"
"strings"
"sync"
)
@ -24,7 +25,7 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
}
return &PageHandler{
PageContentsCache: thePCCMap,
PageProviders: GetProviders(config.CacheSettings.EnableTemplateCaching),
PageProviders: GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage),
pageContentsCacheRWMutex: theMutex,
RangeSupported: config.RangeSupported,
CacheSettings: config.CacheSettings,
@ -35,6 +36,34 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque
//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) {
if ph.CacheSettings.EnableContentsCaching {
if path == "" {

View File

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

View File

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