7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/wsio.rb', line 7
def start(args)
host_and_path = args.pop
if host_and_path.include? '@'
auth, host_and_path = host_and_path.split('@')
user, pass = (auth.split(':')<<nil)[0,2]
end
raise "No host specified" unless host_and_path
args = args.join.gsub('-', '')
scheme = (args.include? 's') ? 'https' : 'http'
url = "#{scheme}://#{host_and_path}"
if args.include? 'l'
c = HTTPClient.new
c.get_content(url) do |chunk|
obj = JSON.parse(chunk)
if obj.is_a? Array
puts obj.first
else
puts chunk
end
end
else
STDIN.each_line do |line|
begin
obj = JSON.parse(line)
if obj.is_a? Hash
body = line
= {"Content-Type" => "application/json"}
elsif obj.is_a? Array
body = obj.first.to_s
= {"Content-Type" => "text/plain"}
else
body = obj.to_s
= {"Content-Type" => "text/plain"}
end
rescue JSON::ParserError
body = line.strip
if body.split('&').collect{|m| /^[^&^=]+=[^&^=]+$/.match(m) }.all?
= {}
else
= {"Content-Type" => "text/plain"}
end
end
c = HTTPClient.new
['Authorization'] = "Basic #{Base64.encode64([user,pass].join(':')).gsub("\n",'')}" if user c.ssl_config.verify_mode = nil if args.include? 'k'
resp = c.post(url, body, )
if resp.status != 200
puts resp.content
end
end
end
end
|