diff options
| author | Leonardo Bishop <me@leonardobishop.net> | 2025-07-17 23:02:36 +0100 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.net> | 2025-07-17 23:02:36 +0100 |
| commit | d2f932f27e2009cf9a621520ad35598d9841fb03 (patch) | |
| tree | b82c66128ce1b5f5899aa25d5b178a65bda5f79e /pkg/site/fs.go | |
| parent | 912605ee9c19f9217438e4639fec04b0240642c1 (diff) | |
Fix SiteFileServer ServeHTTP implementation
Diffstat (limited to 'pkg/site/fs.go')
| -rw-r--r-- | pkg/site/fs.go | 36 |
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) { |
