From 71f8aaaf16fed39f7f59a180a035783ba41c404c Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Sun, 23 Apr 2023 04:25:43 +0100 Subject: [PATCH] Minor changes to error page loading code --- error-pages/error-pages.go | 23 +++++++++++++++++++++-- servers/https.go | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/error-pages/error-pages.go b/error-pages/error-pages.go index c4531ae..1175f04 100644 --- a/error-pages/error-pages.go +++ b/error-pages/error-pages.go @@ -6,6 +6,8 @@ import ( "log" "net/http" "path/filepath" + "strconv" + "strings" "sync" ) @@ -82,8 +84,21 @@ func (e *ErrorPages) internalCompile(m map[int]func(rw http.ResponseWriter)) err // get file name and extension name := i.Name() ext := filepath.Ext(name) + + // if the extension is not 'html' then ignore the file if ext != "html" { - log.Printf("[ErrorPages] WARNING: non '.html' file in error pages directory: '%s'\n", name) + log.Printf("[ErrorPages] WARNING: ignoring non '.html' file in error pages directory: '%s'\n", name) + continue + } + + // if the name can't be + nameInt, err := strconv.Atoi(strings.TrimSuffix(name, ".html")) + if err != nil { + log.Printf("[ErrorPages] WARNING: ignoring invalid error page in error pages directory: '%s'\n", name) + continue + } + if nameInt < 100 || nameInt >= 600 { + log.Printf("[ErrorPages] WARNING: ignoring invalid error page in error pages directory must be 100-599: '%s'\n", name) continue } @@ -93,7 +108,11 @@ func (e *ErrorPages) internalCompile(m map[int]func(rw http.ResponseWriter)) err return fmt.Errorf("failed to read html file '%s': %w", name, err) } - // TODO: save to map + m[nameInt] = func(rw http.ResponseWriter) { + rw.Header().Set("Content-Type", "text/html; encoding=utf-8") + rw.WriteHeader(nameInt) + _, _ = rw.Write(htmlData) + } } // well no errors happened diff --git a/servers/https.go b/servers/https.go index 6b48e7d..e21f83b 100644 --- a/servers/https.go +++ b/servers/https.go @@ -22,8 +22,11 @@ func NewHttpsServer(conf *Conf) *http.Server { s := &http.Server{ Addr: conf.HttpsListen, Handler: setupRateLimiter(300).Middleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.Header().Set("Content-Type", "text/html") rw.WriteHeader(http.StatusNotImplemented) + _, _ = rw.Write([]byte("
"))
 			_, _ = rw.Write([]byte(fmt.Sprintf("%#v\n", req)))
+			_, _ = rw.Write([]byte("
")) _ = r // TODO: serve from router and proxy // r.ServeHTTP(rw, req)