From 5184d9337fa265e602abc23f343f8780bccf51d9 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Thu, 8 Dec 2022 20:38:02 +0000 Subject: [PATCH] cmd/web: write URL to client instead of forwarding client Signed-off-by: Xe Iaso --- cmd/web/main.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cmd/web/main.go b/cmd/web/main.go index a8f3c6d..ff9d5cc 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -141,7 +141,15 @@ WHERE p.id = ?1` return } - err = r.ParseMultipartForm(formDataLimit) + if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data;") { + err = r.ParseMultipartForm(formDataLimit) + } else if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { + err = r.ParseForm() + } else { + log.Printf("%s: unknown content type: %s", r.RemoteAddr, r.Header.Get("Content-Type")) + http.Error(w, "bad content-type, should be a form", http.StatusBadRequest) + return + } if err != nil { log.Printf("%s: bad form: %v", r.RemoteAddr, err) http.Error(w, err.Error(), http.StatusBadRequest) @@ -186,7 +194,10 @@ VALUES return } - http.Redirect(w, r, "/paste/"+id, http.StatusTemporaryRedirect) + log.Printf("new paste: %s", id) + + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, "http://%s/paste/%s", r.Host, id) }) log.Printf("listening on http://%s", *hostname)