Module: Ronin::Support::Encoding::HTTP
- Defined in:
- lib/ronin/support/encoding/http.rb
Overview
Contains methods for encoding/decoding escaping/unescaping HTTP data.
Features
- Supports uppercase (ex:
%FF
) and lowercase (ex:%ff
) URI encoding.
Core-Ext Methods
Class Method Summary collapse
-
.decode(data) ⇒ String
HTTP decodes the HTTP encoded String.
-
.encode(data, **kwargs) ⇒ String
HTTP encodes each byte of the String.
-
.encode_byte(byte, **kwargs) ⇒ String
Encodes the byte as an escaped HTTP decimal character.
-
.escape(data, **kwargs) ⇒ String
HTTP escapes the special characters in the given data.
-
.escape_byte(byte, **kwargs) ⇒ String
HTTP escapes the Integer.
-
.unescape(data) ⇒ String
HTTP unescapes the String.
Class Method Details
.decode(data) ⇒ String
HTTP decodes the HTTP encoded String.
242 243 244 |
# File 'lib/ronin/support/encoding/http.rb', line 242 def self.decode(data) unescape(data) end |
.encode(data, **kwargs) ⇒ String
HTTP encodes each byte of the String.
219 220 221 222 223 224 225 226 227 |
# File 'lib/ronin/support/encoding/http.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
Encodes the byte as an escaped HTTP decimal character.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ronin/support/encoding/http.rb', line 71 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
HTTP escapes the special characters in the given data.
159 160 161 162 163 164 165 166 167 |
# File 'lib/ronin/support/encoding/http.rb', line 159 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
HTTP escapes the Integer.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ronin/support/encoding/http.rb', line 118 def self.escape_byte(byte,**kwargs) if (byte >= 0) && (byte <= 0xff) if (byte == 45) || (byte == 46) || ((byte >= 48) && (byte <= 57)) || ((byte >= 65) && (byte <= 90)) || (byte == 95) || ((byte >= 97) && (byte <= 122)) || (byte == 126) byte.chr elsif byte == 0x20 '+' else encode_byte(byte,**kwargs) end else raise(RangeError,"#{byte.inspect} out of char range") end end |
.unescape(data) ⇒ String
HTTP unescapes the String.
182 183 184 185 186 187 188 189 190 |
# File 'lib/ronin/support/encoding/http.rb', line 182 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 |