flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit f0ec25f3b1de5cba69cf8ed647c9391c9b7ea81d
parent 2ed447ea6a2a629e27d47f88c3770886ba75e962
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  5 Dec 2020 16:25:17 -0800

Refactor serve file

Diffstat:
Mhttp.go | 18+++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/http.go b/http.go @@ -436,18 +436,19 @@ func getFavicon(user string) string { // 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 @@ func userFile(w http.ResponseWriter, r *http.Request) { 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 {