rest-man 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/{multi-matrix-test.yml → ci.yml} +10 -1
- data/.github/workflows/single-matrix-test.yml +1 -0
- data/.gitignore +2 -0
- data/.rubocop-disables.yml +4 -29
- data/AUTHORS +5 -1
- data/CHANGELOG.md +14 -0
- data/Gemfile +3 -0
- data/README.md +30 -37
- data/Rakefile +1 -59
- data/_doc/lib/restman/abstract_response/_follow_redirection.rdoc +7 -0
- data/_doc/lib/restman/abstract_response/beautify_headers.rdoc +24 -0
- data/_doc/lib/restman/abstract_response/cookie_jar.rdoc +4 -0
- data/_doc/lib/restman/abstract_response/cookies.rdoc +12 -0
- data/_doc/lib/restman/abstract_response/follow_get_redirection.rdoc +2 -0
- data/_doc/lib/restman/abstract_response/follow_redirection.rdoc +2 -0
- data/_doc/lib/restman/abstract_response/headers.rdoc +2 -0
- data/_doc/lib/restman/abstract_response/response_set_vars.rdoc +5 -0
- data/_doc/lib/restman/abstract_response/return.rdoc +9 -0
- data/_doc/lib/restman/add_before_execution_proc.rdoc +2 -0
- data/_doc/lib/restman/create_log.rdoc +2 -0
- data/_doc/lib/restman/exception.rdoc +6 -0
- data/_doc/lib/restman/exceptions/timeout.rdoc +4 -0
- data/_doc/lib/restman/exceptions.rdoc +4 -0
- data/_doc/lib/restman/log=.rdoc +3 -0
- data/_doc/lib/restman/params_array/new.rdoc +20 -0
- data/_doc/lib/restman/params_array/process_pair.rdoc +4 -0
- data/_doc/lib/restman/params_array.rdoc +11 -0
- data/_doc/lib/restman/platform/jruby?.rdoc +4 -0
- data/_doc/lib/restman/platform/mac_mri?.rdoc +5 -0
- data/_doc/lib/restman/proxy.rdoc +2 -0
- data/_doc/lib/restman/proxy_set?.rdoc +5 -0
- data/_doc/lib/restman/raw_response/new.rdoc +6 -0
- data/_doc/lib/restman/raw_response.rdoc +10 -0
- data/_doc/lib/restman/request/cookie_jar.rdoc +3 -0
- data/_doc/lib/restman/request/cookies.rdoc +11 -0
- data/_doc/lib/restman/request/default_headers.rdoc +5 -0
- data/_doc/lib/restman/request/default_ssl_cert_store.rdoc +8 -0
- data/_doc/lib/restman/request/init/cookie_jar.rdoc +55 -0
- data/_doc/lib/restman/request/init/http_method.rdoc +15 -0
- data/_doc/lib/restman/request/make_cookie_header.rdoc +8 -0
- data/_doc/lib/restman/request/make_headers.rdoc +25 -0
- data/_doc/lib/restman/request/maybe_convert_extension.rdoc +18 -0
- data/_doc/lib/restman/request/process_result.rdoc +4 -0
- data/_doc/lib/restman/request/proxy_uri.rdoc +7 -0
- data/_doc/lib/restman/request/stringify_headers.rdoc +9 -0
- data/_doc/lib/restman/request/use_ssl.rdoc +4 -0
- data/_doc/lib/restman/request.rdoc +46 -0
- data/_doc/lib/restman/reset_before_execution_procs.rdoc +1 -0
- data/_doc/lib/restman/resource/[].rdoc +25 -0
- data/_doc/lib/restman/resource.rdoc +33 -0
- data/_doc/lib/restman/response/body.rdoc +7 -0
- data/_doc/lib/restman/response/create.rdoc +10 -0
- data/_doc/lib/restman/response/fix_encoding.rdoc +2 -0
- data/_doc/lib/restman/statuses.rdoc +11 -0
- data/_doc/lib/restman/utils/cgi_parse_header.rdoc +6 -0
- data/_doc/lib/restman/utils/encode_query_string.rdoc +90 -0
- data/_doc/lib/restman/utils/escape.rdoc +11 -0
- data/_doc/lib/restman/utils/flatten_params.rdoc +16 -0
- data/_doc/lib/restman/utils/get_encoding_from_headers.rdoc +24 -0
- data/_doc/lib/restman.rdoc +43 -0
- data/bin/console +15 -0
- data/lib/restman/abstract_response.rb +13 -60
- data/lib/restman/exception.rb +43 -0
- data/lib/restman/exceptions/exception_with_response.rb +7 -0
- data/lib/restman/exceptions/exceptions_map.rb +26 -0
- data/lib/restman/exceptions/request_failed.rb +15 -0
- data/lib/restman/exceptions/server_broke_connection.rb +13 -0
- data/lib/restman/exceptions/timeout.rb +37 -0
- data/lib/restman/params_array/process_pair.rb +39 -0
- data/lib/restman/params_array.rb +3 -48
- data/lib/restman/payload/base.rb +57 -0
- data/lib/restman/payload/multipart/write_content_disposition.rb +88 -0
- data/lib/restman/payload/multipart.rb +56 -0
- data/lib/restman/payload/streamed.rb +22 -0
- data/lib/restman/payload/url_encoded.rb +14 -0
- data/lib/restman/payload.rb +14 -196
- data/lib/restman/platform.rb +2 -18
- data/lib/restman/raw_response.rb +2 -14
- data/lib/restman/request/default_ssl_cert_store.rb +13 -0
- data/lib/restman/request/fetch_body_to_tempfile.rb +58 -0
- data/lib/restman/request/init/cookie_jar.rb +65 -0
- data/lib/restman/request/init/ssl_opts.rb +70 -0
- data/lib/restman/request/init/url/add_query_from_headers.rb +51 -0
- data/lib/restman/request/init/url/normalize_url.rb +19 -0
- data/lib/restman/request/init/url.rb +40 -0
- data/lib/restman/request/init.rb +106 -0
- data/lib/restman/request/log_request.rb +46 -0
- data/lib/restman/request/make_cookie_header.rb +16 -0
- data/lib/restman/request/make_headers.rb +39 -0
- data/lib/restman/request/maybe_convert_extension.rb +28 -0
- data/lib/restman/request/net_http_object.rb +25 -0
- data/lib/restman/request/process_result.rb +36 -0
- data/lib/restman/request/proxy_uri.rb +31 -0
- data/lib/restman/request/stringify_headers.rb +36 -0
- data/lib/restman/request/transmit.rb +152 -0
- data/lib/restman/request.rb +60 -745
- data/lib/restman/resource.rb +2 -60
- data/lib/restman/response.rb +3 -21
- data/lib/restman/statuses.rb +75 -0
- data/lib/restman/statuses_compatibility.rb +18 -0
- data/lib/restman/utils.rb +10 -206
- data/lib/restman/version.rb +1 -1
- data/lib/restman.rb +24 -62
- data/matrixeval.yml +19 -1
- data/rest-man.gemspec +4 -10
- data/spec/integration/capath_digicert/ce5e74ef.0 +1 -1
- data/spec/integration/request_spec.rb +13 -1
- data/spec/spec_helper.rb +11 -0
- data/spec/unit/abstract_response_spec.rb +14 -0
- data/spec/unit/exception_spec.rb +64 -0
- data/spec/unit/exceptions/backwards_campatibility_spec.rb +29 -0
- data/spec/unit/exceptions/exceptions_map_spec.rb +89 -0
- data/spec/unit/exceptions/request_failed_spec.rb +51 -0
- data/spec/unit/exceptions/server_broke_connection_spec.rb +8 -0
- data/spec/unit/exceptions/timeout_spec.rb +59 -0
- data/spec/unit/params_array/process_pair_spec.rb +59 -0
- data/spec/unit/params_array_spec.rb +15 -10
- data/spec/unit/payload/multipart_spec.rb +116 -0
- data/spec/unit/payload/streamed_spec.rb +48 -0
- data/spec/unit/payload/url_encoded_spec.rb +65 -0
- data/spec/unit/payload_spec.rb +0 -208
- data/spec/unit/request/init/url/add_query_from_headers_spec.rb +40 -0
- data/spec/unit/request/init/url/normalize_url_spec.rb +25 -0
- data/spec/unit/request/init_spec.rb +83 -0
- data/spec/unit/request_spec.rb +143 -151
- data/spec/unit/utils_spec.rb +96 -104
- metadata +132 -16
- data/lib/restman/exceptions.rb +0 -238
- data/lib/restman/windows/root_certs.rb +0 -105
- data/lib/restman/windows.rb +0 -8
- data/spec/unit/exceptions_spec.rb +0 -108
- data/spec/unit/windows/root_certs_spec.rb +0 -22
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'netrc'
|
3
|
+
|
4
|
+
module RestMan
|
5
|
+
class Request
|
6
|
+
module Init
|
7
|
+
|
8
|
+
autoload :Url, 'restman/request/init/url'
|
9
|
+
autoload :CookieJar, 'restman/request/init/cookie_jar'
|
10
|
+
autoload :SSLOpts, 'restman/request/init/ssl_opts'
|
11
|
+
|
12
|
+
include ActiveMethod
|
13
|
+
|
14
|
+
module_function
|
15
|
+
|
16
|
+
# :include: _doc/lib/restman/request/init/http_method.rdoc
|
17
|
+
def http_method(args)
|
18
|
+
method = args[:method]
|
19
|
+
raise ArgumentError.new('must pass :method') unless method
|
20
|
+
|
21
|
+
method.to_s.downcase
|
22
|
+
end
|
23
|
+
|
24
|
+
def headers(args)
|
25
|
+
(args[:headers] || {}).dup
|
26
|
+
end
|
27
|
+
|
28
|
+
active_method :url, module_function: true
|
29
|
+
|
30
|
+
def uri(url)
|
31
|
+
uri = URI.parse(url)
|
32
|
+
|
33
|
+
if uri.hostname.nil? || uri.hostname == ''
|
34
|
+
raise URI::InvalidURIError.new("bad URI(no host provided): #{url}")
|
35
|
+
end
|
36
|
+
|
37
|
+
uri
|
38
|
+
end
|
39
|
+
|
40
|
+
def auth(uri, args)
|
41
|
+
user = CGI.unescape(uri.user) if uri.user
|
42
|
+
password = CGI.unescape(uri.password) if uri.password
|
43
|
+
|
44
|
+
if !user && !password
|
45
|
+
user, password = Netrc.read[uri.hostname]
|
46
|
+
end
|
47
|
+
|
48
|
+
user = args[:user] if args.include?(:user)
|
49
|
+
password = args[:password] if args.include?(:password)
|
50
|
+
|
51
|
+
[user, password]
|
52
|
+
end
|
53
|
+
|
54
|
+
active_method :cookie_jar, module_function: true
|
55
|
+
|
56
|
+
def read_timeout(args)
|
57
|
+
if args.key?(:read_timeout)
|
58
|
+
yield args[:read_timeout]
|
59
|
+
elsif args.key?(:timeout)
|
60
|
+
yield args[:timeout]
|
61
|
+
else
|
62
|
+
60
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def write_timeout(args)
|
67
|
+
if args.key?(:write_timeout)
|
68
|
+
yield args[:write_timeout]
|
69
|
+
elsif args.key?(:timeout)
|
70
|
+
yield args[:timeout]
|
71
|
+
else
|
72
|
+
60
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def open_timeout(args)
|
77
|
+
if args.key?(:open_timeout)
|
78
|
+
yield args[:open_timeout]
|
79
|
+
elsif args.key?(:timeout)
|
80
|
+
yield args[:timeout]
|
81
|
+
else
|
82
|
+
60
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def keep_alive_timeout(args)
|
87
|
+
if args.key?(:keep_alive_timeout)
|
88
|
+
yield args[:keep_alive_timeout]
|
89
|
+
else
|
90
|
+
2
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def stream_log_percent(args)
|
95
|
+
percent = args[:stream_log_percent] || 10
|
96
|
+
if percent <= 0 || percent > 100
|
97
|
+
raise ArgumentError.new("Invalid :stream_log_percent #{@stream_log_percent.inspect}")
|
98
|
+
end
|
99
|
+
percent
|
100
|
+
end
|
101
|
+
|
102
|
+
active_method :ssl_opts, SSLOpts, module_function: true
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class LogRequest < ActiveMethod::Base
|
4
|
+
|
5
|
+
def call
|
6
|
+
return unless log
|
7
|
+
|
8
|
+
out = []
|
9
|
+
|
10
|
+
out << "RestMan.#{request.method} #{redacted_url.inspect}"
|
11
|
+
out << payload.short_inspect if payload
|
12
|
+
out << request.processed_headers.to_a.sort.map { |(k, v)| [k.inspect, v.inspect].join("=>") }.join(", ")
|
13
|
+
log << out.join(', ') + "\n"
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def log
|
19
|
+
request.log
|
20
|
+
end
|
21
|
+
|
22
|
+
def redacted_uri
|
23
|
+
if uri.password
|
24
|
+
sanitized_uri = uri.dup
|
25
|
+
sanitized_uri.password = 'REDACTED'
|
26
|
+
sanitized_uri
|
27
|
+
else
|
28
|
+
uri
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def redacted_url
|
33
|
+
redacted_uri.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def payload
|
37
|
+
request.payload
|
38
|
+
end
|
39
|
+
|
40
|
+
def uri
|
41
|
+
request.uri
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class MakeCookieHeader < ActiveMethod::Base
|
4
|
+
|
5
|
+
def call
|
6
|
+
return nil if request.cookie_jar.nil?
|
7
|
+
|
8
|
+
arr = request.cookie_jar.cookies(request.url)
|
9
|
+
return nil if arr.empty?
|
10
|
+
|
11
|
+
return HTTP::Cookie.cookie_value(arr)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class MakeHeaders < ActiveMethod::Base
|
4
|
+
|
5
|
+
argument :user_headers
|
6
|
+
|
7
|
+
def call
|
8
|
+
headers = StringifyHeaders.call(default_headers).merge(StringifyHeaders.call(user_headers))
|
9
|
+
|
10
|
+
# override headers from the payload (e.g. Content-Type, Content-Length)
|
11
|
+
if payload
|
12
|
+
headers = payload.headers.merge(headers)
|
13
|
+
end
|
14
|
+
|
15
|
+
# merge in cookies
|
16
|
+
cookies = request.make_cookie_header
|
17
|
+
if cookies && !cookies.empty?
|
18
|
+
if headers['Cookie']
|
19
|
+
warn('warning: overriding "Cookie" header with :cookies option')
|
20
|
+
end
|
21
|
+
headers['Cookie'] = cookies
|
22
|
+
end
|
23
|
+
|
24
|
+
headers
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def default_headers
|
30
|
+
request.default_headers
|
31
|
+
end
|
32
|
+
|
33
|
+
def payload
|
34
|
+
request.payload
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class MaybeConvertExtension < ActiveMethod::Base
|
4
|
+
|
5
|
+
argument :ext
|
6
|
+
|
7
|
+
def call
|
8
|
+
unless ext =~ /\A[a-zA-Z0-9_@-]+\z/
|
9
|
+
# Don't look up strings unless they look like they could be a file
|
10
|
+
# extension known to mime-types.
|
11
|
+
#
|
12
|
+
# There currently isn't any API public way to look up extensions
|
13
|
+
# directly out of MIME::Types, but the type_for() method only strips
|
14
|
+
# off after a period anyway.
|
15
|
+
return ext
|
16
|
+
end
|
17
|
+
|
18
|
+
types = MIME::Types.type_for(ext)
|
19
|
+
if types.empty?
|
20
|
+
ext
|
21
|
+
else
|
22
|
+
types.first.content_type
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class NetHTTPObject < ActiveMethod::Base
|
4
|
+
|
5
|
+
argument :hostname
|
6
|
+
argument :port
|
7
|
+
|
8
|
+
def call
|
9
|
+
p_uri = request.proxy_uri
|
10
|
+
|
11
|
+
if p_uri.nil?
|
12
|
+
# no proxy set
|
13
|
+
Net::HTTP.new(hostname, port)
|
14
|
+
elsif !p_uri
|
15
|
+
# proxy explicitly set to none
|
16
|
+
Net::HTTP.new(hostname, port, nil, nil, nil, nil)
|
17
|
+
else
|
18
|
+
Net::HTTP.new(hostname, port,
|
19
|
+
p_uri.hostname, p_uri.port, p_uri.user, p_uri.password)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class ProcessResult < ActiveMethod::Base
|
4
|
+
|
5
|
+
argument :res
|
6
|
+
argument :start_time
|
7
|
+
argument :tempfile, default: nil
|
8
|
+
|
9
|
+
def call(&block)
|
10
|
+
if raw_response
|
11
|
+
unless tempfile
|
12
|
+
raise ArgumentError.new('tempfile is required')
|
13
|
+
end
|
14
|
+
response = RawResponse.new(tempfile, res, request, start_time)
|
15
|
+
else
|
16
|
+
response = Response.create(res.body, res, request, start_time)
|
17
|
+
end
|
18
|
+
|
19
|
+
response.log_response
|
20
|
+
|
21
|
+
if block_given?
|
22
|
+
block.call(response, request, res, & block)
|
23
|
+
else
|
24
|
+
response.return!(&block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def raw_response
|
31
|
+
request.raw_response
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class ProxyURI < ActiveMethod::Base
|
4
|
+
|
5
|
+
def call
|
6
|
+
if request.instance_variable_defined?(:@proxy)
|
7
|
+
if request.proxy
|
8
|
+
URI.parse(request.proxy)
|
9
|
+
else
|
10
|
+
false
|
11
|
+
end
|
12
|
+
elsif RestMan.proxy_set?
|
13
|
+
if RestMan.proxy
|
14
|
+
URI.parse(RestMan.proxy)
|
15
|
+
else
|
16
|
+
false
|
17
|
+
end
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def proxy
|
26
|
+
request.proxy
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class StringifyHeaders < ActiveMethod::Base
|
4
|
+
|
5
|
+
argument :headers
|
6
|
+
|
7
|
+
def call
|
8
|
+
headers.inject({}) do |result, (key, value)|
|
9
|
+
|
10
|
+
if key.is_a? Symbol
|
11
|
+
key = key.to_s.split(/_/).map(&:capitalize).join('-')
|
12
|
+
end
|
13
|
+
|
14
|
+
if 'CONTENT-TYPE' == key.upcase
|
15
|
+
result[key] = MaybeConvertExtension.call(value.to_s)
|
16
|
+
elsif 'ACCEPT' == key.upcase
|
17
|
+
# Accept can be composed of several comma-separated values
|
18
|
+
if value.is_a? Array
|
19
|
+
target_values = value
|
20
|
+
else
|
21
|
+
target_values = value.to_s.split ','
|
22
|
+
end
|
23
|
+
result[key] = target_values.map { |ext|
|
24
|
+
MaybeConvertExtension.call(ext.to_s.strip)
|
25
|
+
}.join(', ')
|
26
|
+
else
|
27
|
+
result[key] = value.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
result
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
module RestMan
|
2
|
+
class Request
|
3
|
+
class Transmit < ActiveMethod::Base
|
4
|
+
|
5
|
+
argument :uri
|
6
|
+
argument :req
|
7
|
+
argument :payload
|
8
|
+
|
9
|
+
def call
|
10
|
+
# We set this to true in the net/http block so that we can distinguish
|
11
|
+
# read_timeout from open_timeout. Now that we only support Ruby 2.0+,
|
12
|
+
# this is only needed for Timeout exceptions thrown outside of Net::HTTP.
|
13
|
+
established_connection = false
|
14
|
+
|
15
|
+
request.setup_credentials req
|
16
|
+
|
17
|
+
net = request.net_http_object(uri.hostname, uri.port)
|
18
|
+
net.use_ssl = uri.is_a?(URI::HTTPS)
|
19
|
+
net.ssl_version = request.ssl_version if request.ssl_version
|
20
|
+
net.min_version = request.ssl_min_version if request.ssl_min_version
|
21
|
+
net.max_version = request.ssl_max_version if request.ssl_max_version
|
22
|
+
net.ssl_timeout = request.ssl_timeout if request.ssl_timeout
|
23
|
+
net.ciphers = request.ssl_ciphers if request.ssl_ciphers
|
24
|
+
|
25
|
+
net.verify_mode = request.verify_ssl
|
26
|
+
|
27
|
+
net.cert = request.ssl_client_cert if request.ssl_client_cert
|
28
|
+
net.key = request.ssl_client_key if request.ssl_client_key
|
29
|
+
net.ca_file = request.ssl_ca_file if request.ssl_ca_file
|
30
|
+
net.ca_path = request.ssl_ca_path if request.ssl_ca_path
|
31
|
+
net.cert_store = request.ssl_cert_store if request.ssl_cert_store
|
32
|
+
|
33
|
+
net.max_retries = request.max_retries
|
34
|
+
|
35
|
+
net.keep_alive_timeout = request.keep_alive_timeout if request.keep_alive_timeout
|
36
|
+
net.close_on_empty_response = request.close_on_empty_response if request.close_on_empty_response
|
37
|
+
net.local_host = request.local_host if request.local_host
|
38
|
+
net.local_port = request.local_port if request.local_port
|
39
|
+
|
40
|
+
# We no longer rely on net.verify_callback for the main SSL verification
|
41
|
+
# because it's not well supported on all platforms (see comments below).
|
42
|
+
# But do allow users to set one if they want.
|
43
|
+
if request.ssl_verify_callback
|
44
|
+
net.verify_callback = request.ssl_verify_callback
|
45
|
+
|
46
|
+
# Hilariously, jruby only calls the callback when cert_store is set to
|
47
|
+
# something, so make sure to set one.
|
48
|
+
# https://github.com/jruby/jruby/issues/597
|
49
|
+
if RestMan::Platform.jruby?
|
50
|
+
net.cert_store ||= OpenSSL::X509::Store.new
|
51
|
+
end
|
52
|
+
|
53
|
+
if request.ssl_verify_callback_warnings != false
|
54
|
+
if print_verify_callback_warnings
|
55
|
+
warn('pass :ssl_verify_callback_warnings => false to silence this')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE
|
61
|
+
warn('WARNING: OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE')
|
62
|
+
warn('This dangerous monkey patch leaves you open to MITM attacks!')
|
63
|
+
warn('Try passing :verify_ssl => false instead.')
|
64
|
+
end
|
65
|
+
|
66
|
+
net.read_timeout = request.read_timeout
|
67
|
+
net.open_timeout = request.open_timeout
|
68
|
+
net.write_timeout = request.write_timeout
|
69
|
+
|
70
|
+
RestMan.before_execution_procs.each do |before_proc|
|
71
|
+
before_proc.call(req, request.args)
|
72
|
+
end
|
73
|
+
|
74
|
+
if request.before_execution_proc
|
75
|
+
request.before_execution_proc.call(req, request.args)
|
76
|
+
end
|
77
|
+
|
78
|
+
request.log_request
|
79
|
+
|
80
|
+
start_time = Time.now
|
81
|
+
tempfile = nil
|
82
|
+
|
83
|
+
net.start do |http|
|
84
|
+
established_connection = true
|
85
|
+
|
86
|
+
if request.block_response
|
87
|
+
net_http_do_request(http, req, payload, &request.block_response)
|
88
|
+
else
|
89
|
+
res = net_http_do_request(http, req, payload) { |http_response|
|
90
|
+
if request.raw_response
|
91
|
+
# fetch body into tempfile
|
92
|
+
tempfile = request.fetch_body_to_tempfile(http_response)
|
93
|
+
else
|
94
|
+
# fetch body
|
95
|
+
http_response.read_body
|
96
|
+
end
|
97
|
+
http_response
|
98
|
+
}
|
99
|
+
if block_given?
|
100
|
+
request.process_result(res, start_time, tempfile) do
|
101
|
+
yield
|
102
|
+
end
|
103
|
+
else
|
104
|
+
request.process_result(res, start_time, tempfile)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
rescue EOFError
|
109
|
+
raise RestMan::ServerBrokeConnection
|
110
|
+
rescue Net::OpenTimeout => err
|
111
|
+
raise RestMan::Exceptions::OpenTimeout.new(nil, err)
|
112
|
+
rescue Net::ReadTimeout => err
|
113
|
+
raise RestMan::Exceptions::ReadTimeout.new(nil, err)
|
114
|
+
rescue Net::WriteTimeout => err
|
115
|
+
raise RestMan::Exceptions::WriteTimeout.new(nil, err)
|
116
|
+
rescue Timeout::Error, Errno::ETIMEDOUT => err
|
117
|
+
# handling for non-Net::HTTP timeouts
|
118
|
+
if established_connection
|
119
|
+
raise RestMan::Exceptions::ReadTimeout.new(nil, err)
|
120
|
+
else
|
121
|
+
raise RestMan::Exceptions::OpenTimeout.new(nil, err)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
def print_verify_callback_warnings
|
128
|
+
warned = false
|
129
|
+
if RestMan::Platform.mac_mri?
|
130
|
+
warn('warning: ssl_verify_callback return code is ignored on OS X')
|
131
|
+
warned = true
|
132
|
+
end
|
133
|
+
if RestMan::Platform.jruby?
|
134
|
+
warn('warning: SSL verify_callback may not work correctly in jruby')
|
135
|
+
warn('see https://github.com/jruby/jruby/issues/597')
|
136
|
+
warned = true
|
137
|
+
end
|
138
|
+
warned
|
139
|
+
end
|
140
|
+
|
141
|
+
def net_http_do_request(http, req, body=nil, &block)
|
142
|
+
if body && body.respond_to?(:read)
|
143
|
+
req.body_stream = body
|
144
|
+
return http.request(req, nil, &block)
|
145
|
+
else
|
146
|
+
return http.request(req, body, &block)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|