mirror of
https://github.com/bluenviron/mediamtx.git
synced 2026-01-09 11:52:02 -08:00
This commit is contained in:
parent
4ccb245feb
commit
aade2eedb9
4 changed files with 50 additions and 10 deletions
|
|
@ -146,11 +146,7 @@ func (s *hlsHTTPServer) onRequest(ctx *gin.Context) {
|
|||
dir, fname = pa, ""
|
||||
|
||||
if !strings.HasSuffix(dir, "/") {
|
||||
l := ctx.Request.URL.Path[1:] + "/"
|
||||
if ctx.Request.URL.RawQuery != "" {
|
||||
l += "?" + ctx.Request.URL.RawQuery
|
||||
}
|
||||
ctx.Writer.Header().Set("Location", l)
|
||||
ctx.Writer.Header().Set("Location", httpserv.LocationWithTrailingSlash(ctx.Request.URL))
|
||||
ctx.Writer.WriteHeader(http.StatusMovedPermanently)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -352,11 +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] != '/':
|
||||
l := ctx.Request.URL.Path[1:] + "/"
|
||||
if ctx.Request.URL.RawQuery != "" {
|
||||
l += "?" + ctx.Request.URL.RawQuery
|
||||
}
|
||||
ctx.Writer.Header().Set("Location", l)
|
||||
ctx.Writer.Header().Set("Location", httpserv.LocationWithTrailingSlash(ctx.Request.URL))
|
||||
ctx.Writer.WriteHeader(http.StatusMovedPermanently)
|
||||
|
||||
default:
|
||||
|
|
|
|||
12
internal/protocols/httpserv/location_with_trailing_slash.go
Normal file
12
internal/protocols/httpserv/location_with_trailing_slash.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package httpserv
|
||||
|
||||
import "net/url"
|
||||
|
||||
// LocationWithTrailingSlash returns the URL in a relative format, with a trailing slash.
|
||||
func LocationWithTrailingSlash(u *url.URL) string {
|
||||
l := "./" + u.Path[1:] + "/"
|
||||
if u.RawQuery != "" {
|
||||
l += "?" + u.RawQuery
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package httpserv
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLocationWithTrailingSlash(t *testing.T) {
|
||||
for _, ca := range []struct {
|
||||
name string
|
||||
url *url.URL
|
||||
loc string
|
||||
}{
|
||||
{
|
||||
"with query",
|
||||
&url.URL{
|
||||
Path: "/test",
|
||||
RawQuery: "key=value",
|
||||
},
|
||||
"./test/?key=value",
|
||||
},
|
||||
{
|
||||
"xss",
|
||||
&url.URL{
|
||||
Path: "/www.example.com",
|
||||
},
|
||||
"./www.example.com/",
|
||||
},
|
||||
} {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
require.Equal(t, ca.loc, LocationWithTrailingSlash(ca.url))
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue