Handle trailing slashes in URL (#2988)
When a user includes a trailing slash in LIVEKIT_URL, it would produce double slashes in the path, i.e. `https://myhost.livekit.cloud//twirp/RoomService.ListRooms` Currently the server will send a 302 MOVED response, causing Twirp requests to fail. We now remove the double slash in front within the middleware.
This commit is contained in:
@@ -102,6 +102,7 @@ func NewLivekitServer(conf *config.Config,
|
|||||||
// allow preflight to be cached for a day
|
// allow preflight to be cached for a day
|
||||||
MaxAge: 86400,
|
MaxAge: 86400,
|
||||||
}),
|
}),
|
||||||
|
negroni.HandlerFunc(RemoveDoubleSlashes),
|
||||||
}
|
}
|
||||||
if keyProvider != nil {
|
if keyProvider != nil {
|
||||||
middlewares = append(middlewares, NewAPIKeyAuthMiddleware(keyProvider))
|
middlewares = append(middlewares, NewAPIKeyAuthMiddleware(keyProvider))
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
)
|
)
|
||||||
@@ -40,6 +41,13 @@ func boolValue(s string) bool {
|
|||||||
return s == "1" || s == "true"
|
return s == "1" || s == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveDoubleSlashes(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||||
|
if strings.HasPrefix(r.URL.Path, "//") {
|
||||||
|
r.URL.Path = r.URL.Path[1:]
|
||||||
|
}
|
||||||
|
next(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
func IsValidDomain(domain string) bool {
|
func IsValidDomain(domain string) bool {
|
||||||
domainRegexp := regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)
|
domainRegexp := regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)
|
||||||
return domainRegexp.MatchString(domain)
|
return domainRegexp.MatchString(domain)
|
||||||
|
|||||||
@@ -376,6 +376,20 @@ func TestSingleNodeCORS(t *testing.T) {
|
|||||||
require.Equal(t, "testhost.com", res.Header.Get("Access-Control-Allow-Origin"))
|
require.Equal(t, "testhost.com", res.Header.Get("Access-Control-Allow-Origin"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSingleNodeDoubleSlash(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.SkipNow()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s, finish := setupSingleNodeTest("TestSingleNodeDoubleSlash")
|
||||||
|
defer finish()
|
||||||
|
// client contains trailing slash in URL, causing path to contain double //
|
||||||
|
// without our middleware, this would cause a 302 redirect
|
||||||
|
roomClient = livekit.NewRoomServiceJSONClient(fmt.Sprintf("http://localhost:%d/", s.HTTPPort()), &http.Client{})
|
||||||
|
_, err := roomClient.ListRooms(contextWithToken(listRoomToken()), &livekit.ListRoomsRequest{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestPingPong(t *testing.T) {
|
func TestPingPong(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
|
|||||||
Reference in New Issue
Block a user