1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-05-06 04:36:09 +02:00

fs: Fix panic on indexing URL of zero length

This commit is contained in:
Adnan Maolood 2021-04-21 11:36:41 -04:00
parent e5cf345273
commit eb32c32063

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
}