diff options
Diffstat (limited to 'actionpack/lib/action_dispatch')
11 files changed, 126 insertions, 47 deletions
diff --git a/actionpack/lib/action_dispatch/http/cache.rb b/actionpack/lib/action_dispatch/http/cache.rb index 5ee4c044ea..0b895e7860 100644 --- a/actionpack/lib/action_dispatch/http/cache.rb +++ b/actionpack/lib/action_dispatch/http/cache.rb @@ -84,17 +84,29 @@ module ActionDispatch LAST_MODIFIED = "Last-Modified".freeze ETAG = "ETag".freeze CACHE_CONTROL = "Cache-Control".freeze + SPESHUL_KEYS = %w[extras no-cache max-age public must-revalidate] + + def cache_control_headers + cache_control = {} + if cc = self[CACHE_CONTROL] + cc.delete(' ').split(',').each do |segment| + directive, argument = segment.split('=', 2) + case directive + when *SPESHUL_KEYS + key = directive.tr('-', '_') + cache_control[key.to_sym] = argument || true + else + cache_control[:extras] ||= [] + cache_control[:extras] << segment + end + end + end + cache_control + end def prepare_cache_control! - @cache_control = {} + @cache_control = cache_control_headers @etag = self[ETAG] - - if cache_control = self[CACHE_CONTROL] - cache_control.split(/,\s*/).each do |segment| - first, last = segment.split("=") - @cache_control[first.to_sym] = last || true - end - end end def handle_conditional_get! @@ -110,14 +122,24 @@ module ActionDispatch MUST_REVALIDATE = "must-revalidate".freeze def set_conditional_cache_control! - return if self[CACHE_CONTROL].present? + control = {} + cc_headers = cache_control_headers + if extras = cc_headers.delete(:extras) + @cache_control[:extras] ||= [] + @cache_control[:extras] += extras + @cache_control[:extras].uniq! + end - control = @cache_control + control.merge! cc_headers + control.merge! @cache_control if control.empty? headers[CACHE_CONTROL] = DEFAULT_CACHE_CONTROL elsif control[:no_cache] headers[CACHE_CONTROL] = NO_CACHE + if control[:extras] + headers[CACHE_CONTROL] += ", #{control[:extras].join(', ')}" + end else extras = control[:extras] max_age = control[:max_age] diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index 0eaae80461..ee1913dbf9 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -99,9 +99,9 @@ module Mime end def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false) - Mime.const_set(symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms)) + Mime.const_set(symbol.upcase, Type.new(string, symbol, mime_type_synonyms)) - SET << Mime.const_get(symbol.to_s.upcase) + SET << Mime.const_get(symbol.upcase) ([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup ([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last } @@ -194,7 +194,7 @@ module Mime # # Mime::Type.unregister(:mobile) def unregister(symbol) - symbol = symbol.to_s.upcase + symbol = symbol.upcase mime = Mime.const_get(symbol) Mime.instance_eval { remove_const(symbol) } diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index aa5ba3e8a5..8cea17c7a6 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -263,6 +263,27 @@ module ActionDispatch LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip end + protected + + # Remove nils from the params hash + def deep_munge(hash) + hash.each_value do |v| + case v + when Array + v.grep(Hash) { |x| deep_munge(x) } + v.compact! + when Hash + deep_munge(v) + end + end + + hash + end + + def parse_query(qs) + deep_munge(super) + end + private def check_method(name) diff --git a/actionpack/lib/action_dispatch/middleware/public_exceptions.rb b/actionpack/lib/action_dispatch/middleware/public_exceptions.rb index 85b8d178bf..53bedaa40a 100644 --- a/actionpack/lib/action_dispatch/middleware/public_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/public_exceptions.rb @@ -1,5 +1,4 @@ module ActionDispatch - # A simple Rack application that renders exceptions in the given public path. class PublicExceptions attr_accessor :public_path @@ -8,23 +7,41 @@ module ActionDispatch end def call(env) - status = env["PATH_INFO"][1..-1] - locale_path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale - path = "#{public_path}/#{status}.html" - - if locale_path && File.exist?(locale_path) - render(status, File.read(locale_path)) - elsif File.exist?(path) - render(status, File.read(path)) + exception = env["action_dispatch.exception"] + status = env["PATH_INFO"][1..-1] + request = ActionDispatch::Request.new(env) + content_type = request.formats.first + body = { :status => status, :error => exception.message } + + render(status, content_type, body) + end + + private + + def render(status, content_type, body) + format = content_type && "to_#{content_type.to_sym}" + if format && body.respond_to?(format) + render_format(status, content_type, body.public_send(format)) else - [404, { "X-Cascade" => "pass" }, []] + render_html(status) end end - private + def render_format(status, content_type, body) + [status, {'Content-Type' => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}", + 'Content-Length' => body.bytesize.to_s}, [body]] + end + + def render_html(status) + found = false + path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale + path = "#{public_path}/#{status}.html" unless path && (found = File.exist?(path)) - def render(status, body) - [status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]] + if found || File.exist?(path) + render_format(status, 'text/html', File.read(path)) + else + [404, { "X-Cascade" => "pass" }, []] + end end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_dispatch/request/session.rb b/actionpack/lib/action_dispatch/request/session.rb index 4ad7071820..d8bcc28613 100644 --- a/actionpack/lib/action_dispatch/request/session.rb +++ b/actionpack/lib/action_dispatch/request/session.rb @@ -87,6 +87,14 @@ module ActionDispatch alias :key? :has_key? alias :include? :has_key? + def keys + @delegate.keys + end + + def values + @delegate.values + end + def []=(key, value) load_for_write! @delegate[key.to_s] = value diff --git a/actionpack/lib/action_dispatch/routing.rb b/actionpack/lib/action_dispatch/routing.rb index 38a0270151..29090882a5 100644 --- a/actionpack/lib/action_dispatch/routing.rb +++ b/actionpack/lib/action_dispatch/routing.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'active_support/core_ext/object/to_param' require 'active_support/core_ext/regexp' @@ -218,6 +219,12 @@ module ActionDispatch # # match "/stories" => redirect("/posts") # + # == Unicode character routes + # + # You can specify unicode character routes in your router: + # + # match "こんにちは" => "welcome#index" + # # == Routing to Rack Applications # # Instead of a String, like <tt>posts#index</tt>, which corresponds to the diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 67a208263b..94242ad962 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1318,7 +1318,7 @@ module ActionDispatch def draw(name) path = @draw_paths.find do |_path| - _path.join("#{name}.rb").file? + File.exists? "#{_path}/#{name}.rb" end unless path @@ -1327,9 +1327,9 @@ module ActionDispatch msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n") raise ArgumentError, msg end - - route_path = path.join("#{name}.rb") - instance_eval(route_path.read, route_path.to_s) + + route_path = "#{path}/#{name}.rb" + instance_eval(File.read(route_path), route_path.to_s) end # match 'path' => 'controller#action' @@ -1387,7 +1387,7 @@ module ActionDispatch options[:as] = name_for_action(options[:as], action) end - mapping = Mapping.new(@set, @scope, path, options) + mapping = Mapping.new(@set, @scope, URI.parser.escape(path), options) app, conditions, requirements, defaults, as, anchor = mapping.to_route @set.add_route(app, conditions, requirements, defaults, as, anchor) end diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 8fde667108..86ce7a83b9 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -95,7 +95,7 @@ module ActionDispatch end record = extract_record(record_or_hash_or_array) - record = record.to_model if record.respond_to?(:to_model) + record = convert_to_model(record) args = Array === record_or_hash_or_array ? record_or_hash_or_array.dup : @@ -122,6 +122,8 @@ module ActionDispatch args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options end + args.collect! { |a| convert_to_model(a) } + (proxy || self).send(named_route, *args) end @@ -152,6 +154,10 @@ module ActionDispatch options[:action] ? "#{options[:action]}_" : '' end + def convert_to_model(object) + object.respond_to?(:to_model) ? object.to_model : object + end + def routing_type(options) options[:routing_type] || :url end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 7872f4007e..64b1d58ae9 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -252,15 +252,11 @@ module ActionDispatch self.draw_paths = [] self.request_class = request_class - @valid_conditions = {} - + @valid_conditions = { :controller => true, :action => true } request_class.public_instance_methods.each { |m| - @valid_conditions[m.to_sym] = true + @valid_conditions[m] = true } - @valid_conditions[:controller] = true - @valid_conditions[:action] = true - - self.valid_conditions.delete(:id) + @valid_conditions.delete(:id) @append = [] @prepend = [] diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 08fd28d72d..50ca28395b 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -17,8 +17,8 @@ module ActionDispatch # a Hash, or a String that is appropriately encoded # (<tt>application/x-www-form-urlencoded</tt> or # <tt>multipart/form-data</tt>). - # - +headers+: Additional HTTP headers to pass, as a Hash. The keys will - # automatically be upcased, with the prefix 'HTTP_' added if needed. + # - +headers+: Additional headers to pass, as a Hash. The headers will be + # merged into the Rack env hash. # # This method returns an Response object, which one can use to # inspect the details of the response. Furthermore, if this method was @@ -73,8 +73,7 @@ module ActionDispatch # # The request_method is +:get+, +:post+, +:patch+, +:put+, +:delete+ or # +:head+; the parameters are +nil+, a hash, or a url-encoded or multipart - # string; the headers are a hash. Keys are automatically upcased and - # prefixed with 'HTTP_' if not already. + # string; the headers are a hash. def xml_http_request(request_method, path, parameters = nil, headers = nil) headers ||= {} headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @@ -194,8 +193,11 @@ module ActionDispatch # If the app is a Rails app, make url_helpers available on the session # This makes app.url_for and app.foo_path available in the console - if app.respond_to?(:routes) && app.routes.respond_to?(:url_helpers) - singleton_class.class_eval { include app.routes.url_helpers } + if app.respond_to?(:routes) + singleton_class.class_eval do + include app.routes.url_helpers if app.routes.respond_to?(:url_helpers) + include app.routes.mounted_helpers if app.routes.respond_to?(:mounted_helpers) + end end reset! diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb index d04be2099c..a86b510719 100644 --- a/actionpack/lib/action_dispatch/testing/test_request.rb +++ b/actionpack/lib/action_dispatch/testing/test_request.rb @@ -11,7 +11,7 @@ module ActionDispatch end def initialize(env = {}) - env = Rails.application.env_config.merge(env) if defined?(Rails.application) + env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application super(DEFAULT_ENV.merge(env)) self.host = 'test.host' |