module Ronin::Support::Encoding::URI
Contains methods for encoding/decoding escaping/unescaping URI
data.
## Features
-
Supports uppercase (ex: ‘%FF`) and lowercase (ex: `%ff`)
URI
encoding. -
Supports {URI::Form
URI
form} encoding.
## Core-Ext Methods
-
{Integer#uri_encode}
-
{Integer#uri_escape}
-
{Integer#uri_form_encode}
-
{Integer#uri_form_escape}
-
{String#uri_escape}
-
{String#uri_unescape}
-
{String#uri_encode}
-
{String#uri_decode}
-
{String#uri_form_escape}
-
{String#uri_form_unescape}
-
{String#uri_form_encode}
-
{String#uri_form_decode}
@api public
Public Class Methods
Source
# File lib/ronin/support/encoding/uri.rb, line 240 def self.decode(data) unescape(data) end
Alias for {unescape}.
@param [String] data
The URI escaped data to decode.
@return [String]
The decoded URI form of the String.
@see unescape
Source
# 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
@param [String] data
The data to URI encode.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments.
@option kwargs [:lower, :upper, nil] :case
Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.
@return [String]
The URI encoded form of the String.
@example
Encoding::URI.encode("plain text") # => "%70%6C%61%69%6E%20%74%65%78%74"
@example Lowercase encoding:
Encoding::URI.escape("plain text", case: :lower) # => "%70%6c%61%69%6e%20%74%65%78%74"
Source
# 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
URI
encodes the byte.
@param [Integer] byte
The byte to URI encode.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments.
@option kwargs [:lower, :upper, nil] :case
Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.
@return [String]
The URI encoded byte.
@raise [ArgumentError]
The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.
@raise [RangeError]
The byte value is negative or greater than 255.
@example
Encoding::URI.encode_byte(0x41) # => "%41"
@example Lowercase encoding:
Encoding::URI.encode_byte(0xff, case: :lower) # => "%ff"
Source
# 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
@param [String] data
The data to URI escape.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments.
@option kwargs [:lower, :upper, nil] :case
Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.
@return [String]
The URI escaped form of the String.
@raise [ArgumentError]
The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.
@example
Encoding::URI.escape("x > y") # => "x%20%3E%20y"
@example Lowercase encoding:
Encoding::URI.escape("x > y", case: :lower) # => "x%20%3e%20y"
@api public
Source
# 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
URI
escapes the byte.
@param [Integer] byte
The byte to URI escape.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments.
@option kwargs [:lower, :upper, nil] :case
Controls whether to output lowercase or uppercase hexadecimal. Defaults to uppercase hexadecimal.
@return [String]
The URI escaped byte.
@raise [ArgumentError]
The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.
@raise [RangeError]
The byte value is negative or greater than 255.
@example
Encoding::URI.escape_byte(0x41) # => "A" Encoding::URI.escape_byte(0x3d) # => "%3D"
@example Lowercase encoding:
Encoding::URI.escape_byte(0xff, case: :lower) # => "%ff"
Source
# 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
@param [String] data
The URI escaped data to unescape.
@return [String]
The unescaped URI form of the String.
@example
Encoding::URI.unescape("sweet%20%26%20sour") # => "sweet & sour"