From 61c1d99c43b89378f230524571ecd72135204886 Mon Sep 17 00:00:00 2001 From: scruzin Date: Wed, 13 Mar 2019 18:47:00 +1030 Subject: [PATCH] Made parseURL() more robust. --- rtmp/parseurl.go | 18 +++++++----------- rtmp/parseurl_test.go | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/rtmp/parseurl.go b/rtmp/parseurl.go index cf3e4fba..132c9acd 100644 --- a/rtmp/parseurl.go +++ b/rtmp/parseurl.go @@ -75,20 +75,16 @@ func parseURL(addr string) (protocol int32, host string, port uint16, app, playp port = uint16(pi) } - if !path.IsAbs(u.Path) { - return protocol, host, port, app, playpath, nil - } - - elems := strings.SplitN(u.Path[1:], "/", 3) - app = elems[0] - if len(elems[len(elems)-1]) == 0 { - elems = elems[:len(elems)-1] - } - if app == "" || len(elems) < 2 { + if len(u.Path) < 1 || !path.IsAbs(u.Path) { return protocol, host, port, app, playpath, errInvalidURL } - + elems := strings.SplitN(u.Path[1:], "/", 3) + if len(elems) < 2 || elems[0] == "" || elems[1] == "" { + return protocol, host, port, app, playpath, errInvalidURL + } + app = elems[0] playpath = path.Join(elems[1:]...) + switch ext := path.Ext(playpath); ext { case ".f4v", ".mp4": playpath = playpath[:len(playpath)-len(ext)] diff --git a/rtmp/parseurl_test.go b/rtmp/parseurl_test.go index 7f5e3029..47743693 100644 --- a/rtmp/parseurl_test.go +++ b/rtmp/parseurl_test.go @@ -41,7 +41,7 @@ var parseURLTests = []struct { }{ { url: "rtmp://addr", - wantHost: "addr", + wantErr: errInvalidURL, }, { url: "rtmp://addr/",