relax form-data requiremnt for api
Ted Unangst tedu@tedunangst.com
Tue, 10 Nov 2020 23:11:50 -0500
3 files changed,
15 insertions(+),
7 deletions(-)
M
docs/changelog.txt
→
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.
M
docs/honk.3
→
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
M
web.go
→
web.go
@@ -1418,12 +1418,16 @@ return true
} 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 @@ w.Write([]byte(h.XID))
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))