Modul:URLutil: Unterschied zwischen den Versionen
update
w>PerfektesChaos (REfix) |
w>PerfektesChaos (update) |
||
Zeile 1: | Zeile 1: | ||
--[=[ URLutil 2013- | --[=[ URLutil 2013-07-10 | ||
Utilities for URL etc. on www. | Utilities for URL etc. on www. | ||
* getAuthority() | * getAuthority() | ||
Zeile 7: | Zeile 7: | ||
* getTLD() | * getTLD() | ||
* getTop2domain() | * getTop2domain() | ||
* getTop3domain() | |||
* isAuthority() | * isAuthority() | ||
* isDomain() | * isDomain() | ||
* isDomainExample() | |||
* isHost() | * isHost() | ||
* isIP() | * isIP() | ||
* isIPlocal() | |||
* isIPv4() | * isIPv4() | ||
* isIPv6() | * isIPv6() | ||
Zeile 31: | Zeile 34: | ||
-- table for export | -- table for export | ||
local URLutil = {} | local URLutil = {} | ||
local getTopDomain = function ( url, mode ) | |||
local host = URLutil.getHost( url ) | |||
if host then | |||
local pattern = "[%w%%]+%.[a-z][a-z]+)$" | |||
if mode == 3 then | |||
pattern = "[%w%%]+%." .. pattern | |||
end | |||
host = mw.ustring.match( "." .. host, "%.(" .. pattern ) | |||
if host then | |||
return host | |||
end | |||
end | |||
return false | |||
end -- getTopDomain() | |||
Zeile 106: | Zeile 126: | ||
URLutil.getTop2domain = function ( url ) | URLutil.getTop2domain = function ( url ) | ||
return getTopDomain( url, 2 ) | |||
end -- URLutil.getTop2domain() | end -- URLutil.getTop2domain() | ||
URLutil.getTop3domain = function ( url ) | |||
return getTopDomain( url, 3 ) | |||
end -- URLutil.getTop3domain() | |||
Zeile 152: | Zeile 170: | ||
return false | return false | ||
end -- URLutil.isDomain() | end -- URLutil.isDomain() | ||
URLutil.isDomainExample = function ( url ) | |||
-- RFC 2606: example.com example.net example.org example.edu | |||
local r = getTopDomain( url, 2 ) | |||
if r then | |||
local s = r:lower():match( "^example%.([a-z][a-z][a-z])$" ) | |||
if s then | |||
r = ( s == "com" or | |||
s == "edu" or | |||
s == "net" or | |||
s == "org" ) | |||
else | |||
r = false | |||
end | |||
end | |||
return r | |||
end -- URLutil.isDomainExample() | |||
Zeile 164: | Zeile 201: | ||
return URLutil.isIPv4( s ) and 4 or URLutil.isIPv6( s ) and 6 | return URLutil.isIPv4( s ) and 4 or URLutil.isIPv6( s ) and 6 | ||
end -- URLutil.isIP() | end -- URLutil.isIP() | ||
URLutil.isIPlocal = function ( s ) | |||
-- IPv4 according to RFC 1918, RFC 1122; even any 0.0.0.0 (RFC 5735) | |||
local r = false | |||
local num = s:match( "^ *([01][0-9]*)%." ) | |||
if num then | |||
num = tonumber( num ) | |||
if num == 0 then | |||
r = s:match( "^ *0+%.[0-9]+%.[0-9]+%.[0-9]+ *$" ) | |||
elseif num == 10 or num == 127 then | |||
-- loopback; private/local host: 127.0.0.1 | |||
r = URLutil.isIPv4( s ) | |||
elseif num == 169 then | |||
-- 169.254.*.* | |||
elseif num == 172 then | |||
-- 172.(16...31).*.* | |||
num = s:match( "^ *0*172%.([0-9]+)%." ) | |||
if num then | |||
num = tonumber( num ) | |||
if num >= 16 and num <= 31 then | |||
r = URLutil.isIPv4( s ) | |||
end | |||
end | |||
elseif beg == 192 then | |||
-- 192.168.*.* | |||
num = s:match( "^ *0*192%.([0-9]+)%." ) | |||
if num then | |||
num = tonumber( num ) | |||
if num == 168 then | |||
r = URLutil.isIPv4( s ) | |||
end | |||
end | |||
end | |||
end | |||
if r then | |||
r = true | |||
end | |||
return r | |||
end -- URLutil.isIPlocal() | |||
Zeile 361: | Zeile 439: | ||
function p.getTop2domain( frame ) | function p.getTop2domain( frame ) | ||
return URLutil.getTop2domain( frame.args[ 1 ] ) or "" | return URLutil.getTop2domain( frame.args[ 1 ] ) or "" | ||
end | |||
function p.getTop3domain( frame ) | |||
return URLutil.getTop3domain( frame.args[ 1 ] ) or "" | |||
end | end | ||
function p.isAuthority( frame ) | function p.isAuthority( frame ) | ||
Zeile 367: | Zeile 448: | ||
function p.isDomain( frame ) | function p.isDomain( frame ) | ||
return URLutil.isDomain( frame.args[ 1 ] ) and "1" or "" | return URLutil.isDomain( frame.args[ 1 ] ) and "1" or "" | ||
end | |||
function p.isDomainExample( frame ) | |||
return URLutil.isDomainExample( frame.args[ 1 ] ) and "1" or "" | |||
end | end | ||
function p.isHost( frame ) | function p.isHost( frame ) | ||
Zeile 373: | Zeile 457: | ||
function p.isIP( frame ) | function p.isIP( frame ) | ||
return URLutil.isIP( frame.args[ 1 ] ) or "" | return URLutil.isIP( frame.args[ 1 ] ) or "" | ||
end | |||
function p.isIPlocal( frame ) | |||
return URLutil.isIPlocal( frame.args[ 1 ] ) and "1" or "" | |||
end | end | ||
function p.isIPv4( frame ) | function p.isIPv4( frame ) |