summaryrefslogtreecommitdiffstats
path: root/commands/server.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-09-13 11:33:42 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-09-14 14:25:33 +0200
commita5cda5ca4dc9ced8179eb6bcccb1bbdc567afe17 (patch)
treea71f8bb6f4490ba65ff6ace20a205914de5e6350 /commands/server.go
parent5e2b28d6e64cfb5d45ad557e1482b63e4ec84292 (diff)
server: Add 404 support
Diffstat (limited to 'commands/server.go')
-rw-r--r--commands/server.go27
1 files changed, 23 insertions, 4 deletions
diff --git a/commands/server.go b/commands/server.go
index f082164ce..7689f03db 100644
--- a/commands/server.go
+++ b/commands/server.go
@@ -412,12 +412,18 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string
// See https://docs.netlify.com/routing/redirects/rewrites-proxies/
if !redirect.Force {
path := filepath.Clean(strings.TrimPrefix(requestURI, u.Path))
- fi, err := f.c.hugo().BaseFs.PublishFs.Stat(path)
+ if root != "" {
+ path = filepath.Join(root, path)
+ }
+ fs := f.c.publishDirServerFs
+
+ fi, err := fs.Stat(path)
+
if err == nil {
if fi.IsDir() {
// There will be overlapping directories, so we
// need to check for a file.
- _, err = f.c.hugo().BaseFs.PublishFs.Stat(filepath.Join(path, "index.html"))
+ _, err = fs.Stat(filepath.Join(path, "index.html"))
doRedirect = err != nil
} else {
doRedirect = false
@@ -426,15 +432,28 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string
}
if doRedirect {
- if redirect.Status == 200 {
+ switch redirect.Status {
+ case 404:
+ w.WriteHeader(404)
+ file, err := fs.Open(filepath.FromSlash(strings.TrimPrefix(redirect.To, u.Path)))
+ if err == nil {
+ defer file.Close()
+ io.Copy(w, file)
+ } else {
+ fmt.Fprintln(w, "<h1>Page Not Found</h1>")
+ }
+ return
+ case 200:
if r2 := f.rewriteRequest(r, strings.TrimPrefix(redirect.To, u.Path)); r2 != nil {
requestURI = redirect.To
r = r2
}
- } else {
+ fallthrough
+ default:
w.Header().Set("Content-Type", "")
http.Redirect(w, r, redirect.To, redirect.Status)
return
+
}
}