basic test support for rewrite conditions

This commit is contained in:
John Bintz 2010-05-10 11:22:02 -04:00
parent 023925caab
commit e6d92383e7
2 changed files with 48 additions and 6 deletions

View File

@ -14,8 +14,8 @@ module Apache
alias :allow_from_all! :allow_from_all alias :allow_from_all! :allow_from_all
def allow_from(where) def allow_from(*where)
allow "from_#{where}".to_sym self << "Allow from #{quoteize(*where) * " "}"
end end
def order(*args) def order(*args)
@ -39,7 +39,7 @@ module Apache
alias :order! :order alias :order! :order
def basic_authentication(zone, users_file, requires) def basic_authentication(zone, users_file, requires = {})
exist? users_file exist? users_file
auth_type :basic auth_type :basic
auth_name zone auth_name zone
@ -51,7 +51,7 @@ module Apache
alias :basic_authentication! :basic_authentication alias :basic_authentication! :basic_authentication
def ldap_authentication(zone, url, requires) def ldap_authentication(zone, url, requires = {})
auth_type :basic auth_type :basic
auth_name zone auth_name zone
auth_basic_provider :ldap auth_basic_provider :ldap

View File

@ -84,10 +84,17 @@ module Apache
module RegularExpressionMatcher module RegularExpressionMatcher
def test(from, opts = {}) def test(from, opts = {})
from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |m| '\\' + $1 }) from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |m| '\\' + $1 })
replace_placeholders(from, opts)
end
def replace_placeholders(s, opts)
opts.each do |opt, value| opts.each do |opt, value|
from = from.gsub('%{' + opt.to_s.upcase + '}', value) case value
when String
s = s.gsub('%{' + opt.to_s.upcase + '}', value)
end
end end
from s
end end
end end
@ -161,6 +168,19 @@ module Apache
output output
end end
def test(from, opts = {})
ok = true
@conditions.each do |c|
ok = false if !c.test(from, opts)
end
if ok
super(from, opts)
else
replace_placeholders(from, opts)
end
end
end end
class RedirectMatchPermanent < MatchableThing class RedirectMatchPermanent < MatchableThing
@ -174,6 +194,8 @@ module Apache
end end
class RewriteCondition < MatchableThing class RewriteCondition < MatchableThing
include RegularExpressionMatcher
def tag; 'RewriteCond'; end def tag; 'RewriteCond'; end
def rule(from, to, *opts) def rule(from, to, *opts)
@ -203,5 +225,25 @@ module Apache
def to_s def to_s
"#{tag} #{[quoteize(@from), quoteize(@to), @options].compact.flatten * " "}" "#{tag} #{[quoteize(@from), quoteize(@to), @options].compact.flatten * " "}"
end end
def test(from, opts = {})
super(from, opts)
source = replace_placeholders(@from, opts)
result = false
case @to[0..0]
when '!'
result = !source[Regexp.new(@to[1..-1])]
when '-'
case @to
when '-f'
result = opts[:files].include? source if opts[:files]
end
else
result = source[Regexp.new(@to)]
end
result
end
end end
end end