Merge pull request #1281 from sevenrats/global-regex

This commit is contained in:
Charles Ewert 2023-06-30 11:07:31 -04:00 committed by GitHub
commit c583069308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 13 deletions

View File

@ -194,12 +194,8 @@ end sub
sub addVideoContentURL(video, mediaSourceId, audio_stream_idx, fully_external)
protocol = LCase(m.playbackInfo.MediaSources[0].Protocol)
if protocol <> "file"
uriRegex = CreateObject("roRegex", "^(.*:)//([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$", "")
uri = uriRegex.Match(m.playbackInfo.MediaSources[0].Path)
' proto $1, host $2, port $3, the-rest $4
localhost = CreateObject("roRegex", "^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$", "i")
' https://stackoverflow.com/questions/8426171/what-regex-will-match-all-loopback-addresses
if localhost.isMatch(uri[2])
uri = parseUrl(m.playbackInfo.MediaSources[0].Path)
if isLocalhost(uri[2])
' if the domain of the URI is local to the server,
' create a new URI by appending the received path to the server URL
' later we will substitute the users provided URL for this case

View File

@ -263,13 +263,9 @@ sub AddVideoContent(video as object, mediaSourceId as dynamic, audio_stream_idx
if video.directPlaySupported
protocol = LCase(m.playbackInfo.MediaSources[0].Protocol)
if protocol <> "file"
uriRegex = CreateObject("roRegex", "^(.*:)//([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$", "")
uri = uriRegex.Match(m.playbackInfo.MediaSources[0].Path)
' proto $1, host $2, port $3, the-rest $4
localhost = CreateObject("roRegex", "^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$", "i")
' https://stackoverflow.com/questions/8426171/what-regex-will-match-all-loopback-addresses
if localhost.isMatch(uri[2])
' if the domain of the URI is local to the server,
uri = parseUrl(m.playbackInfo.MediaSources[0].Path)
if isLocalhost(uri[2])
' the domain of the URI is local to the server.
' create a new URI by appending the received path to the server URL
' later we will substitute the users provided URL for this case
video.content.url = buildURL(uri[4])

View File

@ -228,6 +228,21 @@ function isValidAndNotEmpty(input as dynamic) as boolean
end if
end function
' Returns an array from a url - [ url, proto, host, port, subdir/params ]
' If port or subdir are not found, an empty string will be added to the array
' Proto must be declared or array will be empty
function parseUrl(url as string) as object
rgx = CreateObject("roRegex", "^(.*:)//([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$", "")
return rgx.Match(url)
end function
' Returns true if the string is a loopback, such as 'localhost' or '127.0.0.1'
function isLocalhost(url as string) as boolean
' https://stackoverflow.com/questions/8426171/what-regex-will-match-all-loopback-addresses
rgx = CreateObject("roRegex", "^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$", "i")
return rgx.isMatch(url)
end function
' Rounds number to nearest integer
function roundNumber(f as float) as integer
' BrightScript only has a "floor" round

View File

@ -0,0 +1,54 @@
namespace tests
@suite("regex functions")
class regexTests extends tests.BaseTestSuite
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@describe("parseUrl()")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Proto must be declared or array members will be empty
@it("Returns an array of data from a url - proto, host, port, subdir/params")
function _()
demoNoPort = "https://demo.jellyfin.org/stable"
m.assertEqual(parseUrl(demoNoPort), [demoNoPort, "https:", "demo.jellyfin.org", "", "/stable"])
localNoSubdir = "http://192.168.0.2:8097"
m.assertEqual(parseUrl(localNoSubdir), [localNoSubdir, "http:", "192.168.0.2", ":8097", ""])
localWithSubdir = "http://192.168.0.2:80/jellyfin"
m.assertEqual(parseUrl(localWithSubdir), [localWithSubdir, "http:", "192.168.0.2", ":80", "/jellyfin"])
badIP = "http://192.168.2"
m.assertEqual(parseUrl(badIP), [badIP, "http:", "192.168.2", "", ""])
noProto = "192.168.0.2"
m.assertEqual(parseUrl(noProto), [])
end function
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@describe("isLocalhost()")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@it("only returns true for a valid loopback address")
@params("", false)
@params(" ", false)
@params("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Augue neque gravida in fermentum et. Eget lorem dolor sed viverra ipsum nunc. At quis risus sed vulputate odio ut enim. Ultricies integer quis auctor elit sed. Egestas congue quisque egestas diam in. Aliquam sem fringilla ut morbi tincidunt. Malesuada bibendum arcu vitae elementum curabitur. Aliquet sagittis id consectetur purus ut faucibus pulvinar. Eget gravida cum sociis natoque. Sollicitudin aliquam ultrices sagittis orci. Ut etiam sit amet nisl purus. Luctus venenatis lectus magna fringilla urna porttitor rhoncus dolor purus. Vitae ultricies leo integer malesuada nunc. Vitae ultricies leo integer malesuada nunc vel risus commodo. Luctus accumsan tortor posuere ac ut. Urna cursus eget nunc scelerisque viverra mauris in. Accumsan sit amet nulla facilisi morbi tempus iaculis urna id. Mauris vitae ultricies leo integer malesuada nunc vel risus commodo. Morbi tincidunt augue interdum velit euismod in pellentesque.", false)
@params("~!@#$%^&*()_-+=`\|]}';:.,/?", false)
@params("true", false)
@params("false", false)
@params("invalid", false)
@params("localhost", true)
@params("google.com", false)
@params("127.0.0.1", true)
@params("127.1.1.1", true)
@params("127.0.0.127", true)
@params("1.0.0.127", false)
@params("126.0.0.1", false)
@params("0:0:0:0:0:0:0:1", true)
@params("::1", true)
@params("::2", false)
@params("2001:4860:4860::8888", false)
@params("2001:4860:4860:0:0:0:0:8888", false)
@params("2001:4860:4860:0000:0000:0000:0000:8888", false)
function _(value, expectedassertResult)
m.assertEqual(isLocalhost(value), expectedassertResult)
end function
end class
end namespace