Modul:URLutil: Unterschied zwischen den Versionen
2016-03-02
w>PerfektesChaos (2016-03-01 getNormalized) |
w>PerfektesChaos (2016-03-02) |
||
Zeile 1: | Zeile 1: | ||
local URLutil = { suite = "URLutil", | local URLutil = { suite = "URLutil", | ||
serial = "2016-03- | serial = "2016-03-02" }; | ||
--[=[ | --[=[ | ||
Utilities for URL etc. on www. | Utilities for URL etc. on www. | ||
Zeile 55: | Zeile 55: | ||
local | |||
local decodeComponentProtect = { F = "\"#%<>[\]^`{|}", | |||
P = "\"#%<>[\]^`{|}/?", | |||
Q = "\"#%<>[\]^`{|}&=+;", | |||
X = "\"#%<>[\]^`{|}&=+;/?" } | |||
local decodeComponentEscape = function ( averse, adapt ) | |||
return adapt == 20 or adapt == 127 or | |||
decodeComponentProtect[ averse ]:find( string.char( adapt ), | |||
1, | |||
true ) | |||
end -- decodeComponentEscape() | |||
local decodeComponentML = function ( ask ) | |||
local i = 1 | |||
local j, n, s | |||
while ( i ) do | |||
i = ask:find( "&#[xX]%x%x+;", i ) | |||
if i then | |||
j = ask:find( ";", i + 3, true ) | |||
s = ask:sub( i + 2, j - 1 ):upper() | |||
n = s:byte( 1, 1 ) | |||
if n == 88 then | |||
n = tonumber( s:sub( 2 ), 16 ) | |||
elseif s:match( "^%d+$" ) then | |||
n = tonumber( s ) | |||
else | |||
n = false | |||
end | |||
if n then | |||
if n >= 128 then | |||
s = string.format( "&#%d;", n ) | |||
elseif decodeComponentEscape( "X", n ) then | |||
s = string.format( "%%%02X", n ) | |||
else | |||
s = string.format( "%c", n ) | |||
end | |||
j = j + 1 | |||
if i == 1 then | |||
ask = s .. ask:sub( j ) | |||
else | |||
ask = string.format( "%s%s%s", | |||
ask:sub( 1, i - 1 ), | |||
s, | |||
ask:sub( j ) ) | |||
end | |||
end | |||
i = i + 1 | |||
end | |||
end -- while i | |||
return ask | |||
end -- decodeComponentML() | |||
local decodeComponentPercent = function ( ask, averse ) | |||
local i = 1 | |||
local j, k, m, n | local j, k, m, n | ||
while ( i ) do | while ( i ) do | ||
i = ask:find( "%%[2-7]%x", i ) | i = ask:find( "%%[2-7]%x", i ) | ||
if i then | if i then | ||
j = i + 1 | j = i + 1 | ||
k = j + 1 | k = j + 1 | ||
Zeile 77: | Zeile 135: | ||
end | end | ||
n = ( ask:byte( j, j ) - 48 ) * 16 + n | n = ( ask:byte( j, j ) - 48 ) * 16 + n | ||
if | if decodeComponentEscape( averse, n ) then | ||
if m then | if m then | ||
ask = string.format( "%s%c%s", | ask = string.format( "%s%c%s", | ||
Zeile 97: | Zeile 154: | ||
end -- while i | end -- while i | ||
return ask | return ask | ||
end -- | end -- decodeComponentPercent() | ||
Zeile 250: | Zeile 307: | ||
if r == "" then | if r == "" then | ||
r = false | r = false | ||
else | |||
r = decodeComponentML( r ) | |||
end | end | ||
else | else | ||
Zeile 267: | Zeile 326: | ||
sF = false | sF = false | ||
else | else | ||
sF = | sF = decodeComponentPercent( sF, "F" ) | ||
end | end | ||
end | end | ||
Zeile 274: | Zeile 333: | ||
sQ = r:sub( i ) | sQ = r:sub( i ) | ||
r = r:sub( 1, i - 1 ) | r = r:sub( 1, i - 1 ) | ||
sQ = | sQ = decodeComponentPercent( sQ, "Q" ) | ||
end | end | ||
if j then | if j then | ||
if #r > j then | if #r > j then | ||
sP = | sP = r:sub( j + 1 ) | ||
sP = decodeComponentPercent( sP, "P" ) | |||
end | end | ||
r = r:sub( 1, j - 1 ) | r = r:sub( 1, j - 1 ) | ||
Zeile 305: | Zeile 364: | ||
end | end | ||
end | end | ||
r = r:gsub( "%[", "%5B" ) | |||
:gsub( "|", "%7C" ) | |||
:gsub( "%]", "%5D" ) | |||
end | end | ||
return r | return r |