feat: x-www-form-urlencoded support

This commit is contained in:
klutrem
2025-03-06 16:03:44 +03:00
parent 39c14e6556
commit b8390331af
+31 -2
View File
@@ -1,9 +1,11 @@
package webrtc package webrtc
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -62,8 +64,8 @@ func syncHandler(w http.ResponseWriter, r *http.Request) {
// 2. application/sdp - receive/response SDP via WebRTC-HTTP Egress Protocol (WHEP) // 2. application/sdp - receive/response SDP via WebRTC-HTTP Egress Protocol (WHEP)
// 3. other - receive/response raw SDP // 3. other - receive/response raw SDP
func outputWebRTC(w http.ResponseWriter, r *http.Request) { func outputWebRTC(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("src") uri := r.URL.Query().Get("src")
stream := streams.Get(url) stream := streams.Get(uri)
if stream == nil { if stream == nil {
http.Error(w, api.StreamNotFound, http.StatusNotFound) http.Error(w, api.StreamNotFound, http.StatusNotFound)
return return
@@ -87,6 +89,28 @@ func outputWebRTC(w http.ResponseWriter, r *http.Request) {
} }
offer = desc.SDP offer = desc.SDP
case "application/x-www-form-urlencoded":
body, err := io.ReadAll(r.Body)
if err != nil {
log.Error().Err(err).Caller().Send()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
values, err := url.ParseQuery(string(body))
if err != nil {
log.Error().Err(err).Caller().Send()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
encodedOffer := values.Get("data")
decodedOffer, err := base64.StdEncoding.DecodeString(encodedOffer)
if err != nil {
log.Error().Err(err).Caller().Send()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
offer = string(decodedOffer)
default: default:
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
@@ -124,6 +148,11 @@ func outputWebRTC(w http.ResponseWriter, r *http.Request) {
} }
err = json.NewEncoder(w).Encode(v) err = json.NewEncoder(w).Encode(v)
case "application/x-www-form-urlencoded":
w.Header().Set("Content-Type", mediaType)
encodedAnswer := base64.StdEncoding.EncodeToString([]byte(answer))
_, err = w.Write([]byte(encodedAnswer))
case MimeSDP: case MimeSDP:
w.Header().Set("Content-Type", mediaType) w.Header().Set("Content-Type", mediaType)
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)