From 8c06c9a4140fbc37bda40baa8c38f73df9f62e84 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 5 Sep 2020 20:53:56 +0200 Subject: [PATCH] support query parameteres when publishing or reading (#74) --- client.go | 16 ++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- main_test.go | 33 +++++++++++++++++++++++++++++++++ utils.go | 9 +++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 6c614e93..a03d8593 100644 --- a/client.go +++ b/client.go @@ -357,6 +357,12 @@ func (c *client) handleRequest(req *gortsplib.Request) error { } pathName = pathName[1:] // strip leading slash + // in RTSP, the control path is inserted after the query. + // therefore, path and query can't be threated separately + if req.Url.RawQuery != "" { + pathName += "?" + req.Url.RawQuery + } + switch req.Method { case gortsplib.OPTIONS: c.conn.WriteResponse(&gortsplib.Response{ @@ -382,6 +388,8 @@ func (c *client) handleRequest(req *gortsplib.Request) error { return errRunTerminate } + pathName = removeQueryFromPath(pathName) + confp := c.p.findConfForPathName(pathName) if confp == nil { c.writeResError(cseq, gortsplib.StatusBadRequest, @@ -411,6 +419,8 @@ func (c *client) handleRequest(req *gortsplib.Request) error { return errRunTerminate } + pathName = removeQueryFromPath(pathName) + if len(pathName) == 0 { c.writeResError(cseq, gortsplib.StatusBadRequest, fmt.Errorf("empty base path")) return errRunTerminate @@ -495,6 +505,8 @@ func (c *client) handleRequest(req *gortsplib.Request) error { return errRunTerminate } + basePath = removeQueryFromPath(basePath) + switch c.state { // play case clientStateInitial, clientStatePrePlay: @@ -752,6 +764,8 @@ func (c *client) handleRequest(req *gortsplib.Request) error { return errRunTerminate } + pathName = removeQueryFromPath(pathName) + // path can end with a slash, remove it pathName = strings.TrimSuffix(pathName, "/") @@ -785,6 +799,8 @@ func (c *client) handleRequest(req *gortsplib.Request) error { return errRunTerminate } + pathName = removeQueryFromPath(pathName) + // path can end with a slash, remove it pathName = strings.TrimSuffix(pathName, "/") diff --git a/go.mod b/go.mod index 02ba1fee..563664db 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.12 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect - github.com/aler9/gortsplib v0.0.0-20200905123647-6d46442a79c9 + github.com/aler9/gortsplib v0.0.0-20200905183437-37e3a1f29f40 github.com/davecgh/go-spew v1.1.1 // indirect github.com/stretchr/testify v1.6.1 gopkg.in/alecthomas/kingpin.v2 v2.2.6 diff --git a/go.sum b/go.sum index e29c7f1c..8d11fcce 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= -github.com/aler9/gortsplib v0.0.0-20200905123647-6d46442a79c9 h1:0igRBY+6YHv6YoAB5WTftCf6pFnoItrSy7NrQV8tfco= -github.com/aler9/gortsplib v0.0.0-20200905123647-6d46442a79c9/go.mod h1:XybE/Zt1yFtnNEjNhAyg2w6VjD8aJ79GFfpSjkfLNd8= +github.com/aler9/gortsplib v0.0.0-20200905183437-37e3a1f29f40 h1:l6De9lyccqCRoIlUrTi5XfHku8oO2V1LAbqROGeJuo4= +github.com/aler9/gortsplib v0.0.0-20200905183437-37e3a1f29f40/go.mod h1:XybE/Zt1yFtnNEjNhAyg2w6VjD8aJ79GFfpSjkfLNd8= github.com/aler9/sdp-dirty/v3 v3.0.0-20200905103724-214b7cc25cfd h1:s/l20rPNGiyjggMdkhsLu0aQ0K0OFcROUMBDu7fGT+I= github.com/aler9/sdp-dirty/v3 v3.0.0-20200905103724-214b7cc25cfd/go.mod h1:5bO/aUQr9m3OasDatNNcVqKAgs7r5hgGXmszWHaC6mI= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/main_test.go b/main_test.go index e9dfbf05..95cba516 100644 --- a/main_test.go +++ b/main_test.go @@ -281,6 +281,39 @@ func TestPathWithSlash(t *testing.T) { require.Equal(t, 0, code) } +func TestPathWithQuery(t *testing.T) { + p, err := newProgram([]string{}, bytes.NewBuffer(nil)) + require.NoError(t, err) + defer p.close() + + time.Sleep(1 * time.Second) + + cnt1, err := newContainer("ffmpeg", "publish", []string{ + "-re", + "-stream_loop", "-1", + "-i", "/emptyvideo.ts", + "-c", "copy", + "-f", "rtsp", + "-rtsp_transport", "udp", + "rtsp://" + ownDockerIp + ":8554/test?param1=val¶m2=val", + }) + require.NoError(t, err) + defer cnt1.close() + + cnt2, err := newContainer("ffmpeg", "read", []string{ + "-rtsp_transport", "udp", + "-i", "rtsp://" + ownDockerIp + ":8554/test?param3=otherval", + "-vframes", "1", + "-f", "image2", + "-y", "/dev/null", + }) + require.NoError(t, err) + defer cnt2.close() + + code := cnt2.wait() + require.Equal(t, 0, code) +} + func TestAuth(t *testing.T) { t.Run("publish", func(t *testing.T) { stdin := []byte("\n" + diff --git a/utils.go b/utils.go index 2fd37035..c988d441 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "regexp" + "strings" ) func parseIpCidrList(in []string) ([]interface{}, error) { @@ -100,6 +101,14 @@ func splitPath(path string) (string, string, error) { return basePath, controlPath, nil } +func removeQueryFromPath(path string) string { + i := strings.Index(path, "?") + if i >= 0 { + return path[:i] + } + return path +} + var rePathName = regexp.MustCompile("^[0-9a-zA-Z_\\-/]+$") func checkPathName(name string) error {