codecs/text: comments to specs only
This commit is contained in:
1 parent
f626fb4295
commit
492d6662df
12 files changed
+97
-294
No files matched your search
@@ -1,10 +1,7 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/text/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.
|
||||
-- codecs/text/Url: RFC 3986 reference resolution (section 5.2), local-path classification and percent-encoding.
|
||||
-- Absorbed from playlist-luau 3fc7fd3 (same author, no third-party code).
|
||||
local Url = {}
|
||||
|
||||
export type Location = { url: string, playable: boolean, reason: string?, [string]: any }
|
||||
@@ -17,7 +14,7 @@ export type Parts = {
|
||||
fragment: string?,
|
||||
}
|
||||
|
||||
-- Splits a URI reference into its five components (RFC 3986 appendix B).
|
||||
-- RFC 3986 appendix B: the five components of a URI reference
|
||||
function Url.split(ref: string): Parts
|
||||
local scheme, rest = string.match(ref, "^([%a][%w+%.%-]*):(.*)$")
|
||||
if not scheme then
|
||||
@@ -48,7 +45,7 @@ function Url.split(ref: string): Parts
|
||||
return { scheme = scheme, authority = authority, path = s, query = query, fragment = fragment }
|
||||
end
|
||||
|
||||
-- Recomposes components (RFC 3986 section 5.3).
|
||||
-- RFC 3986 section 5.3
|
||||
function Url.join(p: Parts): string
|
||||
local out = {}
|
||||
if p.scheme then
|
||||
@@ -99,7 +96,7 @@ function Url.removeDotSegments(path: string): string
|
||||
return table.concat(output)
|
||||
end
|
||||
|
||||
-- Resolves a reference against an absolute base URI (RFC 3986 section 5.2.2, strict).
|
||||
-- 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 = "" }
|
||||
@@ -136,15 +133,13 @@ function Url.resolve(ref: string, base: string): string
|
||||
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.
|
||||
-- percent-encoded: controls, space, non-ASCII, "<>\"^`{|}" and "\\"; existing %XX escapes and reserved delimiters stay
|
||||
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
|
||||
@@ -152,7 +147,6 @@ function Url.isWindowsPath(s: string): boolean
|
||||
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
|
||||
@@ -161,9 +155,6 @@ function Url.scheme(s: string): string?
|
||||
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
|
||||
@@ -172,7 +163,6 @@ function Url.locate(raw: string, base: string?, baseScheme: string?): Location
|
||||
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
|
||||
@@ -181,7 +171,6 @@ function Url.locate(raw: string, base: string?, baseScheme: string?): Location
|
||||
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
|
||||
@@ -195,8 +184,6 @@ function Url.locate(raw: string, base: string?, baseScheme: string?): Location
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user