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.

Since:

  • 2.0.0

Instance Method Summary collapse

Methods included from SyntaxHighlighting

#initialize, #syntax_formatter, #syntax_lexer, #syntax_lexer_for, #syntax_theme

Instance Method Details

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.

Parameters:

  • response (Net::HTTPResponse)

    The HTTP response object.

Since:

  • 2.0.0



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

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.

Parameters:

  • response (Net::HTTPResponse)

    The HTTP response object.

Since:

  • 2.0.0



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

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.

Parameters:

  • response (Net::HTTPResponse)

    The HTTP response object.

Since:

  • 2.0.0



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

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.

Parameters:

  • text (String)

    The previous text that was printed.

Since:

  • 2.0.0



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

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.

Parameters:

  • response (Net::HTTPResponse)

    The HTTP response object.

Since:

  • 2.0.0



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

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.

Parameters:

  • response (Net::HTTPResponse)

    The HTTP response object.

  • show_headers (Boolean) (defaults to: nil)

    Controls whether the response headers are printed.

Since:

  • 2.0.0



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.

Parameters:

  • content_type (String, nil)

    The HTTP Content-Type header value.

Returns:

  • (Rouge::Lexers::HTML, Rouge::Lexers::XML, Rouge::Lexers::JavaScript, Rouge::Lexers::JSON, Rouge::Lexers::PlainText)

Since:

  • 2.0.0



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