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:
David Zhao
2024-09-07 11:30:24 -07:00
committed by GitHub
parent e8fcbefcec
commit 50576b503e
3 changed files with 23 additions and 0 deletions
+1
View File
@@ -102,6 +102,7 @@ func NewLivekitServer(conf *config.Config,
// allow preflight to be cached for a day
MaxAge: 86400,
}),
negroni.HandlerFunc(RemoveDoubleSlashes),
}
if keyProvider != nil {
middlewares = append(middlewares, NewAPIKeyAuthMiddleware(keyProvider))
+8
View File
@@ -20,6 +20,7 @@ import (
"net"
"net/http"
"regexp"
"strings"
"github.com/livekit/protocol/logger"
)
@@ -40,6 +41,13 @@ func boolValue(s string) bool {
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 {
domainRegexp := regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)
return domainRegexp.MatchString(domain)