aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.net>2025-07-17 23:02:36 +0100
committerLeonardo Bishop <me@leonardobishop.net>2025-07-17 23:02:36 +0100
commitd2f932f27e2009cf9a621520ad35598d9841fb03 (patch)
treeb82c66128ce1b5f5899aa25d5b178a65bda5f79e /pkg
parent912605ee9c19f9217438e4639fec04b0240642c1 (diff)
Fix SiteFileServer ServeHTTP implementation
Diffstat (limited to 'pkg')
-rw-r--r--pkg/site/fs.go36
1 files changed, 19 insertions, 17 deletions
diff --git a/pkg/site/fs.go b/pkg/site/fs.go
index 55abeac..6a3b1d6 100644
--- a/pkg/site/fs.go
+++ b/pkg/site/fs.go
@@ -29,38 +29,31 @@ func (fs *SiteFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := filepath.Clean(r.URL.Path)
+ var info os.FileInfo
file, err := fs.root.Open(path)
if err != nil {
if strings.HasSuffix(path, ".html") {
- w.WriteHeader(http.StatusNotFound)
- html.NotFoundUrlPage(path, fs.siteConfig.Host).Render(w)
- return
+ goto notFound
}
htmlPath := path + ".html"
file, err = fs.root.Open(htmlPath)
if err != nil {
- w.WriteHeader(http.StatusNotFound)
- html.NotFoundUrlPage(path, fs.siteConfig.Host).Render(w)
- return
+ goto notFound
}
}
defer file.Close()
- info, err := file.Stat()
+ info, err = file.Stat()
if err != nil {
- w.WriteHeader(http.StatusNotFound)
- html.NotFoundUrlPage(path, fs.siteConfig.Host).Render(w)
- return
+ goto notFound
}
if info.IsDir() {
indexPath := filepath.Join(path, "index.html")
- if _, err := fs.root.Open(indexPath); os.IsNotExist(err) {
+ if file, err = fs.root.Open(indexPath); os.IsNotExist(err) {
if fs.siteConfig.Flags&config.FlagIndex == 0 {
- w.WriteHeader(http.StatusNotFound)
- html.NotFoundUrlPage(path, fs.siteConfig.Host).Render(w)
- return
+ goto notFound
}
files, err := fs.listFiles(path)
if path != "/" {
@@ -74,10 +67,19 @@ func (fs *SiteFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
return
}
- http.ServeFile(w, r, indexPath)
- } else {
- http.ServeFile(w, r, path)
+
+ info, err = file.Stat()
+ if err != nil {
+ goto notFound
+ }
}
+
+ http.ServeContent(w, r, info.Name(), info.ModTime(), file)
+ return
+
+notFound:
+ w.WriteHeader(http.StatusNotFound)
+ html.NotFoundUrlPage(path, fs.siteConfig.Host).Render(w)
}
func (fs *SiteFileServer) listFiles(dir string) ([]html.File, error) {