all repos — flounder @ f0ec25f3b1de5cba69cf8ed647c9391c9b7ea81d

A small site builder for the Gemini protocol

Refactor serve file
alex wennerberg alex@alexwennerberg.com
Sat, 05 Dec 2020 16:25:17 -0800
commit

f0ec25f3b1de5cba69cf8ed647c9391c9b7ea81d

parent

2ed447ea6a2a629e27d47f88c3770886ba75e962

1 files changed, 9 insertions(+), 9 deletions(-)

jump to
M http.gohttp.go

@@ -436,18 +436,19 @@

// Server a user's file func userFile(w http.ResponseWriter, r *http.Request) { userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary - query := r.URL.Query() p := filepath.Clean(r.URL.Path) var isDir bool fileName := path.Join(c.FilesDirectory, userName, p) - stat, _ := os.Stat(fileName) + stat, err := os.Stat(fileName) if stat != nil { isDir = stat.IsDir() } if p == "/" || isDir { fileName = path.Join(fileName, "index.gmi") - } else if strings.HasPrefix(p, "/.hidden") { - renderDefaultError(w, http.StatusNotFound) + } + + if strings.HasPrefix(p, "/.hidden") { + renderDefaultError(w, http.StatusForbidden) return } if r.URL.Path == "/style.css" {

@@ -455,19 +456,18 @@ http.ServeFile(w, r, path.Join(c.TemplatesDirectory, "static/style.css"))

return } - _, err := os.Stat(fileName) - if err != nil { + _, err = os.Stat(fileName) + if os.IsNotExist(err) { renderDefaultError(w, http.StatusNotFound) return } + // Dumb content negotiation extension := path.Ext(fileName) - _, raw := query["raw"] + _, raw := r.URL.Query()["raw"] acceptsGemini := strings.Contains(r.Header.Get("Accept"), "text/gemini") if !raw && !acceptsGemini && (extension == ".gmi" || extension == ".gemini") { - file, _ := os.Open(fileName) - htmlString := textToHTML(gmi.ParseText(file)) favicon := getFavicon(userName) data := struct {