77 "fmt"
88 "io"
99 "log"
10+ "mime"
1011 "net/http"
1112 "os"
1213 "path"
@@ -76,8 +77,23 @@ func logHTTPError(w http.ResponseWriter, err string, code int) {
7677 http .Error (w , err , code )
7778}
7879
80+ func patchHandler (res http.ResponseWriter , r * http.Request , sessionId , body string , isWHIP bool ) {
81+ mediaType , _ , err := mime .ParseMediaType (r .Header .Get ("Content-Type" ))
82+ if err != nil || mediaType != "application/trickle-ice-sdpfrag" {
83+ logHTTPError (res , "invalid content type" , http .StatusUnsupportedMediaType )
84+ return
85+ }
86+
87+ if err = webrtc .HandlePatch (sessionId , body , isWHIP ); err != nil {
88+ logHTTPError (res , err .Error (), http .StatusBadRequest )
89+ return
90+ }
91+
92+ res .WriteHeader (http .StatusNoContent )
93+ }
94+
7995func whipHandler (res http.ResponseWriter , r * http.Request ) {
80- if r .Method != "POST" {
96+ if r .Method != "POST" && r . Method != "PATCH" {
8197 return
8298 }
8399
@@ -87,13 +103,18 @@ func whipHandler(res http.ResponseWriter, r *http.Request) {
87103 return
88104 }
89105
90- offer , err := io .ReadAll (r .Body )
106+ body , err := io .ReadAll (r .Body )
91107 if err != nil {
92108 logHTTPError (res , err .Error (), http .StatusBadRequest )
93109 return
94110 }
95111
96- answer , err := webrtc .WHIP (string (offer ), streamKey )
112+ if r .Method == "PATCH" {
113+ patchHandler (res , r , streamKey , string (body ), true )
114+ return
115+ }
116+
117+ answer , err := webrtc .WHIP (string (body ), streamKey )
97118 if err != nil {
98119 logHTTPError (res , err .Error (), http .StatusBadRequest )
99120 return
@@ -108,7 +129,7 @@ func whipHandler(res http.ResponseWriter, r *http.Request) {
108129}
109130
110131func whepHandler (res http.ResponseWriter , req * http.Request ) {
111- if req .Method != "POST" {
132+ if req .Method != "POST" && req . Method != "PATCH" {
112133 return
113134 }
114135
@@ -118,13 +139,18 @@ func whepHandler(res http.ResponseWriter, req *http.Request) {
118139 return
119140 }
120141
121- offer , err := io .ReadAll (req .Body )
142+ body , err := io .ReadAll (req .Body )
122143 if err != nil {
123144 logHTTPError (res , err .Error (), http .StatusBadRequest )
124145 return
125146 }
126147
127- answer , whepSessionId , err := webrtc .WHEP (string (offer ), streamKey )
148+ if req .Method == "PATCH" {
149+ patchHandler (res , req , "TODO" , string (body ), true )
150+ return
151+ }
152+
153+ answer , whepSessionId , err := webrtc .WHEP (string (body ), streamKey )
128154 if err != nil {
129155 logHTTPError (res , err .Error (), http .StatusBadRequest )
130156 return
@@ -133,7 +159,7 @@ func whepHandler(res http.ResponseWriter, req *http.Request) {
133159 apiPath := req .Host + strings .TrimSuffix (req .URL .RequestURI (), "whep" )
134160 res .Header ().Add ("Link" , `<` + apiPath + "sse/" + whepSessionId + `>; rel="urn:ietf:params:whep:ext:core:server-sent-events"; events="layers"` )
135161 res .Header ().Add ("Link" , `<` + apiPath + "layer/" + whepSessionId + `>; rel="urn:ietf:params:whep:ext:core:layer"` )
136- res .Header ().Add ("Location" , "/api/whep" )
162+ res .Header ().Add ("Location" , "/api/whep/" + whepSessionId )
137163 res .Header ().Add ("Content-Type" , "application/sdp" )
138164 res .WriteHeader (http .StatusCreated )
139165 if _ , err = fmt .Fprint (res , answer ); err != nil {
0 commit comments