Module: Ronin::Support::Encoding::URI

Defined in:
lib/ronin/support/encoding/uri.rb

Overview

Contains methods for encoding/decoding escaping/unescaping URI data.

Features

  • Supports uppercase (ex: %FF) and lowercase (ex: %ff) URI encoding.
  • Supports URI form encoding.

Core-Ext Methods

Since:

  • 1.0.0

Defined Under Namespace

Modules: Form

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ String

Alias for unescape.

Parameters:

  • data (String)

    The URI escaped data to decode.

Returns:

  • (String)

    The decoded URI form of the String.

See Also:

Since:

  • 1.0.0



240
241
242
# File 'lib/ronin/support/encoding/uri.rb', line 240

def self.decode(data)
  unescape(data)
end

.encode(data, **kwargs) ⇒ String

URI encodes the String.

Examples:

Encoding::URI.encode("plain text")
# => "%70%6C%61%69%6E%20%74%65%78%74"

Lowercase encoding:

Encoding::URI.escape("plain text", case: :lower)
# => "%70%6c%61%69%6e%20%74%65%78%74"

Parameters:

  • data (String)

    The data to URI encode.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.

Returns:

  • (String)

    The URI encoded form of the String.

Since:

  • 1.0.0



219
220
221
222
223
224
225
226
227
# File 'lib/ronin/support/encoding/uri.rb', line 219

def self.encode(data,**kwargs)
  encoded = String.new

  data.each_byte do |byte|
    encoded << encode_byte(byte,**kwargs)
  end

  return encoded
end

.encode_byte(byte, **kwargs) ⇒ String

URI encodes the byte.

Examples:

Encoding::URI.encode_byte(0x41)
# => "%41"

Lowercase encoding:

Encoding::URI.encode_byte(0xff, case: :lower)
# => "%ff"

Parameters:

  • byte (Integer)

    The byte to URI encode.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.

Returns:

  • (String)

    The URI encoded byte.

Raises:

  • (ArgumentError)

    The case: keyword argument was not :lower, :upper, or nil.

  • (RangeError)

    The byte value is negative or greater than 255.

Since:

  • 1.0.0



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ronin/support/encoding/uri.rb', line 78

def self.encode_byte(byte,**kwargs)
  if (byte >= 0) && (byte <= 0xff)
    case kwargs[:case]
    when :lower
      "%%%.2x" % byte
    when :upper, nil
      "%%%.2X" % byte
    else
      raise(ArgumentError,"case (#{kwargs[:case].inspect}) keyword argument must be either :lower, :upper, or nil")
    end
  else
    raise(RangeError,"#{byte.inspect} out of char range")
  end
end

.escape(data, **kwargs) ⇒ String

URI escapes the String.

Examples:

Encoding::URI.escape("x > y")
# => "x%20%3E%20y"

Lowercase encoding:

Encoding::URI.escape("x > y", case: :lower)
# => "x%20%3e%20y"

Parameters:

  • data (String)

    The data to URI escape.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.

Returns:

  • (String)

    The URI escaped form of the String.

Raises:

  • (ArgumentError)

    The case: keyword argument was not :lower, :upper, or nil.

Since:

  • 1.0.0



166
167
168
169
170
171
172
173
174
# File 'lib/ronin/support/encoding/uri.rb', line 166

def self.escape(data,**kwargs)
  escaped = String.new

  data.each_byte do |byte|
    escaped << escape_byte(byte,**kwargs)
  end

  return escaped
end

.escape_byte(byte, **kwargs) ⇒ String

URI escapes the byte.

Examples:

Encoding::URI.escape_byte(0x41)
# => "A"
Encoding::URI.escape_byte(0x3d)
# => "%3D"

Lowercase encoding:

Encoding::URI.escape_byte(0xff, case: :lower)
# => "%ff"

Parameters:

  • byte (Integer)

    The byte to URI escape.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.

Returns:

  • (String)

    The URI escaped byte.

Raises:

  • (ArgumentError)

    The case: keyword argument was not :lower, :upper, or nil.

  • (RangeError)

    The byte value is negative or greater than 255.

Since:

  • 1.0.0



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ronin/support/encoding/uri.rb', line 125

def self.escape_byte(byte,**kwargs)
  if (byte >= 0) && (byte <= 0xff)
    if (byte == 33) || (byte == 36) || (byte == 38) || ((byte >= 39) && (byte <= 59)) || (byte == 61) || ((byte >= 63) && (byte <= 91)) || (byte == 93) || (byte == 95) || ((byte >= 97) && (byte <= 122)) || (byte == 126)
      byte.chr
    else
      encode_byte(byte,**kwargs)
    end
  else
    raise(RangeError,"#{byte.inspect} out of char range")
  end
end

.unescape(data) ⇒ String

URI unescapes the String.

Examples:

Encoding::URI.unescape("sweet%20%26%20sour")
# => "sweet & sour"

Parameters:

  • data (String)

    The URI escaped data to unescape.

Returns:

  • (String)

    The unescaped URI form of the String.

Since:

  • 1.0.0



189
190
191
192
193
# File 'lib/ronin/support/encoding/uri.rb', line 189

def self.unescape(data)
  data.gsub(/%[A-Fa-f0-9]{2}/) do |escaped_char|
    escaped_char[1..].to_i(16).chr
  end
end