|
1 | 1 | #!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
2 | 3 |
|
3 | 4 | require 'acquia-http-hmac' |
4 | 5 | require 'optparse' |
5 | 6 | require 'ostruct' |
6 | 7 |
|
7 | | -if ARGV[0] |
8 | | - url = ARGV[0] |
9 | | -end |
| 8 | +url = ARGV[0] if ARGV[0] |
10 | 9 |
|
11 | 10 | # Argument processing |
12 | 11 | options = OpenStruct.new |
13 | 12 | options.realm = 'Test' |
14 | 13 | options.http_method = 'GET' |
15 | 14 | o = OptionParser.new do |opts| |
16 | | - opts.banner = "Usage: #{$0} URL -u ID:PASSWORD" |
17 | | - opts.on("-r", "--realm [REALM]", "Server auth realm. Default 'Test'.") { |v| options.realm = v } |
18 | | - opts.on("-u", "--user [ID:PASSWORD]", "HMAC creds") { |v| options.user = v } |
19 | | - opts.on("-d", "--data [DATA]", "Data to POST.") { |v| options.data = v } |
20 | | - opts.on("-X", "--request [METHOD]", "HTTP method. Defaults to GET, or POST if --data is specified.") { |v| options.http_method = v.upcase } |
| 15 | + opts.banner = "Usage: #{$PROGRAM_NAME} URL -u ID:PASSWORD" |
| 16 | + opts.on('-r', '--realm [REALM]', "Server auth realm. Default 'Test'.") { |v| options.realm = v } |
| 17 | + opts.on('-u', '--user [ID:PASSWORD]', 'HMAC creds') { |v| options.user = v } |
| 18 | + opts.on('-d', '--data [DATA]', 'Data to POST.') { |v| options.data = v } |
| 19 | + opts.on('-X', '--request [METHOD]', 'HTTP method. Defaults to GET, or POST if --data is specified.') { |v| options.http_method = v.upcase } |
21 | 20 | end |
22 | 21 | begin |
23 | 22 | o.parse! |
24 | | -rescue Exception => e |
| 23 | +rescue StandardError => e |
25 | 24 | puts e.message |
26 | 25 | puts o.help |
27 | 26 | exit 1 |
28 | 27 | end |
29 | 28 |
|
30 | | -if ARGV.empty? or !options.user |
| 29 | +if ARGV.empty? || !options.user |
31 | 30 | puts o.help |
32 | 31 | exit |
33 | 32 | end |
34 | 33 |
|
35 | 34 | uri = URI(Addressable::URI.escape.encode(url)) |
36 | 35 |
|
37 | | -if uri.path == '' |
38 | | - uri.path = '/' |
39 | | -end |
| 36 | +uri.path = '/' if uri.path == '' |
40 | 37 |
|
41 | 38 | host = uri.host |
42 | | -if uri.port && uri.port != '443' |
43 | | - host << ':' + uri.port |
44 | | -end |
| 39 | +host << ":#{uri.port}" if uri.port && uri.port != '443' |
45 | 40 |
|
46 | 41 | id, password = options.user.split(':', 2) |
47 | 42 |
|
48 | 43 | mac = Acquia::HTTPHmac::Auth.new(options.realm, password) |
49 | 44 |
|
50 | | - |
51 | | - |
52 | 45 | args = { |
53 | 46 | http_method: options.http_method, |
54 | 47 | host: host, |
55 | 48 | id: id, |
56 | | - path_info: uri.path, |
| 49 | + path_info: uri.path |
57 | 50 | } |
58 | 51 |
|
59 | | -case |
60 | | -when options.http_method == 'GET' |
61 | | - req = Net::HTTP::GET.new(uri) |
62 | | -when options.http_method == 'HEAD' |
63 | | - req = Net::HTTP::HEAD.new(uri) |
64 | | -when options.http_method == 'POST' |
65 | | - req = Net::HTTP::POST.new(uri) |
66 | | -when options.http_method == 'PUT' |
67 | | - req = Net::HTTP::PUT.new(uri) |
68 | | -when options.http_method == 'DELETE' |
69 | | - req = Net::HTTP::DELETE.new(uri) |
| 52 | +case options.http_method |
| 53 | +when 'GET' |
| 54 | + Net::HTTP::GET.new(uri) |
| 55 | +when 'HEAD' |
| 56 | + Net::HTTP::HEAD.new(uri) |
| 57 | +when 'POST' |
| 58 | + Net::HTTP::POST.new(uri) |
| 59 | +when 'PUT' |
| 60 | + Net::HTTP::PUT.new(uri) |
| 61 | +when 'DELETE' |
| 62 | + Net::HTTP::DELETE.new(uri) |
70 | 63 | else |
71 | | - fail("Unsupported HTTP verb #{options.http_method}") |
| 64 | + raise("Unsupported HTTP verb #{options.http_method}") |
72 | 65 | end |
73 | 66 | mac.prepare_request_headers(args).each do |name, value| |
74 | | - #header(name, value) |
| 67 | + # header(name, value) |
75 | 68 | end |
76 | 69 |
|
77 | 70 | net = Net::HTTP.new(uri.host, uri.port) |
78 | | -net.use_ssl= uri.host != 'localhost' |
79 | | - |
| 71 | +net.use_ssl = uri.host != 'localhost' |
0 commit comments