Method: Rex::Text.html_encode

Defined in:
lib/rex/text.rb

.html_encode(str, mode = 'hex') ⇒ String

Encode a string in a manner useful for HTTP URIs and URI Parameters.

Parameters:

  • str (String)

    The string to be encoded

  • mode ("hex", "int", "int-wide") (defaults to: 'hex')

Returns:

  • (String)

Raises:

  • (TypeError)

    if mode is not one of the three available modes



830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/rex/text.rb', line 830

def self.html_encode(str, mode = 'hex')
  case mode
  when 'hex'
    return str.unpack('C*').collect{ |i| "&#x" + ("%.2x" % i) + ";"}.join
  when 'int'
    return str.unpack('C*').collect{ |i| "&#" + i.to_s + ";"}.join
  when 'int-wide'
    return str.unpack('C*').collect{ |i| "&#" + ("0" * (7 - i.to_s.length)) + i.to_s + ";" }.join
  else
    raise TypeError, 'invalid mode'
  end
end