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.7"
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:
73 74 75 76 77 78 79 |
# File 'lib/runc.rb', line 73 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 |
# File 'lib/runc.rb', line 56 def self.help # :nodoc: puts " -B, --no-body discard the response body\n -H, --no-header discard the response header\n -h, --help print this message\n -n, --host host name (default 'localhost')\n -p, --port port (default 8000)\n -v, --version print runc version\n eos\n exit\nend\n" |
.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:
68 69 70 71 |
# File 'lib/runc.rb', line 68 def self.version # :nodoc: puts "runc #{VERSION}" exit end |
Instance Method Details
#ask(prompt) ⇒ Object
prompt user for input
159 160 161 162 |
# File 'lib/runc.rb', line 159 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’
83 84 85 |
# File 'lib/runc.rb', line 83 def protocol_file '/etc/services' end |
#read_protocols ⇒ Object
generate a hash of protocols and port numbers
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/runc.rb', line 89 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:
104 105 106 107 108 109 110 111 112 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 |
# File 'lib/runc.rb', line 104 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.message}" 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.message}" end end |
#update_header ⇒ Object
reads a hash from user input hash should be of the form: key1: val1 [, key2: val2 [, …]]
148 149 150 151 152 153 154 155 |
# File 'lib/runc.rb', line 148 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:
164 165 166 |
# File 'lib/runc.rb', line 164 def update_uri # :nodoc: @uri=URI("#{@protocol[port]}://#{@header['host']}:#{port}/#{@req.sub(/^\w+ */,'')}") end |