From 7eac2c0c07840f24e3e235ee1172e46d06bd233e Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sun, 21 Mar 2021 17:08:29 +0000 Subject: [PATCH] fup/fuphttp: add badRequest handler We might get bad requests for e.g. file uploads, so we should have an error handler for that. This also disables mime type sniffing for the other text/plain error handler. --- web/fup/fuphttp/fuphttp.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/web/fup/fuphttp/fuphttp.go b/web/fup/fuphttp/fuphttp.go index 9405e6e995..f921114c7a 100644 --- a/web/fup/fuphttp/fuphttp.go +++ b/web/fup/fuphttp/fuphttp.go @@ -83,11 +83,19 @@ func (a *Application) notFound(rw http.ResponseWriter, r *http.Request) { } func (a *Application) internalError(rw http.ResponseWriter, r *http.Request) { - rw.Header().Set("Content-type", "text/plain") + rw.Header().Set("Content-type", "text/plain; charset=utf-8") + rw.Header().Set("X-Content-Type-Options", "nosniff") rw.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(rw, "hammed server :(\n") } +func (a *Application) badRequest(rw http.ResponseWriter, r *http.Request, err error) { + rw.Header().Set("Content-type", "text/plain; charset=utf-8") + rw.Header().Set("X-Content-Type-Options", "nosniff") + rw.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(rw, "bad request: %v\n", err.Error()) +} + func parseTemplate(t *template.Template, fsys fs.FS, name string) (*template.Template, error) { bs, err := fs.ReadFile(fsys, name) if err != nil {