mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 09:38:15 +03:00
don't serve static file when requested as dir, and fix 500 internal server errors when a file below such a file-as-dir is requested
e.g. when /index.html/ returned content. and /index.html/image.png would result in 500 internal server errors. now they all return 404 not found.
This commit is contained in:
parent
c5fdb7309f
commit
f531a9bf35
@ -1,6 +1,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
htmltemplate "html/template"
|
htmltemplate "html/template"
|
||||||
"io"
|
"io"
|
||||||
@ -11,6 +12,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mjl-/mox/config"
|
"github.com/mjl-/mox/config"
|
||||||
@ -160,6 +162,8 @@ func HandleStatic(h *config.WebStatic, w http.ResponseWriter, r *http.Request) (
|
|||||||
} else {
|
} else {
|
||||||
fspath = filepath.Join(h.Root, r.URL.Path)
|
fspath = filepath.Join(h.Root, r.URL.Path)
|
||||||
}
|
}
|
||||||
|
// fspath will not have a trailing slash anymore, we'll correct for it
|
||||||
|
// later when the path turns out to be file instead of a directory.
|
||||||
|
|
||||||
serveFile := func(name string, mtime time.Time, content *os.File) {
|
serveFile := func(name string, mtime time.Time, content *os.File) {
|
||||||
// ServeContent only sets a content-type if not already present in the response headers.
|
// ServeContent only sets a content-type if not already present in the response headers.
|
||||||
@ -172,7 +176,7 @@ func HandleStatic(h *config.WebStatic, w http.ResponseWriter, r *http.Request) (
|
|||||||
|
|
||||||
f, err := os.Open(fspath)
|
f, err := os.Open(fspath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) || errors.Is(err, syscall.ENOTDIR) {
|
||||||
if h.ContinueNotFound {
|
if h.ContinueNotFound {
|
||||||
// We haven't handled this request, try a next WebHandler in the list.
|
// We haven't handled this request, try a next WebHandler in the list.
|
||||||
return false
|
return false
|
||||||
@ -215,6 +219,12 @@ func HandleStatic(h *config.WebStatic, w http.ResponseWriter, r *http.Request) (
|
|||||||
if fi.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
|
if fi.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
|
||||||
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
||||||
return true
|
return true
|
||||||
|
} else if !fi.IsDir() && strings.HasSuffix(r.URL.Path, "/") {
|
||||||
|
if h.ContinueNotFound {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user