aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-03-19 11:11:55 +0200
committerSergey Poznyakoff <gray@gnu.org>2021-03-19 11:11:55 +0200
commit475d9d334049fa4f7e628cb41e033bf9c72ac39e (patch)
treeddc2157f7414095e30eaf8caa40ac5bc1e32b0cf
parentef152fcb50337cf30827db0cc86bcbfa3b16e09d (diff)
downloadhaproxy-bulkredirect-475d9d334049fa4f7e628cb41e033bf9c72ac39e.tar.gz
haproxy-bulkredirect-475d9d334049fa4f7e628cb41e033bf9c72ac39e.tar.bz2
Add optional IDNA support
Requires https://puszcza.gnu.org.ua/projects/lua-idna * bulkredirect.lua: Add missing local qualifiers. (load_redirect_file): Try to load the idna.lua module. If loaded, use it to convert Unicode names to ASCII * t/g.tab: New test file. * t/testsuite: New tests (conditional)
-rw-r--r--bulkredirect.lua28
-rw-r--r--t/g.tab6
-rwxr-xr-xt/testsuite17
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 ()
diff --git a/t/g.tab b/t/g.tab
new file mode 100644
index 0000000..aaa8ebb
--- /dev/null
+++ b/t/g.tab
@@ -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

Return to:

Send suggestions and report system problems to the System administrator.