/
filter_redirect.rb
50 lines (44 loc) · 1.08 KB
/
filter_redirect.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true
# :markup: markdown
module ActionDispatch
module Http
module FilterRedirect
FILTERED = "[FILTERED]" # :nodoc:
def filtered_location # :nodoc:
if location_filter_match?
FILTERED
else
parameter_filtered_location
end
end
private
def location_filters
if request
request.get_header("action_dispatch.redirect_filter") || []
else
[]
end
end
def location_filter_match?
location_filters.any? do |filter|
if String === filter
location.include?(filter)
elsif Regexp === filter
location.match?(filter)
end
end
end
def parameter_filtered_location
uri = URI.parse(location)
unless uri.query.nil? || uri.query.empty?
uri.query.gsub!(FilterParameters::PAIR_RE) do
request.parameter_filter.filter($1 => $2).first.join("=")
end
end
uri.to_s
rescue URI::Error
FILTERED
end
end
end
end