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
Defined Under Namespace
Modules: Form
Class Method Summary collapse
-
.decode(data) ⇒ String
Alias for URI.unescape.
-
.encode(data, **kwargs) ⇒ String
URI encodes the String.
-
.encode_byte(byte, **kwargs) ⇒ String
URI encodes the byte.
-
.escape(data, **kwargs) ⇒ String
URI escapes the String.
-
.escape_byte(byte, **kwargs) ⇒ String
URI escapes the byte.
-
.unescape(data) ⇒ String
URI unescapes the String.
Class Method Details
.decode(data) ⇒ String
Alias for unescape.
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.
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.
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.
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.
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.
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 |