From c7497bb22fd476765472637754f7645224d478c1 Mon Sep 17 00:00:00 2001 From: neilyoung Date: Sat, 6 Apr 2024 18:10:40 +0200 Subject: [PATCH 1/3] Update http_server.go This PR addresses a problem with DELETE (missing FQDN, discussed here https://github.com/bluenviron/mediamtx/issues/3177) and fixes some CORS issues, which prevented this client to operate properly in trickle-ICE mode https://github.com/Eyevinn/whip --- internal/servers/webrtc/http_server.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/servers/webrtc/http_server.go b/internal/servers/webrtc/http_server.go index 95bddf2b483..f55673e1d09 100644 --- a/internal/servers/webrtc/http_server.go +++ b/internal/servers/webrtc/http_server.go @@ -158,8 +158,8 @@ func (s *httpServer) onWHIPOptions(ctx *gin.Context, path string, publish bool) } ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH, DELETE") - ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, If-Match") - ctx.Writer.Header().Set("Access-Control-Expose-Headers", "Link") + ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, If-Match, Access-Control-Allow-Methods, ETag") + ctx.Writer.Header().Set("Access-Control-Expose-Headers", "Link, Access-Control-Allow-Methods, ETag") ctx.Writer.Header()["Link"] = webrtc.LinkHeaderMarshal(servers) ctx.Writer.WriteHeader(http.StatusNoContent) } @@ -198,12 +198,16 @@ func (s *httpServer) onWHIPPost(ctx *gin.Context, path string, publish bool) { } ctx.Writer.Header().Set("Content-Type", "application/sdp") - ctx.Writer.Header().Set("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location") + ctx.Writer.Header().Set("Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location, Access-Control-Allow-Methods") ctx.Writer.Header().Set("ETag", "*") ctx.Writer.Header().Set("ID", res.sx.uuid.String()) ctx.Writer.Header().Set("Accept-Patch", "application/trickle-ice-sdpfrag") ctx.Writer.Header()["Link"] = webrtc.LinkHeaderMarshal(servers) - ctx.Writer.Header().Set("Location", sessionLocation(publish, res.sx.secret)) + scheme := ctx.Request.URL.Scheme + if scheme == "" { + scheme = "http" + } + ctx.Writer.Header().Set("Location", scheme + "://" + ctx.Request.Host + ctx.Request.URL.Path + "/" + res.sx.secret.String()) ctx.Writer.WriteHeader(http.StatusCreated) ctx.Writer.Write(res.answer) } @@ -285,7 +289,7 @@ func (s *httpServer) onRequest(ctx *gin.Context) { if ctx.Request.Method == http.MethodOptions && ctx.Request.Header.Get("Access-Control-Request-Method") != "" { ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH, DELETE") - ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, If-Match") + ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, If-Match, Access-Control-Allow-Methods, ETag") ctx.Writer.WriteHeader(http.StatusNoContent) return } From abe2c91dc4d66fa232aa12cb28efbd11966effb3 Mon Sep 17 00:00:00 2001 From: neilyoung Date: Tue, 9 Apr 2024 09:43:22 +0200 Subject: [PATCH 2/3] Update http_server.go Added NGINX detection in order to provide the proper scheme --- internal/servers/webrtc/http_server.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/servers/webrtc/http_server.go b/internal/servers/webrtc/http_server.go index f55673e1d09..2ab5fb92a77 100644 --- a/internal/servers/webrtc/http_server.go +++ b/internal/servers/webrtc/http_server.go @@ -203,10 +203,14 @@ func (s *httpServer) onWHIPPost(ctx *gin.Context, path string, publish bool) { ctx.Writer.Header().Set("ID", res.sx.uuid.String()) ctx.Writer.Header().Set("Accept-Patch", "application/trickle-ice-sdpfrag") ctx.Writer.Header()["Link"] = webrtc.LinkHeaderMarshal(servers) - scheme := ctx.Request.URL.Scheme + scheme := ctx.Request.URL.Scheme if scheme == "" { scheme = "http" } + // Nginx detection + if ctx.Request.Header.Get("X-Forwarded-Proto") == "https" { + scheme = "https" + } ctx.Writer.Header().Set("Location", scheme + "://" + ctx.Request.Host + ctx.Request.URL.Path + "/" + res.sx.secret.String()) ctx.Writer.WriteHeader(http.StatusCreated) ctx.Writer.Write(res.answer) From 2cec9d747cfcf3370c7d7909420e06ab91e638b7 Mon Sep 17 00:00:00 2001 From: neilyoung Date: Tue, 9 Apr 2024 14:34:16 +0200 Subject: [PATCH 3/3] Update http_server.go An even easier approach to solve this problem after discussion --- internal/servers/webrtc/http_server.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/internal/servers/webrtc/http_server.go b/internal/servers/webrtc/http_server.go index 2ab5fb92a77..708927fd1c3 100644 --- a/internal/servers/webrtc/http_server.go +++ b/internal/servers/webrtc/http_server.go @@ -203,15 +203,7 @@ func (s *httpServer) onWHIPPost(ctx *gin.Context, path string, publish bool) { ctx.Writer.Header().Set("ID", res.sx.uuid.String()) ctx.Writer.Header().Set("Accept-Patch", "application/trickle-ice-sdpfrag") ctx.Writer.Header()["Link"] = webrtc.LinkHeaderMarshal(servers) - scheme := ctx.Request.URL.Scheme - if scheme == "" { - scheme = "http" - } - // Nginx detection - if ctx.Request.Header.Get("X-Forwarded-Proto") == "https" { - scheme = "https" - } - ctx.Writer.Header().Set("Location", scheme + "://" + ctx.Request.Host + ctx.Request.URL.Path + "/" + res.sx.secret.String()) + ctx.Writer.Header().Set("Location", ctx.Request.URL.Path + "/" + res.sx.secret.String()) ctx.Writer.WriteHeader(http.StatusCreated) ctx.Writer.Write(res.answer) }