api: return 404 when an entity is not found (#2582) (#2920)

This commit is contained in:
Alessandro Ros 2024-01-18 23:28:56 +01:00 committed by GitHub
parent b3eaec50c1
commit 7b9617f2e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 269 additions and 41 deletions

View File

@ -808,6 +808,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: path not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -876,6 +882,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: path not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -910,6 +922,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: path not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -938,6 +956,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: path not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1008,6 +1032,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: muxer not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1078,6 +1108,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: path not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1148,6 +1184,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1218,6 +1260,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: session not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1246,6 +1294,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: session not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1316,6 +1370,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1386,6 +1446,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: session not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1414,6 +1480,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: session not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1484,6 +1556,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1512,6 +1590,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1582,6 +1666,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1610,6 +1700,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1680,6 +1776,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1708,6 +1810,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: connection not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1778,6 +1886,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: session not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:
@ -1806,6 +1920,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
'404':
description: session not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500': '500':
description: server error. description: server error.
content: content:

View File

@ -3,6 +3,7 @@ package api
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
@ -19,6 +20,11 @@ import (
"github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/protocols/httpserv" "github.com/bluenviron/mediamtx/internal/protocols/httpserv"
"github.com/bluenviron/mediamtx/internal/restrictnetwork" "github.com/bluenviron/mediamtx/internal/restrictnetwork"
"github.com/bluenviron/mediamtx/internal/servers/hls"
"github.com/bluenviron/mediamtx/internal/servers/rtmp"
"github.com/bluenviron/mediamtx/internal/servers/rtsp"
"github.com/bluenviron/mediamtx/internal/servers/srt"
"github.com/bluenviron/mediamtx/internal/servers/webrtc"
) )
func interfaceIsEmpty(i interface{}) bool { func interfaceIsEmpty(i interface{}) bool {
@ -264,7 +270,6 @@ func (a *API) Log(level logger.Level, format string, args ...interface{}) {
a.Parent.Log(level, "[API] "+format, args...) a.Parent.Log(level, "[API] "+format, args...)
} }
// error coming from something the user inserted into the request.
func (a *API) writeError(ctx *gin.Context, status int, err error) { func (a *API) writeError(ctx *gin.Context, status int, err error) {
// show error in logs // show error in logs
a.Log(logger.Error, err.Error()) a.Log(logger.Error, err.Error())
@ -385,7 +390,7 @@ func (a *API) onConfigPathsGet(ctx *gin.Context) {
p, ok := c.Paths[name] p, ok := c.Paths[name]
if !ok { if !ok {
a.writeError(ctx, http.StatusInternalServerError, fmt.Errorf("path configuration not found")) a.writeError(ctx, http.StatusNotFound, fmt.Errorf("path configuration not found"))
return return
} }
@ -450,7 +455,11 @@ func (a *API) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl
err = newConf.PatchPath(name, &p) err = newConf.PatchPath(name, &p)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusBadRequest, err) if errors.Is(err, conf.ErrPathNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusBadRequest, err)
}
return return
} }
@ -487,7 +496,11 @@ func (a *API) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl
err = newConf.ReplacePath(name, &p) err = newConf.ReplacePath(name, &p)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusBadRequest, err) if errors.Is(err, conf.ErrPathNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusBadRequest, err)
}
return return
} }
@ -517,7 +530,11 @@ func (a *API) onConfigPathsDelete(ctx *gin.Context) {
err := newConf.RemovePath(name) err := newConf.RemovePath(name)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusBadRequest, err) if errors.Is(err, conf.ErrPathNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusBadRequest, err)
}
return return
} }
@ -560,7 +577,11 @@ func (a *API) onPathsGet(ctx *gin.Context) {
data, err := a.PathManager.APIPathsGet(name) data, err := a.PathManager.APIPathsGet(name)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, conf.ErrPathNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -594,7 +615,11 @@ func (a *API) onRTSPConnsGet(ctx *gin.Context) {
data, err := a.RTSPServer.APIConnsGet(uuid) data, err := a.RTSPServer.APIConnsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtsp.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -628,7 +653,11 @@ func (a *API) onRTSPSessionsGet(ctx *gin.Context) {
data, err := a.RTSPServer.APISessionsGet(uuid) data, err := a.RTSPServer.APISessionsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtsp.ErrSessionNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -644,7 +673,11 @@ func (a *API) onRTSPSessionsKick(ctx *gin.Context) {
err = a.RTSPServer.APISessionsKick(uuid) err = a.RTSPServer.APISessionsKick(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtsp.ErrSessionNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -678,7 +711,11 @@ func (a *API) onRTSPSConnsGet(ctx *gin.Context) {
data, err := a.RTSPSServer.APIConnsGet(uuid) data, err := a.RTSPSServer.APIConnsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtsp.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -712,7 +749,11 @@ func (a *API) onRTSPSSessionsGet(ctx *gin.Context) {
data, err := a.RTSPSServer.APISessionsGet(uuid) data, err := a.RTSPSServer.APISessionsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtsp.ErrSessionNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -728,7 +769,11 @@ func (a *API) onRTSPSSessionsKick(ctx *gin.Context) {
err = a.RTSPSServer.APISessionsKick(uuid) err = a.RTSPSServer.APISessionsKick(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtsp.ErrSessionNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -762,7 +807,11 @@ func (a *API) onRTMPConnsGet(ctx *gin.Context) {
data, err := a.RTMPServer.APIConnsGet(uuid) data, err := a.RTMPServer.APIConnsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtmp.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -778,7 +827,11 @@ func (a *API) onRTMPConnsKick(ctx *gin.Context) {
err = a.RTMPServer.APIConnsKick(uuid) err = a.RTMPServer.APIConnsKick(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtmp.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -812,7 +865,11 @@ func (a *API) onRTMPSConnsGet(ctx *gin.Context) {
data, err := a.RTMPSServer.APIConnsGet(uuid) data, err := a.RTMPSServer.APIConnsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtmp.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -828,7 +885,11 @@ func (a *API) onRTMPSConnsKick(ctx *gin.Context) {
err = a.RTMPSServer.APIConnsKick(uuid) err = a.RTMPSServer.APIConnsKick(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, rtmp.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -862,7 +923,11 @@ func (a *API) onHLSMuxersGet(ctx *gin.Context) {
data, err := a.HLSServer.APIMuxersGet(name) data, err := a.HLSServer.APIMuxersGet(name)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, hls.ErrMuxerNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -896,7 +961,11 @@ func (a *API) onWebRTCSessionsGet(ctx *gin.Context) {
data, err := a.WebRTCServer.APISessionsGet(uuid) data, err := a.WebRTCServer.APISessionsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, webrtc.ErrSessionNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -912,7 +981,11 @@ func (a *API) onWebRTCSessionsKick(ctx *gin.Context) {
err = a.WebRTCServer.APISessionsKick(uuid) err = a.WebRTCServer.APISessionsKick(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, webrtc.ErrSessionNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -946,7 +1019,11 @@ func (a *API) onSRTConnsGet(ctx *gin.Context) {
data, err := a.SRTServer.APIConnsGet(uuid) data, err := a.SRTServer.APIConnsGet(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, srt.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }
@ -962,7 +1039,11 @@ func (a *API) onSRTConnsKick(ctx *gin.Context) {
err = a.SRTServer.APIConnsKick(uuid) err = a.SRTServer.APIConnsKick(uuid)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusInternalServerError, err) if errors.Is(err, srt.ErrConnNotFound) {
a.writeError(ctx, http.StatusNotFound, err)
} else {
a.writeError(ctx, http.StatusInternalServerError, err)
}
return return
} }

View File

@ -4,6 +4,7 @@ package conf
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
@ -21,6 +22,9 @@ import (
"github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/logger"
) )
// ErrPathNotFound is returned when a path is not found.
var ErrPathNotFound = errors.New("path not found")
func sortedKeys(paths map[string]*OptionalPath) []string { func sortedKeys(paths map[string]*OptionalPath) []string {
ret := make([]string, len(paths)) ret := make([]string, len(paths))
i := 0 i := 0
@ -531,7 +535,7 @@ func (conf *Conf) AddPath(name string, p *OptionalPath) error {
func (conf *Conf) PatchPath(name string, optional2 *OptionalPath) error { func (conf *Conf) PatchPath(name string, optional2 *OptionalPath) error {
optional, ok := conf.OptionalPaths[name] optional, ok := conf.OptionalPaths[name]
if !ok { if !ok {
return fmt.Errorf("path not found") return ErrPathNotFound
} }
copyStructFields(optional.Values, optional2.Values) copyStructFields(optional.Values, optional2.Values)
@ -542,7 +546,7 @@ func (conf *Conf) PatchPath(name string, optional2 *OptionalPath) error {
func (conf *Conf) ReplacePath(name string, optional2 *OptionalPath) error { func (conf *Conf) ReplacePath(name string, optional2 *OptionalPath) error {
_, ok := conf.OptionalPaths[name] _, ok := conf.OptionalPaths[name]
if !ok { if !ok {
return fmt.Errorf("path not found") return ErrPathNotFound
} }
conf.OptionalPaths[name] = optional2 conf.OptionalPaths[name] = optional2
@ -552,7 +556,7 @@ func (conf *Conf) ReplacePath(name string, optional2 *OptionalPath) error {
// RemovePath removes a path. // RemovePath removes a path.
func (conf *Conf) RemovePath(name string) error { func (conf *Conf) RemovePath(name string) error {
if _, ok := conf.OptionalPaths[name]; !ok { if _, ok := conf.OptionalPaths[name]; !ok {
return fmt.Errorf("path not found") return ErrPathNotFound
} }
delete(conf.OptionalPaths, name) delete(conf.OptionalPaths, name)

View File

@ -374,7 +374,7 @@ func TestAPIConfigPathsDelete(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer res.Body.Close() defer res.Body.Close()
require.Equal(t, http.StatusInternalServerError, res.StatusCode) require.Equal(t, http.StatusNotFound, res.StatusCode)
checkError(t, "path configuration not found", res.Body) checkError(t, "path configuration not found", res.Body)
}() }()
} }
@ -630,7 +630,7 @@ func TestAPIPathsGet(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer res.Body.Close() defer res.Body.Close()
require.Equal(t, http.StatusInternalServerError, res.StatusCode) require.Equal(t, http.StatusNotFound, res.StatusCode)
checkError(t, "path not found", res.Body) checkError(t, "path not found", res.Body)
} }
}) })
@ -1317,7 +1317,7 @@ func TestAPIProtocolGetNotFound(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer res.Body.Close() defer res.Body.Close()
require.Equal(t, http.StatusInternalServerError, res.StatusCode) require.Equal(t, http.StatusNotFound, res.StatusCode)
switch ca { switch ca {
case "rtsp conns", "rtsps conns", "rtmp", "rtmps", "srt": case "rtsp conns", "rtsps conns", "rtmp", "rtmps", "srt":
@ -1541,7 +1541,7 @@ func TestAPIProtocolKickNotFound(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer res.Body.Close() defer res.Body.Close()
require.Equal(t, http.StatusInternalServerError, res.StatusCode) require.Equal(t, http.StatusNotFound, res.StatusCode)
switch ca { switch ca {
case "rtsp conns", "rtsps conns", "rtmp", "rtmps", "srt": case "rtsp conns", "rtsps conns", "rtmp", "rtmps", "srt":

View File

@ -388,7 +388,7 @@ func (pm *pathManager) doAPIPathsList(req pathAPIPathsListReq) {
func (pm *pathManager) doAPIPathsGet(req pathAPIPathsGetReq) { func (pm *pathManager) doAPIPathsGet(req pathAPIPathsGetReq) {
path, ok := pm.paths[req.name] path, ok := pm.paths[req.name]
if !ok { if !ok {
req.res <- pathAPIPathsGetRes{err: fmt.Errorf("path not found")} req.res <- pathAPIPathsGetRes{err: conf.ErrPathNotFound}
return return
} }

View File

@ -3,6 +3,7 @@ package hls
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sort" "sort"
"sync" "sync"
@ -12,6 +13,9 @@ import (
"github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/logger"
) )
// ErrMuxerNotFound is returned when a muxer is not found.
var ErrMuxerNotFound = errors.New("muxer not found")
type serverAPIMuxersListRes struct { type serverAPIMuxersListRes struct {
data *defs.APIHLSMuxerList data *defs.APIHLSMuxerList
err error err error
@ -179,7 +183,7 @@ outer:
case req := <-s.chAPIMuxerGet: case req := <-s.chAPIMuxerGet:
muxer, ok := s.muxers[req.name] muxer, ok := s.muxers[req.name]
if !ok { if !ok {
req.res <- serverAPIMuxersGetRes{err: fmt.Errorf("muxer not found")} req.res <- serverAPIMuxersGetRes{err: ErrMuxerNotFound}
continue continue
} }

View File

@ -4,6 +4,7 @@ package rtmp
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net" "net"
"sort" "sort"
@ -18,6 +19,9 @@ import (
"github.com/bluenviron/mediamtx/internal/restrictnetwork" "github.com/bluenviron/mediamtx/internal/restrictnetwork"
) )
// ErrConnNotFound is returned when a connection is not found.
var ErrConnNotFound = errors.New("connection not found")
type serverAPIConnsListRes struct { type serverAPIConnsListRes struct {
data *defs.APIRTMPConnList data *defs.APIRTMPConnList
err error err error
@ -196,7 +200,7 @@ outer:
case req := <-s.chAPIConnsGet: case req := <-s.chAPIConnsGet:
c := s.findConnByUUID(req.uuid) c := s.findConnByUUID(req.uuid)
if c == nil { if c == nil {
req.res <- serverAPIConnsGetRes{err: fmt.Errorf("connection not found")} req.res <- serverAPIConnsGetRes{err: ErrConnNotFound}
continue continue
} }
@ -205,7 +209,7 @@ outer:
case req := <-s.chAPIConnsKick: case req := <-s.chAPIConnsKick:
c := s.findConnByUUID(req.uuid) c := s.findConnByUUID(req.uuid)
if c == nil { if c == nil {
req.res <- serverAPIConnsKickRes{err: fmt.Errorf("connection not found")} req.res <- serverAPIConnsKickRes{err: ErrConnNotFound}
continue continue
} }

View File

@ -4,6 +4,7 @@ package rtsp
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
@ -22,6 +23,12 @@ import (
"github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/logger"
) )
// ErrConnNotFound is returned when a connection is not found.
var ErrConnNotFound = errors.New("connection not found")
// ErrSessionNotFound is returned when a session is not found.
var ErrSessionNotFound = errors.New("session not found")
func printAddresses(srv *gortsplib.Server) string { func printAddresses(srv *gortsplib.Server) string {
var ret []string var ret []string
@ -356,7 +363,7 @@ func (s *Server) APIConnsGet(uuid uuid.UUID) (*defs.APIRTSPConn, error) {
conn := s.findConnByUUID(uuid) conn := s.findConnByUUID(uuid)
if conn == nil { if conn == nil {
return nil, fmt.Errorf("connection not found") return nil, ErrConnNotFound
} }
return conn.apiItem(), nil return conn.apiItem(), nil
@ -401,7 +408,7 @@ func (s *Server) APISessionsGet(uuid uuid.UUID) (*defs.APIRTSPSession, error) {
_, sx := s.findSessionByUUID(uuid) _, sx := s.findSessionByUUID(uuid)
if sx == nil { if sx == nil {
return nil, fmt.Errorf("session not found") return nil, ErrSessionNotFound
} }
return sx.apiItem(), nil return sx.apiItem(), nil
@ -420,7 +427,7 @@ func (s *Server) APISessionsKick(uuid uuid.UUID) error {
key, sx := s.findSessionByUUID(uuid) key, sx := s.findSessionByUUID(uuid)
if sx == nil { if sx == nil {
return fmt.Errorf("session not found") return ErrSessionNotFound
} }
sx.Close() sx.Close()

View File

@ -3,6 +3,7 @@ package srt
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sort" "sort"
"sync" "sync"
@ -17,6 +18,9 @@ import (
"github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/logger"
) )
// ErrConnNotFound is returned when a connection is not found.
var ErrConnNotFound = errors.New("connection not found")
func srtMaxPayloadSize(u int) int { func srtMaxPayloadSize(u int) int {
return ((u - 16) / 188) * 188 // 16 = SRT header, 188 = MPEG-TS packet return ((u - 16) / 188) * 188 // 16 = SRT header, 188 = MPEG-TS packet
} }
@ -189,7 +193,7 @@ outer:
case req := <-s.chAPIConnsGet: case req := <-s.chAPIConnsGet:
c := s.findConnByUUID(req.uuid) c := s.findConnByUUID(req.uuid)
if c == nil { if c == nil {
req.res <- serverAPIConnsGetRes{err: fmt.Errorf("connection not found")} req.res <- serverAPIConnsGetRes{err: ErrConnNotFound}
continue continue
} }
@ -198,7 +202,7 @@ outer:
case req := <-s.chAPIConnsKick: case req := <-s.chAPIConnsKick:
c := s.findConnByUUID(req.uuid) c := s.findConnByUUID(req.uuid)
if c == nil { if c == nil {
req.res <- serverAPIConnsKickRes{err: fmt.Errorf("connection not found")} req.res <- serverAPIConnsKickRes{err: ErrConnNotFound}
continue continue
} }

View File

@ -7,6 +7,7 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha1" "crypto/sha1"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -33,6 +34,9 @@ const (
webrtcPayloadMaxSize = 1188 // 1200 - 12 (RTP header) webrtcPayloadMaxSize = 1188 // 1200 - 12 (RTP header)
) )
// ErrSessionNotFound is returned when a session is not found.
var ErrSessionNotFound = errors.New("session not found")
type nilWriter struct{} type nilWriter struct{}
func (nilWriter) Write(p []byte) (int, error) { func (nilWriter) Write(p []byte) (int, error) {
@ -334,7 +338,7 @@ outer:
case req := <-s.chAddSessionCandidates: case req := <-s.chAddSessionCandidates:
sx, ok := s.sessionsBySecret[req.secret] sx, ok := s.sessionsBySecret[req.secret]
if !ok { if !ok {
req.res <- webRTCAddSessionCandidatesRes{err: fmt.Errorf("session not found")} req.res <- webRTCAddSessionCandidatesRes{err: ErrSessionNotFound}
continue continue
} }
@ -343,7 +347,7 @@ outer:
case req := <-s.chDeleteSession: case req := <-s.chDeleteSession:
sx, ok := s.sessionsBySecret[req.secret] sx, ok := s.sessionsBySecret[req.secret]
if !ok { if !ok {
req.res <- webRTCDeleteSessionRes{err: fmt.Errorf("session not found")} req.res <- webRTCDeleteSessionRes{err: ErrSessionNotFound}
continue continue
} }
@ -371,7 +375,7 @@ outer:
case req := <-s.chAPISessionsGet: case req := <-s.chAPISessionsGet:
sx := s.findSessionByUUID(req.uuid) sx := s.findSessionByUUID(req.uuid)
if sx == nil { if sx == nil {
req.res <- serverAPISessionsGetRes{err: fmt.Errorf("session not found")} req.res <- serverAPISessionsGetRes{err: ErrSessionNotFound}
continue continue
} }
@ -380,7 +384,7 @@ outer:
case req := <-s.chAPIConnsKick: case req := <-s.chAPIConnsKick:
sx := s.findSessionByUUID(req.uuid) sx := s.findSessionByUUID(req.uuid)
if sx == nil { if sx == nil {
req.res <- serverAPISessionsKickRes{err: fmt.Errorf("session not found")} req.res <- serverAPISessionsKickRes{err: ErrSessionNotFound}
continue continue
} }