-
Notifications
You must be signed in to change notification settings - Fork 510
/
routing.rb
1016 lines (915 loc) · 36.3 KB
/
routing.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'http_router' unless defined?(HttpRouter)
require 'padrino-core/support_lite' unless defined?(SupportLite)
##
# Adds to Sinatra +controller+ informations
#
# @private
class Sinatra::Request
attr_accessor :route_obj
def controller
route_obj && route_obj.controller
end
def action
route_obj && route_obj.action
end
end
##
# HttpRouter adapter
#
# @private
class HttpRouter
def rewrite_partial_path_info(env, request); end
def rewrite_path_info(env, request); end
def process_destination_path(path, env)
Thread.current['padrino.instance'].instance_eval do
request.route_obj = path.route
@_response_buffer = nil
@route = path.route
@params ||= {}
@params.update(env['router.params'])
@block_params = if match_data = env['router.request'].extra_env['router.regex_match']
params_list = match_data.to_a
params_list.shift
@params[:captures] = params_list
params_list
else
env['router.request'].params
end
# Provide access to the current controller to the request
# Now we can eval route, but because we have "throw halt" we need to be
# (en)sure to reset old layout and run controller after filters.
original_params = @params
parent_layout = @layout
successful = false
begin
filter! :before
(@route.before_filters - settings.filters[:before]).each { |block| instance_eval(&block) }
@layout = path.route.use_layout if path.route.use_layout
@route.custom_conditions.each { |block| pass if block.bind(self).call == false } if @route.custom_conditions
halt_response = catch(:halt) { route_eval { @route.dest[self, @block_params] } }
@_response_buffer = halt_response.is_a?(Array) ? halt_response.last : halt_response
successful = true
halt halt_response
ensure
(@route.after_filters - settings.filters[:after]).each { |block| instance_eval(&block) } if successful
@layout = parent_layout
@params = original_params
end
end
end
# @private
class Route
VALID_HTTP_VERBS.replace %w[GET POST PUT PATCH DELETE HEAD OPTIONS LINK UNLINK]
attr_accessor :use_layout, :controller, :action, :cache, :cache_key, :cache_expires_in, :parent
def before_filters(&block)
@_before_filters ||= []
@_before_filters << block if block_given?
@_before_filters
end
def after_filters(&block)
@_after_filters ||= []
@_after_filters << block if block_given?
@_after_filters
end
def custom_conditions(&block)
@_custom_conditions ||= []
@_custom_conditions << block if block_given?
@_custom_conditions
end
def significant_variable_names
@significant_variable_names ||= @original_path.nil? ? [] : @original_path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
end
end
class Node::Path
def to_code
path_ivar = inject_root_ivar(self)
"#{"if !callback && request.path.size == 1 && request.path.first == '' && (request.rack_request.head? || request.rack_request.get?) && request.rack_request.path_info[-1] == ?/
catch(:pass) do
response = ::Rack::Response.new
response.redirect(request.rack_request.path_info[0, request.rack_request.path_info.size - 1], 302)
return response.finish
end
end" if router.redirect_trailing_slash?}
#{"if request.#{router.ignore_trailing_slash? ? 'path_finished?' : 'path.empty?'}" unless route.match_partially}
catch(:pass) do
if callback
request.called = true
callback.call(Response.new(request, #{path_ivar}))
else
env = request.rack_request.dup.env
env['router.request'] = request
env['router.params'] ||= {}
#{"env['router.params'].merge!(Hash[#{param_names.inspect}.zip(request.params)])" if dynamic?}
@router.rewrite#{"_partial" if route.match_partially}_path_info(env, request)
response = @router.process_destination_path(#{path_ivar}, env)
return response unless router.pass_on_response(response)
end
end
#{"end" unless route.match_partially}"
end
end
end
module Padrino
class Filter # @private
attr_reader :block
def initialize(mode, scoped_controller, options, args, &block)
@mode, @scoped_controller, @options, @args, @block = mode, scoped_controller, options, args, block
end
def apply?(request)
detect = @args.any? do |arg|
case arg
when Symbol then request.route_obj && (request.route_obj.name == arg or request.route_obj.name == [@scoped_controller, arg].flatten.join("_").to_sym)
else arg === request.path_info
end
end || @options.any? do |name, val|
case name
when :agent then val === request.user_agent
else val === request.send(name)
end
end
detect ^ !@mode
end
def to_proc
if @args.empty? && @options.empty?
block
else
filter = self
proc { instance_eval(&filter.block) if filter.apply?(request) }
end
end
end
##
# Padrino provides advanced routing definition support to make routes and
# url generation much easier. This routing system supports named route
# aliases and easy access to url paths. The benefits of this is that instead
# of having to hard-code route urls into every area of your application, now
# we can just define the urls in a single spot and then attach an alias
# which can be used to refer to the url throughout the application.
#
module Routing
# Defines common content-type alias mappings
CONTENT_TYPE_ALIASES = { :htm => :html } unless defined?(CONTENT_TYPE_ALIASES)
# Defines the available route priorities supporting route deferrals.
ROUTE_PRIORITY = {:high => 0, :normal => 1, :low => 2} unless defined?(ROUTE_PRIORITY)
# Raised when a route was invalid or cannot be processed.
class UnrecognizedException < RuntimeError; end
class Parent < String # @private
attr_reader :map
attr_reader :optional
attr_reader :options
alias_method :optional?, :optional
def initialize(value, options={})
super(value.to_s)
@map = options.delete(:map)
@optional = options.delete(:optional)
@options = options
end
end
class << self
##
# Main class that register this extension.
#
def registered(app)
app.send(:include, InstanceMethods)
app.extend(ClassMethods)
end
alias :included :registered
end
# Class methods responsible for enhanced routing for controllers.
module ClassMethods
##
# Method to organize our routes in a better way.
#
# @param [Array] args
# Controller arguments.
#
# @yield []
# The given block will be used to define the routes within the
# Controller.
#
# @example
# controller :admin do
# get :index do; ...; end
# get :show, :with => :id do; ...; end
# end
#
# url(:admin_index) # => "/admin"
# url(:admin_show, :id => 1) # "/admin/show/1"
#
# @example Using named routes follow the sinatra way:
# controller "/admin" do
# get "/index" do; ...; end
# get "/show/:id" do; ...; end
# end
#
# @example Supply +:provides+ to all controller routes:
# controller :provides => [:html, :xml, :json] do
# get :index do; "respond to html, xml and json"; end
# post :index do; "respond to html, xml and json"; end
# get :foo do; "respond to html, xml and json"; end
# end
#
# @example Specify parent resources in padrino with the +:parent+ option on the controller:
# controllers :product, :parent => :user do
# get :index do
# # url is generated as "/user/#{params[:user_id]}/product"
# # url_for(:product, :index, :user_id => 5) => "/user/5/product"
# end
# get :show, :with => :id do
# # url is generated as "/user/#{params[:user_id]}/product/show/#{params[:id]}"
# # url_for(:product, :show, :user_id => 5, :id => 10) => "/user/5/product/show/10"
# end
# end
#
# @example Specify conditions to run for all routes:
# controller :conditions => {:protect => true} do
# def self.protect(protected)
# condition do
# halt 403, "No secrets for you!" unless params[:key] == "s3cr3t"
# end if protected
# end
#
# # This route will only return "secret stuff" if the user goes to
# # `/private?key=s3cr3t`.
# get("/private") { "secret stuff" }
#
# # And this one, too!
# get("/also-private") { "secret stuff" }
#
# # But you can override the conditions for each route as needed.
# # This route will be publicly accessible without providing the
# # secret key.
# get :index, :protect => false do
# "Welcome!"
# end
# end
#
# @example Supply default values:
# controller :lang => :de do
# get :index, :map => "/:lang" do; "params[:lang] == :de"; end
# end
#
# In a controller, before and after filters are scoped and don't affect other controllers or the main app.
# In a controller, layouts are scoped and don't affect other controllers or the main app.
#
# @example
# controller :posts do
# layout :post
# before { foo }
# after { bar }
# end
#
def controller(*args, &block)
if block_given?
options = args.extract_options!
# Controller defaults
@_controller, original_controller = args, @_controller
@_parents, original_parent = options.delete(:parent), @_parents
@_provides, original_provides = options.delete(:provides), @_provides
@_use_format, original_use_format = options.delete(:use_format), @_use_format
@_cache, original_cache = options.delete(:cache), @_cache
@_map, original_map = options.delete(:map), @_map
@_conditions, original_conditions = options.delete(:conditions), @_conditions
@_defaults, original_defaults = options, @_defaults
# Application defaults
@filters, original_filters = { :before => @filters[:before].dup, :after => @filters[:after].dup }, @filters
@layout, original_layout = nil, @layout
instance_eval(&block)
# Application defaults
@filters = original_filters
@layout = original_layout
# Controller defaults
@_controller, @_parents, @_cache = original_controller, original_parent, original_cache
@_defaults, @_provides, @_map = original_defaults, original_provides, original_map
@_conditions, @_use_format = original_conditions, original_use_format
else
include(*args) if extensions.any?
end
end
alias :controllers :controller
##
# Add a before filter hook
#
# @see #construct_filter
#
def before(*args, &block)
add_filter :before, &(args.empty? ? block : construct_filter(*args, &block))
end
##
# Add an after filter hook
#
# @see #construct_filter
#
def after(*args, &block)
add_filter :after, &(args.empty? ? block : construct_filter(*args, &block))
end
##
# Adds a filter hook to a request.
#
def add_filter(type, &block)
filters[type] << block
end
##
# Creates a filter to process before/after the matching route.
#
# @param [Array] args
#
# @example We are be able to filter with String path
# before('/') { 'only to :index' }
# get(:index} { 'foo' } # => filter match only before this.
# get(:main) { 'bar' }
#
# @example is the same of
# before(:index) { 'only to :index' }
# get(:index} { 'foo' } # => filter match only before this.
# get(:main) { 'bar' }
#
# @example it works only for the given controller
# controller :foo do
# before(:index) { 'only to for :foo_index' }
# get(:index} { 'foo' } # => filter match only before this.
# get(:main) { 'bar' }
# end
#
# controller :bar do
# before(:index) { 'only to for :bar_index' }
# get(:index} { 'foo' } # => filter match only before this.
# get(:main) { 'bar' }
# end
#
# @example if filters based on a symbol or regexp
# before :index, /main/ do; ... end
# # => match oly path that are +/+ or contains +main+
#
# @example filtering everything except an occurency
# before :except => :index do; ...; end
#
# @example you can also filter using a request param
# before :agent => /IE/ do; ...; end
# # => match +HTTP_USER_AGENT+ containing +IE+
#
# @see http://www.padrinorb.com/guides/controllers#route-filters
#
def construct_filter(*args, &block)
options = args.last.is_a?(Hash) ? args.pop : {}
except = options.key?(:except) && Array(options.delete(:except))
raise("You cannot use except with other options specified") if except && (!args.empty? || !options.empty?)
options = except.last.is_a?(Hash) ? except.pop : {} if except
Filter.new(!except, @_controller, options, Array(except || args), &block)
end
##
# Provides many parents with shallowing.
#
# @param [Symbol] name
# The parent name.
#
# @param [Hash] options
# Additional options.
#
# @example
# controllers :product do
# parent :shop, :optional => true, :map => "/my/stand"
# parent :category, :optional => true
# get :show, :with => :id do
# # generated urls:
# # "/product/show/#{params[:id]}"
# # "/my/stand/#{params[:shop_id]}/product/show/#{params[:id]}"
# # "/my/stand/#{params[:shop_id]}/category/#{params[:category_id]}/product/show/#{params[:id]}"
# # url_for(:product, :show, :id => 10) => "/product/show/10"
# # url_for(:product, :show, :shop_id => 5, :id => 10) => "/my/stand/5/product/show/10"
# # url_for(:product, :show, :shop_id => 5, :category_id => 1, :id => 10) => "/my/stand/5/category/1/product/show/10"
# end
# end
#
def parent(name, options={})
defaults = { :optional => false, :map => name.to_s }
options = defaults.merge(options)
@_parents = Array(@_parents) unless @_parents.is_a?(Array)
@_parents << Parent.new(name, options)
end
##
# Using {HttpRouter}, for features and configurations.
#
# @example
# router.add('/greedy/:greed')
# router.recognize('/simple')
#
# @see http://github.com/joshbuddy/http_router
#
def router
@router ||= HttpRouter.new
block_given? ? yield(@router) : @router
end
alias :urls :router
# Compiles the routes including deferred routes.
def compiled_router
if deferred_routes.empty?
router
else
deferred_routes.each { |_, routes| routes.each { |(route, dest)| route.to(dest) } }
@deferred_routes = nil
router
end
end
# Returns all routes that were deferred based on their priority.
def deferred_routes
@deferred_routes ||= Hash[ROUTE_PRIORITY.values.sort.map{|p| [p, []]}]
end
##
# Resets the http router and all deferred routes.
#
def reset_router!
@deferred_routes = nil
router.reset!
end
##
# Recognize a given path
#
# @param [String] path
# Path+Query to parse
#
# @return [Symbol, Hash]
# Returns controller and query params.
#
# @example Giving a controller like:
# controller :foo do
# get :bar, :map => 'foo-bar-:id'; ...; end
# end
#
# @example You should be able to reverse:
# MyApp.url(:foo_bar, :id => :mine)
# # => /foo-bar-mine
#
# @example Into this:
# MyApp.recognize_path('foo-bar-mine')
# # => [:foo_bar, :id => :mine]
#
def recognize_path(path)
responses = @router.recognize(Rack::MockRequest.env_for(path))
responses = responses[0] if responses[0].is_a?(Array)
[responses[0].path.route.name, responses[0].params]
end
##
# Instance method for url generation.
#
# @example
# url(:show, :id => 1)
# url(:show, :name => 'test', :id => 24)
# url(:show, 1)
# url(:controller_name, :show, :id => 21)
# url(:controller_show, :id => 29)
#
def url(*args)
params = args.extract_options! # parameters is hash at end
names, params_array = args.partition{|a| a.is_a?(Symbol)}
name = names.join("_").to_sym # route name is concatenated with underscores
if params.is_a?(Hash)
params[:format] = params[:format].to_s unless params[:format].nil?
params = value_to_param(params)
end
url =
if params_array.empty?
compiled_router.path(name, params)
else
compiled_router.path(name, *(params_array << params))
end
url[0,0] = conform_uri(uri_root) if defined?(uri_root)
url[0,0] = conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI']
url = "/" if url.blank?
url
rescue HttpRouter::InvalidRouteException
route_error = "route mapping for url(#{name.inspect}) could not be found!"
raise Padrino::Routing::UnrecognizedException.new(route_error)
end
alias :url_for :url
def get(path, *args, &block) # @private
conditions = @conditions.dup
route('GET', path, *args, &block)
@conditions = conditions
route('HEAD', path, *args, &block)
end
private
# Parse params from the url method
def value_to_param(value)
case value
when Array
value.map { |v| value_to_param(v) }.compact
when Hash
value.inject({}) do |memo, (k,v)|
v = value_to_param(v)
memo[k] = v unless v.nil?
memo
end
when nil then nil
else value.respond_to?(:to_param) ? value.to_param : value
end
end
# Add prefix slash if its not present and remove trailing slashes.
def conform_uri(uri_string)
uri_string.gsub(/^(?!\/)(.*)/, '/\1').gsub(/[\/]+$/, '')
end
##
# Rewrite default routes.
#
# @example
# get :index # => "/"
# get :index, "/" # => "/"
# get :index, :map => "/" # => "/"
# get :show, "/show-me" # => "/show-me"
# get :show, :map => "/show-me" # => "/show-me"
# get "/foo/bar" # => "/show"
# get :index, :parent => :user # => "/user/:user_id/index"
# get :show, :with => :id, :parent => :user # => "/user/:user_id/show/:id"
# get :show, :with => :id # => "/show/:id"
# get [:show, :id] # => "/show/:id"
# get :show, :with => [:id, :name] # => "/show/:id/:name"
# get [:show, :id, :name] # => "/show/:id/:name"
# get :list, :provides => :js # => "/list.{:format,js)"
# get :list, :provides => :any # => "/list(.:format)"
# get :list, :provides => [:js, :json] # => "/list.{!format,js|json}"
# get :list, :provides => [:html, :js, :json] # => "/list(.{!format,js|json})"
# get :list, :priority => :low # Defers route to be last
# get /pattern/, :name => :foo, :generate_with => '/foo' # Generates :foo as /foo
def route(verb, path, *args, &block)
options = case args.size
when 2
args.last.merge(:map => args.first)
when 1
map = args.shift if args.first.is_a?(String)
if args.first.is_a?(Hash)
map ? args.first.merge(:map => map) : args.first
else
{:map => map || args.first}
end
when 0
{}
else raise
end
# Do padrino parsing. We dup options so we can build HEAD request correctly
route_options = options.dup
route_options[:provides] = @_provides if @_provides
# CSRF protection is always active except when explicitly switched off
if allow_disabled_csrf
unless route_options[:csrf_protection] == false
route_options[:csrf_protection] = true
end
end
path, *route_options[:with] = path if path.is_a?(Array)
action = path
path, name, route_parents, options, route_options = *parse_route(path, route_options, verb)
options.reverse_merge!(@_conditions) if @_conditions
# Sinatra defaults
method_name = "#{verb} #{path}"
unbound_method = generate_method(method_name, &block)
block = block.arity != 0 ?
proc { |a,p| unbound_method.bind(a).call(*p) } :
proc { |a,p| unbound_method.bind(a).call }
invoke_hook(:route_added, verb, path, block)
# HTTPRouter route construction
route = router.add(path, route_options)
route.name = name if name
route.action = action
priority_name = options.delete(:priority) || :normal
priority = ROUTE_PRIORITY[priority_name] or raise("Priority #{priority_name} not recognized, try #{ROUTE_PRIORITY.keys.join(', ')}")
route.cache = options.key?(:cache) ? options.delete(:cache) : @_cache
route.parent = route_parents ? (route_parents.count == 1 ? route_parents.first : route_parents) : route_parents
route.add_request_method(verb.downcase.to_sym)
route.host = options.delete(:host) if options.key?(:host)
route.user_agent = options.delete(:agent) if options.key?(:agent)
if options.key?(:default_values)
defaults = options.delete(:default_values)
route.add_default_values(defaults) if defaults
end
options.delete_if do |option, args|
if route.significant_variable_names.include?(option)
route.add_match_with(option => Array(args).first)
true
end
end
# Add Sinatra conditions
options.each { |o, a| route.respond_to?(o) ? route.send(o, *a) : send(o, *a) }
conditions, @conditions = @conditions, []
route.custom_conditions.concat(conditions)
invoke_hook(:padrino_route_added, route, verb, path, args, options, block)
# Add Application defaults
route.before_filters.concat(@filters[:before])
route.after_filters.concat(@filters[:after])
if @_controller
route.use_layout = @layout
route.controller = Array(@_controller)[0].to_s
end
deferred_routes[priority] << [route, block]
route
end
##
# Returns the final parsed route details (modified to reflect all
# Padrino options) given the raw route. Raw route passed in could be
# a named alias or a string and is parsed to reflect provides formats,
# controllers, parents, 'with' parameters, and other options.
#
def parse_route(path, options, verb)
# We need save our originals path/options so we can perform correctly cache.
original = [path, options.dup]
# options for the route directly
route_options = {}
# We need check if path is a symbol, if that it's a named route
map = options.delete(:map)
if path.kind_of?(Symbol) # path i.e :index or :show
name = path # The route name
path = map ? map.dup : (path == :index ? '/' : path.to_s) # The route path
end
# Build our controller
controller = Array(@_controller).map { |c| c.to_s }
case path
when String # path i.e "/index" or "/show"
# Now we need to parse our 'with' params
if with_params = options.delete(:with)
path = process_path_for_with_params(path, with_params)
end
# Now we need to parse our provides
options.delete(:provides) if options[:provides].nil?
if @_use_format or format_params = options[:provides]
process_path_for_provides(path, format_params)
# options[:add_match_with] ||= {}
# options[:add_match_with][:format] = /[^\.]+/
end
absolute_map = map && map[0] == ?/
unless controller.empty?
# Now we need to add our controller path only if not mapped directly
if map.blank? and !absolute_map
controller_path = controller.join("/")
path.gsub!(%r{^\(/\)|/\?}, "")
path = File.join(controller_path, path) unless @_map
end
# Here we build the correct name route
end
# Now we need to parse our 'parent' params and parent scope
if !absolute_map and parent_params = options.delete(:parent) || @_parents
parent_params = (Array(@_parents) + Array(parent_params)).uniq
path = process_path_for_parent_params(path, parent_params)
end
# Add any controller level map to the front of the path
path = "#{@_map}/#{path}".squeeze('/') unless absolute_map or @_map.blank?
# Small reformats
path.gsub!(%r{/\?$}, '(/)') # Remove index path
path.gsub!(%r{//$}, '/') # Remove index path
path[0,0] = "/" if path !~ %r{^\(?/} # Paths must start with a /
path.sub!(%r{/(\))?$}, '\\1') if path != "/" # Remove latest trailing delimiter
path.gsub!(/\/(\(\.|$)/, '\\1') # Remove trailing slashes
path.squeeze!('/')
when Regexp
route_options[:path_for_generation] = options.delete(:generate_with) if options.key?(:generate_with)
end
name = options.delete(:route_name) if name.nil? && options.key?(:route_name)
name = options.delete(:name) if name.nil? && options.key?(:name)
if name
controller_name = controller.join("_")
name = "#{controller_name}_#{name}".to_sym unless controller_name.blank?
end
# Merge in option defaults
options.reverse_merge!(:default_values => @_defaults)
[path, name, parent_params, options, route_options]
end
##
# Processes the existing path and appends the 'with' parameters onto the route
# Used for calculating path in route method.
#
def process_path_for_with_params(path, with_params)
File.join(path, Array(with_params).map(&:inspect).join("/"))
end
##
# Processes the existing path and prepends the 'parent' parameters onto the route
# Used for calculating path in route method.
#
def process_path_for_parent_params(path, parent_params)
parent_prefix = parent_params.flatten.compact.uniq.map do |param|
map = (param.respond_to?(:map) && param.map ? param.map : param.to_s)
part = "#{map}/:#{param.to_s.singularize}_id/"
part = "(#{part})" if param.respond_to?(:optional) && param.optional?
part
end
[parent_prefix, path].flatten.join("")
end
##
# Processes the existing path and appends the 'format' suffix onto the route
# Used for calculating path in route method.
#
def process_path_for_provides(path, format_params)
path << "(.:format)" unless path[-10, 10] == '(.:format)'
end
##
# Allows routing by MIME-types specified in the URL or ACCEPT header.
#
# By default, if a non-provided mime-type is specified in a URL, the
# route will not match an thus return a 404.
#
# Setting the :treat_format_as_accept option to true allows treating
# missing mime types specified in the URL as if they were specified
# in the ACCEPT header and thus return 406.
#
# If no type is specified, the first in the provides-list will be
# returned.
#
# @example
# get "/a", :provides => [:html, :js]
# # => GET /a => :html
# # => GET /a.js => :js
# # => GET /a.xml => 404
#
# get "/b", :provides => [:html]
# # => GET /b; ACCEPT: html => html
# # => GET /b; ACCEPT: js => 406
#
# enable :treat_format_as_accept
# get "/c", :provides => [:html, :js]
# # => GET /c.xml => 406
#
def provides(*types)
@_use_format = true
condition do
mime_types = types.map { |t| mime_type(t) }
url_format = params[:format].to_sym if params[:format]
accepts = request.accept.map { |a| a.to_str }
# per rfc2616-sec14:
# Assume */* if no ACCEPT header is given.
catch_all = (accepts.delete "*/*" || accepts.empty?)
matching_types = accepts.empty? ? mime_types.slice(0,1) : (accepts & mime_types)
if !url_format && matching_types.first
type = ::Rack::Mime::MIME_TYPES.find { |k, v| v == matching_types.first }[0].sub(/\./,'').to_sym
accept_format = CONTENT_TYPE_ALIASES[type] || type
elsif catch_all
type = types.first
accept_format = CONTENT_TYPE_ALIASES[type] || type
end
matched_format = types.include?(:any) ||
types.include?(accept_format) ||
types.include?(url_format) ||
((!url_format) && request.accept.empty? && types.include?(:html))
# per rfc2616-sec14:
# answer with 406 if accept is given but types to not match any
# provided type
halt 406 if
(!url_format && !accepts.empty? && !matched_format) ||
(settings.respond_to?(:treat_format_as_accept) && settings.treat_format_as_accept && url_format && !matched_format)
if matched_format
@_content_type = url_format || accept_format || :html
content_type(@_content_type, :charset => 'utf-8')
end
matched_format
end
end
##
# Implements CSRF checking when `allow_disabled_csrf` is set to true.
#
# This condition is always on, except when it is explicitly switched
# off.
#
# @example
# post("/", :csrf_protection => false)
#
def csrf_protection(on = true)
if on
condition { halt 403 if request.env['protection.csrf.failed'] }
end
end
end
##
# Instance methods related to recognizing and processing routes and serving static files.
#
module InstanceMethods
##
# Instance method for url generation.
#
# @example
# url(:show, :id => 1)
# url(:show, :name => :test)
# url(:show, 1)
# url("/foo")
#
# @see Padrino::Routing::ClassMethods#url
#
def url(*args)
# Delegate to Sinatra 1.2 for simple url("/foo")
# http://www.sinatrarb.com/intro#Generating%20URLs
return super if args.first.is_a?(String) && !args[1].is_a?(Hash)
# Delegate to Padrino named route url generation
settings.url(*args)
end
alias :url_for :url
##
# Returns the recognized path for a route.
#
def recognize_path(path)
settings.recognize_path(path)
end
##
# Returns the current path within a route from specified +path_params+.
#
def current_path(*path_params)
if path_params.last.is_a?(Hash)
path_params[-1] = params.merge(path_params[-1])
else
path_params << params
end
@route.path(*path_params)
end
##
# Returns the current route
#
# @example
# -if route.controller == :press
# %li=show_article
#
def route
@route
end
##
# This is mostly just a helper so request.path_info isn't changed when
# serving files from the public directory.
#
def static_file?(path_info)
return if (public_dir = settings.public_folder).nil?
public_dir = File.expand_path(public_dir)
path = File.expand_path(public_dir + unescape(path_info))
return if path[0, public_dir.length] != public_dir
return unless File.file?(path)
return path
end
#
# Method for deliver static files.
#
def static!
if path = static_file?(request.path_info)
env['sinatra.static_file'] = path
cache_control *settings.static_cache_control if settings.static_cache_control?
send_file(path, :disposition => nil)
end
end
##
# Return the request format, this is useful when we need to respond to
# a given Content-Type.
#
# @param [Symbol, nil] type
#
# @param [Hash] params
#
# @example
# get :index, :provides => :any do
# case content_type
# when :js then ...
# when :json then ...
# when :html then ...
# end
# end
#
def content_type(type=nil, params={})
unless type.nil?
super(type, params)
@_content_type = type
end
@_content_type
end
private
def filter!(type, base=settings)
base.filters[type].each { |block| instance_eval(&block) }
end
def dispatch!
invoke do
static! if settings.static? && (request.get? || request.head?)
route!
end
rescue ::Exception => boom
filter! :before if boom.kind_of? ::Sinatra::NotFound
invoke { @boom_handled = handle_exception!(boom) }
ensure
@boom_handled or begin
filter! :after unless env['sinatra.static_file']
rescue ::Exception => boom
invoke { handle_exception!(boom) } unless @env['sinatra.error']
end
end
def route!(base=settings, pass_block=nil)
Thread.current['padrino.instance'] = self
if base.compiled_router and match = base.compiled_router.call(@request.env)
if match.respond_to?(:each)
route_eval do
match[1].each { |k,v| response[k] = v }
status match[0]
route_missing if match[0] == 404
route_missing if allow = response['Allow'] and allow.include?(request.env['REQUEST_METHOD'])
end
end
else