Sha256: de06eb95b0b0e9090f944d00266d2a4aa68dd59237269005aa8e3c2113637c39

Contents?: true

Size: 1.84 KB

Versions: 22

Compression:

Stored size: 1.84 KB

Contents

require 'uri'

module ActionDispatch
  module Journey # :nodoc:
    class Router # :nodoc:
      class Utils # :nodoc:
        # Normalizes URI path.
        #
        # Strips off trailing slash and ensures there is a leading slash.
        # Also converts downcase url encoded string to uppercase.
        #
        #   normalize_path("/foo")  # => "/foo"
        #   normalize_path("/foo/") # => "/foo"
        #   normalize_path("foo")   # => "/foo"
        #   normalize_path("")      # => "/"
        #   normalize_path("/%ab")  # => "/%AB"
        def self.normalize_path(path)
          path = "/#{path}"
          path.squeeze!('/')
          path.sub!(%r{/+\Z}, '')
          path.gsub!(/(%[a-f0-9]{2})/) { $1.upcase }
          path = '/' if path == ''
          path
        end

        # URI path and fragment escaping
        # http://tools.ietf.org/html/rfc3986
        module UriEscape # :nodoc:
          # Symbol captures can generate multiple path segments, so include /.
          reserved_segment  = '/'
          reserved_fragment = '/?'
          reserved_pchar    = ':@&=+$,;%'

          safe_pchar    = "#{URI::REGEXP::PATTERN::UNRESERVED}#{reserved_pchar}"
          safe_segment  = "#{safe_pchar}#{reserved_segment}"
          safe_fragment = "#{safe_pchar}#{reserved_fragment}"
          UNSAFE_SEGMENT  = Regexp.new("[^#{safe_segment}]", false).freeze
          UNSAFE_FRAGMENT = Regexp.new("[^#{safe_fragment}]", false).freeze
        end

        Parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI

        def self.escape_path(path)
          Parser.escape(path.to_s, UriEscape::UNSAFE_SEGMENT)
        end

        def self.escape_fragment(fragment)
          Parser.escape(fragment.to_s, UriEscape::UNSAFE_FRAGMENT)
        end

        def self.unescape_uri(uri)
          Parser.unescape(uri)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
actionpack-4.0.13 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.13.rc1 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.11.1 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.12 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.11 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.10 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.10.rc2 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.10.rc1 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.9 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.8 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.7 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.6 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.6.rc3 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.6.rc2 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.6.rc1 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.5 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.4 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.4.rc1 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.3 lib/action_dispatch/journey/router/utils.rb
actionpack-4.0.2 lib/action_dispatch/journey/router/utils.rb