flounder

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

commit b18554dca962686cbb07d660ec3bf2875bcd8f84
parent 543b6a470d38804179b76ecdc947a3d969c279a1
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Fri, 23 Oct 2020 22:55:19 -0700

convert user gemini content

Diffstat:
Mhttp.go | 25++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/http.go b/http.go @@ -1,9 +1,11 @@ package main import ( + "git.sr.ht/~adnano/gmi" "html/template" "log" "net/http" + "os" "path" "strings" ) @@ -139,9 +141,26 @@ func registerHandler(w http.ResponseWriter, r *http.Request) { func userFile(w http.ResponseWriter, r *http.Request) { userName := strings.Split(r.Host, ".")[0] fileName := path.Join(c.FilesDirectory, userName, r.URL.Path) - // if gemini -- parse, convert, serve - // else - http.ServeFile(w, r, fileName) + extension := path.Ext(fileName) + log.Println(extension) + if extension == ".gmi" || extension == ".gemini" { + if strings.Contains(fileName, "..") { + // prevent directory traversal TODO verify + http.Error(w, "invalid URL path", http.StatusBadRequest) + } else { + // covert to html + stat, _ := os.Stat(fileName) + file, _ := os.Open(fileName) + htmlString := gmi.Parse(file).HTML() + log.Println(htmlString) + reader := strings.NewReader(htmlString) + w.Header().Set("Content-Type", "text/html") + http.ServeContent(w, r, fileName, stat.ModTime(), reader) + } + // TODO clean + } else { + http.ServeFile(w, r, fileName) + } } func runHTTPServer() {