Class: ActionDispatch::Journey::Router::Utils::UriEncoder

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_dispatch/journey/router/utils.rb

Overview

URI path and fragment escaping tools.ietf.org/html/rfc3986

Constant Summary collapse

ENCODE =
"%%%02X"
US_ASCII =
Encoding::US_ASCII
UTF_8 =
Encoding::UTF_8
EMPTY =
(+"").force_encoding(US_ASCII).freeze
DEC2HEX =
(0..255).map { |i| (ENCODE % i).force_encoding(US_ASCII) }
ALPHA =
"a-zA-Z"
DIGIT =
"0-9"
UNRESERVED =
"#{ALPHA}#{DIGIT}\\-\\._~"
SUB_DELIMS =
"!\\$&'\\(\\)\\*\\+,;="
ESCAPED =
/%[a-zA-Z0-9]{2}/.freeze
FRAGMENT =
/[^#{UNRESERVED}#{SUB_DELIMS}:@\/?]/.freeze
SEGMENT =
/[^#{UNRESERVED}#{SUB_DELIMS}:@]/.freeze
PATH =
/[^#{UNRESERVED}#{SUB_DELIMS}:@\/]/.freeze

Instance Method Summary collapse

Instance Method Details

#escape_fragment(fragment) ⇒ Object



51
52
53
# File 'actionpack/lib/action_dispatch/journey/router/utils.rb', line 51

def escape_fragment(fragment)
  escape(fragment, FRAGMENT)
end

#escape_path(path) ⇒ Object



55
56
57
# File 'actionpack/lib/action_dispatch/journey/router/utils.rb', line 55

def escape_path(path)
  escape(path, PATH)
end

#escape_segment(segment) ⇒ Object



59
60
61
# File 'actionpack/lib/action_dispatch/journey/router/utils.rb', line 59

def escape_segment(segment)
  escape(segment, SEGMENT)
end

#unescape_uri(uri) ⇒ Object



63
64
65
66
# File 'actionpack/lib/action_dispatch/journey/router/utils.rb', line 63

def unescape_uri(uri)
  encoding = uri.encoding == US_ASCII ? UTF_8 : uri.encoding
  uri.gsub(ESCAPED) { |match| [match[1, 2].hex].pack("C") }.force_encoding(encoding)
end