serve/httplib: Write the template to a buffer to catch render errors

Fixes #3559
This commit is contained in:
Fionera 2019-09-22 22:31:11 +02:00 committed by Nick Craig-Wood
parent 28cc2009d4
commit 49d6d6425c

View File

@ -1,6 +1,7 @@
package serve package serve
import ( import (
"bytes"
"fmt" "fmt"
"html/template" "html/template"
"net/http" "net/http"
@ -65,11 +66,13 @@ func (d *Directory) AddEntry(remote string, isDir bool) {
}) })
} }
// Error returns an http.StatusInternalServerError and logs the error // Error logs the error and if a ResponseWriter is given it writes a http.StatusInternalServerError
func Error(what interface{}, w http.ResponseWriter, text string, err error) { func Error(what interface{}, w http.ResponseWriter, text string, err error) {
fs.CountError(err) fs.CountError(err)
fs.Errorf(what, "%s: %v", text, err) fs.Errorf(what, "%s: %v", text, err)
http.Error(w, text+".", http.StatusInternalServerError) if w != nil {
http.Error(w, text+".", http.StatusInternalServerError)
}
} }
// Serve serves a directory // Serve serves a directory
@ -80,9 +83,14 @@ func (d *Directory) Serve(w http.ResponseWriter, r *http.Request) {
fs.Infof(d.DirRemote, "%s: Serving directory", r.RemoteAddr) fs.Infof(d.DirRemote, "%s: Serving directory", r.RemoteAddr)
err := d.HTMLTemplate.Execute(w, d) buf := &bytes.Buffer{}
err := d.HTMLTemplate.Execute(buf, d)
if err != nil { if err != nil {
Error(d.DirRemote, w, "Failed to render template", err) Error(d.DirRemote, w, "Failed to render template", err)
return return
} }
_, err = buf.WriteTo(w)
if err != nil {
Error(d.DirRemote, nil, "Failed to drain template buffer", err)
}
} }