From f6397e9034861d2cf5b6aa49145fe6320758a8ee Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 6 May 2010 22:26:12 -0400 Subject: [PATCH] rewrite work and log file cleanup --- lib/apache/config.rb | 2 +- lib/apache/logging.rb | 26 ++++++----------- lib/apache/master.rb | 7 +++++ lib/apache/rewrites.rb | 64 ++++++++++++++++++++++++++++++++++-------- 4 files changed, 70 insertions(+), 29 deletions(-) diff --git a/lib/apache/config.rb b/lib/apache/config.rb index 9497e95..70e5d75 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -172,6 +172,6 @@ module Apache end - block_methods :virtual_host, :files_match, :location + block_methods :virtual_host, :files_match, :location, :files end end diff --git a/lib/apache/logging.rb b/lib/apache/logging.rb index 8d0e6e1..ab85a24 100644 --- a/lib/apache/logging.rb +++ b/lib/apache/logging.rb @@ -1,23 +1,15 @@ module Apache module Logging - def error_log(*opts) - handle_log 'ErrorLog', opts.first, opts.first, opts[1..-1] - end + [ :custom, :error, :script, :rewrite ].each do |type| + class_eval <<-EOT + def #{type}_log(*opts) + handle_log '#{type.to_s.capitalize}Log', opts.first, opts.first, opts[1..-1] + end - def custom_log(*opts) - handle_log 'CustomLog', opts.first, opts.first, opts[1..-1] - end - - def rotate_custom_log(*opts) - handle_log 'CustomLog', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1] - end - - def rotate_error_log(*opts) - handle_log 'ErrorLog', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1] - end - - def rotate_script_log(*opts) - handle_log 'ScriptLog', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1] + def rotate_#{type}_log(*opts) + handle_log '#{type.to_s.capitalize}Log', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1] + end + EOT end def combined_log_format(name = 'combined') diff --git a/lib/apache/master.rb b/lib/apache/master.rb index 7af27a9..e401909 100644 --- a/lib/apache/master.rb +++ b/lib/apache/master.rb @@ -46,5 +46,12 @@ module Apache end alias :script_alias! :script_alias + + def add_type!(mime, extension, options = {}) + self << "AddType #{mime} #{extension}" + options.each do |type, value| + self << "Add#{type.to_s.capitalize} #{value} #{extension}" + end + end end end diff --git a/lib/apache/rewrites.rb b/lib/apache/rewrites.rb index 0b3b652..d9a2a3f 100644 --- a/lib/apache/rewrites.rb +++ b/lib/apache/rewrites.rb @@ -31,14 +31,27 @@ module Apache self.instance_eval(&block) - @rewrites + @rewrites.collect(&:to_a).flatten + end + + def commit! + @rewrites << @rewrite + @rewrite = nil + end + + def ensure_rewrite! + @rewrite = RewriteRule.new if !@rewrite end def rewrite(*opts) - @rewrite = RewriteRule.new + ensure_rewrite! @rewrite.rule(*opts) + commit! + end - @rewrites << @rewrite + def cond(*opts) + ensure_rewrite! + @rewrite.cond(*opts) end def r301(*opts) @@ -63,6 +76,8 @@ module Apache end class MatchableThing + include Apache::Quoteize + def tag; raise 'Override this method'; end def initialize @@ -75,7 +90,14 @@ module Apache def rule(from, to, *opts) @from = from @to = to - @options = opts + @options = opts.first + end + + def cond(from, to, *opts) + rewrite_cond = RewriteCondition.new + rewrite_cond.cond(from, to, *opts) + + @conditions << rewrite_cond end def test(from, opts) @@ -86,18 +108,29 @@ module Apache from end - def to_s - output = [] - - @conditions.each do |condition| + def to_a + output = @conditions.collect(&:to_s) + options = @options.collect do |key, value| + case key + when :last + 'L' + when :preserve_query_string + 'QSA' + end end - output << "#{tag} #{[@from.source, @to, @options].flatten * " "}" + if !options.empty? + options = "[#{options * ','}]" + else + options = nil + end - output * "\n" + output << "#{tag} #{[quoteize(@from.source), quoteize(@to), options].compact.flatten * " "}" + + output end -end + end class RewriteRule < MatchableThing def tag; 'RewriteRule'; end @@ -106,4 +139,13 @@ end class RedirectMatchPermanent < MatchableThing def tag; 'RedirectMatch permanent'; end end + + class RewriteCondition < MatchableThing + def tag; 'RewriteCond'; end + alias :cond :rule + + def to_s + "#{tag} #{[quoteize(@from), quoteize(@to), @options].flatten * " "}" + end + end end