From 7ffc431c9895c22e730d61c7628da2ae899012f4 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 3 Nov 2023 17:30:42 +0100 Subject: [PATCH] webrtc: restore compatibility when running behind a proxy, in a subpath --- internal/core/webrtc_http_server.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/core/webrtc_http_server.go b/internal/core/webrtc_http_server.go index 8119c77faff..d6fa55ac6fd 100644 --- a/internal/core/webrtc_http_server.go +++ b/internal/core/webrtc_http_server.go @@ -6,7 +6,6 @@ import ( "io" "net" "net/http" - "net/url" "regexp" "strings" "time" @@ -34,20 +33,23 @@ var ( reWHIPWHEPWithID = regexp.MustCompile("^/(.+?)/(whip|whep)/(.+?)$") ) -func relativeLocation(u *url.URL) string { - p := u.Path - if u.RawQuery != "" { - p += "?" + u.RawQuery - } - return p -} - func webrtcWriteError(ctx *gin.Context, statusCode int, err error) { ctx.JSON(statusCode, &defs.APIError{ Error: err.Error(), }) } +func sessionLocation(publish bool, secret uuid.UUID) string { + ret := "" + if publish { + ret += "whip" + } else { + ret += "whep" + } + ret += "/" + secret.String() + return ret +} + type webRTCHTTPServerParent interface { logger.Writer generateICEServers() ([]pwebrtc.ICEServer, error) @@ -222,8 +224,7 @@ func (s *webRTCHTTPServer) onWHIPPost(ctx *gin.Context, path string, publish boo 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.Request.URL.Path += "/" + res.sx.secret.String() - ctx.Writer.Header().Set("Location", relativeLocation(ctx.Request.URL)) + ctx.Writer.Header().Set("Location", sessionLocation(publish, res.sx.secret)) ctx.Writer.WriteHeader(http.StatusCreated) ctx.Writer.Write(res.answer) } @@ -351,8 +352,7 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) { s.onPage(ctx, ctx.Request.URL.Path[1:len(ctx.Request.URL.Path)-len("/publish")], true) case ctx.Request.URL.Path[len(ctx.Request.URL.Path)-1] != '/': - ctx.Request.URL.Path += "/" - ctx.Writer.Header().Set("Location", relativeLocation(ctx.Request.URL)) + ctx.Writer.Header().Set("Location", ctx.Request.URL.Path[1:]+"/") ctx.Writer.WriteHeader(http.StatusMovedPermanently) default: