rkh-bundler 1.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/.gitignore +22 -0
  2. data/.travis.yml +42 -0
  3. data/CHANGELOG.md +1105 -0
  4. data/ISSUES.md +67 -0
  5. data/LICENSE +23 -0
  6. data/README.md +31 -0
  7. data/Rakefile +208 -0
  8. data/UPGRADING.md +103 -0
  9. data/bin/bundle +31 -0
  10. data/bundler.gemspec +31 -0
  11. data/lib/bundler.rb +353 -0
  12. data/lib/bundler/capistrano.rb +11 -0
  13. data/lib/bundler/cli.rb +693 -0
  14. data/lib/bundler/definition.rb +568 -0
  15. data/lib/bundler/dep_proxy.rb +43 -0
  16. data/lib/bundler/dependency.rb +134 -0
  17. data/lib/bundler/deployment.rb +58 -0
  18. data/lib/bundler/dsl.rb +256 -0
  19. data/lib/bundler/endpoint_specification.rb +78 -0
  20. data/lib/bundler/environment.rb +47 -0
  21. data/lib/bundler/fetcher.rb +225 -0
  22. data/lib/bundler/gem_helper.rb +162 -0
  23. data/lib/bundler/gem_helpers.rb +23 -0
  24. data/lib/bundler/gem_installer.rb +9 -0
  25. data/lib/bundler/gem_tasks.rb +2 -0
  26. data/lib/bundler/graph.rb +148 -0
  27. data/lib/bundler/index.rb +187 -0
  28. data/lib/bundler/installer.rb +190 -0
  29. data/lib/bundler/lazy_specification.rb +79 -0
  30. data/lib/bundler/lockfile_parser.rb +127 -0
  31. data/lib/bundler/match_platform.rb +13 -0
  32. data/lib/bundler/psyched_yaml.rb +15 -0
  33. data/lib/bundler/remote_specification.rb +57 -0
  34. data/lib/bundler/resolver.rb +486 -0
  35. data/lib/bundler/ruby_version.rb +94 -0
  36. data/lib/bundler/rubygems_ext.rb +153 -0
  37. data/lib/bundler/rubygems_integration.rb +394 -0
  38. data/lib/bundler/runtime.rb +233 -0
  39. data/lib/bundler/settings.rb +128 -0
  40. data/lib/bundler/setup.rb +23 -0
  41. data/lib/bundler/shared_helpers.rb +71 -0
  42. data/lib/bundler/source.rb +869 -0
  43. data/lib/bundler/spec_set.rb +137 -0
  44. data/lib/bundler/templates/Executable +16 -0
  45. data/lib/bundler/templates/Executable.standalone +12 -0
  46. data/lib/bundler/templates/Gemfile +4 -0
  47. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  48. data/lib/bundler/templates/newgem/LICENSE.tt +22 -0
  49. data/lib/bundler/templates/newgem/README.md.tt +29 -0
  50. data/lib/bundler/templates/newgem/Rakefile.tt +2 -0
  51. data/lib/bundler/templates/newgem/bin/newgem.tt +3 -0
  52. data/lib/bundler/templates/newgem/gitignore.tt +17 -0
  53. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +9 -0
  54. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  55. data/lib/bundler/templates/newgem/newgem.gemspec.tt +17 -0
  56. data/lib/bundler/ui.rb +88 -0
  57. data/lib/bundler/vendor/net/http/faster.rb +27 -0
  58. data/lib/bundler/vendor/net/http/persistent.rb +468 -0
  59. data/lib/bundler/vendor/thor.rb +358 -0
  60. data/lib/bundler/vendor/thor/actions.rb +314 -0
  61. data/lib/bundler/vendor/thor/actions/create_file.rb +105 -0
  62. data/lib/bundler/vendor/thor/actions/create_link.rb +57 -0
  63. data/lib/bundler/vendor/thor/actions/directory.rb +93 -0
  64. data/lib/bundler/vendor/thor/actions/empty_directory.rb +134 -0
  65. data/lib/bundler/vendor/thor/actions/file_manipulation.rb +270 -0
  66. data/lib/bundler/vendor/thor/actions/inject_into_file.rb +109 -0
  67. data/lib/bundler/vendor/thor/base.rb +576 -0
  68. data/lib/bundler/vendor/thor/core_ext/file_binary_read.rb +9 -0
  69. data/lib/bundler/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  70. data/lib/bundler/vendor/thor/core_ext/ordered_hash.rb +100 -0
  71. data/lib/bundler/vendor/thor/error.rb +30 -0
  72. data/lib/bundler/vendor/thor/group.rb +273 -0
  73. data/lib/bundler/vendor/thor/invocation.rb +168 -0
  74. data/lib/bundler/vendor/thor/parser.rb +4 -0
  75. data/lib/bundler/vendor/thor/parser/argument.rb +67 -0
  76. data/lib/bundler/vendor/thor/parser/arguments.rb +161 -0
  77. data/lib/bundler/vendor/thor/parser/option.rb +120 -0
  78. data/lib/bundler/vendor/thor/parser/options.rb +172 -0
  79. data/lib/bundler/vendor/thor/rake_compat.rb +66 -0
  80. data/lib/bundler/vendor/thor/runner.rb +309 -0
  81. data/lib/bundler/vendor/thor/shell.rb +88 -0
  82. data/lib/bundler/vendor/thor/shell/basic.rb +302 -0
  83. data/lib/bundler/vendor/thor/shell/color.rb +108 -0
  84. data/lib/bundler/vendor/thor/shell/html.rb +121 -0
  85. data/lib/bundler/vendor/thor/task.rb +113 -0
  86. data/lib/bundler/vendor/thor/util.rb +229 -0
  87. data/lib/bundler/vendor/thor/version.rb +3 -0
  88. data/lib/bundler/vendored_thor.rb +7 -0
  89. data/lib/bundler/version.rb +6 -0
  90. data/lib/bundler/vlad.rb +11 -0
  91. data/man/bundle-config.ronn +130 -0
  92. data/man/bundle-exec.ronn +111 -0
  93. data/man/bundle-install.ronn +335 -0
  94. data/man/bundle-package.ronn +59 -0
  95. data/man/bundle-update.ronn +176 -0
  96. data/man/bundle.ronn +83 -0
  97. data/man/gemfile.5.ronn +324 -0
  98. data/man/index.txt +6 -0
  99. data/spec/bundler/dsl_spec.rb +48 -0
  100. data/spec/bundler/gem_helper_spec.rb +174 -0
  101. data/spec/bundler/source_spec.rb +25 -0
  102. data/spec/cache/gems_spec.rb +239 -0
  103. data/spec/cache/git_spec.rb +124 -0
  104. data/spec/cache/path_spec.rb +103 -0
  105. data/spec/cache/platform_spec.rb +57 -0
  106. data/spec/install/deploy_spec.rb +211 -0
  107. data/spec/install/gems/c_ext_spec.rb +48 -0
  108. data/spec/install/gems/dependency_api_spec.rb +402 -0
  109. data/spec/install/gems/env_spec.rb +107 -0
  110. data/spec/install/gems/flex_spec.rb +313 -0
  111. data/spec/install/gems/groups_spec.rb +268 -0
  112. data/spec/install/gems/packed_spec.rb +84 -0
  113. data/spec/install/gems/platform_spec.rb +208 -0
  114. data/spec/install/gems/post_install_spec.rb +47 -0
  115. data/spec/install/gems/resolving_spec.rb +72 -0
  116. data/spec/install/gems/simple_case_spec.rb +814 -0
  117. data/spec/install/gems/standalone_spec.rb +260 -0
  118. data/spec/install/gems/sudo_spec.rb +74 -0
  119. data/spec/install/gems/win32_spec.rb +26 -0
  120. data/spec/install/gemspec_spec.rb +170 -0
  121. data/spec/install/git_spec.rb +796 -0
  122. data/spec/install/invalid_spec.rb +35 -0
  123. data/spec/install/path_spec.rb +405 -0
  124. data/spec/install/upgrade_spec.rb +26 -0
  125. data/spec/lock/git_spec.rb +35 -0
  126. data/spec/lock/lockfile_spec.rb +809 -0
  127. data/spec/other/check_spec.rb +265 -0
  128. data/spec/other/clean_spec.rb +492 -0
  129. data/spec/other/config_spec.rb +138 -0
  130. data/spec/other/console_spec.rb +54 -0
  131. data/spec/other/exec_spec.rb +229 -0
  132. data/spec/other/ext_spec.rb +37 -0
  133. data/spec/other/help_spec.rb +39 -0
  134. data/spec/other/init_spec.rb +40 -0
  135. data/spec/other/newgem_spec.rb +87 -0
  136. data/spec/other/open_spec.rb +35 -0
  137. data/spec/other/outdated_spec.rb +93 -0
  138. data/spec/other/platform_spec.rb +881 -0
  139. data/spec/other/show_spec.rb +88 -0
  140. data/spec/quality_spec.rb +62 -0
  141. data/spec/realworld/edgecases_spec.rb +177 -0
  142. data/spec/resolver/basic_spec.rb +20 -0
  143. data/spec/resolver/platform_spec.rb +82 -0
  144. data/spec/runtime/executable_spec.rb +120 -0
  145. data/spec/runtime/load_spec.rb +107 -0
  146. data/spec/runtime/platform_spec.rb +90 -0
  147. data/spec/runtime/require_spec.rb +261 -0
  148. data/spec/runtime/setup_spec.rb +755 -0
  149. data/spec/runtime/with_clean_env_spec.rb +80 -0
  150. data/spec/spec_helper.rb +98 -0
  151. data/spec/support/artifice/endopint_marshal_fail_basic_authentication.rb +13 -0
  152. data/spec/support/artifice/endpoint.rb +54 -0
  153. data/spec/support/artifice/endpoint_500.rb +37 -0
  154. data/spec/support/artifice/endpoint_api_missing.rb +16 -0
  155. data/spec/support/artifice/endpoint_basic_authentication.rb +13 -0
  156. data/spec/support/artifice/endpoint_extra.rb +27 -0
  157. data/spec/support/artifice/endpoint_extra_missing.rb +15 -0
  158. data/spec/support/artifice/endpoint_fallback.rb +18 -0
  159. data/spec/support/artifice/endpoint_marshal_fail.rb +11 -0
  160. data/spec/support/artifice/endpoint_redirect.rb +15 -0
  161. data/spec/support/builders.rb +604 -0
  162. data/spec/support/fakeweb/rack-1.0.0.marshal +2 -0
  163. data/spec/support/fakeweb/windows.rb +23 -0
  164. data/spec/support/helpers.rb +317 -0
  165. data/spec/support/indexes.rb +112 -0
  166. data/spec/support/matchers.rb +77 -0
  167. data/spec/support/path.rb +73 -0
  168. data/spec/support/platforms.rb +86 -0
  169. data/spec/support/ruby_ext.rb +20 -0
  170. data/spec/support/rubygems_ext.rb +37 -0
  171. data/spec/support/rubygems_hax/platform.rb +22 -0
  172. data/spec/support/sudo.rb +21 -0
  173. data/spec/update/gems_spec.rb +134 -0
  174. data/spec/update/git_spec.rb +196 -0
  175. data/spec/update/source_spec.rb +51 -0
  176. metadata +338 -0
@@ -0,0 +1,137 @@
1
+ require 'tsort'
2
+ require 'forwardable'
3
+
4
+ module Bundler
5
+ class SpecSet
6
+ extend Forwardable
7
+ include TSort, Enumerable
8
+
9
+ def_delegators :@specs, :<<, :length, :add, :remove
10
+ def_delegators :sorted, :each
11
+
12
+ def initialize(specs)
13
+ @specs = specs.sort_by { |s| s.name }
14
+ end
15
+
16
+ def for(dependencies, skip = [], check = false, match_current_platform = false)
17
+ handled, deps, specs = {}, dependencies.dup, []
18
+ skip << 'bundler'
19
+
20
+ until deps.empty?
21
+ dep = deps.shift
22
+ next if handled[dep] || skip.include?(dep.name)
23
+
24
+ spec = lookup[dep.name].find do |s|
25
+ match_current_platform ?
26
+ Gem::Platform.match(s.platform) :
27
+ s.match_platform(dep.__platform)
28
+ end
29
+
30
+ handled[dep] = true
31
+
32
+ if spec
33
+ specs << spec
34
+
35
+ spec.dependencies.each do |d|
36
+ next if d.type == :development
37
+ d = DepProxy.new(d, dep.__platform) unless match_current_platform
38
+ deps << d
39
+ end
40
+ elsif check
41
+ return false
42
+ end
43
+ end
44
+
45
+ if spec = lookup['bundler'].first
46
+ specs << spec
47
+ end
48
+
49
+ check ? true : SpecSet.new(specs)
50
+ end
51
+
52
+ def valid_for?(deps)
53
+ self.for(deps, [], true)
54
+ end
55
+
56
+ def [](key)
57
+ key = key.name if key.respond_to?(:name)
58
+ lookup[key].reverse
59
+ end
60
+
61
+ def []=(key, value)
62
+ @specs << value
63
+ @lookup = nil
64
+ @sorted = nil
65
+ value
66
+ end
67
+
68
+ def sort!
69
+ self
70
+ end
71
+
72
+ def to_a
73
+ sorted.dup
74
+ end
75
+
76
+ def to_hash
77
+ lookup.dup
78
+ end
79
+
80
+ def materialize(deps, missing_specs = nil)
81
+ materialized = self.for(deps, [], false, true).to_a
82
+ deps = materialized.map {|s| s.name }.uniq
83
+ materialized.map! do |s|
84
+ next s unless s.is_a?(LazySpecification)
85
+ s.source.dependency_names = deps if s.source.respond_to?(:dependency_names=)
86
+ spec = s.__materialize__
87
+ if missing_specs
88
+ missing_specs << s unless spec
89
+ else
90
+ raise GemNotFound, "Could not find #{s.full_name} in any of the sources" unless spec
91
+ end
92
+ spec if spec
93
+ end
94
+ SpecSet.new(materialized.compact)
95
+ end
96
+
97
+ def merge(set)
98
+ arr = sorted.dup
99
+ set.each do |s|
100
+ next if arr.any? { |s2| s2.name == s.name && s2.version == s.version && s2.platform == s.platform }
101
+ arr << s
102
+ end
103
+ SpecSet.new(arr)
104
+ end
105
+
106
+ private
107
+
108
+ def sorted
109
+ rake = @specs.find { |s| s.name == 'rake' }
110
+ @sorted ||= ([rake] + tsort).compact.uniq
111
+ end
112
+
113
+ def lookup
114
+ @lookup ||= begin
115
+ lookup = Hash.new { |h,k| h[k] = [] }
116
+ specs = @specs.sort_by do |s|
117
+ s.platform.to_s == 'ruby' ? "\0" : s.platform.to_s
118
+ end
119
+ specs.reverse_each do |s|
120
+ lookup[s.name] << s
121
+ end
122
+ lookup
123
+ end
124
+ end
125
+
126
+ def tsort_each_node
127
+ @specs.each { |s| yield s }
128
+ end
129
+
130
+ def tsort_each_child(s)
131
+ s.dependencies.sort_by { |d| d.name }.each do |d|
132
+ next if d.type == :development
133
+ lookup[d.name].each { |s2| yield s2 }
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env <%= Bundler.settings[:shebang] || 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,12 @@
1
+ #!/usr/bin/env <%= Bundler.settings[:shebang] || 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
+ $:.unshift File.expand_path '../<%= standalone_path %>', __FILE__
10
+
11
+ require 'bundler/setup'
12
+ load File.expand_path '../<%= executable_path %>', __FILE__
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ # gem "rails"
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in <%=config[:name]%>.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) <%=Time.now.year%> <%=config[:author]%>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # <%=config[:constant_name]%>
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem '<%=config[:name]%>'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install <%=config[:name]%>
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require '<%= config[:name] %>'
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,9 @@
1
+ require "<%=config[:name]%>/version"
2
+
3
+ <%- config[:constant_array].each_with_index do |c,i| -%>
4
+ <%= ' '*i %>module <%= c %>
5
+ <%- end -%>
6
+ <%= ' '*config[:constant_array].size %># Your code goes here...
7
+ <%- (config[:constant_array].size-1).downto(0) do |i| -%>
8
+ <%= ' '*i %>end
9
+ <%- 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,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/<%=config[:name]%>/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = [<%=config[:author].inspect%>]
6
+ gem.email = [<%=config[:email].inspect%>]
7
+ gem.description = %q{TODO: Write a gem description}
8
+ gem.summary = %q{TODO: Write a gem summary}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = <%=config[:name].inspect%>
15
+ gem.require_paths = ["lib"]
16
+ gem.version = <%=config[:constant_name]%>::VERSION
17
+ end
@@ -0,0 +1,88 @@
1
+ require 'rubygems/user_interaction'
2
+
3
+ module Bundler
4
+ class UI
5
+ def warn(message, newline = nil)
6
+ end
7
+
8
+ def debug(message, newline = nil)
9
+ end
10
+
11
+ def error(message, newline = nil)
12
+ end
13
+
14
+ def info(message, newline = nil)
15
+ end
16
+
17
+ def confirm(message, newline = nil)
18
+ end
19
+
20
+ def debug?
21
+ false
22
+ end
23
+
24
+ class Shell < UI
25
+ attr_writer :shell
26
+
27
+ def initialize(shell)
28
+ @shell = shell
29
+ @quiet = false
30
+ @debug = ENV['DEBUG']
31
+ end
32
+
33
+ def info(msg, newline = nil)
34
+ tell_me(msg, nil, newline) if !@quiet
35
+ end
36
+
37
+ def confirm(msg, newline = nil)
38
+ tell_me(msg, :green, newline) if !@quiet
39
+ end
40
+
41
+ def warn(msg, newline = nil)
42
+ tell_me(msg, :yellow, newline)
43
+ end
44
+
45
+ def error(msg, newline = nil)
46
+ tell_me(msg, :red, newline)
47
+ end
48
+
49
+ def be_quiet!
50
+ @quiet = true
51
+ end
52
+
53
+ def debug?
54
+ # needs to be false instead of nil to be newline param to other methods
55
+ !!@debug && !@quiet
56
+ end
57
+
58
+ def debug!
59
+ @debug = true
60
+ end
61
+
62
+ def debug(msg, newline = nil)
63
+ tell_me(msg, nil, newline) if debug?
64
+ end
65
+
66
+ private
67
+ # valimism
68
+ def tell_me(msg, color = nil, newline = nil)
69
+ newline.nil? ? @shell.say(msg, color) : @shell.say(msg, color, newline)
70
+ end
71
+ end
72
+
73
+ class RGProxy < ::Gem::SilentUI
74
+ def initialize(ui)
75
+ @ui = ui
76
+ super()
77
+ end
78
+
79
+ def say(message)
80
+ if message =~ /native extensions/
81
+ @ui.info "with native extensions "
82
+ else
83
+ @ui.debug(message)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ 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, 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'
27
+
@@ -0,0 +1,468 @@
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
+ if uri.user or uri.password
352
+ req.basic_auth uri.user, uri.password
353
+ end
354
+
355
+ req.add_field 'Connection', 'keep-alive'
356
+ req.add_field 'Keep-Alive', @keep_alive
357
+
358
+ connection = connection_for uri
359
+ connection_id = connection.object_id
360
+
361
+ begin
362
+ Thread.current[@request_key][connection_id] += 1
363
+ response = connection.request req, &block
364
+
365
+ rescue Net::HTTPBadResponse => e
366
+ message = error_message connection
367
+
368
+ finish connection
369
+
370
+ raise Error, "too many bad responses #{message}" if
371
+ bad_response or not idempotent? req
372
+
373
+ bad_response = true
374
+ retry
375
+ rescue IOError, EOFError, Timeout::Error,
376
+ Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE => e
377
+
378
+ if retried or not idempotent? req
379
+ due_to = "(due to #{e.message} - #{e.class})"
380
+ message = error_message connection
381
+
382
+ finish connection
383
+
384
+ raise Error, "too many connection resets #{due_to} #{message}"
385
+ end
386
+
387
+ reset connection
388
+
389
+ retried = true
390
+ retry
391
+ end
392
+
393
+ @http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
394
+
395
+ response
396
+ end
397
+
398
+ ##
399
+ # Shuts down all connections for +thread+.
400
+ #
401
+ # Uses the current thread by default.
402
+ #
403
+ # If you've used Net::HTTP::Persistent across multiple threads you should
404
+ # call this in each thread when you're done making HTTP requests.
405
+ #
406
+ # *NOTE*: Calling shutdown for another thread can be dangerous!
407
+ #
408
+ # If the thread is still using the connection it may cause an error! It is
409
+ # best to call #shutdown in the thread at the appropriate time instead!
410
+
411
+ def shutdown thread = Thread.current
412
+ connections = thread[@connection_key]
413
+
414
+ connections.each do |_, connection|
415
+ begin
416
+ connection.finish
417
+ rescue IOError
418
+ end
419
+ end if connections
420
+
421
+ thread[@connection_key] = nil
422
+ thread[@request_key] = nil
423
+ end
424
+
425
+ ##
426
+ # Shuts down all connections in all threads
427
+ #
428
+ # *NOTE*: THIS METHOD IS VERY DANGEROUS!
429
+ #
430
+ # Do not call this method if other threads are still using their
431
+ # connections! Call #shutdown at the appropriate time instead!
432
+ #
433
+ # Use this method only as a last resort!
434
+
435
+ def shutdown_in_all_threads
436
+ Thread.list.each do |thread|
437
+ shutdown thread
438
+ end
439
+
440
+ nil
441
+ end
442
+
443
+ ##
444
+ # Enables SSL on +connection+
445
+
446
+ def ssl connection
447
+ require 'net/https'
448
+ connection.use_ssl = true
449
+
450
+ # suppress warning but allow override
451
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @verify_mode
452
+
453
+ if @ca_file then
454
+ connection.ca_file = @ca_file
455
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
456
+ connection.verify_callback = @verify_callback if @verify_callback
457
+ end
458
+
459
+ if @certificate and @private_key then
460
+ connection.cert = @certificate
461
+ connection.key = @private_key
462
+ end
463
+
464
+ connection.verify_mode = @verify_mode if @verify_mode
465
+ end
466
+
467
+ end
468
+