bundler_package_git 1.1.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/.gitignore +22 -0
  2. data/.rvmrc +1 -0
  3. data/CHANGELOG.md +659 -0
  4. data/ISSUES.md +47 -0
  5. data/LICENSE +23 -0
  6. data/README.md +29 -0
  7. data/Rakefile +167 -0
  8. data/UPGRADING.md +103 -0
  9. data/bin/bundle +22 -0
  10. data/bundler.gemspec +30 -0
  11. data/lib/bundler.rb +283 -0
  12. data/lib/bundler/capistrano.rb +11 -0
  13. data/lib/bundler/cli.rb +490 -0
  14. data/lib/bundler/definition.rb +429 -0
  15. data/lib/bundler/dependency.rb +130 -0
  16. data/lib/bundler/deployment.rb +53 -0
  17. data/lib/bundler/dsl.rb +243 -0
  18. data/lib/bundler/environment.rb +47 -0
  19. data/lib/bundler/fetcher.rb +101 -0
  20. data/lib/bundler/gem_helper.rb +146 -0
  21. data/lib/bundler/graph.rb +130 -0
  22. data/lib/bundler/index.rb +131 -0
  23. data/lib/bundler/installer.rb +117 -0
  24. data/lib/bundler/lazy_specification.rb +71 -0
  25. data/lib/bundler/lockfile_parser.rb +108 -0
  26. data/lib/bundler/remote_specification.rb +57 -0
  27. data/lib/bundler/resolver.rb +470 -0
  28. data/lib/bundler/rubygems_ext.rb +226 -0
  29. data/lib/bundler/runtime.rb +201 -0
  30. data/lib/bundler/settings.rb +117 -0
  31. data/lib/bundler/setup.rb +16 -0
  32. data/lib/bundler/shared_helpers.rb +167 -0
  33. data/lib/bundler/source.rb +675 -0
  34. data/lib/bundler/spec_set.rb +134 -0
  35. data/lib/bundler/templates/Executable +16 -0
  36. data/lib/bundler/templates/Gemfile +4 -0
  37. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  38. data/lib/bundler/templates/newgem/Rakefile.tt +2 -0
  39. data/lib/bundler/templates/newgem/bin/newgem.tt +3 -0
  40. data/lib/bundler/templates/newgem/gitignore.tt +4 -0
  41. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +7 -0
  42. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  43. data/lib/bundler/templates/newgem/newgem.gemspec.tt +21 -0
  44. data/lib/bundler/ui.rb +73 -0
  45. data/lib/bundler/vendor/net/http/faster.rb +27 -0
  46. data/lib/bundler/vendor/net/http/persistent.rb +464 -0
  47. data/lib/bundler/vendor/thor.rb +319 -0
  48. data/lib/bundler/vendor/thor/actions.rb +297 -0
  49. data/lib/bundler/vendor/thor/actions/create_file.rb +105 -0
  50. data/lib/bundler/vendor/thor/actions/directory.rb +93 -0
  51. data/lib/bundler/vendor/thor/actions/empty_directory.rb +134 -0
  52. data/lib/bundler/vendor/thor/actions/file_manipulation.rb +229 -0
  53. data/lib/bundler/vendor/thor/actions/inject_into_file.rb +104 -0
  54. data/lib/bundler/vendor/thor/base.rb +556 -0
  55. data/lib/bundler/vendor/thor/core_ext/file_binary_read.rb +9 -0
  56. data/lib/bundler/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  57. data/lib/bundler/vendor/thor/core_ext/ordered_hash.rb +100 -0
  58. data/lib/bundler/vendor/thor/error.rb +30 -0
  59. data/lib/bundler/vendor/thor/invocation.rb +168 -0
  60. data/lib/bundler/vendor/thor/parser.rb +4 -0
  61. data/lib/bundler/vendor/thor/parser/argument.rb +67 -0
  62. data/lib/bundler/vendor/thor/parser/arguments.rb +161 -0
  63. data/lib/bundler/vendor/thor/parser/option.rb +120 -0
  64. data/lib/bundler/vendor/thor/parser/options.rb +174 -0
  65. data/lib/bundler/vendor/thor/shell.rb +88 -0
  66. data/lib/bundler/vendor/thor/shell/basic.rb +275 -0
  67. data/lib/bundler/vendor/thor/shell/color.rb +108 -0
  68. data/lib/bundler/vendor/thor/shell/html.rb +121 -0
  69. data/lib/bundler/vendor/thor/task.rb +114 -0
  70. data/lib/bundler/vendor/thor/util.rb +229 -0
  71. data/lib/bundler/vendor/thor/version.rb +3 -0
  72. data/lib/bundler/version.rb +6 -0
  73. data/lib/bundler/vlad.rb +9 -0
  74. data/man/bundle-config.ronn +90 -0
  75. data/man/bundle-exec.ronn +111 -0
  76. data/man/bundle-install.ronn +314 -0
  77. data/man/bundle-package.ronn +59 -0
  78. data/man/bundle-update.ronn +176 -0
  79. data/man/bundle.ronn +80 -0
  80. data/man/gemfile.5.ronn +279 -0
  81. data/man/index.txt +6 -0
  82. data/spec/cache/gems_spec.rb +219 -0
  83. data/spec/cache/git_spec.rb +9 -0
  84. data/spec/cache/path_spec.rb +27 -0
  85. data/spec/cache/platform_spec.rb +57 -0
  86. data/spec/install/deploy_spec.rb +197 -0
  87. data/spec/install/deprecated_spec.rb +37 -0
  88. data/spec/install/gems/c_ext_spec.rb +48 -0
  89. data/spec/install/gems/dependency_api_spec.rb +85 -0
  90. data/spec/install/gems/env_spec.rb +107 -0
  91. data/spec/install/gems/flex_spec.rb +313 -0
  92. data/spec/install/gems/groups_spec.rb +245 -0
  93. data/spec/install/gems/packed_spec.rb +84 -0
  94. data/spec/install/gems/platform_spec.rb +208 -0
  95. data/spec/install/gems/resolving_spec.rb +72 -0
  96. data/spec/install/gems/simple_case_spec.rb +715 -0
  97. data/spec/install/gems/standalone_spec.rb +162 -0
  98. data/spec/install/gems/sudo_spec.rb +73 -0
  99. data/spec/install/gems/win32_spec.rb +26 -0
  100. data/spec/install/gemspec_spec.rb +108 -0
  101. data/spec/install/git_spec.rb +571 -0
  102. data/spec/install/invalid_spec.rb +17 -0
  103. data/spec/install/path_spec.rb +353 -0
  104. data/spec/install/upgrade_spec.rb +26 -0
  105. data/spec/lock/git_spec.rb +35 -0
  106. data/spec/lock/lockfile_spec.rb +683 -0
  107. data/spec/other/check_spec.rb +221 -0
  108. data/spec/other/clean_spec.rb +202 -0
  109. data/spec/other/config_spec.rb +40 -0
  110. data/spec/other/console_spec.rb +54 -0
  111. data/spec/other/exec_spec.rb +241 -0
  112. data/spec/other/ext_spec.rb +16 -0
  113. data/spec/other/gem_helper_spec.rb +128 -0
  114. data/spec/other/help_spec.rb +38 -0
  115. data/spec/other/init_spec.rb +40 -0
  116. data/spec/other/newgem_spec.rb +24 -0
  117. data/spec/other/open_spec.rb +35 -0
  118. data/spec/other/show_spec.rb +82 -0
  119. data/spec/pack/gems_spec.rb +54 -0
  120. data/spec/quality_spec.rb +58 -0
  121. data/spec/resolver/basic_spec.rb +20 -0
  122. data/spec/resolver/platform_spec.rb +82 -0
  123. data/spec/runtime/executable_spec.rb +110 -0
  124. data/spec/runtime/load_spec.rb +107 -0
  125. data/spec/runtime/platform_spec.rb +90 -0
  126. data/spec/runtime/require_spec.rb +231 -0
  127. data/spec/runtime/setup_spec.rb +688 -0
  128. data/spec/runtime/with_clean_env_spec.rb +15 -0
  129. data/spec/spec_helper.rb +85 -0
  130. data/spec/support/artifice/endpoint.rb +50 -0
  131. data/spec/support/artifice/endpoint_fallback.rb +22 -0
  132. data/spec/support/artifice/endpoint_marshal_fail.rb +11 -0
  133. data/spec/support/artifice/endpoint_redirect.rb +11 -0
  134. data/spec/support/builders.rb +574 -0
  135. data/spec/support/fakeweb/rack-1.0.0.marshal +2 -0
  136. data/spec/support/fakeweb/windows.rb +23 -0
  137. data/spec/support/helpers.rb +246 -0
  138. data/spec/support/indexes.rb +112 -0
  139. data/spec/support/matchers.rb +89 -0
  140. data/spec/support/path.rb +73 -0
  141. data/spec/support/platforms.rb +53 -0
  142. data/spec/support/ruby_ext.rb +20 -0
  143. data/spec/support/rubygems_ext.rb +35 -0
  144. data/spec/support/rubygems_hax/platform.rb +11 -0
  145. data/spec/support/sudo.rb +21 -0
  146. data/spec/update/gems_spec.rb +121 -0
  147. data/spec/update/git_spec.rb +196 -0
  148. data/spec/update/source_spec.rb +51 -0
  149. metadata +294 -0
@@ -0,0 +1,134 @@
1
+ require 'tsort'
2
+
3
+ module Bundler
4
+ class SpecSet
5
+ include TSort, Enumerable
6
+
7
+ def initialize(specs)
8
+ @specs = specs.sort_by { |s| s.name }
9
+ end
10
+
11
+ def each
12
+ sorted.each { |s| yield s }
13
+ end
14
+
15
+ def length
16
+ @specs.length
17
+ end
18
+
19
+ def for(dependencies, skip = [], check = false, match_current_platform = false)
20
+ handled, deps, specs = {}, dependencies.dup, []
21
+ skip << 'bundler'
22
+
23
+ until deps.empty?
24
+ dep = deps.shift
25
+ next if handled[dep] || skip.include?(dep.name)
26
+
27
+ spec = lookup[dep.name].find do |s|
28
+ match_current_platform ?
29
+ Gem::Platform.match(s.platform) :
30
+ s.match_platform(dep.__platform)
31
+ end
32
+
33
+ handled[dep] = true
34
+
35
+ if spec
36
+ specs << spec
37
+
38
+ spec.dependencies.each do |d|
39
+ next if d.type == :development
40
+ d = DepProxy.new(d, dep.__platform) unless match_current_platform
41
+ deps << d
42
+ end
43
+ elsif check
44
+ return false
45
+ end
46
+ end
47
+
48
+ if spec = lookup['bundler'].first
49
+ specs << spec
50
+ end
51
+
52
+ check ? true : SpecSet.new(specs)
53
+ end
54
+
55
+ def valid_for?(deps)
56
+ self.for(deps, [], true)
57
+ end
58
+
59
+ def [](key)
60
+ key = key.name if key.respond_to?(:name)
61
+ lookup[key].reverse
62
+ end
63
+
64
+ def []=(key, value)
65
+ @specs << value
66
+ @lookup = nil
67
+ @sorted = nil
68
+ value
69
+ end
70
+
71
+ def to_a
72
+ sorted.dup
73
+ end
74
+
75
+ def to_hash
76
+ lookup.dup
77
+ end
78
+
79
+ def materialize(deps, missing_specs = nil)
80
+ materialized = self.for(deps, [], false, true).to_a
81
+ materialized.map! do |s|
82
+ next s unless s.is_a?(LazySpecification)
83
+ spec = s.__materialize__
84
+ if missing_specs
85
+ missing_specs << s unless spec
86
+ else
87
+ raise GemNotFound, "Could not find #{s.full_name} in any of the sources" unless spec
88
+ end
89
+ spec if spec
90
+ end
91
+ SpecSet.new(materialized.compact)
92
+ end
93
+
94
+ def merge(set)
95
+ arr = sorted.dup
96
+ set.each do |s|
97
+ next if arr.any? { |s2| s2.name == s.name && s2.version == s.version && s2.platform == s.platform }
98
+ arr << s
99
+ end
100
+ SpecSet.new(arr)
101
+ end
102
+
103
+ private
104
+
105
+ def sorted
106
+ rake = @specs.find { |s| s.name == 'rake' }
107
+ @sorted ||= ([rake] + tsort).compact.uniq
108
+ end
109
+
110
+ def lookup
111
+ @lookup ||= begin
112
+ lookup = Hash.new { |h,k| h[k] = [] }
113
+ specs = @specs.sort_by do |s|
114
+ s.platform.to_s == 'ruby' ? "\0" : s.platform.to_s
115
+ end
116
+ specs.reverse_each do |s|
117
+ lookup[s.name] << s
118
+ end
119
+ lookup
120
+ end
121
+ end
122
+
123
+ def tsort_each_node
124
+ @specs.each { |s| yield s }
125
+ end
126
+
127
+ def tsort_each_child(s)
128
+ s.dependencies.sort_by { |d| d.name }.each do |d|
129
+ next if d.type == :development
130
+ lookup[d.name].each { |s2| yield s2 }
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env <%= RbConfig::CONFIG['ruby_install_name'] %>
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application '<%= executable %>' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../<%= relative_gemfile_path %>",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('<%= spec.name %>', '<%= executable %>')
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "http://rubygems.org"
3
+
4
+ # gem "rails"
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in <%=config[:name]%>.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "<%= config[:name] %>"
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
@@ -0,0 +1,7 @@
1
+ <%- config[:constant_array].each_with_index do |c,i| -%>
2
+ <%= ' '*i %>module <%= c %>
3
+ <%- end -%>
4
+ <%= ' '*config[:constant_array].size %># Your code goes here...
5
+ <%- (config[:constant_array].size-1).downto(0) do |i| -%>
6
+ <%= ' '*i %>end
7
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ <%- config[:constant_array].each_with_index do |c,i| -%>
2
+ <%= ' '*i %>module <%= c %>
3
+ <%- end -%>
4
+ <%= ' '*config[:constant_array].size %>VERSION = "0.0.1"
5
+ <%- (config[:constant_array].size-1).downto(0) do |i| -%>
6
+ <%= ' '*i %>end
7
+ <%- end -%>
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "<%=config[:name]%>/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = <%=config[:name].inspect%>
7
+ s.version = <%=config[:constant_name]%>::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["TODO: Write your name"]
10
+ s.email = ["TODO: Write your email address"]
11
+ s.homepage = ""
12
+ s.summary = %q{TODO: Write a gem summary}
13
+ s.description = %q{TODO: Write a gem description}
14
+
15
+ s.rubyforge_project = <%=config[:name].inspect%>
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,73 @@
1
+ require 'rubygems/user_interaction'
2
+
3
+ module Bundler
4
+ class UI
5
+ def warn(message)
6
+ end
7
+
8
+ def debug(message)
9
+ end
10
+
11
+ def error(message)
12
+ end
13
+
14
+ def info(message)
15
+ end
16
+
17
+ def confirm(message)
18
+ end
19
+
20
+ class Shell < UI
21
+ attr_writer :shell
22
+
23
+ def initialize(shell)
24
+ @shell = shell
25
+ @quiet = false
26
+ @debug = ENV['DEBUG']
27
+ end
28
+
29
+ def debug(msg)
30
+ @shell.say(msg) if @debug && !@quiet
31
+ end
32
+
33
+ def info(msg)
34
+ @shell.say(msg) if !@quiet
35
+ end
36
+
37
+ def confirm(msg)
38
+ @shell.say(msg, :green) if !@quiet
39
+ end
40
+
41
+ def warn(msg)
42
+ @shell.say(msg, :yellow)
43
+ end
44
+
45
+ def error(msg)
46
+ @shell.say(msg, :red)
47
+ end
48
+
49
+ def be_quiet!
50
+ @quiet = true
51
+ end
52
+
53
+ def debug!
54
+ @debug = true
55
+ end
56
+ end
57
+
58
+ class RGProxy < ::Gem::SilentUI
59
+ def initialize(ui)
60
+ @ui = ui
61
+ super()
62
+ end
63
+
64
+ def say(message)
65
+ if message =~ /native extensions/
66
+ @ui.info "with native extensions "
67
+ else
68
+ @ui.debug(message)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,27 @@
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 => 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'
27
+
@@ -0,0 +1,464 @@
1
+ require 'net/http'
2
+ require 'net/http/faster'
3
+ require 'uri'
4
+ require 'cgi' # for escaping
5
+
6
+ ##
7
+ # Persistent connections for Net::HTTP
8
+ #
9
+ # Net::HTTP::Persistent maintains persistent connections across all the
10
+ # servers you wish to talk to. For each host:port you communicate with a
11
+ # single persistent connection is created.
12
+ #
13
+ # Multiple Net::HTTP::Persistent objects will share the same set of
14
+ # connections.
15
+ #
16
+ # For each thread you start a new connection will be created. A
17
+ # Net::HTTP::Persistent connection will not be shared across threads.
18
+ #
19
+ # You can shut down the HTTP connections when done by calling #shutdown. You
20
+ # should name your Net::HTTP::Persistent object if you intend to call this
21
+ # method.
22
+ #
23
+ # Example:
24
+ #
25
+ # uri = URI.parse 'http://example.com/awesome/web/service'
26
+ # http = Net::HTTP::Persistent.new
27
+ # stuff = http.request uri # performs a GET
28
+ #
29
+ # # perform a POST
30
+ # post_uri = uri + 'create'
31
+ # post = Net::HTTP::Post.new post_uri.path
32
+ # post.set_form_data 'some' => 'cool data'
33
+ # http.request post_uri, post # URI is always required
34
+
35
+ class Net::HTTP::Persistent
36
+
37
+ ##
38
+ # The version of Net::HTTP::Persistent use are using
39
+
40
+ VERSION = '1.4.1'
41
+
42
+ ##
43
+ # Error class for errors raised by Net::HTTP::Persistent. Various
44
+ # SystemCallErrors are re-raised with a human-readable message under this
45
+ # class.
46
+
47
+ class Error < StandardError; end
48
+
49
+ ##
50
+ # This client's OpenSSL::X509::Certificate
51
+
52
+ attr_accessor :certificate
53
+
54
+ ##
55
+ # An SSL certificate authority. Setting this will set verify_mode to
56
+ # VERIFY_PEER.
57
+
58
+ attr_accessor :ca_file
59
+
60
+ ##
61
+ # Where this instance's connections live in the thread local variables
62
+
63
+ attr_reader :connection_key # :nodoc:
64
+
65
+ ##
66
+ # Sends debug_output to this IO via Net::HTTP#set_debug_output.
67
+ #
68
+ # Never use this method in production code, it causes a serious security
69
+ # hole.
70
+
71
+ attr_accessor :debug_output
72
+
73
+ ##
74
+ # Headers that are added to every request
75
+
76
+ attr_reader :headers
77
+
78
+ ##
79
+ # Maps host:port to an HTTP version. This allows us to enable version
80
+ # specific features.
81
+
82
+ attr_reader :http_versions
83
+
84
+ ##
85
+ # The value sent in the Keep-Alive header. Defaults to 30. Not needed for
86
+ # HTTP/1.1 servers.
87
+ #
88
+ # This may not work correctly for HTTP/1.0 servers
89
+ #
90
+ # This method may be removed in a future version as RFC 2616 does not
91
+ # require this header.
92
+
93
+ attr_accessor :keep_alive
94
+
95
+ ##
96
+ # A name for this connection. Allows you to keep your connections apart
97
+ # from everybody else's.
98
+
99
+ attr_reader :name
100
+
101
+ ##
102
+ # Seconds to wait until a connection is opened. See Net::HTTP#open_timeout
103
+
104
+ attr_accessor :open_timeout
105
+
106
+ ##
107
+ # This client's SSL private key
108
+
109
+ attr_accessor :private_key
110
+
111
+ ##
112
+ # The URL through which requests will be proxied
113
+
114
+ attr_reader :proxy_uri
115
+
116
+ ##
117
+ # Seconds to wait until reading one block. See Net::HTTP#read_timeout
118
+
119
+ attr_accessor :read_timeout
120
+
121
+ ##
122
+ # Where this instance's request counts live in the thread local variables
123
+
124
+ attr_reader :request_key # :nodoc:
125
+
126
+ ##
127
+ # SSL verification callback. Used when ca_file is set.
128
+
129
+ attr_accessor :verify_callback
130
+
131
+ ##
132
+ # HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_NONE which ignores
133
+ # certificate problems.
134
+ #
135
+ # You can use +verify_mode+ to override any default values.
136
+
137
+ attr_accessor :verify_mode
138
+
139
+ ##
140
+ # Creates a new Net::HTTP::Persistent.
141
+ #
142
+ # Set +name+ to keep your connections apart from everybody else's. Not
143
+ # required currently, but highly recommended. Your library name should be
144
+ # good enough. This parameter will be required in a future version.
145
+ #
146
+ # +proxy+ may be set to a URI::HTTP or :ENV to pick up proxy options from
147
+ # the environment. See proxy_from_env for details.
148
+ #
149
+ # In order to use a URI for the proxy you'll need to do some extra work
150
+ # beyond URI.parse:
151
+ #
152
+ # proxy = URI.parse 'http://proxy.example'
153
+ # proxy.user = 'AzureDiamond'
154
+ # proxy.password = 'hunter2'
155
+
156
+ def initialize name = nil, proxy = nil
157
+ @name = name
158
+
159
+ @proxy_uri = case proxy
160
+ when :ENV then proxy_from_env
161
+ when URI::HTTP then proxy
162
+ when nil then # ignore
163
+ else raise ArgumentError, 'proxy must be :ENV or a URI::HTTP'
164
+ end
165
+
166
+ if @proxy_uri then
167
+ @proxy_args = [
168
+ @proxy_uri.host,
169
+ @proxy_uri.port,
170
+ @proxy_uri.user,
171
+ @proxy_uri.password,
172
+ ]
173
+
174
+ @proxy_connection_id = [nil, *@proxy_args].join ':'
175
+ end
176
+
177
+ @debug_output = nil
178
+ @headers = {}
179
+ @http_versions = {}
180
+ @keep_alive = 30
181
+ @open_timeout = nil
182
+ @read_timeout = nil
183
+
184
+ key = ['net_http_persistent', name, 'connections'].compact.join '_'
185
+ @connection_key = key.intern
186
+ key = ['net_http_persistent', name, 'requests'].compact.join '_'
187
+ @request_key = key.intern
188
+
189
+ @certificate = nil
190
+ @ca_file = nil
191
+ @private_key = nil
192
+ @verify_callback = nil
193
+ @verify_mode = nil
194
+ end
195
+
196
+ ##
197
+ # Creates a new connection for +uri+
198
+
199
+ def connection_for uri
200
+ Thread.current[@connection_key] ||= {}
201
+ Thread.current[@request_key] ||= Hash.new 0
202
+
203
+ connections = Thread.current[@connection_key]
204
+
205
+ net_http_args = [uri.host, uri.port]
206
+ connection_id = net_http_args.join ':'
207
+
208
+ if @proxy_uri then
209
+ connection_id << @proxy_connection_id
210
+ net_http_args.concat @proxy_args
211
+ end
212
+
213
+ unless connection = connections[connection_id] then
214
+ connections[connection_id] = Net::HTTP.new(*net_http_args)
215
+ connection = connections[connection_id]
216
+ ssl connection if uri.scheme == 'https'
217
+ end
218
+
219
+ unless connection.started? then
220
+ connection.set_debug_output @debug_output if @debug_output
221
+ connection.open_timeout = @open_timeout if @open_timeout
222
+ connection.read_timeout = @read_timeout if @read_timeout
223
+
224
+ connection.start
225
+ end
226
+
227
+ connection
228
+ rescue Errno::ECONNREFUSED
229
+ raise Error, "connection refused: #{connection.address}:#{connection.port}"
230
+ rescue Errno::EHOSTDOWN
231
+ raise Error, "host down: #{connection.address}:#{connection.port}"
232
+ end
233
+
234
+ ##
235
+ # Returns an error message containing the number of requests performed on
236
+ # this connection
237
+
238
+ def error_message connection
239
+ requests =
240
+ Thread.current[@request_key][connection.object_id]
241
+
242
+ "after #{requests} requests on #{connection.object_id}"
243
+ end
244
+
245
+ ##
246
+ # URI::escape wrapper
247
+
248
+ def escape str
249
+ CGI.escape str if str
250
+ end
251
+
252
+ ##
253
+ # Finishes the Net::HTTP +connection+
254
+
255
+ def finish connection
256
+ Thread.current[@request_key].delete connection.object_id
257
+
258
+ connection.finish
259
+ rescue IOError
260
+ end
261
+
262
+ ##
263
+ # Returns the HTTP protocol version for +uri+
264
+
265
+ def http_version uri
266
+ @http_versions["#{uri.host}:#{uri.port}"]
267
+ end
268
+
269
+ ##
270
+ # Is +req+ idempotent according to RFC 2616?
271
+
272
+ def idempotent? req
273
+ case req
274
+ when Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head,
275
+ Net::HTTP::Options, Net::HTTP::Put, Net::HTTP::Trace then
276
+ true
277
+ end
278
+ end
279
+
280
+ ##
281
+ # Adds "http://" to the String +uri+ if it is missing.
282
+
283
+ def normalize_uri uri
284
+ (uri =~ /^https?:/) ? uri : "http://#{uri}"
285
+ end
286
+
287
+ ##
288
+ # Creates a URI for an HTTP proxy server from ENV variables.
289
+ #
290
+ # If +HTTP_PROXY+ is set a proxy will be returned.
291
+ #
292
+ # If +HTTP_PROXY_USER+ or +HTTP_PROXY_PASS+ are set the URI is given the
293
+ # indicated user and password unless HTTP_PROXY contains either of these in
294
+ # the URI.
295
+ #
296
+ # For Windows users lowercase ENV variables are preferred over uppercase ENV
297
+ # variables.
298
+
299
+ def proxy_from_env
300
+ env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
301
+
302
+ return nil if env_proxy.nil? or env_proxy.empty?
303
+
304
+ uri = URI.parse normalize_uri env_proxy
305
+
306
+ unless uri.user or uri.password then
307
+ uri.user = escape ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER']
308
+ uri.password = escape ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS']
309
+ end
310
+
311
+ uri
312
+ end
313
+
314
+ ##
315
+ # Finishes then restarts the Net::HTTP +connection+
316
+
317
+ def reset connection
318
+ Thread.current[@request_key].delete connection.object_id
319
+
320
+ finish connection
321
+
322
+ connection.start
323
+ rescue Errno::ECONNREFUSED
324
+ raise Error, "connection refused: #{connection.address}:#{connection.port}"
325
+ rescue Errno::EHOSTDOWN
326
+ raise Error, "host down: #{connection.address}:#{connection.port}"
327
+ end
328
+
329
+ ##
330
+ # Makes a request on +uri+. If +req+ is nil a Net::HTTP::Get is performed
331
+ # against +uri+.
332
+ #
333
+ # If a block is passed #request behaves like Net::HTTP#request (the body of
334
+ # the response will not have been read).
335
+ #
336
+ # +req+ must be a Net::HTTPRequest subclass (see Net::HTTP for a list).
337
+ #
338
+ # If there is an error and the request is idempontent according to RFC 2616
339
+ # it will be retried automatically.
340
+
341
+ def request uri, req = nil, &block
342
+ retried = false
343
+ bad_response = false
344
+
345
+ req = Net::HTTP::Get.new uri.request_uri unless req
346
+
347
+ headers.each do |pair|
348
+ req.add_field(*pair)
349
+ end
350
+
351
+ req.add_field 'Connection', 'keep-alive'
352
+ req.add_field 'Keep-Alive', @keep_alive
353
+
354
+ connection = connection_for uri
355
+ connection_id = connection.object_id
356
+
357
+ begin
358
+ Thread.current[@request_key][connection_id] += 1
359
+ response = connection.request req, &block
360
+
361
+ rescue Net::HTTPBadResponse => e
362
+ message = error_message connection
363
+
364
+ finish connection
365
+
366
+ raise Error, "too many bad responses #{message}" if
367
+ bad_response or not idempotent? req
368
+
369
+ bad_response = true
370
+ retry
371
+ rescue IOError, EOFError, Timeout::Error,
372
+ Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE => e
373
+
374
+ if retried or not idempotent? req
375
+ due_to = "(due to #{e.message} - #{e.class})"
376
+ message = error_message connection
377
+
378
+ finish connection
379
+
380
+ raise Error, "too many connection resets #{due_to} #{message}"
381
+ end
382
+
383
+ reset connection
384
+
385
+ retried = true
386
+ retry
387
+ end
388
+
389
+ @http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
390
+
391
+ response
392
+ end
393
+
394
+ ##
395
+ # Shuts down all connections for +thread+.
396
+ #
397
+ # Uses the current thread by default.
398
+ #
399
+ # If you've used Net::HTTP::Persistent across multiple threads you should
400
+ # call this in each thread when you're done making HTTP requests.
401
+ #
402
+ # *NOTE*: Calling shutdown for another thread can be dangerous!
403
+ #
404
+ # If the thread is still using the connection it may cause an error! It is
405
+ # best to call #shutdown in the thread at the appropriate time instead!
406
+
407
+ def shutdown thread = Thread.current
408
+ connections = thread[@connection_key]
409
+
410
+ connections.each do |_, connection|
411
+ begin
412
+ connection.finish
413
+ rescue IOError
414
+ end
415
+ end if connections
416
+
417
+ thread[@connection_key] = nil
418
+ thread[@request_key] = nil
419
+ end
420
+
421
+ ##
422
+ # Shuts down all connections in all threads
423
+ #
424
+ # *NOTE*: THIS METHOD IS VERY DANGEROUS!
425
+ #
426
+ # Do not call this method if other threads are still using their
427
+ # connections! Call #shutdown at the appropriate time instead!
428
+ #
429
+ # Use this method only as a last resort!
430
+
431
+ def shutdown_in_all_threads
432
+ Thread.list.each do |thread|
433
+ shutdown thread
434
+ end
435
+
436
+ nil
437
+ end
438
+
439
+ ##
440
+ # Enables SSL on +connection+
441
+
442
+ def ssl connection
443
+ require 'net/https'
444
+ connection.use_ssl = true
445
+
446
+ # suppress warning but allow override
447
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @verify_mode
448
+
449
+ if @ca_file then
450
+ connection.ca_file = @ca_file
451
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
452
+ connection.verify_callback = @verify_callback if @verify_callback
453
+ end
454
+
455
+ if @certificate and @private_key then
456
+ connection.cert = @certificate
457
+ connection.key = @private_key
458
+ end
459
+
460
+ connection.verify_mode = @verify_mode if @verify_mode
461
+ end
462
+
463
+ end
464
+