From 98dac85215987c0d93c44729aa87b784df245295 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 15 Jul 2022 10:46:45 +0100 Subject: [PATCH] Fix up page handling. Remove timeout support. Add identification middleware. --- cmd/wappcityuni/main.go | 6 +--- cmd/wappcityuni/timeout-listener.go | 49 ----------------------------- cmd/wappcityuni/utils.go | 7 ----- conf/listen.go | 28 +++-------------- conf/serve.go | 1 + pageHandler/get-router.go | 12 +++++++ pageHandler/page-handler.go | 31 +++++++++++++++++- pageHandler/page-provider.go | 5 ++- pageHandler/pages.go | 2 +- 9 files changed, 53 insertions(+), 88 deletions(-) delete mode 100644 cmd/wappcityuni/timeout-listener.go diff --git a/cmd/wappcityuni/main.go b/cmd/wappcityuni/main.go index e9f86de..cbe62fa 100644 --- a/cmd/wappcityuni/main.go +++ b/cmd/wappcityuni/main.go @@ -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) diff --git a/cmd/wappcityuni/timeout-listener.go b/cmd/wappcityuni/timeout-listener.go deleted file mode 100644 index d870bf6..0000000 --- a/cmd/wappcityuni/timeout-listener.go +++ /dev/null @@ -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 -} diff --git a/cmd/wappcityuni/utils.go b/cmd/wappcityuni/utils.go index f75cb23..e7a0cd8 100644 --- a/cmd/wappcityuni/utils.go +++ b/cmd/wappcityuni/utils.go @@ -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) { diff --git a/conf/listen.go b/conf/listen.go index 9ef1795..04ad4ce 100644 --- a/conf/listen.go +++ b/conf/listen.go @@ -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 - } + Web string `yaml:"web"` + WebMethod string `yaml:"webMethod"` + WebNetwork string `yaml:"webNetwork"` + Identify bool `yaml:"identify"` } diff --git a/conf/serve.go b/conf/serve.go index 2cab102..19fb367 100644 --- a/conf/serve.go +++ b/conf/serve.go @@ -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"` diff --git a/pageHandler/get-router.go b/pageHandler/get-router.go index 804b5f5..09289c1 100644 --- a/pageHandler/get-router.go +++ b/pageHandler/get-router.go @@ -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) + }) +} diff --git a/pageHandler/page-handler.go b/pageHandler/page-handler.go index 7d311be..1e8603d 100644 --- a/pageHandler/page-handler.go +++ b/pageHandler/page-handler.go @@ -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 == "" { diff --git a/pageHandler/page-provider.go b/pageHandler/page-provider.go index d8ebb44..b3bbe32 100644 --- a/pageHandler/page-provider.go +++ b/pageHandler/page-provider.go @@ -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() } diff --git a/pageHandler/pages.go b/pageHandler/pages.go index 32bd7b6..7178013 100644 --- a/pageHandler/pages.go +++ b/pageHandler/pages.go @@ -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