--!strict --!optimize 2 -- codecs/Url: RFC 3986 reference resolution (section 5.2), the classification of a playlist / manifest location (URL, -- Windows / UNC / POSIX local path, relative reference) and percent-encoding of bytes that may not appear in a URI -- (raw spaces and UTF-8 in playlists). Shared by the playlist formats (m3u / HLS, pls, xspf); later DASH / glTF URIs. -- Absorbed from playlist-luau 3fc7fd3 (same author, no third-party code): Url.luau, plus locate / base from its -- init.luau. Never throws. local Url = {} export type Location = { url: string, playable: boolean, reason: string?, [string]: any } export type Parts = { scheme: string?, authority: string?, path: string, query: string?, fragment: string?, } -- Splits a URI reference into its five components (RFC 3986 appendix B). function Url.split(ref: string): Parts local scheme, rest = string.match(ref, "^([%a][%w+%.%-]*):(.*)$") if not scheme then rest = ref end local s = rest :: string local fragment, query, authority local i = string.find(s, "#", 1, true) if i then fragment = string.sub(s, i + 1) s = string.sub(s, 1, i - 1) end i = string.find(s, "?", 1, true) if i then query = string.sub(s, i + 1) s = string.sub(s, 1, i - 1) end if string.sub(s, 1, 2) == "//" then local j = string.find(s, "/", 3, true) if j then authority = string.sub(s, 3, j - 1) s = string.sub(s, j) else authority = string.sub(s, 3) s = "" end end return { scheme = scheme, authority = authority, path = s, query = query, fragment = fragment } end -- Recomposes components (RFC 3986 section 5.3). function Url.join(p: Parts): string local out = {} if p.scheme then table.insert(out, p.scheme .. ":") end if p.authority then table.insert(out, "//" .. p.authority) end table.insert(out, p.path) if p.query then table.insert(out, "?" .. p.query) end if p.fragment then table.insert(out, "#" .. p.fragment) end return table.concat(out) end -- RFC 3986 section 5.2.4 function Url.removeDotSegments(path: string): string local input = path local output = {} while input ~= "" do if string.sub(input, 1, 3) == "../" then input = string.sub(input, 4) elseif string.sub(input, 1, 2) == "./" then input = string.sub(input, 3) elseif string.sub(input, 1, 3) == "/./" then input = string.sub(input, 3) elseif input == "/." then input = "/" elseif string.sub(input, 1, 4) == "/../" then input = string.sub(input, 4) table.remove(output) elseif input == "/.." then input = "/" table.remove(output) elseif input == "." or input == ".." then input = "" else -- move the first segment (with its leading "/", up to the next "/") to the output local j = string.find(input, "/", 2, true) local seg = if j then string.sub(input, 1, j - 1) else input table.insert(output, seg) input = string.sub(input, #seg + 1) end end return table.concat(output) end -- Resolves a reference against an absolute base URI (RFC 3986 section 5.2.2, strict). function Url.resolve(ref: string, base: string): string local r, b = Url.split(ref), Url.split(base) local t: Parts = { path = "" } if r.scheme then t.scheme, t.authority, t.path, t.query = r.scheme, r.authority, Url.removeDotSegments(r.path), r.query else if r.authority then t.authority, t.path, t.query = r.authority, Url.removeDotSegments(r.path), r.query else if r.path == "" then t.path = b.path t.query = if r.query then r.query else b.query else if string.sub(r.path, 1, 1) == "/" then t.path = Url.removeDotSegments(r.path) else -- merge (5.2.3) local merged if b.authority and b.path == "" then merged = "/" .. r.path else local k = string.match(b.path, "^.*()/") merged = (if k then string.sub(b.path, 1, k) else "") .. r.path end t.path = Url.removeDotSegments(merged) end t.query = r.query end t.authority = b.authority end t.scheme = b.scheme end t.fragment = r.fragment return Url.join(t) end -- Percent-encodes the bytes that may not appear in a URI (controls, space, non-ASCII, "<>\"^`{|}" and "\\"), -- leaving existing %XX escapes and the reserved delimiters alone. function Url.encodeUnsafe(s: string): string return (string.gsub(s, "[%z\1-\32\127-\255<>\"%^`{|}\\]", function(c: string): string return string.format("%%%02X", string.byte(c)) end)) end -- Is a location a local Windows path ("C:\x", "C:/x", "\\server\share", "\x")? function Url.isWindowsPath(s: string): boolean return string.match(s, "^%a:[\\/]") ~= nil or string.match(s, "^%a:$") ~= nil or string.sub(s, 1, 2) == "\\\\" or string.sub(s, 1, 1) == "\\" end -- The scheme of an absolute URI (lower case), or nil. Windows drive letters are not schemes. function Url.scheme(s: string): string? if Url.isWindowsPath(s) then return nil end local scheme = string.match(s, "^([%a][%w+%.%-]*):") return if scheme then string.lower(scheme) else nil end -- locate(raw, base?, baseScheme?) -> { url, playable, reason? }: an entry location resolved against the playlist's -- base (from Url.base). Windows paths stay as they are and are local; relative paths without a base, file: URLs and -- schemes other than http(s) are not playable (reason says why). function Url.locate(raw: string, base: string?, baseScheme: string?): Location local loc = (string.match(raw, "^%s*(.-)%s*$") :: string) if Url.isWindowsPath(loc) then return { url = loc, playable = false, reason = "local path" } end local scheme = Url.scheme(loc) local url = loc if not scheme and baseScheme == "windows" and base then -- a playlist on a Windows disk: relative entries are paths next to it local dir = string.match(base, "^(.*[\\/])") or "" return { url = dir .. loc, playable = false, reason = "local path" } end if scheme then if scheme == "http" or scheme == "https" then url = Url.encodeUnsafe(Url.resolve(loc, loc)) end elseif base and baseScheme then -- a relative reference: Windows separators become URL ones url = Url.resolve(Url.encodeUnsafe((string.gsub(loc, "\\", "/"))), base) scheme = baseScheme else return { url = loc, playable = false, reason = "relative path without a base URL" } end if scheme == "http" or scheme == "https" then return { url = url, playable = true } elseif scheme == "file" then return { url = url, playable = false, reason = "local path" } end return { url = url, playable = false, reason = "scheme " .. tostring(scheme) } end -- base(baseUrl?, warnings?) -> base, baseScheme: the base for Url.locate from the playlist's own URL or path ("windows" -- for a Windows path); a non-absolute base is ignored (with a warning) function Url.base(baseUrl: string?, warnings: { string }?): (string?, string?) if type(baseUrl) ~= "string" or baseUrl == "" then return nil, nil end local s = Url.scheme(baseUrl) if s and Url.split(baseUrl).authority ~= nil or s == "file" then return Url.encodeUnsafe(baseUrl), s elseif Url.isWindowsPath(baseUrl) then return baseUrl, "windows" end if warnings then table.insert(warnings, "base URL is not absolute: " .. baseUrl) end return nil, nil end return Url