a lot more cleanup

This commit is contained in:
John Bintz 2010-05-19 09:37:57 -04:00
parent 487c503b21
commit f336947d15
2 changed files with 47 additions and 34 deletions

View File

@ -9,7 +9,7 @@ module Apache
# RewriteEngine on # RewriteEngine on
# RewriteLogLevel 1 # RewriteLogLevel 1
def enable_rewrite_engine(options = {}) def enable_rewrite_engine(options = {})
self << '' blank_line!
rewrite_engine! :on rewrite_engine! :on
options.each do |option, value| options.each do |option, value|
case option case option
@ -17,7 +17,7 @@ module Apache
rewrite_log_level! value rewrite_log_level! value
end end
end end
self << '' blank_line!
end end
# Pass the block to RewriteManager.build # Pass the block to RewriteManager.build
@ -49,30 +49,34 @@ module Apache
# Reset the current list of rewrites # Reset the current list of rewrites
def reset! def reset!
@rewrites = [] @rewrites = []
@any_tests = false
@needs_tests = false
end end
# Build rewritable things from the provided block # Build rewritable things from the provided block
def build(*opt, &block) def build(*opt, &block)
reset! reset!
@any_tests = false
@needs_tests = false
self.instance_eval(&block) self.instance_eval(&block)
name = opt.first || (@rewrites.empty? ? 'unnamed block' : "#{@rewrites.first.from} => #{@rewrites.first.to}") name = block_name(opt.first)
if !@any_tests && !@rewrites.empty? show_messages! name
puts " [#{"rewrite".foreground(:blue)}] no tests found for #{name}"
[ "# #{name}", @rewrites.collect(&:to_a) ].flatten
end end
if @needs_tests def block_name(first)
puts " [#{"rewrite".foreground(:blue)}] #{name} needs more tests" first_rewrite = @rewrites.first
first || (@rewrites.empty? ? 'unnamed block' : "#{first_rewrite.from} => #{first_rewrite.to}")
end end
output = @rewrites.collect(&:to_a).flatten def show_messages!(name)
output.unshift("# #{name}") if opt.first blue = "rewrite".foreground(:blue)
output puts " [#{blue}] no tests found for #{name}" if !@any_tests && !@rewrites.empty?
puts " [#{blue}] #{name} needs more tests" if @needs_tests
end end
# Commit the latest rewritable thing to the list of rewrites # Commit the latest rewritable thing to the list of rewrites
@ -122,19 +126,18 @@ module Apache
def rewrite_test(from, to, opts = {}) def rewrite_test(from, to, opts = {})
@any_tests = true @any_tests = true
orig_from = from.dup orig_from = from.dup
@rewrites.each do |r| @rewrites.each do |rewrite|
pre_from = from.dup if rewrite.match?(from, opts)
if r.match?(from, opts) from = rewrite.from_tester(from, opts)
from = r.test(from, opts) break if rewrite.stop_if_match?
from = pre_from if (r.to == '-')
from = :http_forbidden if (r.forbidden?)
break if r.stop_if_match?
end end
end end
if from != to if from != to
puts " [#{"rewrite".foreground(:blue)}] #{orig_from} >> #{to} failed!" [ "#{orig_from} >> #{to} failed!", "Result: #{from}"
puts " [#{"rewrite".foreground(:blue)}] Result: #{from}" ].each do |line|
puts " [#{"rewrite".foreground(:blue)}] #{line}"
end
end end
end end
@ -208,6 +211,13 @@ module Apache
def stop_if_match?; false; end def stop_if_match?; false; end
def forbidden?; false; end def forbidden?; false; end
def require_regexp?; false; end def require_regexp?; false; end
def from_tester(from, opts)
from = test(from, opts)
from = @from if (@to == '-')
from = :http_forbidden if (forbidden?)
from
end
end end
# A RewriteRule definition # A RewriteRule definition
@ -350,20 +360,23 @@ module Apache
"#{tag} #{[@from.quoteize, @to.quoteize, @options].compact.flatten * " "}" "#{tag} #{[@from.quoteize, @to.quoteize, @options].compact.flatten * " "}"
end end
def inverse_result?
@to[0..0] == '!'
end
def actual_to
to = @to
to = to[1..-1] if inverse_result?
to
end
# Test this RewriteCond # Test this RewriteCond
def test(from, opts = {}) def test(from, opts = {})
super(from, opts) super(from, opts)
source = @from.replace_placeholderize(opts) source = @from.replace_placeholderize(opts)
to = @to to = actual_to
reverse = false
if @to[0..0] == '!'
reverse = true
to = @to[1..-1]
end
result = false
case to case to
when '-f' when '-f'
result = opts[:files].include?(source) if opts[:files] result = opts[:files].include?(source) if opts[:files]
@ -371,7 +384,7 @@ module Apache
result = source[Regexp.new(to)] result = source[Regexp.new(to)]
end end
reverse ? !result : result inverse_result? ? !result : result
end end
end end
end end

View File

@ -25,7 +25,7 @@ describe Apache::Config, "rewrites" do
apache.rewrites do apache.rewrites do
rule %r{^/$}, '/test' rule %r{^/$}, '/test'
end end
apache.to_a.should == [ apache.to_a[1..-1].should == [
'', 'RewriteRule "^/$" "/test"', '' '', 'RewriteRule "^/$" "/test"', ''
] ]
end end
@ -45,7 +45,7 @@ describe Apache::RewriteManager, "specific rewrite rules" do
rewrite_test '/', '/test' rewrite_test '/', '/test'
rewrite_test '/fail', '/test' rewrite_test '/fail', '/test'
rewrite_test '/%{REQUEST_FILENAME}', '/test', :request_filename => 'success' rewrite_test '/%{REQUEST_FILENAME}', '/test', :request_filename => 'success'
end.should == [ end[1..-1].should == [
'', '',
'RewriteCond "%{REQUEST_FILENAME}" "test"', 'RewriteCond "%{REQUEST_FILENAME}" "test"',
'RewriteRule "^/$" "/test"', 'RewriteRule "^/$" "/test"',