improved rewrite tests

This commit is contained in:
John Bintz 2010-05-11 09:25:29 -04:00
parent 113abc74f7
commit 710831190d
2 changed files with 45 additions and 5 deletions

View File

@ -101,5 +101,12 @@ module Apache
self << output self << output
end end
end end
def server_name(*opts)
if first = opts.shift
self << "ServerName #{quoteize(first)}"
opts.each { |o| server_alias o } if !opts.empty?
end
end
end end
end end

View File

@ -1,3 +1,5 @@
require 'pp'
module Apache module Apache
# Handle the creation of RewriteRules, RewriteConds, Redirects, and RedirectMatches # Handle the creation of RewriteRules, RewriteConds, Redirects, and RedirectMatches
module Rewrites module Rewrites
@ -98,7 +100,12 @@ module Apache
def rewrite_test(from, to, opts = {}) def rewrite_test(from, to, opts = {})
orig_from = from.dup orig_from = from.dup
@rewrites.each do |r| @rewrites.each do |r|
from = r.test(from, opts) pre_from = from.dup
if r.match?(from, opts)
from = r.test(from, opts)
from = pre_from if (from == '-')
break if r.stop_if_match?
end
end end
if from != to if from != to
@ -117,6 +124,10 @@ module Apache
replace_placeholders(from, opts) replace_placeholders(from, opts)
end end
def match?(from, opts = {})
replace_placeholders(from, opts)[@from]
end
# Replace the placeholders in this rewritable thing # Replace the placeholders in this rewritable thing
def replace_placeholders(s, opts) def replace_placeholders(s, opts)
opts.each do |opt, value| opts.each do |opt, value|
@ -125,7 +136,7 @@ module Apache
s = s.gsub('%{' + opt.to_s.upcase + '}', value) s = s.gsub('%{' + opt.to_s.upcase + '}', value)
end end
end end
s s.gsub(%r{%\{[^\}]+\}}, '')
end end
end end
@ -153,6 +164,8 @@ module Apache
def to_a def to_a
[ to_s ] [ to_s ]
end end
def stop_if_match?; false; end
end end
# A RewriteRule definition # A RewriteRule definition
@ -165,26 +178,31 @@ module Apache
super super
@conditions = [] @conditions = []
@options = nil @options = nil
@input_options = {}
end end
# Define the rule, passing in additional options # Define the rule, passing in additional options
# #
# rule %r{^/here}, '/there', { :last => true, :preserve_query_string => true } # rule %r{^/here}, '/there', { :last => true, :preserve_query_string => true }
def rule(from, to,options = {}) def rule(from, to, options = {})
super(from, to) super(from, to)
raise "from must be a Regexp" if !from.kind_of?(Regexp) raise "from must be a Regexp" if !from.kind_of?(Regexp)
@input_options = options
options = options.collect do |key, value| options = options.collect do |key, value|
case key case key
when :last when :last
'L' 'L'
when :redirect
'R'
when :pass_through when :pass_through
'PT' 'PT'
when :preserve_query_string when :preserve_query_string
'QSA' 'QSA'
end end
end.sort end.compact.sort
@options = !options.empty? ? "[#{options * ','}]" : nil @options = !options.empty? ? "[#{options * ','}]" : nil
end end
@ -207,7 +225,16 @@ module Apache
# Test this RewriteRule, ensuring the RewriteConds also match # Test this RewriteRule, ensuring the RewriteConds also match
def test(from, opts = {}) def test(from, opts = {})
result = from
result = super(from, opts) if match?(from, opts)
replace_placeholders(result, opts)
end
def match?(from, opts = {})
ok = true ok = true
@conditions.each do |c| @conditions.each do |c|
ok = false if !c.test(from, opts) ok = false if !c.test(from, opts)
end end
@ -215,9 +242,13 @@ module Apache
if ok if ok
super(from, opts) super(from, opts)
else else
replace_placeholders(from, opts) false
end end
end end
def stop_if_match?
@input_options[:last]
end
end end
# A permanent RedirectMatch # A permanent RedirectMatch
@ -235,6 +266,8 @@ module Apache
def to_s def to_s
"#{tag} #{[quoteize(@from.source), quoteize(@to)].compact.flatten * " "}" "#{tag} #{[quoteize(@from.source), quoteize(@to)].compact.flatten * " "}"
end end
def stop_if_match; true; end
end end
# A RewriteCond # A RewriteCond