2010-05-11 13:25:29 +00:00
|
|
|
require 'pp'
|
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
module Apache
|
2010-05-10 19:57:34 +00:00
|
|
|
# Handle the creation of RewriteRules, RewriteConds, Redirects, and RedirectMatches
|
2010-05-05 16:25:07 +00:00
|
|
|
module Rewrites
|
2010-05-10 19:57:34 +00:00
|
|
|
# Enable the rewrite engine, optionally setting the logging level
|
|
|
|
#
|
|
|
|
# enable_rewrite_engine :log_level => 1 #=>
|
|
|
|
# RewriteEngine on
|
|
|
|
# RewriteLogLevel 1
|
2010-05-10 21:43:34 +00:00
|
|
|
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
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Pass the block to RewriteManager.build
|
2010-05-11 19:19:16 +00:00
|
|
|
def rewrites(*opt, &block)
|
|
|
|
self + indent(RewriteManager.build(*opt, &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
|
|
|
|
2010-05-11 19:19:16 +00:00
|
|
|
def rewrite(*opt, &block)
|
|
|
|
raise "You probably want rewrites #{quoteize(*opt) * " "} do" if block
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Create a permanent Redirect
|
|
|
|
#
|
|
|
|
# r301 '/here', '/there' #=> Redirect permanent "/here" "/there"
|
2010-05-05 18:39:36 +00:00
|
|
|
def r301(*opt)
|
2010-05-12 18:02:22 +00:00
|
|
|
if opt.first && !opt.first.kind_of?(::String)
|
|
|
|
raise "First parameter should be a String. Did you mean to wrap this in a rewrites block? #{opt.first}"
|
|
|
|
end
|
2010-05-05 18:39:36 +00:00
|
|
|
self << "Redirect permanent #{quoteize(*opt) * " "}"
|
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Handle the creation of Rewritable things
|
2010-05-05 16:25:07 +00:00
|
|
|
class RewriteManager
|
|
|
|
class << self
|
|
|
|
attr_accessor :rewrites
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Reset the current list of 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
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Build rewritable things from the provided block
|
2010-05-11 19:19:16 +00:00
|
|
|
def build(*opt, &block)
|
2010-05-07 20:04:06 +00:00
|
|
|
reset!
|
2010-05-05 16:25:07 +00:00
|
|
|
|
2010-05-11 19:19:16 +00:00
|
|
|
@any_tests = false
|
|
|
|
@needs_tests = false
|
2010-05-05 16:25:07 +00:00
|
|
|
self.instance_eval(&block)
|
|
|
|
|
2010-05-11 19:19:16 +00:00
|
|
|
name = opt.first || (@rewrites.empty? ? 'unnamed block' : "#{@rewrites.first.from} => #{@rewrites.first.to}")
|
|
|
|
|
|
|
|
if !@any_tests && !@rewrites.empty?
|
|
|
|
puts " [#{"rewrite".foreground(:blue)}] no tests found for #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if @needs_tests
|
|
|
|
puts " [#{"rewrite".foreground(:blue)}] #{name} needs more tests"
|
|
|
|
end
|
|
|
|
|
|
|
|
output = @rewrites.collect(&:to_a).flatten
|
|
|
|
output.unshift("# #{name}") if opt.first
|
|
|
|
|
|
|
|
output
|
2010-05-07 02:26:12 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Commit the latest rewritable thing to the list of rewrites
|
2010-05-07 02:26:12 +00:00
|
|
|
def commit!
|
|
|
|
@rewrites << @rewrite
|
|
|
|
@rewrite = nil
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Ensure that there's a RewriteRule to be worked with
|
2010-05-07 02:26:12 +00:00
|
|
|
def ensure_rewrite!
|
|
|
|
@rewrite = RewriteRule.new if !@rewrite
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Create a RewriteRule with the given options
|
|
|
|
#
|
|
|
|
# rewrite %r{/here(.*)}, '/there$1', :last => true #=>
|
|
|
|
# RewriteRule "/here(.*)" "/there$1" [L]
|
2010-05-05 16:25:07 +00:00
|
|
|
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-10 19:57:34 +00:00
|
|
|
# Create a RewriteCond with the given options
|
|
|
|
#
|
|
|
|
# cond "%{REQUEST_FILENAME}", "^/here" #=>
|
|
|
|
# RewriteCond "%{REQUEST_FILENAME}", "^/here"
|
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-10 19:57:34 +00:00
|
|
|
# Create a permanent RedirectMatch
|
|
|
|
#
|
|
|
|
# r301 %r{/here(.*)}, "/there$1" #=>
|
|
|
|
# RedirectMatch permanent "/here(.*)" "/there$1"
|
2010-05-05 18:39:36 +00:00
|
|
|
def r301(*opts)
|
|
|
|
redirect = RedirectMatchPermanent.new
|
|
|
|
redirect.rule(*opts)
|
|
|
|
|
|
|
|
@rewrites << redirect
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Test the rewritable things defined in this block
|
2010-05-05 16:25:07 +00:00
|
|
|
def rewrite_test(from, to, opts = {})
|
2010-05-11 19:19:16 +00:00
|
|
|
@any_tests = true
|
2010-05-05 16:25:07 +00:00
|
|
|
orig_from = from.dup
|
|
|
|
@rewrites.each do |r|
|
2010-05-11 13:25:29 +00:00
|
|
|
pre_from = from.dup
|
|
|
|
if r.match?(from, opts)
|
|
|
|
from = r.test(from, opts)
|
2010-05-11 19:19:16 +00:00
|
|
|
from = pre_from if (r.to == '-')
|
|
|
|
from = :http_forbidden if (r.forbidden?)
|
2010-05-11 13:25:29 +00:00
|
|
|
break if r.stop_if_match?
|
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if from != to
|
2010-05-11 19:19:16 +00:00
|
|
|
puts " [#{"rewrite".foreground(:blue)}] #{orig_from} >> #{to} failed!"
|
|
|
|
puts " [#{"rewrite".foreground(:blue)}] Result: #{from}"
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
end
|
2010-05-11 19:19:16 +00:00
|
|
|
|
|
|
|
def needs_tests
|
|
|
|
@needs_tests = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def cond_not_a_file(opts = {})
|
|
|
|
cond_file_flag '!-f', opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def cond_is_a_file(opts = {})
|
|
|
|
cond_file_flag '-f', opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def cond_not_a_directory(opts = {})
|
|
|
|
cond_file_flag '!-d', opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def cond_is_a_directory(opts = {})
|
|
|
|
cond_file_flag '-d', opts
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def cond_file_flag(flag, opts)
|
|
|
|
cond opts[:filename_only] ? "%{REQUEST_FILENAME}" : "%{DOCUMENT_ROOT}%{REQUEST_FILENAME}", flag
|
|
|
|
end
|
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Common methods for testing rewritable things that use regular expressions
|
2010-05-07 20:04:06 +00:00
|
|
|
module RegularExpressionMatcher
|
2010-05-10 19:57:34 +00:00
|
|
|
# Test this rewritable thing
|
2010-05-07 20:04:06 +00:00
|
|
|
def test(from, opts = {})
|
2010-05-12 18:24:35 +00:00
|
|
|
from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |match| '\\' + $1 })
|
2010-05-10 15:22:02 +00:00
|
|
|
replace_placeholders(from, opts)
|
|
|
|
end
|
|
|
|
|
2010-05-11 13:25:29 +00:00
|
|
|
def match?(from, opts = {})
|
|
|
|
replace_placeholders(from, opts)[@from]
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Replace the placeholders in this rewritable thing
|
2010-05-12 18:24:35 +00:00
|
|
|
def replace_placeholders(string, 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
|
2010-05-12 18:24:35 +00:00
|
|
|
string = string.gsub('%{' + opt.to_s.upcase + '}', value)
|
2010-05-10 15:22:02 +00:00
|
|
|
end
|
2010-05-07 20:04:06 +00:00
|
|
|
end
|
2010-05-12 18:24:35 +00:00
|
|
|
string.gsub(%r{%\{[^\}]+\}}, '')
|
2010-05-07 20:04:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# A matchable thing to be extended
|
2010-05-05 18:39:36 +00:00
|
|
|
class MatchableThing
|
2010-05-07 02:26:12 +00:00
|
|
|
include Apache::Quoteize
|
|
|
|
|
2010-05-11 19:19:16 +00:00
|
|
|
attr_reader :from, :to
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# The Apache directive tag for this thing
|
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-11 13:25:29 +00:00
|
|
|
|
|
|
|
def stop_if_match?; false; end
|
2010-05-11 19:19:16 +00:00
|
|
|
def forbidden?; false; end
|
2010-05-07 20:04:06 +00:00
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# A RewriteRule definition
|
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-11 13:25:29 +00:00
|
|
|
@input_options = {}
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Define the rule, passing in additional options
|
|
|
|
#
|
|
|
|
# rule %r{^/here}, '/there', { :last => true, :preserve_query_string => true }
|
2010-05-11 13:25:29 +00:00
|
|
|
def rule(from, to, options = {})
|
2010-05-07 20:04:06 +00:00
|
|
|
super(from, to)
|
|
|
|
|
|
|
|
raise "from must be a Regexp" if !from.kind_of?(Regexp)
|
2010-05-05 16:25:07 +00:00
|
|
|
|
2010-05-11 13:25:29 +00:00
|
|
|
@input_options = options
|
|
|
|
|
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'
|
2010-05-11 19:19:16 +00:00
|
|
|
when :forbidden
|
|
|
|
'F'
|
|
|
|
when :no_escape
|
|
|
|
'NE'
|
2010-05-11 13:25:29 +00:00
|
|
|
when :redirect
|
2010-05-11 19:19:16 +00:00
|
|
|
(value == true) ? 'R' : "R=#{value}"
|
2010-05-10 21:43:34 +00:00
|
|
|
when :pass_through
|
|
|
|
'PT'
|
2010-05-11 21:31:41 +00:00
|
|
|
when :preserve_query_string, :query_string_append
|
2010-05-07 02:26:12 +00:00
|
|
|
'QSA'
|
2010-05-11 19:19:16 +00:00
|
|
|
when :env
|
|
|
|
"E=#{value}"
|
2010-05-07 02:26:12 +00:00
|
|
|
end
|
2010-05-11 13:25:29 +00:00
|
|
|
end.compact.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-10 19:57:34 +00:00
|
|
|
# Add a RewriteCondition to this RewriteRule
|
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
|
2010-05-11 19:19:16 +00:00
|
|
|
[ ('' if !@conditions.empty?), @conditions.collect(&:to_s), super ].flatten
|
2010-05-07 20:04:06 +00:00
|
|
|
end
|
2010-05-10 15:22:02 +00:00
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Test this RewriteRule, ensuring the RewriteConds also match
|
2010-05-10 15:22:02 +00:00
|
|
|
def test(from, opts = {})
|
2010-05-11 19:19:16 +00:00
|
|
|
opts[:request_uri] = from
|
2010-05-11 13:25:29 +00:00
|
|
|
result = from
|
|
|
|
|
|
|
|
result = super(from, opts) if match?(from, opts)
|
|
|
|
|
|
|
|
replace_placeholders(result, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def match?(from, opts = {})
|
2010-05-11 19:19:16 +00:00
|
|
|
opts[:request_uri] = from
|
2010-05-10 15:22:02 +00:00
|
|
|
ok = true
|
2010-05-11 13:25:29 +00:00
|
|
|
|
2010-05-10 15:22:02 +00:00
|
|
|
@conditions.each do |c|
|
|
|
|
ok = false if !c.test(from, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
if ok
|
|
|
|
super(from, opts)
|
|
|
|
else
|
2010-05-11 13:25:29 +00:00
|
|
|
false
|
2010-05-10 15:22:02 +00:00
|
|
|
end
|
|
|
|
end
|
2010-05-11 13:25:29 +00:00
|
|
|
|
|
|
|
def stop_if_match?
|
|
|
|
@input_options[:last]
|
|
|
|
end
|
2010-05-11 19:19:16 +00:00
|
|
|
|
|
|
|
def forbidden?
|
|
|
|
@input_options[:forbidden]
|
|
|
|
end
|
2010-05-05 18:39:36 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# A permanent RedirectMatch
|
2010-05-05 18:39:36 +00:00
|
|
|
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
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
def rule(from, to)
|
|
|
|
super(from, to)
|
|
|
|
|
|
|
|
raise "from must be a Regexp" if !from.kind_of?(Regexp)
|
|
|
|
end
|
|
|
|
|
2010-05-07 20:04:06 +00:00
|
|
|
def to_s
|
|
|
|
"#{tag} #{[quoteize(@from.source), quoteize(@to)].compact.flatten * " "}"
|
|
|
|
end
|
2010-05-11 13:25:29 +00:00
|
|
|
|
|
|
|
def stop_if_match; true; end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
2010-05-07 02:26:12 +00:00
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# A RewriteCond
|
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
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Define a RewriteCond
|
|
|
|
#
|
|
|
|
# rule "%{REQUEST_FILENAME}", "^/here", :case_insensitive #=>
|
|
|
|
# RewriteCond "%{REQUEST_FILENAME}" "^/here" [NC]
|
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
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Test this RewriteCond
|
2010-05-10 15:22:02 +00:00
|
|
|
def test(from, opts = {})
|
|
|
|
super(from, opts)
|
|
|
|
source = replace_placeholders(@from, opts)
|
|
|
|
|
2010-05-11 19:19:16 +00:00
|
|
|
to = @to
|
|
|
|
reverse = false
|
|
|
|
|
|
|
|
if @to[0..0] == '!'
|
|
|
|
reverse = true
|
|
|
|
to = @to[1..-1]
|
|
|
|
end
|
|
|
|
|
2010-05-10 15:22:02 +00:00
|
|
|
result = false
|
2010-05-12 18:24:35 +00:00
|
|
|
case to
|
|
|
|
when '-f'
|
|
|
|
result = opts[:files].include?(source) if opts[:files]
|
2010-05-10 15:22:02 +00:00
|
|
|
else
|
2010-05-11 19:19:16 +00:00
|
|
|
result = source[Regexp.new(to)]
|
2010-05-10 15:22:02 +00:00
|
|
|
end
|
|
|
|
|
2010-05-11 19:19:16 +00:00
|
|
|
reverse ? !result : result
|
2010-05-10 15:22:02 +00:00
|
|
|
end
|
2010-05-07 02:26:12 +00:00
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|