From 57822ea89da0bd360b20683e8291078184fd501a Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 10 Nov 2020 23:11:50 -0500 Subject: [PATCH] relax form-data requiremnt for api --- docs/changelog.txt | 2 ++ docs/honk.3 | 2 +- web.go | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 67ddb37..54dd101 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -2,6 +2,8 @@ changelog === next ++ Relax requirement for multipart/form-data posts in API. + + Dedupe blob file data. - Custom lingo for those who don't like honking. diff --git a/docs/honk.3 b/docs/honk.3 index 84d1e65..5144ec1 100644 --- a/docs/honk.3 +++ b/docs/honk.3 @@ -67,7 +67,7 @@ The .Fa action value should be .Dq honk . -Content type must be multipart/form-data. +Content type should be multipart/form-data if an attachment is included. The following values are recognized: .Bl -tag -width placename .It Fa noise diff --git a/web.go b/web.go index ec14167..069ca5e 100644 --- a/web.go +++ b/web.go @@ -1418,12 +1418,16 @@ func canedithonk(user *WhatAbout, honk *Honk) bool { } func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) { + if !strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "multipart/form-data") { + return nil, nil + } file, filehdr, err := r.FormFile("donk") if err != nil { - if err != http.ErrMissingFile { - log.Printf("error reading donk: %s", err) - http.Error(w, "error reading donk", http.StatusUnsupportedMediaType) + if err == http.ErrMissingFile { + return nil, nil } + log.Printf("error reading donk: %s", err) + http.Error(w, "error reading donk", http.StatusUnsupportedMediaType) return nil, err } var buf bytes.Buffer @@ -2236,9 +2240,11 @@ func apihandler(w http.ResponseWriter, r *http.Request) { case "donk": d, err := submitdonk(w, r) if err != nil { - if err == http.ErrMissingFile { - http.Error(w, "missing donk", http.StatusBadRequest) - } + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if d == nil { + http.Error(w, "missing donk", http.StatusBadRequest) return } w.Write([]byte(d.XID))