module Ronin::Support::Encoding::HTTP
Contains methods for encoding/decoding escaping/unescaping HTTP
data.
## Features
-
Supports uppercase (ex: ‘%FF`) and lowercase (ex: `%ff`)
URI
encoding.
## Core-Ext Methods
-
{Integer#http_encode}
-
{Integer#http_escape}
-
{String#http_escape}
-
{String#http_unescape}
-
{String#http_encode}
-
{String#http_decode}
@api public
Public Class Methods
HTTP
encodes each byte of the String
.
@param [String] data
The data to HTTP 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 HTTP hexadecimal encoded form of the String.
@raise [ArgumentError]
The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.
@example
Encoding::HTTP.encode("hello") # => "%68%65%6c%6c%6f"
@example Lowercase encoding:
Encoding::HTTP.encode("hello") # => "%68%65%6c%6c%6f"
# 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
Encodes the byte as an escaped HTTP
decimal character.
@param [Integer] byte
The byte toe HTTP 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 encoded HTTP 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::HTTP.encode_byte(0x41) # => "%41"
@example Lowercase encoding:
Encoding::HTTP.encode_byte(0xff, case: :lower) # => "%ff"
# 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
HTTP
escapes the special characters in the given data.
@param [String] data
The data to HTTP 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 HTTP escaped form of the String.
@raise [ArgumentError]
The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.
@example
Encoding::HTTP.escape("x > y") # => "x+%3E+y"
@example Lowercase encoding:
Encoding::HTTP.escape("x > y", case: :lower) # => "x+%3e+y"
# 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
@param [Integer] byte
The byte toe HTTP 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 HTTP escaped form of the Integer.
@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::HTTP.escape_byte(0x41) # => "A" Encoding::HTTP.escape_byte(62) # => "%3E"
@example Lowercase encoding:
Encoding::HTTP.escape_byte(0xff, case: :lower) # => "%ff"
# 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
@param [String] data
The data to unescape.
@return [String]
The raw String.
@example
Encoding::HTTP.unescape("sweet+%26+sour") # => "sweet & sour"
# 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