Module: Ronin::CLI::Printing::HTTP Private
- Includes:
- SyntaxHighlighting
- Included in:
- Commands::Http, HTTPShell
- Defined in:
- lib/ronin/cli/printing/http.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Common methods for Commands::Http and HTTPShell.
Instance Method Summary collapse
-
#print_body(response) ⇒ Object
private
Prints the response body.
-
#print_headers(response) ⇒ Object
private
Prints the response headers.
-
#print_highlighted_body(response) ⇒ Object
private
Prints a syntax highlighted response body.
-
#print_last_newline(text) ⇒ Object
private
Ensures that a final new-line is printed after the given text.
-
#print_plain_body(response) ⇒ Object
private
Prints a plain unhighlighted response body.
-
#print_response(response, show_headers: nil) ⇒ Object
private
Prints the response.
-
#syntax_lexer_for_content_type(content_type) ⇒ Rouge::Lexers::HTML, ...
private
Returns the syntax lexer for the given
Content-Type
header.
Methods included from SyntaxHighlighting
#initialize, #syntax_formatter, #syntax_lexer, #syntax_lexer_for, #syntax_theme
Instance Method Details
#print_body(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints the response body.
109 110 111 112 113 114 115 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints the response headers.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints a syntax highlighted response body.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Ensures that a final new-line is printed after the given text.
36 37 38 39 40 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints a plain unhighlighted response body.
48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints the response.
147 148 149 150 |
# 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) ⇒ Rouge::Lexers::HTML, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the syntax lexer for the given Content-Type
header.
The specific syntax lexer or nil
if the Content-Type
was not
recognized.
74 75 76 77 78 |
# 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 |