Class: Rack::Rewritten::Url
- Inherits:
-
Object
- Object
- Rack::Rewritten::Url
- Defined in:
- lib/rack/url.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, &block) ⇒ Url
constructor
A new instance of Url.
- #is_external_target?(url) ⇒ Boolean
- #is_internal_target?(url) ⇒ Boolean
- #split_to_path_params(path_and_query) ⇒ Object
Constructor Details
#initialize(app, &block) ⇒ Url
Returns a new instance of Url.
11 12 13 14 15 16 17 18 |
# File 'lib/rack/url.rb', line 11 def initialize(app, &block) @app = app @translate_backwards = false @downcase_before_lookup = false @translate_partial = false @base_url = '' instance_eval(&block) if block_given? end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/rack/url.rb', line 9 def base_url @base_url end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/rack/url.rb', line 28 def call(env) req = Rack::Request.new(env) subdomain = env["SUBDOMAIN"] ? "#{env["SUBDOMAIN"]}:" : "" path = "#{subdomain}#{req.path_info}" path.downcase! if downcase_before_lookup? target_url = ::Rewritten.translate(path) if is_external_target?(target_url) r = Rack::Response.new r.redirect(target_url, 301) r.finish elsif ::Rewritten.includes?(path.chomp("/")) or backwards=( translate_backwards? && ::Rewritten.exist_translation_for?(path) ) to = ::Rewritten.includes?(path.chomp("/")) || path current_path = ::Rewritten.get_current_translation(to) current_path = current_path.split(":").last current_path = current_path.split('?')[0] if (current_path == req.path_info) or (::Rewritten.base_from(req.path_info) == current_path) or ::Rewritten.has_flag?(path, 'L') # if this is the current path, rewrite path and parameters tpath, tparams = split_to_path_params(to) env['QUERY_STRING'] = Rack::Utils.build_nested_query(tparams.merge(req.params)) req.path_info = tpath + ::Rewritten.appendix(req.path_info) @app.call(req.env) else # if this is not the current path, redirect to current path # NOTE: assuming redirection is always to non-subdomain-path r = Rack::Response.new new_path = env["rack.url_scheme"].dup new_path << "://" new_path << env["HTTP_HOST"].dup.sub(/^#{subdomain.chomp(':')}\./, '') new_path << current_path new_path << ::Rewritten.appendix(path) unless backwards new_path << '?' << env["QUERY_STRING"] unless (env["QUERY_STRING"]||'').empty? r.redirect(new_path, 301) a = r.finish end else @app.call(req.env) end end |
#is_external_target?(url) ⇒ Boolean
24 25 26 |
# File 'lib/rack/url.rb', line 24 def is_external_target?(url) !is_internal_target?(url) end |
#is_internal_target?(url) ⇒ Boolean
20 21 22 |
# File 'lib/rack/url.rb', line 20 def is_internal_target?(url) url.nil? or url.start_with?('/') or url.start_with?(@base_url) end |
#split_to_path_params(path_and_query) ⇒ Object
77 78 79 80 |
# File 'lib/rack/url.rb', line 77 def split_to_path_params(path_and_query) path, query_string = path_and_query.split('?').push('')[0..1] [path, Rack::Utils.parse_query(query_string)] end |