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"
"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

View File

@ -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("<pre>"))
_, _ = rw.Write([]byte(fmt.Sprintf("%#v\n", req)))
_, _ = rw.Write([]byte("</pre>"))
_ = r
// TODO: serve from router and proxy
// r.ServeHTTP(rw, req)