-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrouter.rb
184 lines (153 loc) · 5.12 KB
/
router.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true
require 'grape/router/route'
require 'grape/util/cache'
module Grape
class Router
attr_reader :map, :compiled
class Any < AttributeTranslator
attr_reader :pattern, :regexp, :index
def initialize(pattern, regexp, index, **attributes)
@pattern = pattern
@regexp = regexp
@index = index
super(attributes)
end
end
class NormalizePathCache < Grape::Util::Cache
def initialize
@cache = Hash.new do |h, path|
normalized_path = +"/#{path}"
normalized_path.squeeze!('/')
normalized_path.sub!(%r{/+\Z}, '')
normalized_path = '/' if normalized_path.empty?
h[path] = -normalized_path
end
end
end
def self.normalize_path(path)
NormalizePathCache[path]
end
def self.supported_methods
@supported_methods ||= Grape::Http::Headers::SUPPORTED_METHODS + ['*']
end
def initialize
@neutral_map = []
@map = Hash.new { |hash, key| hash[key] = [] }
@optimized_map = Hash.new { |hash, key| hash[key] = // }
end
def compile!
return if compiled
@union = Regexp.union(@neutral_map.map(&:regexp))
self.class.supported_methods.each do |method|
routes = map[method]
@optimized_map[method] = routes.map.with_index do |route, index|
route.index = index
route.regexp = Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
end
@optimized_map[method] = Regexp.union(@optimized_map[method])
end
@compiled = true
end
def append(route)
map[route.request_method] << route
end
def associate_routes(pattern, **options)
regexp = Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
@neutral_map << Any.new(pattern, regexp, @neutral_map.length, **options)
end
def call(env)
with_optimization do
response, route = identity(env)
response || rotation(env, route)
end
end
def recognize_path(input)
any = with_optimization { greedy_match?(input) }
return if any == default_response
any.endpoint
end
private
def identity(env)
route = nil
response = transaction(env) do |input, method|
route = match?(input, method)
process_route(route, env) if route
end
[response, route]
end
def rotation(env, exact_route = nil)
response = nil
input, method = *extract_input_and_method(env)
map[method].each do |route|
next if exact_route == route
next unless route.match?(input)
response = process_route(route, env)
break unless cascade?(response)
end
response
end
def transaction(env)
input, method = *extract_input_and_method(env)
response = yield(input, method)
return response if response && !(cascade = cascade?(response))
neighbor = greedy_match?(input)
# If neighbor exists and request method is OPTIONS,
# return response by using #call_with_allow_headers.
return call_with_allow_headers(
env,
neighbor.allow_header,
neighbor.endpoint
) if neighbor && method == Grape::Http::Headers::OPTIONS && !cascade
route = match?(input, '*')
return neighbor.endpoint.call(env) if neighbor && cascade && route
if route
response = process_route(route, env)
return response if response && !(cascade = cascade?(response))
end
!cascade && neighbor ? call_with_allow_headers(env, neighbor.allow_header, neighbor.endpoint) : nil
end
def process_route(route, env)
input, = *extract_input_and_method(env)
routing_args = env[Grape::Env::GRAPE_ROUTING_ARGS]
env[Grape::Env::GRAPE_ROUTING_ARGS] = make_routing_args(routing_args, route, input)
route.exec(env)
end
def make_routing_args(default_args, route, input)
args = default_args || { route_info: route }
args.merge(route.params(input))
end
def extract_input_and_method(env)
input = string_for(env[Grape::Http::Headers::PATH_INFO])
method = env[Grape::Http::Headers::REQUEST_METHOD]
[input, method]
end
def with_optimization
compile! unless compiled
yield || default_response
end
def default_response
[404, { Grape::Http::Headers::X_CASCADE => 'pass' }, ['404 Not Found']]
end
def match?(input, method)
current_regexp = @optimized_map[method]
return unless current_regexp.match(input)
last_match = Regexp.last_match
@map[method].detect { |route| last_match["_#{route.index}"] }
end
def greedy_match?(input)
return unless @union.match(input)
last_match = Regexp.last_match
@neutral_map.detect { |route| last_match["_#{route.index}"] }
end
def call_with_allow_headers(env, methods, endpoint)
env[Grape::Env::GRAPE_ALLOWED_METHODS] = methods.join(', ').freeze
endpoint.call(env)
end
def cascade?(response)
response && response[1][Grape::Http::Headers::X_CASCADE] == 'pass'
end
def string_for(input)
self.class.normalize_path(input)
end
end
end