shopify-bundler 1.10.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/.rubocop.yml +96 -0
- data/.rubocop_todo.yml +177 -0
- data/.travis.yml +104 -0
- data/CHANGELOG.md +2131 -0
- data/CODE_OF_CONDUCT.md +42 -0
- data/CONTRIBUTING.md +32 -0
- data/DEVELOPMENT.md +118 -0
- data/ISSUES.md +96 -0
- data/LICENSE.md +23 -0
- data/README.md +40 -0
- data/Rakefile +309 -0
- data/bin/rake +14 -0
- data/bin/rspec +10 -0
- data/bin/rubocop +11 -0
- data/bundler.gemspec +34 -0
- data/exe/bundle +21 -0
- data/exe/bundle_ruby +60 -0
- data/exe/bundler +21 -0
- data/lib/bundler.rb +499 -0
- data/lib/bundler/capistrano.rb +16 -0
- data/lib/bundler/cli.rb +435 -0
- data/lib/bundler/cli/binstubs.rb +37 -0
- data/lib/bundler/cli/cache.rb +34 -0
- data/lib/bundler/cli/check.rb +37 -0
- data/lib/bundler/cli/clean.rb +25 -0
- data/lib/bundler/cli/common.rb +56 -0
- data/lib/bundler/cli/config.rb +88 -0
- data/lib/bundler/cli/console.rb +37 -0
- data/lib/bundler/cli/exec.rb +51 -0
- data/lib/bundler/cli/gem.rb +208 -0
- data/lib/bundler/cli/init.rb +32 -0
- data/lib/bundler/cli/inject.rb +32 -0
- data/lib/bundler/cli/install.rb +188 -0
- data/lib/bundler/cli/lock.rb +35 -0
- data/lib/bundler/cli/open.rb +22 -0
- data/lib/bundler/cli/outdated.rb +86 -0
- data/lib/bundler/cli/package.rb +45 -0
- data/lib/bundler/cli/platform.rb +42 -0
- data/lib/bundler/cli/show.rb +74 -0
- data/lib/bundler/cli/update.rb +72 -0
- data/lib/bundler/cli/viz.rb +26 -0
- data/lib/bundler/constants.rb +5 -0
- data/lib/bundler/current_ruby.rb +218 -0
- data/lib/bundler/definition.rb +675 -0
- data/lib/bundler/dep_proxy.rb +45 -0
- data/lib/bundler/dependency.rb +119 -0
- data/lib/bundler/deployment.rb +62 -0
- data/lib/bundler/deprecate.rb +17 -0
- data/lib/bundler/dsl.rb +478 -0
- data/lib/bundler/endpoint_specification.rb +100 -0
- data/lib/bundler/env.rb +82 -0
- data/lib/bundler/environment.rb +41 -0
- data/lib/bundler/fetcher.rb +288 -0
- data/lib/bundler/fetcher/base.rb +26 -0
- data/lib/bundler/fetcher/dependency.rb +88 -0
- data/lib/bundler/fetcher/downloader.rb +60 -0
- data/lib/bundler/fetcher/index.rb +32 -0
- data/lib/bundler/friendly_errors.rb +92 -0
- data/lib/bundler/gem_helper.rb +191 -0
- data/lib/bundler/gem_helpers.rb +26 -0
- data/lib/bundler/gem_installer.rb +9 -0
- data/lib/bundler/gem_path_manipulation.rb +8 -0
- data/lib/bundler/gem_tasks.rb +5 -0
- data/lib/bundler/graph.rb +173 -0
- data/lib/bundler/index.rb +199 -0
- data/lib/bundler/injector.rb +62 -0
- data/lib/bundler/inline.rb +58 -0
- data/lib/bundler/installer.rb +242 -0
- data/lib/bundler/installer/parallel_installer.rb +122 -0
- data/lib/bundler/installer/standalone.rb +48 -0
- data/lib/bundler/lazy_specification.rb +82 -0
- data/lib/bundler/lockfile_parser.rb +199 -0
- data/lib/bundler/match_platform.rb +13 -0
- data/lib/bundler/psyched_yaml.rb +26 -0
- data/lib/bundler/remote_specification.rb +82 -0
- data/lib/bundler/resolver.rb +350 -0
- data/lib/bundler/retry.rb +60 -0
- data/lib/bundler/ruby_dsl.rb +11 -0
- data/lib/bundler/ruby_version.rb +116 -0
- data/lib/bundler/rubygems_ext.rb +175 -0
- data/lib/bundler/rubygems_integration.rb +674 -0
- data/lib/bundler/runtime.rb +285 -0
- data/lib/bundler/settings.rb +263 -0
- data/lib/bundler/setup.rb +26 -0
- data/lib/bundler/shared_helpers.rb +176 -0
- data/lib/bundler/similarity_detector.rb +61 -0
- data/lib/bundler/source.rb +35 -0
- data/lib/bundler/source/git.rb +286 -0
- data/lib/bundler/source/git/git_proxy.rb +190 -0
- data/lib/bundler/source/path.rb +224 -0
- data/lib/bundler/source/path/installer.rb +43 -0
- data/lib/bundler/source/rubygems.rb +436 -0
- data/lib/bundler/source/rubygems/remote.rb +38 -0
- data/lib/bundler/source_list.rb +101 -0
- data/lib/bundler/spec_set.rb +156 -0
- data/lib/bundler/ssl_certs/.document +1 -0
- data/lib/bundler/ssl_certs/AddTrustExternalCARoot-2048.pem +25 -0
- data/lib/bundler/ssl_certs/AddTrustExternalCARoot.pem +32 -0
- data/lib/bundler/ssl_certs/Class3PublicPrimaryCertificationAuthority.pem +14 -0
- data/lib/bundler/ssl_certs/DigiCertHighAssuranceEVRootCA.pem +23 -0
- data/lib/bundler/ssl_certs/EntrustnetSecureServerCertificationAuthority.pem +28 -0
- data/lib/bundler/ssl_certs/GeoTrustGlobalCA.pem +20 -0
- data/lib/bundler/ssl_certs/certificate_manager.rb +64 -0
- data/lib/bundler/stub_specification.rb +23 -0
- data/lib/bundler/templates/Executable +16 -0
- data/lib/bundler/templates/Executable.standalone +12 -0
- data/lib/bundler/templates/Gemfile +4 -0
- data/lib/bundler/templates/newgem/.travis.yml.tt +4 -0
- data/lib/bundler/templates/newgem/CODE_OF_CONDUCT.md.tt +22 -0
- data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
- data/lib/bundler/templates/newgem/LICENSE.txt.tt +21 -0
- data/lib/bundler/templates/newgem/README.md.tt +41 -0
- data/lib/bundler/templates/newgem/Rakefile.tt +29 -0
- data/lib/bundler/templates/newgem/bin/console.tt +14 -0
- data/lib/bundler/templates/newgem/bin/setup.tt +8 -0
- data/lib/bundler/templates/newgem/exe/newgem.tt +3 -0
- data/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt +3 -0
- data/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt +9 -0
- data/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt +6 -0
- data/lib/bundler/templates/newgem/gitignore.tt +16 -0
- data/lib/bundler/templates/newgem/lib/newgem.rb.tt +12 -0
- data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
- data/lib/bundler/templates/newgem/newgem.gemspec.tt +43 -0
- data/lib/bundler/templates/newgem/rspec.tt +2 -0
- data/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt +11 -0
- data/lib/bundler/templates/newgem/spec/spec_helper.rb.tt +2 -0
- data/lib/bundler/templates/newgem/test/newgem_test.rb.tt +11 -0
- data/lib/bundler/templates/newgem/test/test_helper.rb.tt +4 -0
- data/lib/bundler/ui.rb +7 -0
- data/lib/bundler/ui/rg_proxy.rb +17 -0
- data/lib/bundler/ui/shell.rb +108 -0
- data/lib/bundler/ui/silent.rb +44 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo.rb +5 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb +277 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/errors.rb +69 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/gem_metadata.rb +3 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/modules/specification_provider.rb +99 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/modules/ui.rb +63 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/resolution.rb +434 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/resolver.rb +43 -0
- data/lib/bundler/vendor/molinillo/lib/molinillo/state.rb +51 -0
- data/lib/bundler/vendor/net/http/faster.rb +26 -0
- data/lib/bundler/vendor/net/http/persistent.rb +1230 -0
- data/lib/bundler/vendor/net/http/persistent/ssl_reuse.rb +128 -0
- data/lib/bundler/vendor/thor/lib/thor.rb +484 -0
- data/lib/bundler/vendor/thor/lib/thor/actions.rb +319 -0
- data/lib/bundler/vendor/thor/lib/thor/actions/create_file.rb +103 -0
- data/lib/bundler/vendor/thor/lib/thor/actions/create_link.rb +59 -0
- data/lib/bundler/vendor/thor/lib/thor/actions/directory.rb +118 -0
- data/lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
- data/lib/bundler/vendor/thor/lib/thor/actions/file_manipulation.rb +316 -0
- data/lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb +107 -0
- data/lib/bundler/vendor/thor/lib/thor/base.rb +656 -0
- data/lib/bundler/vendor/thor/lib/thor/command.rb +133 -0
- data/lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +77 -0
- data/lib/bundler/vendor/thor/lib/thor/core_ext/io_binary_read.rb +10 -0
- data/lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb +98 -0
- data/lib/bundler/vendor/thor/lib/thor/error.rb +32 -0
- data/lib/bundler/vendor/thor/lib/thor/group.rb +281 -0
- data/lib/bundler/vendor/thor/lib/thor/invocation.rb +178 -0
- data/lib/bundler/vendor/thor/lib/thor/line_editor.rb +17 -0
- data/lib/bundler/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
- data/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
- data/lib/bundler/vendor/thor/lib/thor/parser.rb +4 -0
- data/lib/bundler/vendor/thor/lib/thor/parser/argument.rb +73 -0
- data/lib/bundler/vendor/thor/lib/thor/parser/arguments.rb +175 -0
- data/lib/bundler/vendor/thor/lib/thor/parser/option.rb +125 -0
- data/lib/bundler/vendor/thor/lib/thor/parser/options.rb +218 -0
- data/lib/bundler/vendor/thor/lib/thor/rake_compat.rb +71 -0
- data/lib/bundler/vendor/thor/lib/thor/runner.rb +322 -0
- data/lib/bundler/vendor/thor/lib/thor/shell.rb +81 -0
- data/lib/bundler/vendor/thor/lib/thor/shell/basic.rb +421 -0
- data/lib/bundler/vendor/thor/lib/thor/shell/color.rb +149 -0
- data/lib/bundler/vendor/thor/lib/thor/shell/html.rb +126 -0
- data/lib/bundler/vendor/thor/lib/thor/util.rb +267 -0
- data/lib/bundler/vendor/thor/lib/thor/version.rb +3 -0
- data/lib/bundler/vendored_molinillo.rb +2 -0
- data/lib/bundler/vendored_persistent.rb +11 -0
- data/lib/bundler/vendored_thor.rb +3 -0
- data/lib/bundler/version.rb +6 -0
- data/lib/bundler/vlad.rb +11 -0
- data/lib/bundler/worker.rb +72 -0
- data/man/bundle-config.ronn +187 -0
- data/man/bundle-exec.ronn +136 -0
- data/man/bundle-gem.ronn +77 -0
- data/man/bundle-install.ronn +398 -0
- data/man/bundle-package.ronn +66 -0
- data/man/bundle-platform.ronn +42 -0
- data/man/bundle-update.ronn +188 -0
- data/man/bundle.ronn +98 -0
- data/man/gemfile.5.ronn +495 -0
- data/man/index.txt +8 -0
- metadata +346 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'bundler/vendor/molinillo/lib/molinillo/dependency_graph'
|
2
|
+
|
3
|
+
module Bundler::Molinillo
|
4
|
+
# This class encapsulates a dependency resolver.
|
5
|
+
# The resolver is responsible for determining which set of dependencies to
|
6
|
+
# activate, with feedback from the the {#specification_provider}
|
7
|
+
#
|
8
|
+
#
|
9
|
+
class Resolver
|
10
|
+
require 'bundler/vendor/molinillo/lib/molinillo/resolution'
|
11
|
+
|
12
|
+
# @return [SpecificationProvider] the specification provider used
|
13
|
+
# in the resolution process
|
14
|
+
attr_reader :specification_provider
|
15
|
+
|
16
|
+
# @return [UI] the UI module used to communicate back to the user
|
17
|
+
# during the resolution process
|
18
|
+
attr_reader :resolver_ui
|
19
|
+
|
20
|
+
# @param [SpecificationProvider] specification_provider
|
21
|
+
# see {#specification_provider}
|
22
|
+
# @param [UI] resolver_ui
|
23
|
+
# see {#resolver_ui}
|
24
|
+
def initialize(specification_provider, resolver_ui)
|
25
|
+
@specification_provider = specification_provider
|
26
|
+
@resolver_ui = resolver_ui
|
27
|
+
end
|
28
|
+
|
29
|
+
# Resolves the requested dependencies into a {DependencyGraph},
|
30
|
+
# locking to the base dependency graph (if specified)
|
31
|
+
# @param [Array] requested an array of 'requested' dependencies that the
|
32
|
+
# {#specification_provider} can understand
|
33
|
+
# @param [DependencyGraph,nil] base the base dependency graph to which
|
34
|
+
# dependencies should be 'locked'
|
35
|
+
def resolve(requested, base = DependencyGraph.new)
|
36
|
+
Resolution.new(specification_provider,
|
37
|
+
resolver_ui,
|
38
|
+
requested,
|
39
|
+
base).
|
40
|
+
resolve
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Bundler::Molinillo
|
2
|
+
# A state that a {Resolution} can be in
|
3
|
+
# @attr [String] name
|
4
|
+
# @attr [Array<Object>] requirements
|
5
|
+
# @attr [DependencyGraph] activated
|
6
|
+
# @attr [Object] requirement
|
7
|
+
# @attr [Object] possibility
|
8
|
+
# @attr [Integer] depth
|
9
|
+
# @attr [Set<Object>] conflicts
|
10
|
+
ResolutionState = Struct.new(
|
11
|
+
:name,
|
12
|
+
:requirements,
|
13
|
+
:activated,
|
14
|
+
:requirement,
|
15
|
+
:possibilities,
|
16
|
+
:depth,
|
17
|
+
:conflicts
|
18
|
+
)
|
19
|
+
|
20
|
+
class ResolutionState
|
21
|
+
# Returns an empty resolution state
|
22
|
+
# @return [ResolutionState] an empty state
|
23
|
+
def self.empty
|
24
|
+
new(nil, [], DependencyGraph.new, nil, nil, 0, Set.new)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# A state that encapsulates a set of {#requirements} with an {Array} of
|
29
|
+
# possibilities
|
30
|
+
class DependencyState < ResolutionState
|
31
|
+
# Removes a possibility from `self`
|
32
|
+
# @return [PossibilityState] a state with a single possibility,
|
33
|
+
# the possibility that was removed from `self`
|
34
|
+
def pop_possibility_state
|
35
|
+
PossibilityState.new(
|
36
|
+
name,
|
37
|
+
requirements.dup,
|
38
|
+
activated.dup,
|
39
|
+
requirement,
|
40
|
+
[possibilities.pop],
|
41
|
+
depth + 1,
|
42
|
+
conflicts.dup
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# A state that encapsulates a single possibility to fulfill the given
|
48
|
+
# {#requirement}
|
49
|
+
class PossibilityState < ResolutionState
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'net/protocol'
|
2
|
+
|
3
|
+
##
|
4
|
+
# Aaron Patterson's monkeypatch (accepted into 1.9.1) to fix Net::HTTP's speed
|
5
|
+
# problems.
|
6
|
+
#
|
7
|
+
# http://gist.github.com/251244
|
8
|
+
|
9
|
+
class Net::BufferedIO #:nodoc:
|
10
|
+
alias :old_rbuf_fill :rbuf_fill
|
11
|
+
|
12
|
+
def rbuf_fill
|
13
|
+
if @io.respond_to? :read_nonblock then
|
14
|
+
begin
|
15
|
+
@rbuf << @io.read_nonblock(65536)
|
16
|
+
rescue Errno::EWOULDBLOCK, Errno::EAGAIN => e
|
17
|
+
retry if IO.select [@io], nil, nil, @read_timeout
|
18
|
+
raise Timeout::Error, e.message
|
19
|
+
end
|
20
|
+
else # SSL sockets do not have read_nonblock
|
21
|
+
timeout @read_timeout do
|
22
|
+
@rbuf << @io.sysread(65536)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end if RUBY_VERSION < '1.9'
|
@@ -0,0 +1,1230 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
begin
|
3
|
+
require 'net/https'
|
4
|
+
rescue LoadError
|
5
|
+
# net/https or openssl
|
6
|
+
end if RUBY_VERSION < '1.9' # but only for 1.8
|
7
|
+
require 'net/http/faster'
|
8
|
+
require 'uri'
|
9
|
+
require 'cgi' # for escaping
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'net/http/pipeline'
|
13
|
+
rescue LoadError
|
14
|
+
end
|
15
|
+
|
16
|
+
autoload :OpenSSL, 'openssl'
|
17
|
+
|
18
|
+
##
|
19
|
+
# Persistent connections for Net::HTTP
|
20
|
+
#
|
21
|
+
# Net::HTTP::Persistent maintains persistent connections across all the
|
22
|
+
# servers you wish to talk to. For each host:port you communicate with a
|
23
|
+
# single persistent connection is created.
|
24
|
+
#
|
25
|
+
# Multiple Net::HTTP::Persistent objects will share the same set of
|
26
|
+
# connections.
|
27
|
+
#
|
28
|
+
# For each thread you start a new connection will be created. A
|
29
|
+
# Net::HTTP::Persistent connection will not be shared across threads.
|
30
|
+
#
|
31
|
+
# You can shut down the HTTP connections when done by calling #shutdown. You
|
32
|
+
# should name your Net::HTTP::Persistent object if you intend to call this
|
33
|
+
# method.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
#
|
37
|
+
# require 'net/http/persistent'
|
38
|
+
#
|
39
|
+
# uri = URI 'http://example.com/awesome/web/service'
|
40
|
+
#
|
41
|
+
# http = Net::HTTP::Persistent.new 'my_app_name'
|
42
|
+
#
|
43
|
+
# # perform a GET
|
44
|
+
# response = http.request uri
|
45
|
+
#
|
46
|
+
# # or
|
47
|
+
#
|
48
|
+
# get = Net::HTTP::Get.new uri.request_uri
|
49
|
+
# response = http.request get
|
50
|
+
#
|
51
|
+
# # create a POST
|
52
|
+
# post_uri = uri + 'create'
|
53
|
+
# post = Net::HTTP::Post.new post_uri.path
|
54
|
+
# post.set_form_data 'some' => 'cool data'
|
55
|
+
#
|
56
|
+
# # perform the POST, the URI is always required
|
57
|
+
# response http.request post_uri, post
|
58
|
+
#
|
59
|
+
# Note that for GET, HEAD and other requests that do not have a body you want
|
60
|
+
# to use URI#request_uri not URI#path. The request_uri contains the query
|
61
|
+
# params which are sent in the body for other requests.
|
62
|
+
#
|
63
|
+
# == SSL
|
64
|
+
#
|
65
|
+
# SSL connections are automatically created depending upon the scheme of the
|
66
|
+
# URI. SSL connections are automatically verified against the default
|
67
|
+
# certificate store for your computer. You can override this by changing
|
68
|
+
# verify_mode or by specifying an alternate cert_store.
|
69
|
+
#
|
70
|
+
# Here are the SSL settings, see the individual methods for documentation:
|
71
|
+
#
|
72
|
+
# #certificate :: This client's certificate
|
73
|
+
# #ca_file :: The certificate-authority
|
74
|
+
# #cert_store :: An SSL certificate store
|
75
|
+
# #private_key :: The client's SSL private key
|
76
|
+
# #reuse_ssl_sessions :: Reuse a previously opened SSL session for a new
|
77
|
+
# connection
|
78
|
+
# #ssl_version :: Which specific SSL version to use
|
79
|
+
# #verify_callback :: For server certificate verification
|
80
|
+
# #verify_mode :: How connections should be verified
|
81
|
+
#
|
82
|
+
# == Proxies
|
83
|
+
#
|
84
|
+
# A proxy can be set through #proxy= or at initialization time by providing a
|
85
|
+
# second argument to ::new. The proxy may be the URI of the proxy server or
|
86
|
+
# <code>:ENV</code> which will consult environment variables.
|
87
|
+
#
|
88
|
+
# See #proxy= and #proxy_from_env for details.
|
89
|
+
#
|
90
|
+
# == Headers
|
91
|
+
#
|
92
|
+
# Headers may be specified for use in every request. #headers are appended to
|
93
|
+
# any headers on the request. #override_headers replace existing headers on
|
94
|
+
# the request.
|
95
|
+
#
|
96
|
+
# The difference between the two can be seen in setting the User-Agent. Using
|
97
|
+
# <code>http.headers['User-Agent'] = 'MyUserAgent'</code> will send "Ruby,
|
98
|
+
# MyUserAgent" while <code>http.override_headers['User-Agent'] =
|
99
|
+
# 'MyUserAgent'</code> will send "MyUserAgent".
|
100
|
+
#
|
101
|
+
# == Tuning
|
102
|
+
#
|
103
|
+
# === Segregation
|
104
|
+
#
|
105
|
+
# By providing an application name to ::new you can separate your connections
|
106
|
+
# from the connections of other applications.
|
107
|
+
#
|
108
|
+
# === Idle Timeout
|
109
|
+
#
|
110
|
+
# If a connection hasn't been used for this number of seconds it will automatically be
|
111
|
+
# reset upon the next use to avoid attempting to send to a closed connection.
|
112
|
+
# The default value is 5 seconds. nil means no timeout. Set through #idle_timeout.
|
113
|
+
#
|
114
|
+
# Reducing this value may help avoid the "too many connection resets" error
|
115
|
+
# when sending non-idempotent requests while increasing this value will cause
|
116
|
+
# fewer round-trips.
|
117
|
+
#
|
118
|
+
# === Read Timeout
|
119
|
+
#
|
120
|
+
# The amount of time allowed between reading two chunks from the socket. Set
|
121
|
+
# through #read_timeout
|
122
|
+
#
|
123
|
+
# === Max Requests
|
124
|
+
#
|
125
|
+
# The number of requests that should be made before opening a new connection.
|
126
|
+
# Typically many keep-alive capable servers tune this to 100 or less, so the
|
127
|
+
# 101st request will fail with ECONNRESET. If unset (default), this value has no
|
128
|
+
# effect, if set, connections will be reset on the request after max_requests.
|
129
|
+
#
|
130
|
+
# === Open Timeout
|
131
|
+
#
|
132
|
+
# The amount of time to wait for a connection to be opened. Set through
|
133
|
+
# #open_timeout.
|
134
|
+
#
|
135
|
+
# === Socket Options
|
136
|
+
#
|
137
|
+
# Socket options may be set on newly-created connections. See #socket_options
|
138
|
+
# for details.
|
139
|
+
#
|
140
|
+
# === Non-Idempotent Requests
|
141
|
+
#
|
142
|
+
# By default non-idempotent requests will not be retried per RFC 2616. By
|
143
|
+
# setting retry_change_requests to true requests will automatically be retried
|
144
|
+
# once.
|
145
|
+
#
|
146
|
+
# Only do this when you know that retrying a POST or other non-idempotent
|
147
|
+
# request is safe for your application and will not create duplicate
|
148
|
+
# resources.
|
149
|
+
#
|
150
|
+
# The recommended way to handle non-idempotent requests is the following:
|
151
|
+
#
|
152
|
+
# require 'net/http/persistent'
|
153
|
+
#
|
154
|
+
# uri = URI 'http://example.com/awesome/web/service'
|
155
|
+
# post_uri = uri + 'create'
|
156
|
+
#
|
157
|
+
# http = Net::HTTP::Persistent.new 'my_app_name'
|
158
|
+
#
|
159
|
+
# post = Net::HTTP::Post.new post_uri.path
|
160
|
+
# # ... fill in POST request
|
161
|
+
#
|
162
|
+
# begin
|
163
|
+
# response = http.request post_uri, post
|
164
|
+
# rescue Net::HTTP::Persistent::Error
|
165
|
+
#
|
166
|
+
# # POST failed, make a new request to verify the server did not process
|
167
|
+
# # the request
|
168
|
+
# exists_uri = uri + '...'
|
169
|
+
# response = http.get exists_uri
|
170
|
+
#
|
171
|
+
# # Retry if it failed
|
172
|
+
# retry if response.code == '404'
|
173
|
+
# end
|
174
|
+
#
|
175
|
+
# The method of determining if the resource was created or not is unique to
|
176
|
+
# the particular service you are using. Of course, you will want to add
|
177
|
+
# protection from infinite looping.
|
178
|
+
#
|
179
|
+
# === Connection Termination
|
180
|
+
#
|
181
|
+
# If you are done using the Net::HTTP::Persistent instance you may shut down
|
182
|
+
# all the connections in the current thread with #shutdown. This is not
|
183
|
+
# recommended for normal use, it should only be used when it will be several
|
184
|
+
# minutes before you make another HTTP request.
|
185
|
+
#
|
186
|
+
# If you are using multiple threads, call #shutdown in each thread when the
|
187
|
+
# thread is done making requests. If you don't call shutdown, that's OK.
|
188
|
+
# Ruby will automatically garbage collect and shutdown your HTTP connections
|
189
|
+
# when the thread terminates.
|
190
|
+
|
191
|
+
class Net::HTTP::Persistent
|
192
|
+
|
193
|
+
##
|
194
|
+
# The beginning of Time
|
195
|
+
|
196
|
+
EPOCH = Time.at 0 # :nodoc:
|
197
|
+
|
198
|
+
##
|
199
|
+
# Is OpenSSL available? This test works with autoload
|
200
|
+
|
201
|
+
HAVE_OPENSSL = defined? OpenSSL::SSL # :nodoc:
|
202
|
+
|
203
|
+
##
|
204
|
+
# The version of Net::HTTP::Persistent you are using
|
205
|
+
|
206
|
+
VERSION = '2.9.3'
|
207
|
+
|
208
|
+
##
|
209
|
+
# Exceptions rescued for automatic retry on ruby 2.0.0. This overlaps with
|
210
|
+
# the exception list for ruby 1.x.
|
211
|
+
|
212
|
+
RETRIED_EXCEPTIONS = [ # :nodoc:
|
213
|
+
(Net::ReadTimeout if Net.const_defined? :ReadTimeout),
|
214
|
+
IOError,
|
215
|
+
EOFError,
|
216
|
+
Errno::ECONNRESET,
|
217
|
+
Errno::ECONNABORTED,
|
218
|
+
Errno::EPIPE,
|
219
|
+
(OpenSSL::SSL::SSLError if HAVE_OPENSSL),
|
220
|
+
Timeout::Error,
|
221
|
+
].compact
|
222
|
+
|
223
|
+
##
|
224
|
+
# Error class for errors raised by Net::HTTP::Persistent. Various
|
225
|
+
# SystemCallErrors are re-raised with a human-readable message under this
|
226
|
+
# class.
|
227
|
+
|
228
|
+
class Error < StandardError; end
|
229
|
+
|
230
|
+
##
|
231
|
+
# Use this method to detect the idle timeout of the host at +uri+. The
|
232
|
+
# value returned can be used to configure #idle_timeout. +max+ controls the
|
233
|
+
# maximum idle timeout to detect.
|
234
|
+
#
|
235
|
+
# After
|
236
|
+
#
|
237
|
+
# Idle timeout detection is performed by creating a connection then
|
238
|
+
# performing a HEAD request in a loop until the connection terminates
|
239
|
+
# waiting one additional second per loop.
|
240
|
+
#
|
241
|
+
# NOTE: This may not work on ruby > 1.9.
|
242
|
+
|
243
|
+
def self.detect_idle_timeout uri, max = 10
|
244
|
+
uri = URI uri unless uri.is_a?(URI::Generic)
|
245
|
+
uri += '/'
|
246
|
+
|
247
|
+
req = Net::HTTP::Head.new uri.request_uri
|
248
|
+
|
249
|
+
http = new 'net-http-persistent detect_idle_timeout'
|
250
|
+
|
251
|
+
connection = http.connection_for uri
|
252
|
+
|
253
|
+
sleep_time = 0
|
254
|
+
|
255
|
+
loop do
|
256
|
+
response = connection.request req
|
257
|
+
|
258
|
+
$stderr.puts "HEAD #{uri} => #{response.code}" if $DEBUG
|
259
|
+
|
260
|
+
unless response.is_a?(Net::HTTPOK) then
|
261
|
+
raise Error, "bad response code #{response.code} detecting idle timeout"
|
262
|
+
end
|
263
|
+
|
264
|
+
break if sleep_time >= max
|
265
|
+
|
266
|
+
sleep_time += 1
|
267
|
+
|
268
|
+
$stderr.puts "sleeping #{sleep_time}" if $DEBUG
|
269
|
+
sleep sleep_time
|
270
|
+
end
|
271
|
+
rescue
|
272
|
+
# ignore StandardErrors, we've probably found the idle timeout.
|
273
|
+
ensure
|
274
|
+
http.shutdown
|
275
|
+
|
276
|
+
return sleep_time unless $!
|
277
|
+
end
|
278
|
+
|
279
|
+
##
|
280
|
+
# This client's OpenSSL::X509::Certificate
|
281
|
+
|
282
|
+
attr_reader :certificate
|
283
|
+
|
284
|
+
# For Net::HTTP parity
|
285
|
+
alias cert certificate
|
286
|
+
|
287
|
+
##
|
288
|
+
# An SSL certificate authority. Setting this will set verify_mode to
|
289
|
+
# VERIFY_PEER.
|
290
|
+
|
291
|
+
attr_reader :ca_file
|
292
|
+
|
293
|
+
##
|
294
|
+
# An SSL certificate store. Setting this will override the default
|
295
|
+
# certificate store. See verify_mode for more information.
|
296
|
+
|
297
|
+
attr_reader :cert_store
|
298
|
+
|
299
|
+
##
|
300
|
+
# Sends debug_output to this IO via Net::HTTP#set_debug_output.
|
301
|
+
#
|
302
|
+
# Never use this method in production code, it causes a serious security
|
303
|
+
# hole.
|
304
|
+
|
305
|
+
attr_accessor :debug_output
|
306
|
+
|
307
|
+
##
|
308
|
+
# Current connection generation
|
309
|
+
|
310
|
+
attr_reader :generation # :nodoc:
|
311
|
+
|
312
|
+
##
|
313
|
+
# Where this instance's connections live in the thread local variables
|
314
|
+
|
315
|
+
attr_reader :generation_key # :nodoc:
|
316
|
+
|
317
|
+
##
|
318
|
+
# Headers that are added to every request using Net::HTTP#add_field
|
319
|
+
|
320
|
+
attr_reader :headers
|
321
|
+
|
322
|
+
##
|
323
|
+
# Maps host:port to an HTTP version. This allows us to enable version
|
324
|
+
# specific features.
|
325
|
+
|
326
|
+
attr_reader :http_versions
|
327
|
+
|
328
|
+
##
|
329
|
+
# Maximum time an unused connection can remain idle before being
|
330
|
+
# automatically closed.
|
331
|
+
|
332
|
+
attr_accessor :idle_timeout
|
333
|
+
|
334
|
+
##
|
335
|
+
# Maximum number of requests on a connection before it is considered expired
|
336
|
+
# and automatically closed.
|
337
|
+
|
338
|
+
attr_accessor :max_requests
|
339
|
+
|
340
|
+
##
|
341
|
+
# The value sent in the Keep-Alive header. Defaults to 30. Not needed for
|
342
|
+
# HTTP/1.1 servers.
|
343
|
+
#
|
344
|
+
# This may not work correctly for HTTP/1.0 servers
|
345
|
+
#
|
346
|
+
# This method may be removed in a future version as RFC 2616 does not
|
347
|
+
# require this header.
|
348
|
+
|
349
|
+
attr_accessor :keep_alive
|
350
|
+
|
351
|
+
##
|
352
|
+
# A name for this connection. Allows you to keep your connections apart
|
353
|
+
# from everybody else's.
|
354
|
+
|
355
|
+
attr_reader :name
|
356
|
+
|
357
|
+
##
|
358
|
+
# Seconds to wait until a connection is opened. See Net::HTTP#open_timeout
|
359
|
+
|
360
|
+
attr_accessor :open_timeout
|
361
|
+
|
362
|
+
##
|
363
|
+
# Headers that are added to every request using Net::HTTP#[]=
|
364
|
+
|
365
|
+
attr_reader :override_headers
|
366
|
+
|
367
|
+
##
|
368
|
+
# This client's SSL private key
|
369
|
+
|
370
|
+
attr_reader :private_key
|
371
|
+
|
372
|
+
# For Net::HTTP parity
|
373
|
+
alias key private_key
|
374
|
+
|
375
|
+
##
|
376
|
+
# The URL through which requests will be proxied
|
377
|
+
|
378
|
+
attr_reader :proxy_uri
|
379
|
+
|
380
|
+
##
|
381
|
+
# List of host suffixes which will not be proxied
|
382
|
+
|
383
|
+
attr_reader :no_proxy
|
384
|
+
|
385
|
+
##
|
386
|
+
# Seconds to wait until reading one block. See Net::HTTP#read_timeout
|
387
|
+
|
388
|
+
attr_accessor :read_timeout
|
389
|
+
|
390
|
+
##
|
391
|
+
# Where this instance's request counts live in the thread local variables
|
392
|
+
|
393
|
+
attr_reader :request_key # :nodoc:
|
394
|
+
|
395
|
+
##
|
396
|
+
# By default SSL sessions are reused to avoid extra SSL handshakes. Set
|
397
|
+
# this to false if you have problems communicating with an HTTPS server
|
398
|
+
# like:
|
399
|
+
#
|
400
|
+
# SSL_connect [...] read finished A: unexpected message (OpenSSL::SSL::SSLError)
|
401
|
+
|
402
|
+
attr_accessor :reuse_ssl_sessions
|
403
|
+
|
404
|
+
##
|
405
|
+
# An array of options for Socket#setsockopt.
|
406
|
+
#
|
407
|
+
# By default the TCP_NODELAY option is set on sockets.
|
408
|
+
#
|
409
|
+
# To set additional options append them to this array:
|
410
|
+
#
|
411
|
+
# http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
|
412
|
+
|
413
|
+
attr_reader :socket_options
|
414
|
+
|
415
|
+
##
|
416
|
+
# Current SSL connection generation
|
417
|
+
|
418
|
+
attr_reader :ssl_generation # :nodoc:
|
419
|
+
|
420
|
+
##
|
421
|
+
# Where this instance's SSL connections live in the thread local variables
|
422
|
+
|
423
|
+
attr_reader :ssl_generation_key # :nodoc:
|
424
|
+
|
425
|
+
##
|
426
|
+
# SSL version to use.
|
427
|
+
#
|
428
|
+
# By default, the version will be negotiated automatically between client
|
429
|
+
# and server. Ruby 1.9 and newer only.
|
430
|
+
|
431
|
+
attr_reader :ssl_version if RUBY_VERSION > '1.9'
|
432
|
+
|
433
|
+
##
|
434
|
+
# Where this instance's last-use times live in the thread local variables
|
435
|
+
|
436
|
+
attr_reader :timeout_key # :nodoc:
|
437
|
+
|
438
|
+
##
|
439
|
+
# SSL verification callback. Used when ca_file is set.
|
440
|
+
|
441
|
+
attr_reader :verify_callback
|
442
|
+
|
443
|
+
##
|
444
|
+
# HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER which verifies
|
445
|
+
# the server certificate.
|
446
|
+
#
|
447
|
+
# If no ca_file or cert_store is set the default system certificate store is
|
448
|
+
# used.
|
449
|
+
#
|
450
|
+
# You can use +verify_mode+ to override any default values.
|
451
|
+
|
452
|
+
attr_reader :verify_mode
|
453
|
+
|
454
|
+
##
|
455
|
+
# Enable retries of non-idempotent requests that change data (e.g. POST
|
456
|
+
# requests) when the server has disconnected.
|
457
|
+
#
|
458
|
+
# This will in the worst case lead to multiple requests with the same data,
|
459
|
+
# but it may be useful for some applications. Take care when enabling
|
460
|
+
# this option to ensure it is safe to POST or perform other non-idempotent
|
461
|
+
# requests to the server.
|
462
|
+
|
463
|
+
attr_accessor :retry_change_requests
|
464
|
+
|
465
|
+
##
|
466
|
+
# Creates a new Net::HTTP::Persistent.
|
467
|
+
#
|
468
|
+
# Set +name+ to keep your connections apart from everybody else's. Not
|
469
|
+
# required currently, but highly recommended. Your library name should be
|
470
|
+
# good enough. This parameter will be required in a future version.
|
471
|
+
#
|
472
|
+
# +proxy+ may be set to a URI::HTTP or :ENV to pick up proxy options from
|
473
|
+
# the environment. See proxy_from_env for details.
|
474
|
+
#
|
475
|
+
# In order to use a URI for the proxy you may need to do some extra work
|
476
|
+
# beyond URI parsing if the proxy requires a password:
|
477
|
+
#
|
478
|
+
# proxy = URI 'http://proxy.example'
|
479
|
+
# proxy.user = 'AzureDiamond'
|
480
|
+
# proxy.password = 'hunter2'
|
481
|
+
|
482
|
+
def initialize name = nil, proxy = nil
|
483
|
+
@name = name
|
484
|
+
|
485
|
+
@debug_output = nil
|
486
|
+
@proxy_uri = nil
|
487
|
+
@no_proxy = []
|
488
|
+
@headers = {}
|
489
|
+
@override_headers = {}
|
490
|
+
@http_versions = {}
|
491
|
+
@keep_alive = 30
|
492
|
+
@open_timeout = nil
|
493
|
+
@read_timeout = nil
|
494
|
+
@idle_timeout = 5
|
495
|
+
@max_requests = nil
|
496
|
+
@socket_options = []
|
497
|
+
|
498
|
+
@socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
|
499
|
+
Socket.const_defined? :TCP_NODELAY
|
500
|
+
|
501
|
+
key = ['net_http_persistent', name].compact
|
502
|
+
@generation_key = [key, 'generations' ].join('_').intern
|
503
|
+
@ssl_generation_key = [key, 'ssl_generations'].join('_').intern
|
504
|
+
@request_key = [key, 'requests' ].join('_').intern
|
505
|
+
@timeout_key = [key, 'timeouts' ].join('_').intern
|
506
|
+
|
507
|
+
@certificate = nil
|
508
|
+
@ca_file = nil
|
509
|
+
@private_key = nil
|
510
|
+
@ssl_version = nil
|
511
|
+
@verify_callback = nil
|
512
|
+
@verify_mode = nil
|
513
|
+
@cert_store = nil
|
514
|
+
|
515
|
+
@generation = 0 # incremented when proxy URI changes
|
516
|
+
@ssl_generation = 0 # incremented when SSL session variables change
|
517
|
+
|
518
|
+
if HAVE_OPENSSL then
|
519
|
+
@verify_mode = OpenSSL::SSL::VERIFY_PEER
|
520
|
+
@reuse_ssl_sessions = OpenSSL::SSL.const_defined? :Session
|
521
|
+
end
|
522
|
+
|
523
|
+
@retry_change_requests = false
|
524
|
+
|
525
|
+
@ruby_1 = RUBY_VERSION < '2'
|
526
|
+
@retried_on_ruby_2 = !@ruby_1
|
527
|
+
|
528
|
+
self.proxy = proxy if proxy
|
529
|
+
end
|
530
|
+
|
531
|
+
##
|
532
|
+
# Sets this client's OpenSSL::X509::Certificate
|
533
|
+
|
534
|
+
def certificate= certificate
|
535
|
+
@certificate = certificate
|
536
|
+
|
537
|
+
reconnect_ssl
|
538
|
+
end
|
539
|
+
|
540
|
+
# For Net::HTTP parity
|
541
|
+
alias cert= certificate=
|
542
|
+
|
543
|
+
##
|
544
|
+
# Sets the SSL certificate authority file.
|
545
|
+
|
546
|
+
def ca_file= file
|
547
|
+
@ca_file = file
|
548
|
+
|
549
|
+
reconnect_ssl
|
550
|
+
end
|
551
|
+
|
552
|
+
##
|
553
|
+
# Overrides the default SSL certificate store used for verifying
|
554
|
+
# connections.
|
555
|
+
|
556
|
+
def cert_store= store
|
557
|
+
@cert_store = store
|
558
|
+
|
559
|
+
reconnect_ssl
|
560
|
+
end
|
561
|
+
|
562
|
+
##
|
563
|
+
# Finishes all connections on the given +thread+ that were created before
|
564
|
+
# the given +generation+ in the threads +generation_key+ list.
|
565
|
+
#
|
566
|
+
# See #shutdown for a bunch of scary warning about misusing this method.
|
567
|
+
|
568
|
+
def cleanup(generation, thread = Thread.current,
|
569
|
+
generation_key = @generation_key) # :nodoc:
|
570
|
+
timeouts = thread[@timeout_key]
|
571
|
+
|
572
|
+
(0...generation).each do |old_generation|
|
573
|
+
next unless thread[generation_key]
|
574
|
+
|
575
|
+
conns = thread[generation_key].delete old_generation
|
576
|
+
|
577
|
+
conns.each_value do |conn|
|
578
|
+
finish conn, thread
|
579
|
+
|
580
|
+
timeouts.delete conn.object_id if timeouts
|
581
|
+
end if conns
|
582
|
+
end
|
583
|
+
end
|
584
|
+
|
585
|
+
##
|
586
|
+
# Creates a new connection for +uri+
|
587
|
+
|
588
|
+
def connection_for uri
|
589
|
+
Thread.current[@generation_key] ||= Hash.new { |h,k| h[k] = {} }
|
590
|
+
Thread.current[@ssl_generation_key] ||= Hash.new { |h,k| h[k] = {} }
|
591
|
+
Thread.current[@request_key] ||= Hash.new 0
|
592
|
+
Thread.current[@timeout_key] ||= Hash.new EPOCH
|
593
|
+
|
594
|
+
use_ssl = uri.scheme.downcase == 'https'
|
595
|
+
|
596
|
+
if use_ssl then
|
597
|
+
raise Net::HTTP::Persistent::Error, 'OpenSSL is not available' unless
|
598
|
+
HAVE_OPENSSL
|
599
|
+
|
600
|
+
ssl_generation = @ssl_generation
|
601
|
+
|
602
|
+
ssl_cleanup ssl_generation
|
603
|
+
|
604
|
+
connections = Thread.current[@ssl_generation_key][ssl_generation]
|
605
|
+
else
|
606
|
+
generation = @generation
|
607
|
+
|
608
|
+
cleanup generation
|
609
|
+
|
610
|
+
connections = Thread.current[@generation_key][generation]
|
611
|
+
end
|
612
|
+
|
613
|
+
net_http_args = [uri.host, uri.port]
|
614
|
+
connection_id = net_http_args.join ':'
|
615
|
+
|
616
|
+
if @proxy_uri and not proxy_bypass? uri.host, uri.port then
|
617
|
+
connection_id << @proxy_connection_id
|
618
|
+
net_http_args.concat @proxy_args
|
619
|
+
end
|
620
|
+
|
621
|
+
connection = connections[connection_id]
|
622
|
+
|
623
|
+
unless connection = connections[connection_id] then
|
624
|
+
connections[connection_id] = http_class.new(*net_http_args)
|
625
|
+
connection = connections[connection_id]
|
626
|
+
ssl connection if use_ssl
|
627
|
+
else
|
628
|
+
reset connection if expired? connection
|
629
|
+
end
|
630
|
+
|
631
|
+
start connection unless connection.started?
|
632
|
+
|
633
|
+
connection.read_timeout = @read_timeout if @read_timeout
|
634
|
+
connection.keep_alive_timeout = @idle_timeout if @idle_timeout && connection.respond_to?(:keep_alive_timeout=)
|
635
|
+
|
636
|
+
connection
|
637
|
+
rescue Errno::ECONNREFUSED
|
638
|
+
address = connection.proxy_address || connection.address
|
639
|
+
port = connection.proxy_port || connection.port
|
640
|
+
|
641
|
+
raise Error, "connection refused: #{address}:#{port}"
|
642
|
+
rescue Errno::EHOSTDOWN
|
643
|
+
address = connection.proxy_address || connection.address
|
644
|
+
port = connection.proxy_port || connection.port
|
645
|
+
|
646
|
+
raise Error, "host down: #{address}:#{port}"
|
647
|
+
end
|
648
|
+
|
649
|
+
##
|
650
|
+
# Returns an error message containing the number of requests performed on
|
651
|
+
# this connection
|
652
|
+
|
653
|
+
def error_message connection
|
654
|
+
requests = Thread.current[@request_key][connection.object_id] - 1 # fixup
|
655
|
+
last_use = Thread.current[@timeout_key][connection.object_id]
|
656
|
+
|
657
|
+
age = Time.now - last_use
|
658
|
+
|
659
|
+
"after #{requests} requests on #{connection.object_id}, " \
|
660
|
+
"last used #{age} seconds ago"
|
661
|
+
end
|
662
|
+
|
663
|
+
##
|
664
|
+
# URI::escape wrapper
|
665
|
+
|
666
|
+
def escape str
|
667
|
+
CGI.escape str if str
|
668
|
+
end
|
669
|
+
|
670
|
+
##
|
671
|
+
# URI::unescape wrapper
|
672
|
+
|
673
|
+
def unescape str
|
674
|
+
CGI.unescape str if str
|
675
|
+
end
|
676
|
+
|
677
|
+
|
678
|
+
##
|
679
|
+
# Returns true if the connection should be reset due to an idle timeout, or
|
680
|
+
# maximum request count, false otherwise.
|
681
|
+
|
682
|
+
def expired? connection
|
683
|
+
requests = Thread.current[@request_key][connection.object_id]
|
684
|
+
return true if @max_requests && requests >= @max_requests
|
685
|
+
return false unless @idle_timeout
|
686
|
+
return true if @idle_timeout.zero?
|
687
|
+
|
688
|
+
last_used = Thread.current[@timeout_key][connection.object_id]
|
689
|
+
|
690
|
+
Time.now - last_used > @idle_timeout
|
691
|
+
end
|
692
|
+
|
693
|
+
##
|
694
|
+
# Starts the Net::HTTP +connection+
|
695
|
+
|
696
|
+
def start connection
|
697
|
+
connection.set_debug_output @debug_output if @debug_output
|
698
|
+
connection.open_timeout = @open_timeout if @open_timeout
|
699
|
+
|
700
|
+
connection.start
|
701
|
+
|
702
|
+
socket = connection.instance_variable_get :@socket
|
703
|
+
|
704
|
+
if socket then # for fakeweb
|
705
|
+
@socket_options.each do |option|
|
706
|
+
socket.io.setsockopt(*option)
|
707
|
+
end
|
708
|
+
end
|
709
|
+
end
|
710
|
+
|
711
|
+
##
|
712
|
+
# Finishes the Net::HTTP +connection+
|
713
|
+
|
714
|
+
def finish connection, thread = Thread.current
|
715
|
+
if requests = thread[@request_key] then
|
716
|
+
requests.delete connection.object_id
|
717
|
+
end
|
718
|
+
|
719
|
+
connection.finish
|
720
|
+
rescue IOError
|
721
|
+
end
|
722
|
+
|
723
|
+
def http_class # :nodoc:
|
724
|
+
if RUBY_VERSION > '2.0' then
|
725
|
+
Net::HTTP
|
726
|
+
elsif [:Artifice, :FakeWeb, :WebMock].any? { |klass|
|
727
|
+
Object.const_defined?(klass)
|
728
|
+
} or not @reuse_ssl_sessions then
|
729
|
+
Net::HTTP
|
730
|
+
else
|
731
|
+
Net::HTTP::Persistent::SSLReuse
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
##
|
736
|
+
# Returns the HTTP protocol version for +uri+
|
737
|
+
|
738
|
+
def http_version uri
|
739
|
+
@http_versions["#{uri.host}:#{uri.port}"]
|
740
|
+
end
|
741
|
+
|
742
|
+
##
|
743
|
+
# Is +req+ idempotent according to RFC 2616?
|
744
|
+
|
745
|
+
def idempotent? req
|
746
|
+
case req
|
747
|
+
when Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head,
|
748
|
+
Net::HTTP::Options, Net::HTTP::Put, Net::HTTP::Trace then
|
749
|
+
true
|
750
|
+
end
|
751
|
+
end
|
752
|
+
|
753
|
+
##
|
754
|
+
# Is the request +req+ idempotent or is retry_change_requests allowed.
|
755
|
+
#
|
756
|
+
# If +retried_on_ruby_2+ is true, true will be returned if we are on ruby,
|
757
|
+
# retry_change_requests is allowed and the request is not idempotent.
|
758
|
+
|
759
|
+
def can_retry? req, retried_on_ruby_2 = false
|
760
|
+
return @retry_change_requests && !idempotent?(req) if retried_on_ruby_2
|
761
|
+
|
762
|
+
@retry_change_requests || idempotent?(req)
|
763
|
+
end
|
764
|
+
|
765
|
+
if RUBY_VERSION > '1.9' then
|
766
|
+
##
|
767
|
+
# Workaround for missing Net::HTTPHeader#connection_close? on Ruby 1.8
|
768
|
+
|
769
|
+
def connection_close? header
|
770
|
+
header.connection_close?
|
771
|
+
end
|
772
|
+
|
773
|
+
##
|
774
|
+
# Workaround for missing Net::HTTPHeader#connection_keep_alive? on Ruby 1.8
|
775
|
+
|
776
|
+
def connection_keep_alive? header
|
777
|
+
header.connection_keep_alive?
|
778
|
+
end
|
779
|
+
else
|
780
|
+
##
|
781
|
+
# Workaround for missing Net::HTTPRequest#connection_close? on Ruby 1.8
|
782
|
+
|
783
|
+
def connection_close? header
|
784
|
+
header['connection'] =~ /close/ or header['proxy-connection'] =~ /close/
|
785
|
+
end
|
786
|
+
|
787
|
+
##
|
788
|
+
# Workaround for missing Net::HTTPRequest#connection_keep_alive? on Ruby
|
789
|
+
# 1.8
|
790
|
+
|
791
|
+
def connection_keep_alive? header
|
792
|
+
header['connection'] =~ /keep-alive/ or
|
793
|
+
header['proxy-connection'] =~ /keep-alive/
|
794
|
+
end
|
795
|
+
end
|
796
|
+
|
797
|
+
##
|
798
|
+
# Deprecated in favor of #expired?
|
799
|
+
|
800
|
+
def max_age # :nodoc:
|
801
|
+
return Time.now + 1 unless @idle_timeout
|
802
|
+
|
803
|
+
Time.now - @idle_timeout
|
804
|
+
end
|
805
|
+
|
806
|
+
##
|
807
|
+
# Adds "http://" to the String +uri+ if it is missing.
|
808
|
+
|
809
|
+
def normalize_uri uri
|
810
|
+
(uri =~ /^https?:/) ? uri : "http://#{uri}"
|
811
|
+
end
|
812
|
+
|
813
|
+
##
|
814
|
+
# Pipelines +requests+ to the HTTP server at +uri+ yielding responses if a
|
815
|
+
# block is given. Returns all responses recieved.
|
816
|
+
#
|
817
|
+
# See
|
818
|
+
# Net::HTTP::Pipeline[http://docs.seattlerb.org/net-http-pipeline/Net/HTTP/Pipeline.html]
|
819
|
+
# for further details.
|
820
|
+
#
|
821
|
+
# Only if <tt>net-http-pipeline</tt> was required before
|
822
|
+
# <tt>net-http-persistent</tt> #pipeline will be present.
|
823
|
+
|
824
|
+
def pipeline uri, requests, &block # :yields: responses
|
825
|
+
connection = connection_for uri
|
826
|
+
|
827
|
+
connection.pipeline requests, &block
|
828
|
+
end
|
829
|
+
|
830
|
+
##
|
831
|
+
# Sets this client's SSL private key
|
832
|
+
|
833
|
+
def private_key= key
|
834
|
+
@private_key = key
|
835
|
+
|
836
|
+
reconnect_ssl
|
837
|
+
end
|
838
|
+
|
839
|
+
# For Net::HTTP parity
|
840
|
+
alias key= private_key=
|
841
|
+
|
842
|
+
##
|
843
|
+
# Sets the proxy server. The +proxy+ may be the URI of the proxy server,
|
844
|
+
# the symbol +:ENV+ which will read the proxy from the environment or nil to
|
845
|
+
# disable use of a proxy. See #proxy_from_env for details on setting the
|
846
|
+
# proxy from the environment.
|
847
|
+
#
|
848
|
+
# If the proxy URI is set after requests have been made, the next request
|
849
|
+
# will shut-down and re-open all connections.
|
850
|
+
#
|
851
|
+
# The +no_proxy+ query parameter can be used to specify hosts which shouldn't
|
852
|
+
# be reached via proxy; if set it should be a comma separated list of
|
853
|
+
# hostname suffixes, optionally with +:port+ appended, for example
|
854
|
+
# <tt>example.com,some.host:8080</tt>.
|
855
|
+
|
856
|
+
def proxy= proxy
|
857
|
+
@proxy_uri = case proxy
|
858
|
+
when :ENV then proxy_from_env
|
859
|
+
when URI::HTTP then proxy
|
860
|
+
when nil then # ignore
|
861
|
+
else raise ArgumentError, 'proxy must be :ENV or a URI::HTTP'
|
862
|
+
end
|
863
|
+
|
864
|
+
@no_proxy.clear
|
865
|
+
|
866
|
+
if @proxy_uri then
|
867
|
+
@proxy_args = [
|
868
|
+
@proxy_uri.host,
|
869
|
+
@proxy_uri.port,
|
870
|
+
unescape(@proxy_uri.user),
|
871
|
+
unescape(@proxy_uri.password),
|
872
|
+
]
|
873
|
+
|
874
|
+
@proxy_connection_id = [nil, *@proxy_args].join ':'
|
875
|
+
|
876
|
+
if @proxy_uri.query then
|
877
|
+
@no_proxy = CGI.parse(@proxy_uri.query)['no_proxy'].join(',').downcase.split(',').map { |x| x.strip }.reject { |x| x.empty? }
|
878
|
+
end
|
879
|
+
end
|
880
|
+
|
881
|
+
reconnect
|
882
|
+
reconnect_ssl
|
883
|
+
end
|
884
|
+
|
885
|
+
##
|
886
|
+
# Creates a URI for an HTTP proxy server from ENV variables.
|
887
|
+
#
|
888
|
+
# If +HTTP_PROXY+ is set a proxy will be returned.
|
889
|
+
#
|
890
|
+
# If +HTTP_PROXY_USER+ or +HTTP_PROXY_PASS+ are set the URI is given the
|
891
|
+
# indicated user and password unless HTTP_PROXY contains either of these in
|
892
|
+
# the URI.
|
893
|
+
#
|
894
|
+
# The +NO_PROXY+ ENV variable can be used to specify hosts which shouldn't
|
895
|
+
# be reached via proxy; if set it should be a comma separated list of
|
896
|
+
# hostname suffixes, optionally with +:port+ appended, for example
|
897
|
+
# <tt>example.com,some.host:8080</tt>. When set to <tt>*</tt> no proxy will
|
898
|
+
# be returned.
|
899
|
+
#
|
900
|
+
# For Windows users, lowercase ENV variables are preferred over uppercase ENV
|
901
|
+
# variables.
|
902
|
+
|
903
|
+
def proxy_from_env
|
904
|
+
env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
|
905
|
+
|
906
|
+
return nil if env_proxy.nil? or env_proxy.empty?
|
907
|
+
|
908
|
+
uri = URI normalize_uri env_proxy
|
909
|
+
|
910
|
+
env_no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']
|
911
|
+
|
912
|
+
# '*' is special case for always bypass
|
913
|
+
return nil if env_no_proxy == '*'
|
914
|
+
|
915
|
+
if env_no_proxy then
|
916
|
+
uri.query = "no_proxy=#{escape(env_no_proxy)}"
|
917
|
+
end
|
918
|
+
|
919
|
+
unless uri.user or uri.password then
|
920
|
+
uri.user = escape ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER']
|
921
|
+
uri.password = escape ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS']
|
922
|
+
end
|
923
|
+
|
924
|
+
uri
|
925
|
+
end
|
926
|
+
|
927
|
+
##
|
928
|
+
# Returns true when proxy should by bypassed for host.
|
929
|
+
|
930
|
+
def proxy_bypass? host, port
|
931
|
+
host = host.downcase
|
932
|
+
host_port = [host, port].join ':'
|
933
|
+
|
934
|
+
@no_proxy.each do |name|
|
935
|
+
return true if host[-name.length, name.length] == name or
|
936
|
+
host_port[-name.length, name.length] == name
|
937
|
+
end
|
938
|
+
|
939
|
+
false
|
940
|
+
end
|
941
|
+
|
942
|
+
##
|
943
|
+
# Forces reconnection of HTTP connections.
|
944
|
+
|
945
|
+
def reconnect
|
946
|
+
@generation += 1
|
947
|
+
end
|
948
|
+
|
949
|
+
##
|
950
|
+
# Forces reconnection of SSL connections.
|
951
|
+
|
952
|
+
def reconnect_ssl
|
953
|
+
@ssl_generation += 1
|
954
|
+
end
|
955
|
+
|
956
|
+
##
|
957
|
+
# Finishes then restarts the Net::HTTP +connection+
|
958
|
+
|
959
|
+
def reset connection
|
960
|
+
Thread.current[@request_key].delete connection.object_id
|
961
|
+
Thread.current[@timeout_key].delete connection.object_id
|
962
|
+
|
963
|
+
finish connection
|
964
|
+
|
965
|
+
start connection
|
966
|
+
rescue Errno::ECONNREFUSED
|
967
|
+
e = Error.new "connection refused: #{connection.address}:#{connection.port}"
|
968
|
+
e.set_backtrace $@
|
969
|
+
raise e
|
970
|
+
rescue Errno::EHOSTDOWN
|
971
|
+
e = Error.new "host down: #{connection.address}:#{connection.port}"
|
972
|
+
e.set_backtrace $@
|
973
|
+
raise e
|
974
|
+
end
|
975
|
+
|
976
|
+
##
|
977
|
+
# Makes a request on +uri+. If +req+ is nil a Net::HTTP::Get is performed
|
978
|
+
# against +uri+.
|
979
|
+
#
|
980
|
+
# If a block is passed #request behaves like Net::HTTP#request (the body of
|
981
|
+
# the response will not have been read).
|
982
|
+
#
|
983
|
+
# +req+ must be a Net::HTTPRequest subclass (see Net::HTTP for a list).
|
984
|
+
#
|
985
|
+
# If there is an error and the request is idempotent according to RFC 2616
|
986
|
+
# it will be retried automatically.
|
987
|
+
|
988
|
+
def request uri, req = nil, &block
|
989
|
+
retried = false
|
990
|
+
bad_response = false
|
991
|
+
|
992
|
+
req = request_setup req || uri
|
993
|
+
|
994
|
+
connection = connection_for uri
|
995
|
+
connection_id = connection.object_id
|
996
|
+
|
997
|
+
begin
|
998
|
+
Thread.current[@request_key][connection_id] += 1
|
999
|
+
response = connection.request req, &block
|
1000
|
+
|
1001
|
+
if connection_close?(req) or
|
1002
|
+
(response.http_version <= '1.0' and
|
1003
|
+
not connection_keep_alive?(response)) or
|
1004
|
+
connection_close?(response) then
|
1005
|
+
connection.finish
|
1006
|
+
end
|
1007
|
+
rescue Net::HTTPBadResponse => e
|
1008
|
+
message = error_message connection
|
1009
|
+
|
1010
|
+
finish connection
|
1011
|
+
|
1012
|
+
raise Error, "too many bad responses #{message}" if
|
1013
|
+
bad_response or not can_retry? req
|
1014
|
+
|
1015
|
+
bad_response = true
|
1016
|
+
retry
|
1017
|
+
rescue *RETRIED_EXCEPTIONS => e # retried on ruby 2
|
1018
|
+
request_failed e, req, connection if
|
1019
|
+
retried or not can_retry? req, @retried_on_ruby_2
|
1020
|
+
|
1021
|
+
reset connection
|
1022
|
+
|
1023
|
+
retried = true
|
1024
|
+
retry
|
1025
|
+
rescue Errno::EINVAL, Errno::ETIMEDOUT => e # not retried on ruby 2
|
1026
|
+
request_failed e, req, connection if retried or not can_retry? req
|
1027
|
+
|
1028
|
+
reset connection
|
1029
|
+
|
1030
|
+
retried = true
|
1031
|
+
retry
|
1032
|
+
rescue Exception => e
|
1033
|
+
finish connection
|
1034
|
+
|
1035
|
+
raise
|
1036
|
+
ensure
|
1037
|
+
Thread.current[@timeout_key][connection_id] = Time.now
|
1038
|
+
end
|
1039
|
+
|
1040
|
+
@http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
|
1041
|
+
|
1042
|
+
response
|
1043
|
+
end
|
1044
|
+
|
1045
|
+
##
|
1046
|
+
# Raises an Error for +exception+ which resulted from attempting the request
|
1047
|
+
# +req+ on the +connection+.
|
1048
|
+
#
|
1049
|
+
# Finishes the +connection+.
|
1050
|
+
|
1051
|
+
def request_failed exception, req, connection # :nodoc:
|
1052
|
+
due_to = "(due to #{exception.message} - #{exception.class})"
|
1053
|
+
message = "too many connection resets #{due_to} #{error_message connection}"
|
1054
|
+
|
1055
|
+
finish connection
|
1056
|
+
|
1057
|
+
|
1058
|
+
raise Error, message, exception.backtrace
|
1059
|
+
end
|
1060
|
+
|
1061
|
+
##
|
1062
|
+
# Creates a GET request if +req_or_uri+ is a URI and adds headers to the
|
1063
|
+
# request.
|
1064
|
+
#
|
1065
|
+
# Returns the request.
|
1066
|
+
|
1067
|
+
def request_setup req_or_uri # :nodoc:
|
1068
|
+
req = if req_or_uri.is_a?(URI) then
|
1069
|
+
Net::HTTP::Get.new req_or_uri.request_uri
|
1070
|
+
else
|
1071
|
+
req_or_uri
|
1072
|
+
end
|
1073
|
+
|
1074
|
+
@headers.each do |pair|
|
1075
|
+
req.add_field(*pair)
|
1076
|
+
end
|
1077
|
+
|
1078
|
+
@override_headers.each do |name, value|
|
1079
|
+
req[name] = value
|
1080
|
+
end
|
1081
|
+
|
1082
|
+
unless req['Connection'] then
|
1083
|
+
req.add_field 'Connection', 'keep-alive'
|
1084
|
+
req.add_field 'Keep-Alive', @keep_alive
|
1085
|
+
end
|
1086
|
+
|
1087
|
+
req
|
1088
|
+
end
|
1089
|
+
|
1090
|
+
##
|
1091
|
+
# Shuts down all connections for +thread+.
|
1092
|
+
#
|
1093
|
+
# Uses the current thread by default.
|
1094
|
+
#
|
1095
|
+
# If you've used Net::HTTP::Persistent across multiple threads you should
|
1096
|
+
# call this in each thread when you're done making HTTP requests.
|
1097
|
+
#
|
1098
|
+
# *NOTE*: Calling shutdown for another thread can be dangerous!
|
1099
|
+
#
|
1100
|
+
# If the thread is still using the connection it may cause an error! It is
|
1101
|
+
# best to call #shutdown in the thread at the appropriate time instead!
|
1102
|
+
|
1103
|
+
def shutdown thread = Thread.current
|
1104
|
+
generation = reconnect
|
1105
|
+
cleanup generation, thread, @generation_key
|
1106
|
+
|
1107
|
+
ssl_generation = reconnect_ssl
|
1108
|
+
cleanup ssl_generation, thread, @ssl_generation_key
|
1109
|
+
|
1110
|
+
thread[@request_key] = nil
|
1111
|
+
thread[@timeout_key] = nil
|
1112
|
+
end
|
1113
|
+
|
1114
|
+
##
|
1115
|
+
# Shuts down all connections in all threads
|
1116
|
+
#
|
1117
|
+
# *NOTE*: THIS METHOD IS VERY DANGEROUS!
|
1118
|
+
#
|
1119
|
+
# Do not call this method if other threads are still using their
|
1120
|
+
# connections! Call #shutdown at the appropriate time instead!
|
1121
|
+
#
|
1122
|
+
# Use this method only as a last resort!
|
1123
|
+
|
1124
|
+
def shutdown_in_all_threads
|
1125
|
+
Thread.list.each do |thread|
|
1126
|
+
shutdown thread
|
1127
|
+
end
|
1128
|
+
|
1129
|
+
nil
|
1130
|
+
end
|
1131
|
+
|
1132
|
+
##
|
1133
|
+
# Enables SSL on +connection+
|
1134
|
+
|
1135
|
+
def ssl connection
|
1136
|
+
connection.use_ssl = true
|
1137
|
+
|
1138
|
+
connection.ssl_version = @ssl_version if @ssl_version
|
1139
|
+
|
1140
|
+
connection.verify_mode = @verify_mode
|
1141
|
+
|
1142
|
+
if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and
|
1143
|
+
not Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then
|
1144
|
+
warn <<-WARNING
|
1145
|
+
!!!SECURITY WARNING!!!
|
1146
|
+
|
1147
|
+
The SSL HTTP connection to:
|
1148
|
+
|
1149
|
+
#{connection.address}:#{connection.port}
|
1150
|
+
|
1151
|
+
!!!MAY NOT BE VERIFIED!!!
|
1152
|
+
|
1153
|
+
On your platform your OpenSSL implementation is broken.
|
1154
|
+
|
1155
|
+
There is no difference between the values of VERIFY_NONE and VERIFY_PEER.
|
1156
|
+
|
1157
|
+
This means that attempting to verify the security of SSL connections may not
|
1158
|
+
work. This exposes you to man-in-the-middle exploits, snooping on the
|
1159
|
+
contents of your connection and other dangers to the security of your data.
|
1160
|
+
|
1161
|
+
To disable this warning define the following constant at top-level in your
|
1162
|
+
application:
|
1163
|
+
|
1164
|
+
I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nil
|
1165
|
+
|
1166
|
+
WARNING
|
1167
|
+
end
|
1168
|
+
|
1169
|
+
if @ca_file then
|
1170
|
+
connection.ca_file = @ca_file
|
1171
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
1172
|
+
connection.verify_callback = @verify_callback if @verify_callback
|
1173
|
+
end
|
1174
|
+
|
1175
|
+
if @certificate and @private_key then
|
1176
|
+
connection.cert = @certificate
|
1177
|
+
connection.key = @private_key
|
1178
|
+
end
|
1179
|
+
|
1180
|
+
connection.cert_store = if @cert_store then
|
1181
|
+
@cert_store
|
1182
|
+
else
|
1183
|
+
store = OpenSSL::X509::Store.new
|
1184
|
+
store.set_default_paths
|
1185
|
+
store
|
1186
|
+
end
|
1187
|
+
end
|
1188
|
+
|
1189
|
+
##
|
1190
|
+
# Finishes all connections that existed before the given SSL parameter
|
1191
|
+
# +generation+.
|
1192
|
+
|
1193
|
+
def ssl_cleanup generation # :nodoc:
|
1194
|
+
cleanup generation, Thread.current, @ssl_generation_key
|
1195
|
+
end
|
1196
|
+
|
1197
|
+
##
|
1198
|
+
# SSL version to use
|
1199
|
+
|
1200
|
+
def ssl_version= ssl_version
|
1201
|
+
@ssl_version = ssl_version
|
1202
|
+
|
1203
|
+
reconnect_ssl
|
1204
|
+
end if RUBY_VERSION > '1.9'
|
1205
|
+
|
1206
|
+
##
|
1207
|
+
# Sets the HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER.
|
1208
|
+
#
|
1209
|
+
# Setting this to VERIFY_NONE is a VERY BAD IDEA and should NEVER be used.
|
1210
|
+
# Securely transfer the correct certificate and update the default
|
1211
|
+
# certificate store or set the ca file instead.
|
1212
|
+
|
1213
|
+
def verify_mode= verify_mode
|
1214
|
+
@verify_mode = verify_mode
|
1215
|
+
|
1216
|
+
reconnect_ssl
|
1217
|
+
end
|
1218
|
+
|
1219
|
+
##
|
1220
|
+
# SSL verification callback.
|
1221
|
+
|
1222
|
+
def verify_callback= callback
|
1223
|
+
@verify_callback = callback
|
1224
|
+
|
1225
|
+
reconnect_ssl
|
1226
|
+
end
|
1227
|
+
|
1228
|
+
end
|
1229
|
+
|
1230
|
+
require 'net/http/persistent/ssl_reuse'
|