aboutsummaryrefslogtreecommitdiff
path: root/fs.go
diff options
context:
space:
mode:
authorAdnan Maolood <[email protected]>2021-04-21 11:36:41 -0400
committerAdnan Maolood <[email protected]>2021-04-21 11:36:43 -0400
commiteb32c320639f0984949501dd1dafffd89e85add4 (patch)
tree8f560f15109a4b1bd0ddbdfd068098d053441334 /fs.go
parentUpdate README.md (diff)
downloadgo-gemini-eb32c320639f0984949501dd1dafffd89e85add4.tar.xz
go-gemini-eb32c320639f0984949501dd1dafffd89e85add4.zip
fs: Fix panic on indexing URL of zero length
Diffstat (limited to 'fs.go')
-rw-r--r--fs.go6
1 files changed, 3 insertions, 3 deletions
diff --git a/fs.go b/fs.go
index 7bdf43b..1a734b5 100644
--- a/fs.go
+++ b/fs.go
@@ -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
}