diff options
-rw-r--r-- | bulkredirect.lua | 28 | ||||
-rw-r--r-- | t/g.tab | 6 | ||||
-rwxr-xr-x | t/testsuite | 17 |
3 files changed, 43 insertions, 8 deletions
diff --git a/bulkredirect.lua b/bulkredirect.lua index b8eab35..ef7befb 100644 --- a/bulkredirect.lua +++ b/bulkredirect.lua @@ -15,7 +15,6 @@ -- along with this program. If not, see <http://www.gnu.org/licenses/>. local bulkredirect = {} -__ENV = bulkredirect --[[ Iterator factory to successively strip off the trailing pathname @@ -85,7 +84,7 @@ local function url_path_encode (s) return table.concat(t) end -function constant(t) +local function constant(t) return setmetatable({}, { __index = t, __newindex = function(table, key, value) @@ -95,7 +94,7 @@ function constant(t) }); end -RTFLAG = constant { +local RTFLAG = constant { WWW = 0x1, -- The rules for a hostname "X" apply also to -- hostname "www.X" EXACT = 0x2, -- Skip prefix search: the redirection reply @@ -122,7 +121,7 @@ RTFLAG = constant { -- Module global variables: -- -rt = {} +local rt = {} --[[ Redirection table. It is indexed by the request host name (the value given by the HTTP Host header). The corresponding value can be either a string or a table. @@ -163,7 +162,7 @@ rt = {} flag values for this redirect. ]] -rtflags = RTFLAG.DEFAULT +local rtflags = RTFLAG.DEFAULT -- -- Redirect the request if it matches one of the entries in the RT table. @@ -383,9 +382,14 @@ local function load_redirect_file (f, filename) local domain local ln = 0 - local rt = {} local domain_flags = rtflags + local status, idna = pcall(require, 'idna') + if not status then + core.Info(string.format('idna module not found; disabling unicode to ascii domain name conversions')) + idna = nil + end + local parsetab = { { '^#', function () end }, { '^%s*$', function () end }, @@ -400,7 +404,16 @@ local function load_redirect_file (f, filename) }, { '^%s*%[(.+)%]%s*$', function (s) - domain = s + if idna then + domain, label = idna.domain_name_to_ascii(s) + if not domain then + core.Warning(string.format("%s:%d: Can't convert %s to ASCII: failed at %s", filename, ln, s, label)) + domain = s + end + else + domain = s + end + if not rt[domain] then rt[domain] = {} end domain_flags = rtflags end @@ -488,7 +501,6 @@ local function load_redirect_file (f, filename) rtflags = rtflags & ~RTFLAG.WWW end - _ENV['rt'] = rt end local function load_redirect_table () @@ -0,0 +1,6 @@ +# This test requires lua-idna: +# https://puszcza.gnu.org.ua/projects/lua-idna +[verktøyet.example.com] +/ /main.html +[xn--verktyet-94a.example.com] +/api /index diff --git a/t/testsuite b/t/testsuite index 2a62e8d..c9b0af7 100755 --- a/t/testsuite +++ b/t/testsuite @@ -427,6 +427,23 @@ runtest \ URL='example.org/s%c3%b8ndag' \ HTTP_LOCATION='/sunday' +# #################### +if lua -e "require('idna')" >/dev/null 2>&1; then + group_header IDNA Conversions + + runtest \ + RTFILE=g.tab \ + DESCR='Unicode domain name' \ + URL='xn--verktyet-94a.example.com/' \ + HTTP_LOCATION=/main.html + + runtest \ + RTFILE=g.tab \ + DESCR='Well-formed IDNA domain' \ + URL='xn--verktyet-94a.example.com/api' \ + HTTP_LOCATION=/index +fi + echo echo "Run $(($runtest_success_count + $runtest_failure_count)) tests." if [ $runtest_failure_count -eq 0 ]; then |