Minor changes to error page loading code

This commit is contained in:
Melon 2023-04-23 04:25:43 +01:00
parent 61097981d6
commit 71f8aaaf16
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 24 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import (
"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv"
"strings"
"sync" "sync"
) )
@ -82,8 +84,21 @@ func (e *ErrorPages) internalCompile(m map[int]func(rw http.ResponseWriter)) err
// get file name and extension // get file name and extension
name := i.Name() name := i.Name()
ext := filepath.Ext(name) ext := filepath.Ext(name)
// if the extension is not 'html' then ignore the file
if ext != "html" { 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 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) 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 // well no errors happened

View File

@ -22,8 +22,11 @@ func NewHttpsServer(conf *Conf) *http.Server {
s := &http.Server{ s := &http.Server{
Addr: conf.HttpsListen, Addr: conf.HttpsListen,
Handler: setupRateLimiter(300).Middleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { 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.WriteHeader(http.StatusNotImplemented)
_, _ = rw.Write([]byte("<pre>"))
_, _ = rw.Write([]byte(fmt.Sprintf("%#v\n", req))) _, _ = rw.Write([]byte(fmt.Sprintf("%#v\n", req)))
_, _ = rw.Write([]byte("</pre>"))
_ = r _ = r
// TODO: serve from router and proxy // TODO: serve from router and proxy
// r.ServeHTTP(rw, req) // r.ServeHTTP(rw, req)