module Ronin::CLI::Printing::HTTP
Common methods for {Commands::Http} and {HTTPShell}.
Public Instance Methods
print_body(response)
click to toggle source
Prints the response body.
@param [Net::HTTPResponse] response
The HTTP response object.
# File lib/ronin/cli/printing/http.rb, line 109 def print_body(response) if ansi? print_highlighted_body(response) else print_plain_body(response) end end
print_headers(response)
click to toggle source
Prints the response headers.
@param [Net::HTTPResponse] response
The HTTP response object.
# File lib/ronin/cli/printing/http.rb, line 123 def print_headers(response) color = if response.code < '300' then colors.green elsif response.code < '400' then colors.yellow else colors.red end puts "#{colors.bold}#{color}HTTP/#{response.http_version} #{response.code}#{colors.reset}" response.each_capitalized do |name,value| puts "#{color}#{colors.bold(name)}: #{value}#{colors.reset}" end puts end
print_highlighted_body(response)
click to toggle source
Prints a syntax highlighted response body.
@param [Net::HTTPResponse] response
The HTTP response object.
# File lib/ronin/cli/printing/http.rb, line 86 def print_highlighted_body(response) content_type = response['Content-Type'] lexer = syntax_lexer_for_content_type(content_type) last_chunk = nil response.read_body do |chunk| chunk.force_encoding(Encoding::UTF_8) stdout.write(@syntax_formatter.format(lexer.lex(chunk))) last_chunk = chunk end print_last_newline(last_chunk) end
print_last_newline(text)
click to toggle source
Ensures that a final new-line is printed after the given text.
@param [String] text
The previous text that was printed.
# File lib/ronin/cli/printing/http.rb, line 36 def print_last_newline(text) if (stdout.tty? && text && !text.end_with?("\n")) puts end end
print_plain_body(response)
click to toggle source
Prints a plain unhighlighted response body.
@param [Net::HTTPResponse] response
The HTTP response object.
# File lib/ronin/cli/printing/http.rb, line 48 def print_plain_body(response) last_chunk = nil response.read_body do |chunk| stdout.write(chunk) last_chunk = chunk end print_last_newline(last_chunk) end
print_response(response, show_headers: nil)
click to toggle source
Prints the response.
@param [Net::HTTPResponse] response
The HTTP response object.
@param [Boolean] show_headers
Controls whether the response headers are printed.
# File lib/ronin/cli/printing/http.rb, line 147 def print_response(response, show_headers: nil) print_headers(response) if show_headers print_body(response) end
syntax_lexer_for_content_type(content_type)
click to toggle source
Returns the syntax lexer for the given ‘Content-Type` header.
@param [String, nil] content_type
The HTTP `Content-Type` header value.
@return [Rouge::Lexers::HTML,
Rouge::Lexers::XML, Rouge::Lexers::JavaScript, Rouge::Lexers::JSON, Rouge::Lexers::PlainText] The specific syntax lexer or `nil` if the `Content-Type` was not recognized.
# File lib/ronin/cli/printing/http.rb, line 74 def syntax_lexer_for_content_type(content_type) mimetype = content_type && content_type.sub(/;.*$/,'') syntax_lexer_for(mimetype: mimetype) end