webrtc: restore compatibility when running behind a proxy, in a subpath (#2638)
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -34,20 +33,23 @@ var (
|
|||||||
reWHIPWHEPWithID = regexp.MustCompile("^/(.+?)/(whip|whep)/(.+?)$")
|
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) {
|
func webrtcWriteError(ctx *gin.Context, statusCode int, err error) {
|
||||||
ctx.JSON(statusCode, &defs.APIError{
|
ctx.JSON(statusCode, &defs.APIError{
|
||||||
Error: err.Error(),
|
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 {
|
type webRTCHTTPServerParent interface {
|
||||||
logger.Writer
|
logger.Writer
|
||||||
generateICEServers() ([]pwebrtc.ICEServer, error)
|
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("ID", res.sx.uuid.String())
|
||||||
ctx.Writer.Header().Set("Accept-Patch", "application/trickle-ice-sdpfrag")
|
ctx.Writer.Header().Set("Accept-Patch", "application/trickle-ice-sdpfrag")
|
||||||
ctx.Writer.Header()["Link"] = webrtc.LinkHeaderMarshal(servers)
|
ctx.Writer.Header()["Link"] = webrtc.LinkHeaderMarshal(servers)
|
||||||
ctx.Request.URL.Path += "/" + res.sx.secret.String()
|
ctx.Writer.Header().Set("Location", sessionLocation(publish, res.sx.secret))
|
||||||
ctx.Writer.Header().Set("Location", relativeLocation(ctx.Request.URL))
|
|
||||||
ctx.Writer.WriteHeader(http.StatusCreated)
|
ctx.Writer.WriteHeader(http.StatusCreated)
|
||||||
ctx.Writer.Write(res.answer)
|
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)
|
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] != '/':
|
case ctx.Request.URL.Path[len(ctx.Request.URL.Path)-1] != '/':
|
||||||
ctx.Request.URL.Path += "/"
|
ctx.Writer.Header().Set("Location", ctx.Request.URL.Path[1:]+"/")
|
||||||
ctx.Writer.Header().Set("Location", relativeLocation(ctx.Request.URL))
|
|
||||||
ctx.Writer.WriteHeader(http.StatusMovedPermanently)
|
ctx.Writer.WriteHeader(http.StatusMovedPermanently)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user