class Rex::Proto::Http::Response
HTTP response class.
Attributes
Used to store a copy of the original request
Public Class Methods
Constructage of the HTTP response with the supplied code, message, and protocol.
Rex::Proto::Http::Packet::new
# File lib/rex/proto/http/response.rb, line 45 def initialize(code = 200, message = 'OK', proto = DefaultProtocol) super() self.code = code.to_i self.message = message self.proto = proto # Default responses to auto content length on self.auto_cl = true # default chunk sizes (if chunked is used) self.chunk_min_size = 1 self.chunk_max_size = 10 # 100 continue counter self.count_100 = 0 end
Public Instance Methods
Allow 100 Continues to be ignored by the caller
# File lib/rex/proto/http/response.rb, line 188 def check_100 # If this was a 100 continue with no data, reset if self.code == 100 and (self.body_bytes_left == -1 or self.body_bytes_left == 0) and self.count_100 < 5 self.reset_except_queue self.count_100 += 1 end end
Returns the response based command string.
# File lib/rex/proto/http/response.rb, line 218 def cmd_string "HTTP\/#{proto} #{code}#{(message and message.length > 0) ? ' ' + message : ''}\r\n" end
Returns a parsed HTML document. Instead of using regexes to parse the HTML body, you should use this and use the Nokogiri API.
@see www.nokogiri.org/ @return [Nokogiri::HTML::Document]
# File lib/rex/proto/http/response.rb, line 92 def get_html_document Nokogiri::HTML(self.body) end
Returns meta tags. You will probably want to use this the web app's version info (or other stuff) can be found in the metadata.
@return [Array<Nokogiri::XML::Element>]
# File lib/rex/proto/http/response.rb, line 126 def get_html_meta_elements n = get_html_document n.search('//meta') end
Returns parsed JavaScript blocks. The parsed version is a RKelly object that allows you to be able do advanced parsing.
@see github.com/tenderlove/rkelly @return [Array<RKelly::Nodes::SourceElementsNode>]
# File lib/rex/proto/http/response.rb, line 136 def get_html_scripts n = get_html_document rkelly = RKelly::Parser.new n.search('//script').map { |s| rkelly.parse(s.text) } end
Returns a parsed json document. Instead of using regexes to parse the JSON body, you should use this.
@return [Hash]
# File lib/rex/proto/http/response.rb, line 109 def get_json_document json = {} begin json = JSON.parse(self.body) rescue JSON::ParserError => e elog("#{e.class} #{e.message}\n#{e.backtrace * "\n"}") end json end
Returns a parsed XML document. Instead of using regexes to parse the XML body, you should use this and use the Nokogiri API.
@see www.nokogiri.org/ @return [Nokogiri::XML::Document]
# File lib/rex/proto/http/response.rb, line 101 def get_xml_document Nokogiri::XML(self.body) end
Answers if the response is a redirection one.
@return [Boolean] true if the response is a redirection, false otherwise.
# File lib/rex/proto/http/response.rb, line 199 def redirect? [301, 302, 303, 307, 308].include?(code) end
Provides the uri of the redirection location.
@return [URI] the uri of the redirection location. @return [nil] if the response hasn't a Location header or it isn't a valid uri.
# File lib/rex/proto/http/response.rb, line 207 def redirection begin URI(headers['Location']) rescue ::URI::InvalidURIError nil end end
Updates the various parts of the HTTP response command string.
# File lib/rex/proto/http/response.rb, line 173 def update_cmd_parts(str) if (md = str.match(/HTTP\/(.+?)\s+(\d+)\s?(.+?)\r?\n?$/)) self.message = md[3].gsub(/\r/, '') self.code = md[2].to_i self.proto = md[1] else raise RuntimeError, "Invalid response command string", caller end check_100() end