1
0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-09-18 13:31:36 +02:00

fs: Fix panic on indexing URL of zero length

This commit is contained in:
adnano 2021-04-21 11:36:41 -04:00
parent 8c54df59bb
commit 08fac3bf87

6
fs.go
View File

@ -131,13 +131,13 @@ func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect b
url := r.URL.Path
if stat.IsDir() {
// Add trailing slash
if url[len(url)-1] != '/' {
if len(url) != 0 && url[len(url)-1] != '/' {
w.WriteHeader(StatusPermanentRedirect, path.Base(url)+"/")
return
}
} else {
// Remove trailing slash
if url[len(url)-1] == '/' {
if len(url) != 0 && url[len(url)-1] == '/' {
w.WriteHeader(StatusPermanentRedirect, "../"+path.Base(url))
return
}
@ -147,7 +147,7 @@ func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect b
if stat.IsDir() {
// Redirect if the directory name doesn't end in a slash
url := r.URL.Path
if url[len(url)-1] != '/' {
if len(url) != 0 && url[len(url)-1] != '/' {
w.WriteHeader(StatusRedirect, path.Base(url)+"/")
return
}