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
|
2010-05-05 18:39:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
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 })
|
2010-05-10 15:22:02 +00:00
|
|
|
replace_placeholders(from, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_placeholders(s, opts)
|
2010-05-07 20:04:06 +00:00
|
|
|
opts.each do |opt, value|
|
2010-05-10 15:22:02 +00:00
|
|
|
case value
|
|
|
|
when String
|
|
|
|
s = s.gsub('%{' + opt.to_s.upcase + '}', value)
|
|
|
|
end
|
2010-05-07 20:04:06 +00:00
|
|
|
end
|
2010-05-10 15:22:02 +00:00
|
|
|
s
|
2010-05-07 20:04:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
class MatchableThing
|
2010-05-07 02:26:12 +00:00
|
|
|
include Apache::Quoteize
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
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
|
2010-05-10 15:27:56 +00:00
|
|
|
end.sort
|
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-05 18:39:36 +00:00
|
|
|
|
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
|
2010-05-10 15:22:02 +00:00
|
|
|
|
|
|
|
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
|
2010-05-05 18:39:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class RedirectMatchPermanent < MatchableThing
|
2010-05-07 20:04:06 +00:00
|
|
|
include RegularExpressionMatcher
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
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
|
2010-05-10 15:22:02 +00:00
|
|
|
include RegularExpressionMatcher
|
|
|
|
|
2010-05-07 02:26:12 +00:00
|
|
|
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
|
2010-05-10 15:22:02 +00:00
|
|
|
|
|
|
|
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
|
2010-05-07 02:26:12 +00:00
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|