api: fix error in case of nested paths (#2040)

* Fixed API paths/get with nested paths

* Lint error

* Same fix for /v2/hlsmuxers/get

---------

Co-authored-by: Jordy Boezaard <jordy@boezaard.com>
This commit is contained in:
Jordy84 2023-07-10 19:53:25 +02:00 committed by GitHub
parent 625e7da91d
commit 1103e935c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 14 deletions

View File

@ -229,11 +229,11 @@ func newAPI(
if !interfaceIsEmpty(a.hlsManager) {
group.GET("/v2/hlsmuxers/list", a.onHLSMuxersList)
group.GET("/v2/hlsmuxers/get/:name", a.onHLSMuxersGet)
group.GET("/v2/hlsmuxers/get/*name", a.onHLSMuxersGet)
}
group.GET("/v2/paths/list", a.onPathsList)
group.GET("/v2/paths/get/:name", a.onPathsGet)
group.GET("/v2/paths/get/*name", a.onPathsGet)
if !interfaceIsEmpty(a.rtspServer) {
group.GET("/v2/rtspconns/list", a.onRTSPConnsList)
@ -475,7 +475,14 @@ func (a *api) onPathsList(ctx *gin.Context) {
}
func (a *api) onPathsGet(ctx *gin.Context) {
data, err := a.pathManager.apiPathsGet(ctx.Param("name"))
name := ctx.Param("name")
if len(name) < 2 || name[0] != '/' {
ctx.AbortWithStatus(http.StatusBadRequest)
return
}
name = name[1:]
data, err := a.pathManager.apiPathsGet(name)
if err != nil {
abortWithError(ctx, err)
return
@ -771,7 +778,14 @@ func (a *api) onHLSMuxersList(ctx *gin.Context) {
}
func (a *api) onHLSMuxersGet(ctx *gin.Context) {
data, err := a.hlsManager.apiMuxersGet(ctx.Param("name"))
name := ctx.Param("name")
if len(name) < 2 || name[0] != '/' {
ctx.AbortWithStatus(http.StatusBadRequest)
return
}
name = name[1:]
data, err := a.hlsManager.apiMuxersGet(name)
if err != nil {
abortWithError(ctx, err)
return

View File

@ -440,12 +440,7 @@ func TestAPIPathsGet(t *testing.T) {
hc := &http.Client{Transport: &http.Transport{}}
source := gortsplib.Client{}
err := source.StartRecording("rtsp://localhost:8554/mypath", media.Medias{testMediaH264})
require.NoError(t, err)
defer source.Close()
for _, ca := range []string{"ok", "not found"} {
for _, ca := range []string{"ok", "ok-nested", "not found"} {
t.Run(ca, func(t *testing.T) {
type pathSource struct {
Type string `json:"type"`
@ -460,17 +455,26 @@ func TestAPIPathsGet(t *testing.T) {
}
var pathName string
if ca == "ok" {
switch ca {
case "ok":
pathName = "mypath"
} else {
case "ok-nested":
pathName = "my/nested/path"
case "not found":
pathName = "nonexisting"
}
if ca == "ok" {
if ca == "ok" || ca == "ok-nested" {
source := gortsplib.Client{}
err := source.StartRecording("rtsp://localhost:8554/"+pathName, media.Medias{testMediaH264})
require.NoError(t, err)
defer source.Close()
var out path
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/get/"+pathName, nil, &out)
require.Equal(t, path{
Name: "mypath",
Name: pathName,
Source: pathSource{
Type: "rtspSession",
},