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

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

Overview

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

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ String

Alias for unescape.

Parameters:

  • data (String)

    The URI Form escaped data to decode.

Returns:

  • (String)

    The URI Form decoded String.

See Also:

Since:

  • 1.0.0



430
431
432
# File 'lib/ronin/support/encoding/uri.rb', line 430

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

.encode(data, **kwargs) ⇒ String

URI Form encodes every character in the given data.

Examples:

Encoding::URI::Form.encode("hello world")
# => "%68%65%6C%6C%6F+%77%6F%72%6C%64"

Lowercase encoding:

Encoding::URI::Form.encode("hello world", case: :lower)
# => "%68%65%6c%6c%6f+%77%6f%72%6c%64"

Parameters:

  • data (String)

    The given data to URI Form 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 Form encoded String.

Since:

  • 1.0.0



409
410
411
412
413
414
415
416
417
# File 'lib/ronin/support/encoding/uri.rb', line 409

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 Form encodes the given byte.

Examples:

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

Lowercase encoding:

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

Parameters:

  • byte (Integer)

    The byte to URI Form 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 Form encoded byte.

Raises:

  • (RangeError)

    The byte value is negative or greater than 255.

Since:

  • 1.0.0



280
281
282
283
284
# File 'lib/ronin/support/encoding/uri.rb', line 280

def self.encode_byte(byte,**kwargs)
  if byte == 0x20 then '+'
  else                 URI.encode_byte(byte,**kwargs)
  end
end

.escape(data, **kwargs) ⇒ String

URI Form escapes the String.

Examples:

Encoding::URI::Form.escape("hello world")
# => "hello+world"
Encoding::URI::Form.escape("hello\0world")
# => "hello%00world"

Lowercase encoding:

Encoding::URI::Form.escape("hello\xffworld", case: :lower)
# => "hello%ffworld"

Parameters:

  • data (String)

    The data to URI Form 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 Form escaped String.

See Also:

Since:

  • 1.0.0



350
351
352
353
354
355
356
357
358
# File 'lib/ronin/support/encoding/uri.rb', line 350

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 Form escapes the given byte.

Examples:

Encoding::URI::Form.escape_byte(0x41)
# => "A"
Encoding::URI::Form.escape_byte(0x20)
# => "+"

Lowercase encoding:

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

Parameters:

  • byte (Integer)

    The byte to URI Form 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 Form escaped byte.

Raises:

  • (RangeError)

    The byte value is negative or greater than 255.

Since:

  • 1.0.0



315
316
317
318
319
320
321
# File 'lib/ronin/support/encoding/uri.rb', line 315

def self.escape_byte(byte,**kwargs)
  if (byte == 42) || (byte == 45) || (byte == 46) || ((byte >= 48) && (byte <= 57)) || ((byte >= 65) && (byte <= 90)) || (byte == 95) || ((byte >= 97) && (byte <= 122))
    byte.chr
  else
    encode_byte(byte,**kwargs)
  end
end

.unescape(data) ⇒ String

URI Form unescapes the String.

Examples:

Encoding::URI::Form.unescape("hello+world")
# => "hello world"
Encoding::URI::Form.unescape("hello%00world")
# => "hello\u0000world"

Parameters:

  • data (String)

    The URI Form escaped data to unescape.

Returns:

  • (String)

    The URI Form unescaped String.

Since:

  • 1.0.0



375
376
377
378
379
380
381
382
383
# File 'lib/ronin/support/encoding/uri.rb', line 375

def self.unescape(data)
  data.gsub(/(?:\+|%[A-Fa-f0-9]{2})/) do |escaped_char|
    if escaped_char == '+'
      ' '
    else
      escaped_char[1..].to_i(16).chr
    end
  end
end