apache-config-generator/lib/apache/rewrites.rb

208 lines
3.9 KiB
Ruby
Raw Normal View History

2010-05-05 16:25:07 +00:00
module Apache
module Rewrites
def enable_rewrite_engine(options)
2010-05-06 14:40:45 +00:00
self << ''
2010-05-05 16:25:07 +00:00
rewrite_engine! :on
options.each do |option, value|
case option
when :log_level
rewrite_log_level! value
end
end
2010-05-06 14:40:45 +00:00
self << ''
2010-05-05 16:25:07 +00:00
end
def rewrites(&block)
self + indent(RewriteManager.build(&block))
2010-05-06 14:40:45 +00:00
self << ''
2010-05-05 16:25:07 +00:00
end
def r301(*opt)
self << "Redirect permanent #{quoteize(*opt) * " "}"
end
2010-05-05 16:25:07 +00:00
end
class RewriteManager
class << self
attr_accessor :rewrites
2010-05-07 20:04:06 +00:00
def reset!
2010-05-05 16:25:07 +00:00
@rewrites = []
2010-05-07 20:04:06 +00:00
end
def build(&block)
reset!
2010-05-05 16:25:07 +00:00
self.instance_eval(&block)
2010-05-07 02:26:12 +00:00
@rewrites.collect(&:to_a).flatten
end
def commit!
@rewrites << @rewrite
@rewrite = nil
end
def ensure_rewrite!
@rewrite = RewriteRule.new if !@rewrite
2010-05-05 16:25:07 +00:00
end
def rewrite(*opts)
2010-05-07 02:26:12 +00:00
ensure_rewrite!
2010-05-05 16:25:07 +00:00
@rewrite.rule(*opts)
2010-05-07 02:26:12 +00:00
commit!
end
2010-05-05 16:25:07 +00:00
2010-05-07 20:04:06 +00:00
alias :rule :rewrite
2010-05-07 02:26:12 +00:00
def cond(*opts)
ensure_rewrite!
@rewrite.cond(*opts)
2010-05-05 16:25:07 +00:00
end
def r301(*opts)
redirect = RedirectMatchPermanent.new
redirect.rule(*opts)
@rewrites << redirect
end
2010-05-05 16:25:07 +00:00
def rewrite_test(from, to, opts = {})
orig_from = from.dup
@rewrites.each do |r|
from = r.test(from, opts)
end
if from != to
puts "[warn] #{orig_from} >> #{to} failed!"
puts "[warn] Result: #{from}"
end
end
end
end
2010-05-07 20:04:06 +00:00
module RegularExpressionMatcher
def test(from, opts = {})
from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |m| '\\' + $1 })
opts.each do |opt, value|
from = from.gsub('%{' + opt.to_s.upcase + '}', value)
end
from
end
end
class MatchableThing
2010-05-07 02:26:12 +00:00
include Apache::Quoteize
def tag; raise 'Override this method'; end
2010-05-05 16:25:07 +00:00
def initialize
@from = nil
@to = nil
end
2010-05-07 20:04:06 +00:00
def rule(from, to)
2010-05-05 16:25:07 +00:00
@from = from
@to = to
2010-05-07 02:26:12 +00:00
end
2010-05-07 20:04:06 +00:00
def to_s
"#{tag} #{[quoteize(@from), quoteize(@to)].compact.flatten * " "}"
end
2010-05-07 02:26:12 +00:00
2010-05-07 20:04:06 +00:00
def to_a
[ to_s ]
2010-05-05 16:25:07 +00:00
end
2010-05-07 20:04:06 +00:00
end
2010-05-05 16:25:07 +00:00
2010-05-07 20:04:06 +00:00
class RewriteRule < MatchableThing
include RegularExpressionMatcher
def tag; 'RewriteRule'; end
def initialize
super
@conditions = []
@options = nil
2010-05-05 16:25:07 +00:00
end
2010-05-07 20:04:06 +00:00
def rule(from, to,options = {})
super(from, to)
raise "from must be a Regexp" if !from.kind_of?(Regexp)
2010-05-05 16:25:07 +00:00
2010-05-07 20:04:06 +00:00
options = options.collect do |key, value|
2010-05-07 02:26:12 +00:00
case key
when :last
'L'
when :preserve_query_string
'QSA'
end
end
2010-05-05 16:25:07 +00:00
2010-05-07 20:04:06 +00:00
@options = !options.empty? ? "[#{options * ','}]" : nil
end
2010-05-05 16:25:07 +00:00
2010-05-07 20:04:06 +00:00
def cond(from, to, *opts)
rewrite_cond = RewriteCondition.new
rewrite_cond.cond(from, to, *opts)
2010-05-05 16:25:07 +00:00
2010-05-07 20:04:06 +00:00
@conditions << rewrite_cond
2010-05-05 16:25:07 +00:00
end
2010-05-07 20:04:06 +00:00
def to_s
"#{tag} #{[quoteize(@from.source), quoteize(@to), @options].compact.flatten * " "}"
end
def to_a
output = @conditions.collect(&:to_s)
output += super
output
end
end
class RedirectMatchPermanent < MatchableThing
2010-05-07 20:04:06 +00:00
include RegularExpressionMatcher
def tag; 'RedirectMatch permanent'; end
2010-05-07 20:04:06 +00:00
def to_s
"#{tag} #{[quoteize(@from.source), quoteize(@to)].compact.flatten * " "}"
end
2010-05-05 16:25:07 +00:00
end
2010-05-07 02:26:12 +00:00
class RewriteCondition < MatchableThing
def tag; 'RewriteCond'; end
2010-05-07 20:04:06 +00:00
def rule(from, to, *opts)
super(from, to)
options = opts.collect do |opt|
case opt
when :or
'OR'
when :case_insensitive
'NC'
when :no_vary
'NV'
end
end
@options = (!options.empty?) ? "[#{options * ','}]" : nil
end
2010-05-07 02:26:12 +00:00
alias :cond :rule
2010-05-07 20:04:06 +00:00
def initialize
super
@options = nil
end
2010-05-07 02:26:12 +00:00
def to_s
2010-05-07 20:04:06 +00:00
"#{tag} #{[quoteize(@from), quoteize(@to), @options].compact.flatten * " "}"
2010-05-07 02:26:12 +00:00
end
end
2010-05-05 16:25:07 +00:00
end