diff options
Diffstat (limited to 'actionpack/lib')
7 files changed, 67 insertions, 32 deletions
diff --git a/actionpack/lib/action_controller/metal/params_wrapper.rb b/actionpack/lib/action_controller/metal/params_wrapper.rb index cdfc523bd4..e680432127 100644 --- a/actionpack/lib/action_controller/metal/params_wrapper.rb +++ b/actionpack/lib/action_controller/metal/params_wrapper.rb @@ -4,8 +4,8 @@ require 'active_support/core_ext/module/anonymous'  require 'action_dispatch/http/mime_type'  module ActionController -  # Wraps the parameters hash into a nested hash. This will allow clients to submit -  # POST requests without having to specify any root elements. +  # Wraps the parameters hash into a nested hash. This will allow clients to +  # submit requests without having to specify any root elements.    #    # This functionality is enabled in +config/initializers/wrap_parameters.rb+    # and can be customized. @@ -14,7 +14,7 @@ module ActionController    # a non-empty array:    #    #     class UsersController < ApplicationController -  #       wrap_parameters format: [:json, :xml] +  #       wrap_parameters format: [:json, :xml, :url_encoded_form, :multipart_form]    #     end    #    # If you enable +ParamsWrapper+ for +:json+ format, instead of having to diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index 4cb634477e..9e09242872 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -20,7 +20,7 @@ module ActionController #:nodoc:    # Since HTML and JavaScript requests are typically made from the browser, we    # need to ensure to verify request authenticity for the web browser. We can    # use session-oriented authentication for these types of requests, by using -  # the `protect_form_forgery` method in our controllers. +  # the `protect_from_forgery` method in our controllers.    #    # GET requests are not protected since they don't have side effects like writing    # to the database and don't leak sensitive information. JavaScript requests are diff --git a/actionpack/lib/action_controller/metal/url_for.rb b/actionpack/lib/action_controller/metal/url_for.rb index 5a0e5c62e4..dbf7241a14 100644 --- a/actionpack/lib/action_controller/metal/url_for.rb +++ b/actionpack/lib/action_controller/metal/url_for.rb @@ -41,7 +41,11 @@ module ActionController          if original_script_name            options[:original_script_name] = original_script_name          else -          options[:script_name] = same_origin ? request.script_name.dup : script_name +          if same_origin +            options[:script_name] = request.script_name.empty? ? "".freeze : request.script_name.dup +          else +            options[:script_name] = script_name +          end          end          options.freeze        else diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index eab7d0ab57..fd92e89231 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -197,13 +197,13 @@ module ActionDispatch # :nodoc:        @content_type = content_type.to_s      end -    # Sets the HTTP character set. +    # Sets the HTTP character set. In case of nil parameter +    # it sets the charset to utf-8. +    # +    #   response.charset = 'utf-16' # => 'utf-16' +    #   response.charset = nil      # => 'utf-8'      def charset=(charset) -      if nil == charset -        @charset = self.class.default_charset -      else -        @charset = charset -      end +      @charset = charset.nil? ? self.class.default_charset : charset      end      # The response code of the request. diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb index c0566c6fc9..d8bb10ffab 100644 --- a/actionpack/lib/action_dispatch/journey/formatter.rb +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -14,7 +14,7 @@ module ActionDispatch        def generate(name, options, path_parameters, parameterize = nil)          constraints = path_parameters.merge(options) -        missing_keys = [] +        missing_keys = nil # need for variable scope          match_route(name, constraints) do |route|            parameterized_parts = extract_parameterized_parts(route, options, path_parameters, parameterize) @@ -25,22 +25,22 @@ module ActionDispatch            next unless name || route.dispatcher?            missing_keys = missing_keys(route, parameterized_parts) -          next unless missing_keys.empty? +          next if missing_keys && !missing_keys.empty?            params = options.dup.delete_if do |key, _|              parameterized_parts.key?(key) || route.defaults.key?(key)            end            defaults       = route.defaults            required_parts = route.required_parts -          parameterized_parts.delete_if do |key, value| -            value.to_s == defaults[key].to_s && !required_parts.include?(key) +          parameterized_parts.keep_if do |key, value| +            defaults[key].nil? || value.to_s != defaults[key].to_s || required_parts.include?(key)            end            return [route.format(parameterized_parts), params]          end          message = "No route matches #{Hash[constraints.sort_by{|k,v| k.to_s}].inspect}" -        message << " missing required keys: #{missing_keys.sort.inspect}" unless missing_keys.empty? +        message << " missing required keys: #{missing_keys.sort.inspect}" if missing_keys && !missing_keys.empty?          raise ActionController::UrlGenerationError, message        end @@ -54,12 +54,12 @@ module ActionDispatch          def extract_parameterized_parts(route, options, recall, parameterize = nil)            parameterized_parts = recall.merge(options) -          keys_to_keep = route.parts.reverse.drop_while { |part| +          keys_to_keep = route.parts.reverse_each.drop_while { |part|              !options.key?(part) || (options[part] || recall[part]).nil?            } | route.required_parts -          (parameterized_parts.keys - keys_to_keep).each do |bad_key| -            parameterized_parts.delete(bad_key) +          parameterized_parts.delete_if do |bad_key, _| +            !keys_to_keep.include?(bad_key)            end            if parameterize @@ -110,15 +110,36 @@ module ActionDispatch            routes          end +        module RegexCaseComparator +          DEFAULT_INPUT = /[-_.a-zA-Z0-9]+\/[-_.a-zA-Z0-9]+/ +          DEFAULT_REGEX = /\A#{DEFAULT_INPUT}\Z/ + +          def self.===(regex) +            DEFAULT_INPUT == regex +          end +        end +          # Returns an array populated with missing keys if any are present.          def missing_keys(route, parts) -          missing_keys = [] +          missing_keys = nil            tests = route.path.requirements            route.required_parts.each { |key| -            if tests.key?(key) -              missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key] +            case tests[key] +            when nil +              unless parts[key] +                missing_keys ||= [] +                missing_keys << key +              end +            when RegexCaseComparator +              unless RegexCaseComparator::DEFAULT_REGEX === parts[key] +                missing_keys ||= [] +                missing_keys << key +              end              else -              missing_keys << key unless parts[key] +              unless /\A#{tests[key]}\Z/ === parts[key] +                missing_keys ||= [] +                missing_keys << key +              end              end            }            missing_keys diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 42512cad91..a006a146ed 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -267,9 +267,13 @@ module ActionDispatch                  path_params -= controller_options.keys                  path_params -= result.keys                end -              path_params -= inner_options.keys -              path_params.take(args.size).each do |param| -                result[param] = args.shift +              inner_options.each do |key, _| +                path_params.delete(key) +              end + +              args.each_with_index do |arg, index| +                param = path_params[index] +                result[param] = arg if param                end              end @@ -594,8 +598,8 @@ module ActionDispatch          def initialize(named_route, options, recall, set)            @named_route = named_route -          @options     = options.dup -          @recall      = recall.dup +          @options     = options +          @recall      = recall            @set         = set            normalize_recall! @@ -617,7 +621,7 @@ module ActionDispatch          def use_recall_for(key)            if @recall[key] && (!@options.key?(key) || @options[key] == @recall[key])              if !named_route_exists? || segment_keys.include?(key) -              @options[key] = @recall.delete(key) +              @options[key] = @recall[key]              end            end          end @@ -671,12 +675,18 @@ module ActionDispatch          # Remove leading slashes from controllers          def normalize_controller! -          @options[:controller] = controller.sub(%r{^/}, ''.freeze) if controller +          if controller +            if m = controller.match(/\A\/(?<controller_without_leading_slash>.*)/) +              @options[:controller] = m[:controller_without_leading_slash] +            else +              @options[:controller] = controller +            end +          end          end          # Move 'index' action from options to recall          def normalize_action! -          if @options[:action] == 'index' +          if @options[:action] == 'index'.freeze              @recall[:action] = @options.delete(:action)            end          end diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index dc664d5540..0298962409 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -391,7 +391,7 @@ module ActionDispatch        attr_reader :app -      def before_setup +      def before_setup # :nodoc:          @app = nil          @integration_session = nil          super  | 
