From f3ceb0503375dbf02d8f9e6ece3a3ace1ebe7b9f Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 9 May 2024 17:43:18 +0200 Subject: [PATCH] Add Accept header to 415 replies. Also fix return code in WHIP. --- webserver/api.go | 3 +++ webserver/whip.go | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/webserver/api.go b/webserver/api.go index 8dc1c38..61461e4 100644 --- a/webserver/api.go +++ b/webserver/api.go @@ -75,6 +75,7 @@ func sendJSON(w http.ResponseWriter, r *http.Request, v any) { func getText(w http.ResponseWriter, r *http.Request) ([]byte, bool) { ctype := parseContentType(r.Header.Get("Content-Type")) if !strings.EqualFold(ctype, "text/plain") { + w.Header().Set("Accept", "text/plain") http.Error(w, "unsupported content type", http.StatusUnsupportedMediaType) return nil, true @@ -92,6 +93,7 @@ func getText(w http.ResponseWriter, r *http.Request) ([]byte, bool) { func getJSON(w http.ResponseWriter, r *http.Request, v any) bool { ctype := parseContentType(r.Header.Get("Content-Type")) if !strings.EqualFold(ctype, "application/json") { + w.Header().Set("Accept", "application/json") http.Error(w, "unsupported content type", http.StatusUnsupportedMediaType) return true @@ -458,6 +460,7 @@ func keysHandler(w http.ResponseWriter, r *http.Request, g string) { // cannot use getJSON due to the weird content-type ctype := parseContentType(r.Header.Get("Content-Type")) if !strings.EqualFold(ctype, "application/jwk-set+json") { + w.Header().Set("Accept", "application/jwk-set+json") http.Error(w, "unsupported content type", http.StatusUnsupportedMediaType) return diff --git a/webserver/whip.go b/webserver/whip.go index 5d7f5a9..2838375 100644 --- a/webserver/whip.go +++ b/webserver/whip.go @@ -192,7 +192,9 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) { ctype := r.Header.Get("content-type") if !strings.EqualFold(ctype, "application/sdp") { - http.Error(w, "bad content type", http.StatusBadRequest) + w.Header().Set("Accept", "application/sdp") + http.Error(w, "bad content type", + http.StatusUnsupportedMediaType) return } @@ -337,7 +339,9 @@ func whipResourceHandler(w http.ResponseWriter, r *http.Request) { ctype := r.Header.Get("content-type") if !strings.EqualFold(ctype, "application/trickle-ice-sdpfrag") { - http.Error(w, "bad content type", http.StatusBadRequest) + w.Header().Set("Accept", "application/trickle-ice-sdpfrag") + http.Error(w, "bad content type", + http.StatusUnsupportedMediaType) return }