Class: Runc
- Inherits:
-
Object
- Object
- Runc
- Defined in:
- lib/runc.rb,
lib/runc/version.rb
Overview
nc REPL
Constant Summary collapse
- VERSION =
:nodoc
"0.1.9"
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
:stopdoc:.
-
#port ⇒ Object
readonly
:stopdoc:.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#req ⇒ Object
Returns the value of attribute req.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
-
.has_nc ⇒ Object
check if nc is installed.
-
.help ⇒ Object
:nodoc:.
- .main(args) ⇒ Object
-
.version ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#ask(prompt) ⇒ Object
prompt user for input.
-
#initialize(hostname, port, body, res_header) ⇒ Runc
constructor
:nodoc:.
-
#protocol_file ⇒ Object
file with internet protocols and ports.
-
#read_protocols ⇒ Object
generate a hash of protocols and port numbers.
-
#repl ⇒ Object
:nodoc:.
-
#update_header ⇒ Object
reads a hash from user input hash should be of the form: key1: val1 [, key2: val2 [, …]].
-
#update_uri ⇒ Object
:nodoc:.
Constructor Details
#initialize(hostname, port, body, res_header) ⇒ Runc
:nodoc:
82 83 84 85 86 87 88 |
# File 'lib/runc.rb', line 82 def initialize(hostname, port, body, res_header) # :nodoc: @header = {'host' => hostname} @port = port @body = body @res_header = res_header @req = 'get' end |
Instance Attribute Details
#header ⇒ Object (readonly)
:stopdoc:
13 14 15 |
# File 'lib/runc.rb', line 13 def header @header end |
#port ⇒ Object (readonly)
:stopdoc:
13 14 15 |
# File 'lib/runc.rb', line 13 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
15 16 17 |
# File 'lib/runc.rb', line 15 def protocol @protocol end |
#req ⇒ Object
Returns the value of attribute req.
15 16 17 |
# File 'lib/runc.rb', line 15 def req @req end |
#uri ⇒ Object
Returns the value of attribute uri.
15 16 17 |
# File 'lib/runc.rb', line 15 def uri @uri end |
Class Method Details
.has_nc ⇒ Object
check if nc is installed
52 53 54 |
# File 'lib/runc.rb', line 52 def self.has_nc system("type -p nc", [:out, :err] => File::NULL) end |
.help ⇒ Object
:nodoc:
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/runc.rb', line 56 def self.help # :nodoc: puts <<~eos Usage: runc [options] Example: runc header> host: example.com, port: 80 request> get Options: -B, --no-body discard the response body -H, --no-header discard the response header -h, --help print this message -n, --host host name (default 'localhost') -p, --port port (default 8000) -v, --version print runc version eos exit end |
.main(args) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/runc.rb', line 21 def self.main(args) raise(RuntimeError, "Missing nc dependency") unless has_nc help if(args.include?("-h") or args.include?("--help")) version if (args.include?("-v") or args.include?("--version")) if(args.include?('-p') or args.include?('--port')) args.map! {|a| a == '--interval' ? '-i' : a} _, port = args.slice!(args.index('-p'),2) port = port.to_i end port ||= 8000 if(args.include?('-n') or args.include?('--host')) args.map! {|a| a == '--host' ? '-n' : a} _, hostname = args.slice!(args.index('-n'),2) end hostname ||= 'localhost' body = (args.include?("-B") or args.include?("--no-body")) ? nil : true res_header = (args.include?("-H") or args.include?("--no-header")) ? nil : true new(hostname, port, body, res_header).repl rescue => e abort e. end |
.version ⇒ Object
:nodoc:
77 78 79 80 |
# File 'lib/runc.rb', line 77 def self.version # :nodoc: puts "runc #{VERSION}" exit end |
Instance Method Details
#ask(prompt) ⇒ Object
prompt user for input
168 169 170 171 |
# File 'lib/runc.rb', line 168 def ask(prompt) res = Readline.readline("#{prompt}> ", true) res.nil? ? (puts; exit) : res end |
#protocol_file ⇒ Object
file with internet protocols and ports. More info in ‘man services’
92 93 94 |
# File 'lib/runc.rb', line 92 def protocol_file '/etc/services' end |
#read_protocols ⇒ Object
generate a hash of protocols and port numbers
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/runc.rb', line 98 def read_protocols @protocol = {} File.open(protocol_file) do |f| f.each_line do |l| next if l.match?(/^#|$^/) @protocol[l.split[1].sub(/\/.*/i,'').to_i] = l.split[0] end end # default to http if +port+ is larger than 2000 @protocol.keys.each do |port| @protocol[port] = 'http' if port > 2000 end end |
#repl ⇒ Object
:nodoc:
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/runc.rb', line 113 def repl # :nodoc: read_protocols loop do update_header req_tmp=ask('request') @req = req_tmp.empty? ? @req : req_tmp update_uri request=Net::HTTP.const_get(@req[/^\w+/].capitalize).new(@uri, @header) if [Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Patch].include?(request.class) request.body=ask('body') end http=Net::HTTP.new(@uri.host,@uri.port) http.use_ssl = port==443 ? true : false response=http.request(request) # request headers @header.map {|h, v| puts "> \e[1m#{h}\e[m: #{v}"} puts ">" # response headers if @res_header puts "< HTTP/1.1 #{response.code} #{response.}" response.each_header {|h, v| puts "< \e[1m#{h}\e[m: #{v}"} puts "<" end # response body puts(response.body) if @body rescue => e puts "#{caller.first}: #{e.}" end end |
#update_header ⇒ Object
reads a hash from user input hash should be of the form: key1: val1 [, key2: val2 [, …]]
157 158 159 160 161 162 163 164 |
# File 'lib/runc.rb', line 157 def update_header header_tmp=YAML.load("{#{ask('header')}}") # if +header_tmp+ contains a +port+ key, update the port @port=header_tmp.key?('port') ? header_tmp.delete('port') : @port @header.update(header_tmp) end |
#update_uri ⇒ Object
:nodoc:
173 174 175 |
# File 'lib/runc.rb', line 173 def update_uri # :nodoc: @uri=URI("#{@protocol[port]}://#{@header['host']}:#{port}/#{@req.sub(/^\w+ */,'')}") end |