diff options
-rw-r--r-- | bulkredirect.lua | 173 |
1 files changed, 92 insertions, 81 deletions
diff --git a/bulkredirect.lua b/bulkredirect.lua index f995a3c..e1cf7bc 100644 --- a/bulkredirect.lua +++ b/bulkredirect.lua @@ -35,11 +35,12 @@ local function prevsegm (t) end end ---[[ - Module global variables: +-- +-- Module global variables: +-- - rt - Redirection table. It is indexed by the request host name +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. @@ -82,29 +83,33 @@ end 3 - strippath 4 - stripquery 5 - temporary +]] - www - If set to true, the rules for a hostname "X" apply also to +www = false +--[[ If set to true, the rules for a hostname "X" apply also to hostname "www.X" - - exact - If set to true, no path prefix search is done: the redirection reply +]] + +exact = false +--[[ If set to true, no path prefix search is done: the redirection reply is returned only if the input path is present in the table. - - strippath - By default the path components stripped during the look up process +]] + +strippath = false +--[[ By default the path components stripped during the look up process are added back to the returned location. If this variable is set to true, these components are dropped instead. - - stripquery - By default the query part (if any) is appended to the new +]] + +stripquery = false +--[[ By default the query part (if any) is appended to the new location when returning the redirection reply. Setting this variable to true disables this - - temporary - Whether to return temporary (302) or permanent (301) reply. ]] +temporary = false +-- Whether to return temporary (302) or permanent (301) reply. + -- -- Redirect the request if it matches one of the entries in the RT table. -- @@ -333,72 +338,78 @@ local function load_redirect_file (f, filename) local parsetab = { { '^#', function () end }, { '^%s*$', function () end }, - { '^option%s+(.*)', function (s) - local t - if domain then - t = domopt - else - t = _ENV - end - parseopt(s, t, filename .. ':' .. ln) - end }, - { '^%s*%[(.+)%]%s*$', function (s) - domain = s - if not rt[domain] then rt[domain] = {} end - end }, - { '^%s*([^%s]+)%s+([^%s]+)%s*(.*)$', function (src, dst, optlist) - if not domain then - error(filename .. ':' .. ln .. ': declare [domain] first', 0) - end + { '^option%s+(.*)', + function (s) + local t + if domain then + t = domopt + else + t = _ENV + end + parseopt(s, t, filename .. ':' .. ln) + end + }, + { '^%s*%[(.+)%]%s*$', + function (s) + domain = s + if not rt[domain] then rt[domain] = {} end + end + }, + { '^%s*([^%s]+)%s+([^%s]+)%s*(.*)$', + function (src, dst, optlist) + if not domain then + error(filename .. ':' .. ln .. ': declare [domain] first', 0) + end - if src:match('^/') then - src = src:sub(2) - end - - if dst:match('^/') then - dst = dst:sub(2) - end - - local optab = domopt - if optlist ~= '' then - optab = clone(domopt) - parseopt(optlist, optab, filename .. ':' .. ln) - end + if src:match('^/') then + src = src:sub(2) + end + + if dst:match('^/') then + dst = dst:sub(2) + end + + local optab = domopt + if optlist ~= '' then + optab = clone(domopt) + parseopt(optlist, optab, filename .. ':' .. ln) + end - if optab['exact'] ~= exact then - if type(dst) == 'string' then dst = { dst } end - dst[2] = optab['exact'] - end - if optab['strippath'] ~= strippath then - if type(dst) == 'string' then dst = { dst } end - dst[3] = optab['strippath'] - end - if optab['stripquery'] ~= stripquery then - if type(dst) == 'string' then dst = { dst } end - dst[4] = optab['stripquery'] - end - if optab['temporary'] ~= temporary then - if type(dst) == 'string' then dst = { dst } end - dst[5] = optab['temporary'] - end - - if optab['www'] ~= nil then - if www == optab['www'] then - return - elseif www then - populate_www_complements (rt, true) - www = nil - end - end + if optab['exact'] ~= exact then + if type(dst) == 'string' then dst = { dst } end + dst[2] = optab['exact'] + end + if optab['strippath'] ~= strippath then + if type(dst) == 'string' then dst = { dst } end + dst[3] = optab['strippath'] + end + if optab['stripquery'] ~= stripquery then + if type(dst) == 'string' then dst = { dst } end + dst[4] = optab['stripquery'] + end + if optab['temporary'] ~= temporary then + if type(dst) == 'string' then dst = { dst } end + dst[5] = optab['temporary'] + end + + if optab['www'] ~= nil then + if www == optab['www'] then + return + elseif www then + populate_www_complements (rt, true) + www = nil + end + end - set_dst(rt[domain], src, dst) - - if optab['www'] then - local s = www_complement(domain) - if not rt[s] then rt[s] = {} end - set_dst(rt[s], src, dst) - end - end } + set_dst(rt[domain], src, dst) + + if optab['www'] then + local s = www_complement(domain) + if not rt[s] then rt[s] = {} end + set_dst(rt[s], src, dst) + end + end + } } for line in f:lines() do |