/
http_router.rb
217 lines (195 loc) · 5.62 KB
/
http_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require 'http_router'
require 'lotus/utils/io'
require 'lotus/routing/endpoint_resolver'
require 'lotus/routing/route'
require 'lotus/routing/parsers'
require 'lotus/routing/force_ssl'
require 'lotus/utils/path_prefix'
Lotus::Utils::IO.silence_warnings do
HttpRouter::Route::VALID_HTTP_VERBS = %w{GET POST PUT PATCH DELETE HEAD OPTIONS TRACE}
end
module Lotus
module Routing
# Invalid route
# This is raised when the router fails to recognize a route, because of the
# given arguments.
#
# @since 0.1.0
class InvalidRouteException < ::StandardError
end
# HTTP router
#
# This implementation is based on ::HttpRouter (http_router gem).
#
# Lotus::Router wraps an instance of this class, in order to protect its
# public API from any future change of ::HttpRouter.
#
# @since 0.1.0
# @api private
class HttpRouter < ::HttpRouter
# Initialize the router.
#
# @see Lotus::Router#initialize
#
# @since 0.1.0
# @api private
def initialize(options = {}, &blk)
super(options, &nil)
@default_scheme = options[:scheme] if options[:scheme]
@default_host = options[:host] if options[:host]
@default_port = options[:port] if options[:port]
@route_class = options[:route] || Routing::Route
@resolver = options[:resolver] || Routing::EndpointResolver.new(options)
@parsers = Routing::Parsers.new(options[:parsers])
@prefix = Utils::PathPrefix.new(options[:prefix] || '')
@force_ssl = Lotus::Routing::ForceSsl.new(!!options[:force_ssl], host: @default_host, port: @default_port)
end
# Separator between controller and action name.
#
# @see Lotus::Routing::EndpointResolver::ACTION_SEPARATOR
#
# @since 0.1.0
# @api private
def action_separator
@resolver.action_separator
end
# Finds a path from the given options.
#
# @see Lotus::Routing::EndpointResolver#find
#
# @since 0.1.0
# @api private
def find(options)
@resolver.find(options)
end
# Generate a relative URL for a specified named route.
#
# @see Lotus::Router#path
#
# @since 0.1.0
# @api private
def path(route, *args)
_rescue_url_recognition { super }
end
# Generate an absolute URL for a specified named route.
#
# @see Lotus::Router#path
#
# @since 0.1.0
# @api private
def url(route, *args)
_rescue_url_recognition { super }
end
# Support for GET HTTP verb
#
# @see Lotus::Router#options
#
# @since 0.4.1
# @api private
def get(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end
# Support for POST HTTP verb
#
# @see Lotus::Router#post
#
# @since 0.4.1
# @api private
def post(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end
# Support for PUT HTTP verb
#
# @see Lotus::Router#put
#
# @since 0.4.1
# @api private
def put(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end
# Support for PATCH HTTP verb
#
# @see Lotus::Router#patch
#
# @since 0.4.1
# @api private
def patch(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end
# Support for DELETE HTTP verb
#
# @see Lotus::Router#delete
#
# @since 0.4.1
# @api private
def delete(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end
# Support for TRACE HTTP verb
#
# @see Lotus::Router#trace
#
# @since 0.4.1
# @api private
def trace(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end
# Support for OPTIONS HTTP verb
#
# @see Lotus::Router#options
#
# @since 0.1.0
# @api private
def options(path, options = {}, &blk)
add_with_request_method(@prefix.join(path), :options, options, &blk)
end
# Allow to mount a Rack app
#
# @see Lotus::Router#mount
#
# @since 0.1.1
# @api private
def mount(app, options)
add("#{ options.fetch(:at) }*").to(
@resolver.resolve(to: app)
)
end
# @api private
def raw_call(env, &blk)
if response = @force_ssl.call(env)
response
else
super(@parsers.call(env))
end
end
# @api private
def reset!
uncompile
@routes, @named_routes, @root = [], Hash.new{|h,k| h[k] = []}, Node::Root.new(self)
@default_host, @default_port, @default_scheme = 'localhost', 80, 'http'
end
# @api private
def pass_on_response(response)
super response.to_a
end
# @api private
def no_response(request, env)
if request.acceptable_methods.any? && !request.acceptable_methods.include?(env['REQUEST_METHOD'])
[405, {'Allow' => request.acceptable_methods.sort.join(", ")}, []]
else
@default_app.call(env)
end
end
private
def add_with_request_method(path, method, opts = {}, &app)
super.generate(@resolver, opts, &app)
end
def _rescue_url_recognition
yield
rescue ::HttpRouter::InvalidRouteException,
::HttpRouter::TooManyParametersException => e
raise Routing::InvalidRouteException.new(e.message)
end
end
end
end