Method: Rex::Text.html_encode

Defined in:
lib/rex/text/encode.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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rex/text/encode.rb', line 69

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