Skip to content

Commit

Permalink
api: return 404 in /get and /kick endpoints (#1994) (#1995)
Browse files Browse the repository at this point in the history
when an entity is not found
  • Loading branch information
aler9 authored Jun 27, 2023
1 parent 20a3b07 commit f1b9757
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 19 deletions.
30 changes: 30 additions & 0 deletions apidocs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ paths:
$ref: '#/components/schemas/HLSMuxer'
'400':
description: invalid request.
'404':
description: muxer not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -720,6 +722,8 @@ paths:
$ref: '#/components/schemas/Path'
'400':
description: invalid request.
'404':
description: path not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -774,6 +778,8 @@ paths:
$ref: '#/components/schemas/RTSPConn'
'400':
description: invalid request.
'404':
description: connection not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -828,6 +834,8 @@ paths:
$ref: '#/components/schemas/RTSPSession'
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand All @@ -848,6 +856,8 @@ paths:
description: the request was successful.
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -902,6 +912,8 @@ paths:
$ref: '#/components/schemas/RTSPConn'
'400':
description: invalid request.
'404':
description: connection not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -932,6 +944,8 @@ paths:
$ref: '#/components/schemas/RTSPSessionsList'
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand All @@ -956,6 +970,8 @@ paths:
$ref: '#/components/schemas/RTSPSession'
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand All @@ -976,6 +992,8 @@ paths:
description: the request was successful.
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -1030,6 +1048,8 @@ paths:
$ref: '#/components/schemas/RTMPConn'
'400':
description: invalid request.
'404':
description: connection not found.
'500':
description: internal server error.

Expand All @@ -1050,6 +1070,8 @@ paths:
description: the request was successful.
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -1104,6 +1126,8 @@ paths:
$ref: '#/components/schemas/RTMPConn'
'400':
description: invalid request.
'404':
description: connection not found.
'500':
description: internal server error.

Expand All @@ -1124,6 +1148,8 @@ paths:
description: the request was successful.
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand Down Expand Up @@ -1178,6 +1204,8 @@ paths:
$ref: '#/components/schemas/WebRTCSession'
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.

Expand All @@ -1198,5 +1226,7 @@ paths:
description: the request was successful.
'400':
description: invalid request.
'404':
description: session not found.
'500':
description: internal server error.
38 changes: 28 additions & 10 deletions internal/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"encoding/json"
"errors"
"net/http"
"reflect"
"strconv"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/bluenviron/mediamtx/internal/logger"
)

var errAPINotFound = errors.New("not found")

func interfaceIsEmpty(i interface{}) bool {
return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil()
}
Expand Down Expand Up @@ -128,6 +131,14 @@ func paginate(itemsPtr interface{}, itemsPerPageStr string, pageStr string) (int
return paginate2(itemsPtr, itemsPerPage, page), nil
}

func abortWithError(ctx *gin.Context, err error) {
if err == errAPINotFound {
ctx.AbortWithStatus(http.StatusNotFound)
} else {
ctx.AbortWithStatus(http.StatusInternalServerError)
}
}

type apiPathManager interface {
apiPathsList() (*apiPathsList, error)
apiPathsGet(string) (*apiPath, error)
Expand Down Expand Up @@ -422,13 +433,12 @@ func (a *api) onConfigPathsDelete(ctx *gin.Context) {
a.mutex.Lock()
defer a.mutex.Unlock()

newConf := a.conf.Clone()

if _, ok := newConf.Paths[name]; !ok {
ctx.AbortWithStatus(http.StatusBadRequest)
if _, ok := a.conf.Paths[name]; !ok {
ctx.AbortWithStatus(http.StatusNotFound)
return
}

newConf := a.conf.Clone()
delete(newConf.Paths, name)

err := newConf.Check()
Expand Down Expand Up @@ -467,11 +477,7 @@ func (a *api) onPathsList(ctx *gin.Context) {
func (a *api) onPathsGet(ctx *gin.Context) {
data, err := a.pathManager.apiPathsGet(ctx.Param("name"))
if err != nil {
if err.Error() == "not found" {
ctx.AbortWithStatus(http.StatusNotFound)
return
}
ctx.AbortWithStatus(http.StatusInternalServerError)
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -505,6 +511,7 @@ func (a *api) onRTSPConnsGet(ctx *gin.Context) {

data, err := a.rtspServer.apiConnsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -538,6 +545,7 @@ func (a *api) onRTSPSessionsGet(ctx *gin.Context) {

data, err := a.rtspServer.apiSessionsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand All @@ -553,6 +561,7 @@ func (a *api) onRTSPSessionsKick(ctx *gin.Context) {

err = a.rtspServer.apiSessionsKick(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -586,6 +595,7 @@ func (a *api) onRTSPSConnsGet(ctx *gin.Context) {

data, err := a.rtspsServer.apiConnsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -619,6 +629,7 @@ func (a *api) onRTSPSSessionsGet(ctx *gin.Context) {

data, err := a.rtspsServer.apiSessionsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand All @@ -634,6 +645,7 @@ func (a *api) onRTSPSSessionsKick(ctx *gin.Context) {

err = a.rtspsServer.apiSessionsKick(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -667,6 +679,7 @@ func (a *api) onRTMPConnsGet(ctx *gin.Context) {

data, err := a.rtmpServer.apiConnsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand All @@ -682,6 +695,7 @@ func (a *api) onRTMPConnsKick(ctx *gin.Context) {

err = a.rtmpServer.apiConnsKick(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -715,6 +729,7 @@ func (a *api) onRTMPSConnsGet(ctx *gin.Context) {

data, err := a.rtmpsServer.apiConnsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand All @@ -730,6 +745,7 @@ func (a *api) onRTMPSConnsKick(ctx *gin.Context) {

err = a.rtmpsServer.apiConnsKick(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -757,7 +773,7 @@ func (a *api) onHLSMuxersList(ctx *gin.Context) {
func (a *api) onHLSMuxersGet(ctx *gin.Context) {
data, err := a.hlsManager.apiMuxersGet(ctx.Param("name"))
if err != nil {
ctx.AbortWithStatus(http.StatusInternalServerError)
abortWithError(ctx, err)
return
}

Expand Down Expand Up @@ -791,6 +807,7 @@ func (a *api) onWebRTCSessionsGet(ctx *gin.Context) {

data, err := a.webRTCManager.apiSessionsGet(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand All @@ -806,6 +823,7 @@ func (a *api) onWebRTCSessionsKick(ctx *gin.Context) {

err = a.webRTCManager.apiSessionsKick(uuid)
if err != nil {
abortWithError(ctx, err)
return
}

Expand Down
Loading

0 comments on commit f1b9757

Please sign in to comment.