rubygems-update 3.5.5 → 3.5.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +107 -0
  3. data/Manifest.txt +84 -64
  4. data/POLICIES.md +75 -6
  5. data/bundler/CHANGELOG.md +75 -0
  6. data/bundler/lib/bundler/build_metadata.rb +2 -2
  7. data/bundler/lib/bundler/cli/binstubs.rb +1 -1
  8. data/bundler/lib/bundler/cli/gem.rb +1 -1
  9. data/bundler/lib/bundler/cli/lock.rb +5 -4
  10. data/bundler/lib/bundler/cli/plugin.rb +3 -2
  11. data/bundler/lib/bundler/cli.rb +7 -24
  12. data/bundler/lib/bundler/definition.rb +111 -56
  13. data/bundler/lib/bundler/dependency.rb +2 -1
  14. data/bundler/lib/bundler/dsl.rb +16 -1
  15. data/bundler/lib/bundler/environment_preserver.rb +5 -23
  16. data/bundler/lib/bundler/fetcher/downloader.rb +1 -1
  17. data/bundler/lib/bundler/fetcher.rb +2 -2
  18. data/bundler/lib/bundler/gem_version_promoter.rb +42 -38
  19. data/bundler/lib/bundler/injector.rb +3 -2
  20. data/bundler/lib/bundler/installer.rb +2 -2
  21. data/bundler/lib/bundler/man/bundle-add.1 +1 -1
  22. data/bundler/lib/bundler/man/bundle-binstubs.1 +1 -1
  23. data/bundler/lib/bundler/man/bundle-cache.1 +1 -1
  24. data/bundler/lib/bundler/man/bundle-check.1 +3 -1
  25. data/bundler/lib/bundler/man/bundle-check.1.ronn +3 -0
  26. data/bundler/lib/bundler/man/bundle-clean.1 +1 -1
  27. data/bundler/lib/bundler/man/bundle-config.1 +1 -3
  28. data/bundler/lib/bundler/man/bundle-config.1.ronn +0 -3
  29. data/bundler/lib/bundler/man/bundle-console.1 +1 -1
  30. data/bundler/lib/bundler/man/bundle-doctor.1 +1 -1
  31. data/bundler/lib/bundler/man/bundle-exec.1 +1 -1
  32. data/bundler/lib/bundler/man/bundle-gem.1 +1 -1
  33. data/bundler/lib/bundler/man/bundle-help.1 +1 -1
  34. data/bundler/lib/bundler/man/bundle-info.1 +1 -1
  35. data/bundler/lib/bundler/man/bundle-init.1 +1 -1
  36. data/bundler/lib/bundler/man/bundle-inject.1 +1 -1
  37. data/bundler/lib/bundler/man/bundle-install.1 +3 -3
  38. data/bundler/lib/bundler/man/bundle-install.1.ronn +2 -2
  39. data/bundler/lib/bundler/man/bundle-list.1 +1 -1
  40. data/bundler/lib/bundler/man/bundle-lock.1 +1 -1
  41. data/bundler/lib/bundler/man/bundle-open.1 +1 -1
  42. data/bundler/lib/bundler/man/bundle-outdated.1 +1 -1
  43. data/bundler/lib/bundler/man/bundle-platform.1 +1 -1
  44. data/bundler/lib/bundler/man/bundle-plugin.1 +7 -4
  45. data/bundler/lib/bundler/man/bundle-plugin.1.ronn +7 -3
  46. data/bundler/lib/bundler/man/bundle-pristine.1 +1 -1
  47. data/bundler/lib/bundler/man/bundle-remove.1 +1 -1
  48. data/bundler/lib/bundler/man/bundle-show.1 +1 -1
  49. data/bundler/lib/bundler/man/bundle-update.1 +1 -1
  50. data/bundler/lib/bundler/man/bundle-version.1 +1 -1
  51. data/bundler/lib/bundler/man/bundle-viz.1 +1 -1
  52. data/bundler/lib/bundler/man/bundle.1 +1 -1
  53. data/bundler/lib/bundler/man/gemfile.5 +3 -3
  54. data/bundler/lib/bundler/man/gemfile.5.ronn +2 -2
  55. data/bundler/lib/bundler/mirror.rb +3 -3
  56. data/bundler/lib/bundler/plugin/api/source.rb +2 -2
  57. data/bundler/lib/bundler/plugin/installer/path.rb +18 -0
  58. data/bundler/lib/bundler/plugin/installer.rb +36 -16
  59. data/bundler/lib/bundler/plugin/source_list.rb +4 -4
  60. data/bundler/lib/bundler/resolver/candidate.rb +1 -1
  61. data/bundler/lib/bundler/resolver.rb +54 -24
  62. data/bundler/lib/bundler/rubygems_ext.rb +12 -0
  63. data/bundler/lib/bundler/runtime.rb +1 -1
  64. data/bundler/lib/bundler/self_manager.rb +1 -1
  65. data/bundler/lib/bundler/settings.rb +14 -12
  66. data/bundler/lib/bundler/setup.rb +3 -0
  67. data/bundler/lib/bundler/source/git/git_proxy.rb +1 -1
  68. data/bundler/lib/bundler/source/git.rb +1 -1
  69. data/bundler/lib/bundler/source/rubygems/remote.rb +1 -1
  70. data/bundler/lib/bundler/source/rubygems.rb +7 -20
  71. data/bundler/lib/bundler/source_list.rb +17 -4
  72. data/bundler/lib/bundler/spec_set.rb +1 -1
  73. data/bundler/lib/bundler/templates/newgem/newgem.gemspec.tt +4 -3
  74. data/bundler/lib/bundler/templates/newgem/rubocop.yml.tt +0 -5
  75. data/bundler/lib/bundler/uri_credentials_filter.rb +2 -2
  76. data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb +22 -22
  77. data/bundler/lib/bundler/vendor/pub_grub/lib/pub_grub/static_package_source.rb +1 -0
  78. data/bundler/lib/bundler/vendored_net_http.rb +7 -3
  79. data/bundler/lib/bundler/vendored_timeout.rb +7 -3
  80. data/bundler/lib/bundler/vendored_uri.rb +18 -1
  81. data/bundler/lib/bundler/version.rb +1 -1
  82. data/bundler/lib/bundler/yaml_serializer.rb +12 -0
  83. data/bundler/lib/bundler.rb +23 -2
  84. data/lib/rubygems/command.rb +1 -1
  85. data/lib/rubygems/command_manager.rb +2 -1
  86. data/lib/rubygems/commands/build_command.rb +2 -11
  87. data/lib/rubygems/commands/help_command.rb +2 -2
  88. data/lib/rubygems/commands/rdoc_command.rb +1 -8
  89. data/lib/rubygems/commands/rebuild_command.rb +264 -0
  90. data/lib/rubygems/commands/sources_command.rb +2 -2
  91. data/lib/rubygems/commands/update_command.rb +8 -9
  92. data/lib/rubygems/config_file.rb +20 -8
  93. data/lib/rubygems/defaults.rb +4 -4
  94. data/lib/rubygems/dependency.rb +2 -2
  95. data/lib/rubygems/dependency_list.rb +1 -1
  96. data/lib/rubygems/deprecate.rb +79 -77
  97. data/lib/rubygems/ext/cargo_builder.rb +1 -1
  98. data/lib/rubygems/gemcutter_utilities/webauthn_listener.rb +1 -1
  99. data/lib/rubygems/gemcutter_utilities/webauthn_poller.rb +3 -1
  100. data/lib/rubygems/gemcutter_utilities.rb +40 -23
  101. data/lib/rubygems/gemspec_helpers.rb +19 -0
  102. data/lib/rubygems/local_remote_options.rb +6 -6
  103. data/lib/rubygems/package.rb +13 -7
  104. data/lib/rubygems/remote_fetcher.rb +3 -3
  105. data/lib/rubygems/request.rb +5 -5
  106. data/lib/rubygems/request_set.rb +1 -1
  107. data/lib/rubygems/requirement.rb +5 -0
  108. data/lib/rubygems/resolver/api_set.rb +1 -1
  109. data/lib/rubygems/resolver/best_set.rb +1 -1
  110. data/lib/rubygems/resolver/spec_specification.rb +7 -0
  111. data/lib/rubygems/resolver.rb +5 -5
  112. data/lib/rubygems/s3_uri_signer.rb +3 -3
  113. data/lib/rubygems/safe_yaml.rb +10 -1
  114. data/lib/rubygems/security.rb +1 -1
  115. data/lib/rubygems/source/git.rb +2 -2
  116. data/lib/rubygems/source_list.rb +1 -1
  117. data/lib/rubygems/specification.rb +6 -6
  118. data/lib/rubygems/specification_policy.rb +15 -7
  119. data/lib/rubygems/uri.rb +6 -6
  120. data/lib/rubygems/util/licenses.rb +43 -0
  121. data/lib/rubygems/util.rb +1 -1
  122. data/lib/rubygems/vendor/molinillo/lib/molinillo/delegates/resolution_state.rb +57 -0
  123. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/delegates/specification_provider.rb +11 -11
  124. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/action.rb +1 -1
  125. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb +1 -1
  126. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/add_vertex.rb +1 -1
  127. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/delete_edge.rb +1 -1
  128. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb +1 -1
  129. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/log.rb +1 -1
  130. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/set_payload.rb +1 -1
  131. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/tag.rb +1 -1
  132. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph/vertex.rb +1 -1
  133. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/dependency_graph.rb +2 -2
  134. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/errors.rb +1 -1
  135. data/lib/rubygems/vendor/molinillo/lib/molinillo/gem_metadata.rb +6 -0
  136. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/modules/specification_provider.rb +2 -2
  137. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/modules/ui.rb +1 -1
  138. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/resolution.rb +3 -3
  139. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/resolver.rb +1 -1
  140. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo/state.rb +1 -1
  141. data/lib/rubygems/{resolver → vendor}/molinillo/lib/molinillo.rb +2 -2
  142. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/generic_request.rb +9 -9
  143. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/header.rb +2 -2
  144. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/request.rb +3 -3
  145. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/requests.rb +30 -30
  146. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/response.rb +2 -2
  147. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/responses.rb +6 -6
  148. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/status.rb +1 -1
  149. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http.rb +15 -15
  150. data/lib/rubygems/{net-http → vendor/net-http}/lib/net/https.rb +1 -1
  151. data/lib/rubygems/vendor/optparse/lib/optparse/uri.rb +7 -0
  152. data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse.rb +11 -11
  153. data/lib/rubygems/{timeout → vendor/timeout}/lib/timeout.rb +1 -1
  154. data/lib/rubygems/{tsort → vendor/tsort}/lib/tsort.rb +2 -2
  155. data/lib/rubygems/vendor/uri/.document +1 -0
  156. data/lib/rubygems/vendor/uri/LICENSE.txt +22 -0
  157. data/lib/rubygems/vendor/uri/lib/uri/common.rb +853 -0
  158. data/lib/rubygems/vendor/uri/lib/uri/file.rb +100 -0
  159. data/lib/rubygems/vendor/uri/lib/uri/ftp.rb +267 -0
  160. data/lib/rubygems/vendor/uri/lib/uri/generic.rb +1588 -0
  161. data/lib/rubygems/vendor/uri/lib/uri/http.rb +125 -0
  162. data/lib/rubygems/vendor/uri/lib/uri/https.rb +23 -0
  163. data/lib/rubygems/vendor/uri/lib/uri/ldap.rb +261 -0
  164. data/lib/rubygems/vendor/uri/lib/uri/ldaps.rb +22 -0
  165. data/lib/rubygems/vendor/uri/lib/uri/mailto.rb +293 -0
  166. data/lib/rubygems/vendor/uri/lib/uri/rfc2396_parser.rb +539 -0
  167. data/lib/rubygems/vendor/uri/lib/uri/rfc3986_parser.rb +183 -0
  168. data/lib/rubygems/vendor/uri/lib/uri/version.rb +6 -0
  169. data/lib/rubygems/vendor/uri/lib/uri/ws.rb +83 -0
  170. data/lib/rubygems/vendor/uri/lib/uri/wss.rb +23 -0
  171. data/lib/rubygems/vendor/uri/lib/uri.rb +104 -0
  172. data/lib/rubygems/vendored_molinillo.rb +3 -0
  173. data/lib/rubygems/vendored_net_http.rb +5 -0
  174. data/lib/rubygems/vendored_optparse.rb +3 -0
  175. data/lib/rubygems/vendored_timeout.rb +5 -0
  176. data/lib/rubygems/vendored_tsort.rb +3 -0
  177. data/lib/rubygems/yaml_serializer.rb +12 -0
  178. data/lib/rubygems.rb +2 -2
  179. data/rubygems-update.gemspec +7 -2
  180. metadata +90 -67
  181. data/lib/rubygems/net/http.rb +0 -3
  182. data/lib/rubygems/optparse/lib/optparse/uri.rb +0 -7
  183. data/lib/rubygems/optparse.rb +0 -3
  184. data/lib/rubygems/resolver/molinillo/lib/molinillo/delegates/resolution_state.rb +0 -57
  185. data/lib/rubygems/resolver/molinillo/lib/molinillo/gem_metadata.rb +0 -6
  186. data/lib/rubygems/resolver/molinillo.rb +0 -3
  187. data/lib/rubygems/timeout.rb +0 -3
  188. data/lib/rubygems/tsort.rb +0 -3
  189. /data/lib/rubygems/{net-http → vendor/molinillo}/.document +0 -0
  190. /data/lib/rubygems/{resolver → vendor}/molinillo/LICENSE +0 -0
  191. /data/lib/rubygems/{net-protocol → vendor/net-http}/.document +0 -0
  192. /data/lib/rubygems/{net-http → vendor/net-http}/LICENSE.txt +0 -0
  193. /data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/backward.rb +0 -0
  194. /data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/exceptions.rb +0 -0
  195. /data/lib/rubygems/{net-http → vendor/net-http}/lib/net/http/proxy_delta.rb +0 -0
  196. /data/lib/rubygems/{optparse → vendor/net-protocol}/.document +0 -0
  197. /data/lib/rubygems/{net-protocol → vendor/net-protocol}/LICENSE.txt +0 -0
  198. /data/lib/rubygems/{net-protocol → vendor/net-protocol}/lib/net/protocol.rb +0 -0
  199. /data/lib/rubygems/{resolv → vendor/optparse}/.document +0 -0
  200. /data/lib/rubygems/{optparse → vendor/optparse}/COPYING +0 -0
  201. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optionparser.rb +0 -0
  202. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse/ac.rb +0 -0
  203. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse/date.rb +0 -0
  204. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse/kwargs.rb +0 -0
  205. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse/shellwords.rb +0 -0
  206. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse/time.rb +0 -0
  207. /data/lib/rubygems/{optparse → vendor/optparse}/lib/optparse/version.rb +0 -0
  208. /data/lib/rubygems/{resolver/molinillo → vendor/resolv}/.document +0 -0
  209. /data/lib/rubygems/{resolv → vendor/resolv}/LICENSE.txt +0 -0
  210. /data/lib/rubygems/{resolv → vendor/resolv}/lib/resolv.rb +0 -0
  211. /data/lib/rubygems/{timeout → vendor/timeout}/.document +0 -0
  212. /data/lib/rubygems/{timeout → vendor/timeout}/LICENSE.txt +0 -0
  213. /data/lib/rubygems/{tsort → vendor/tsort}/.document +0 -0
  214. /data/lib/rubygems/{tsort → vendor/tsort}/LICENSE.txt +0 -0
@@ -0,0 +1,1588 @@
1
+ # frozen_string_literal: true
2
+
3
+ # = uri/generic.rb
4
+ #
5
+ # Author:: Akira Yamada <akira@ruby-lang.org>
6
+ # License:: You can redistribute it and/or modify it under the same term as Ruby.
7
+ #
8
+ # See Gem::URI for general documentation
9
+ #
10
+
11
+ require_relative 'common'
12
+ autoload :IPSocket, 'socket'
13
+ autoload :IPAddr, 'ipaddr'
14
+
15
+ module Gem::URI
16
+
17
+ #
18
+ # Base class for all Gem::URI classes.
19
+ # Implements generic Gem::URI syntax as per RFC 2396.
20
+ #
21
+ class Generic
22
+ include Gem::URI
23
+
24
+ #
25
+ # A Default port of nil for Gem::URI::Generic.
26
+ #
27
+ DEFAULT_PORT = nil
28
+
29
+ #
30
+ # Returns default port.
31
+ #
32
+ def self.default_port
33
+ self::DEFAULT_PORT
34
+ end
35
+
36
+ #
37
+ # Returns default port.
38
+ #
39
+ def default_port
40
+ self.class.default_port
41
+ end
42
+
43
+ #
44
+ # An Array of the available components for Gem::URI::Generic.
45
+ #
46
+ COMPONENT = [
47
+ :scheme,
48
+ :userinfo, :host, :port, :registry,
49
+ :path, :opaque,
50
+ :query,
51
+ :fragment
52
+ ].freeze
53
+
54
+ #
55
+ # Components of the Gem::URI in the order.
56
+ #
57
+ def self.component
58
+ self::COMPONENT
59
+ end
60
+
61
+ USE_REGISTRY = false # :nodoc:
62
+
63
+ def self.use_registry # :nodoc:
64
+ self::USE_REGISTRY
65
+ end
66
+
67
+ #
68
+ # == Synopsis
69
+ #
70
+ # See ::new.
71
+ #
72
+ # == Description
73
+ #
74
+ # At first, tries to create a new Gem::URI::Generic instance using
75
+ # Gem::URI::Generic::build. But, if exception Gem::URI::InvalidComponentError is raised,
76
+ # then it does Gem::URI::Escape.escape all Gem::URI components and tries again.
77
+ #
78
+ def self.build2(args)
79
+ begin
80
+ return self.build(args)
81
+ rescue InvalidComponentError
82
+ if args.kind_of?(Array)
83
+ return self.build(args.collect{|x|
84
+ if x.is_a?(String)
85
+ DEFAULT_PARSER.escape(x)
86
+ else
87
+ x
88
+ end
89
+ })
90
+ elsif args.kind_of?(Hash)
91
+ tmp = {}
92
+ args.each do |key, value|
93
+ tmp[key] = if value
94
+ DEFAULT_PARSER.escape(value)
95
+ else
96
+ value
97
+ end
98
+ end
99
+ return self.build(tmp)
100
+ end
101
+ end
102
+ end
103
+
104
+ #
105
+ # == Synopsis
106
+ #
107
+ # See ::new.
108
+ #
109
+ # == Description
110
+ #
111
+ # Creates a new Gem::URI::Generic instance from components of Gem::URI::Generic
112
+ # with check. Components are: scheme, userinfo, host, port, registry, path,
113
+ # opaque, query, and fragment. You can provide arguments either by an Array or a Hash.
114
+ # See ::new for hash keys to use or for order of array items.
115
+ #
116
+ def self.build(args)
117
+ if args.kind_of?(Array) &&
118
+ args.size == ::Gem::URI::Generic::COMPONENT.size
119
+ tmp = args.dup
120
+ elsif args.kind_of?(Hash)
121
+ tmp = ::Gem::URI::Generic::COMPONENT.collect do |c|
122
+ if args.include?(c)
123
+ args[c]
124
+ else
125
+ nil
126
+ end
127
+ end
128
+ else
129
+ component = self.class.component rescue ::Gem::URI::Generic::COMPONENT
130
+ raise ArgumentError,
131
+ "expected Array of or Hash of components of #{self.class} (#{component.join(', ')})"
132
+ end
133
+
134
+ tmp << nil
135
+ tmp << true
136
+ return self.new(*tmp)
137
+ end
138
+
139
+ #
140
+ # == Args
141
+ #
142
+ # +scheme+::
143
+ # Protocol scheme, i.e. 'http','ftp','mailto' and so on.
144
+ # +userinfo+::
145
+ # User name and password, i.e. 'sdmitry:bla'.
146
+ # +host+::
147
+ # Server host name.
148
+ # +port+::
149
+ # Server port.
150
+ # +registry+::
151
+ # Registry of naming authorities.
152
+ # +path+::
153
+ # Path on server.
154
+ # +opaque+::
155
+ # Opaque part.
156
+ # +query+::
157
+ # Query data.
158
+ # +fragment+::
159
+ # Part of the Gem::URI after '#' character.
160
+ # +parser+::
161
+ # Parser for internal use [Gem::URI::DEFAULT_PARSER by default].
162
+ # +arg_check+::
163
+ # Check arguments [false by default].
164
+ #
165
+ # == Description
166
+ #
167
+ # Creates a new Gem::URI::Generic instance from ``generic'' components without check.
168
+ #
169
+ def initialize(scheme,
170
+ userinfo, host, port, registry,
171
+ path, opaque,
172
+ query,
173
+ fragment,
174
+ parser = DEFAULT_PARSER,
175
+ arg_check = false)
176
+ @scheme = nil
177
+ @user = nil
178
+ @password = nil
179
+ @host = nil
180
+ @port = nil
181
+ @path = nil
182
+ @query = nil
183
+ @opaque = nil
184
+ @fragment = nil
185
+ @parser = parser == DEFAULT_PARSER ? nil : parser
186
+
187
+ if arg_check
188
+ self.scheme = scheme
189
+ self.userinfo = userinfo
190
+ self.hostname = host
191
+ self.port = port
192
+ self.path = path
193
+ self.query = query
194
+ self.opaque = opaque
195
+ self.fragment = fragment
196
+ else
197
+ self.set_scheme(scheme)
198
+ self.set_userinfo(userinfo)
199
+ self.set_host(host)
200
+ self.set_port(port)
201
+ self.set_path(path)
202
+ self.query = query
203
+ self.set_opaque(opaque)
204
+ self.fragment=(fragment)
205
+ end
206
+ if registry
207
+ raise InvalidURIError,
208
+ "the scheme #{@scheme} does not accept registry part: #{registry} (or bad hostname?)"
209
+ end
210
+
211
+ @scheme&.freeze
212
+ self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2)
213
+ self.set_port(self.default_port) if self.default_port && !@port
214
+ end
215
+
216
+ #
217
+ # Returns the scheme component of the Gem::URI.
218
+ #
219
+ # Gem::URI("http://foo/bar/baz").scheme #=> "http"
220
+ #
221
+ attr_reader :scheme
222
+
223
+ # Returns the host component of the Gem::URI.
224
+ #
225
+ # Gem::URI("http://foo/bar/baz").host #=> "foo"
226
+ #
227
+ # It returns nil if no host component exists.
228
+ #
229
+ # Gem::URI("mailto:foo@example.org").host #=> nil
230
+ #
231
+ # The component does not contain the port number.
232
+ #
233
+ # Gem::URI("http://foo:8080/bar/baz").host #=> "foo"
234
+ #
235
+ # Since IPv6 addresses are wrapped with brackets in URIs,
236
+ # this method returns IPv6 addresses wrapped with brackets.
237
+ # This form is not appropriate to pass to socket methods such as TCPSocket.open.
238
+ # If unwrapped host names are required, use the #hostname method.
239
+ #
240
+ # Gem::URI("http://[::1]/bar/baz").host #=> "[::1]"
241
+ # Gem::URI("http://[::1]/bar/baz").hostname #=> "::1"
242
+ #
243
+ attr_reader :host
244
+
245
+ # Returns the port component of the Gem::URI.
246
+ #
247
+ # Gem::URI("http://foo/bar/baz").port #=> 80
248
+ # Gem::URI("http://foo:8080/bar/baz").port #=> 8080
249
+ #
250
+ attr_reader :port
251
+
252
+ def registry # :nodoc:
253
+ nil
254
+ end
255
+
256
+ # Returns the path component of the Gem::URI.
257
+ #
258
+ # Gem::URI("http://foo/bar/baz").path #=> "/bar/baz"
259
+ #
260
+ attr_reader :path
261
+
262
+ # Returns the query component of the Gem::URI.
263
+ #
264
+ # Gem::URI("http://foo/bar/baz?search=FooBar").query #=> "search=FooBar"
265
+ #
266
+ attr_reader :query
267
+
268
+ # Returns the opaque part of the Gem::URI.
269
+ #
270
+ # Gem::URI("mailto:foo@example.org").opaque #=> "foo@example.org"
271
+ # Gem::URI("http://foo/bar/baz").opaque #=> nil
272
+ #
273
+ # The portion of the path that does not make use of the slash '/'.
274
+ # The path typically refers to an absolute path or an opaque part.
275
+ # (See RFC2396 Section 3 and 5.2.)
276
+ #
277
+ attr_reader :opaque
278
+
279
+ # Returns the fragment component of the Gem::URI.
280
+ #
281
+ # Gem::URI("http://foo/bar/baz?search=FooBar#ponies").fragment #=> "ponies"
282
+ #
283
+ attr_reader :fragment
284
+
285
+ # Returns the parser to be used.
286
+ #
287
+ # Unless a Gem::URI::Parser is defined, DEFAULT_PARSER is used.
288
+ #
289
+ def parser
290
+ if !defined?(@parser) || !@parser
291
+ DEFAULT_PARSER
292
+ else
293
+ @parser || DEFAULT_PARSER
294
+ end
295
+ end
296
+
297
+ # Replaces self by other Gem::URI object.
298
+ #
299
+ def replace!(oth)
300
+ if self.class != oth.class
301
+ raise ArgumentError, "expected #{self.class} object"
302
+ end
303
+
304
+ component.each do |c|
305
+ self.__send__("#{c}=", oth.__send__(c))
306
+ end
307
+ end
308
+ private :replace!
309
+
310
+ #
311
+ # Components of the Gem::URI in the order.
312
+ #
313
+ def component
314
+ self.class.component
315
+ end
316
+
317
+ #
318
+ # Checks the scheme +v+ component against the Gem::URI::Parser Regexp for :SCHEME.
319
+ #
320
+ def check_scheme(v)
321
+ if v && parser.regexp[:SCHEME] !~ v
322
+ raise InvalidComponentError,
323
+ "bad component(expected scheme component): #{v}"
324
+ end
325
+
326
+ return true
327
+ end
328
+ private :check_scheme
329
+
330
+ # Protected setter for the scheme component +v+.
331
+ #
332
+ # See also Gem::URI::Generic.scheme=.
333
+ #
334
+ def set_scheme(v)
335
+ @scheme = v&.downcase
336
+ end
337
+ protected :set_scheme
338
+
339
+ #
340
+ # == Args
341
+ #
342
+ # +v+::
343
+ # String
344
+ #
345
+ # == Description
346
+ #
347
+ # Public setter for the scheme component +v+
348
+ # (with validation).
349
+ #
350
+ # See also Gem::URI::Generic.check_scheme.
351
+ #
352
+ # == Usage
353
+ #
354
+ # require 'rubygems/vendor/uri/lib/uri'
355
+ #
356
+ # uri = Gem::URI.parse("http://my.example.com")
357
+ # uri.scheme = "https"
358
+ # uri.to_s #=> "https://my.example.com"
359
+ #
360
+ def scheme=(v)
361
+ check_scheme(v)
362
+ set_scheme(v)
363
+ v
364
+ end
365
+
366
+ #
367
+ # Checks the +user+ and +password+.
368
+ #
369
+ # If +password+ is not provided, then +user+ is
370
+ # split, using Gem::URI::Generic.split_userinfo, to
371
+ # pull +user+ and +password.
372
+ #
373
+ # See also Gem::URI::Generic.check_user, Gem::URI::Generic.check_password.
374
+ #
375
+ def check_userinfo(user, password = nil)
376
+ if !password
377
+ user, password = split_userinfo(user)
378
+ end
379
+ check_user(user)
380
+ check_password(password, user)
381
+
382
+ return true
383
+ end
384
+ private :check_userinfo
385
+
386
+ #
387
+ # Checks the user +v+ component for RFC2396 compliance
388
+ # and against the Gem::URI::Parser Regexp for :USERINFO.
389
+ #
390
+ # Can not have a registry or opaque component defined,
391
+ # with a user component defined.
392
+ #
393
+ def check_user(v)
394
+ if @opaque
395
+ raise InvalidURIError,
396
+ "can not set user with opaque"
397
+ end
398
+
399
+ return v unless v
400
+
401
+ if parser.regexp[:USERINFO] !~ v
402
+ raise InvalidComponentError,
403
+ "bad component(expected userinfo component or user component): #{v}"
404
+ end
405
+
406
+ return true
407
+ end
408
+ private :check_user
409
+
410
+ #
411
+ # Checks the password +v+ component for RFC2396 compliance
412
+ # and against the Gem::URI::Parser Regexp for :USERINFO.
413
+ #
414
+ # Can not have a registry or opaque component defined,
415
+ # with a user component defined.
416
+ #
417
+ def check_password(v, user = @user)
418
+ if @opaque
419
+ raise InvalidURIError,
420
+ "can not set password with opaque"
421
+ end
422
+ return v unless v
423
+
424
+ if !user
425
+ raise InvalidURIError,
426
+ "password component depends user component"
427
+ end
428
+
429
+ if parser.regexp[:USERINFO] !~ v
430
+ raise InvalidComponentError,
431
+ "bad password component"
432
+ end
433
+
434
+ return true
435
+ end
436
+ private :check_password
437
+
438
+ #
439
+ # Sets userinfo, argument is string like 'name:pass'.
440
+ #
441
+ def userinfo=(userinfo)
442
+ if userinfo.nil?
443
+ return nil
444
+ end
445
+ check_userinfo(*userinfo)
446
+ set_userinfo(*userinfo)
447
+ # returns userinfo
448
+ end
449
+
450
+ #
451
+ # == Args
452
+ #
453
+ # +v+::
454
+ # String
455
+ #
456
+ # == Description
457
+ #
458
+ # Public setter for the +user+ component
459
+ # (with validation).
460
+ #
461
+ # See also Gem::URI::Generic.check_user.
462
+ #
463
+ # == Usage
464
+ #
465
+ # require 'rubygems/vendor/uri/lib/uri'
466
+ #
467
+ # uri = Gem::URI.parse("http://john:S3nsit1ve@my.example.com")
468
+ # uri.user = "sam"
469
+ # uri.to_s #=> "http://sam:V3ry_S3nsit1ve@my.example.com"
470
+ #
471
+ def user=(user)
472
+ check_user(user)
473
+ set_user(user)
474
+ # returns user
475
+ end
476
+
477
+ #
478
+ # == Args
479
+ #
480
+ # +v+::
481
+ # String
482
+ #
483
+ # == Description
484
+ #
485
+ # Public setter for the +password+ component
486
+ # (with validation).
487
+ #
488
+ # See also Gem::URI::Generic.check_password.
489
+ #
490
+ # == Usage
491
+ #
492
+ # require 'rubygems/vendor/uri/lib/uri'
493
+ #
494
+ # uri = Gem::URI.parse("http://john:S3nsit1ve@my.example.com")
495
+ # uri.password = "V3ry_S3nsit1ve"
496
+ # uri.to_s #=> "http://john:V3ry_S3nsit1ve@my.example.com"
497
+ #
498
+ def password=(password)
499
+ check_password(password)
500
+ set_password(password)
501
+ # returns password
502
+ end
503
+
504
+ # Protected setter for the +user+ component, and +password+ if available
505
+ # (with validation).
506
+ #
507
+ # See also Gem::URI::Generic.userinfo=.
508
+ #
509
+ def set_userinfo(user, password = nil)
510
+ unless password
511
+ user, password = split_userinfo(user)
512
+ end
513
+ @user = user
514
+ @password = password if password
515
+
516
+ [@user, @password]
517
+ end
518
+ protected :set_userinfo
519
+
520
+ # Protected setter for the user component +v+.
521
+ #
522
+ # See also Gem::URI::Generic.user=.
523
+ #
524
+ def set_user(v)
525
+ set_userinfo(v, @password)
526
+ v
527
+ end
528
+ protected :set_user
529
+
530
+ # Protected setter for the password component +v+.
531
+ #
532
+ # See also Gem::URI::Generic.password=.
533
+ #
534
+ def set_password(v)
535
+ @password = v
536
+ # returns v
537
+ end
538
+ protected :set_password
539
+
540
+ # Returns the userinfo +ui+ as <code>[user, password]</code>
541
+ # if properly formatted as 'user:password'.
542
+ def split_userinfo(ui)
543
+ return nil, nil unless ui
544
+ user, password = ui.split(':', 2)
545
+
546
+ return user, password
547
+ end
548
+ private :split_userinfo
549
+
550
+ # Escapes 'user:password' +v+ based on RFC 1738 section 3.1.
551
+ def escape_userpass(v)
552
+ parser.escape(v, /[@:\/]/o) # RFC 1738 section 3.1 #/
553
+ end
554
+ private :escape_userpass
555
+
556
+ # Returns the userinfo, either as 'user' or 'user:password'.
557
+ def userinfo
558
+ if @user.nil?
559
+ nil
560
+ elsif @password.nil?
561
+ @user
562
+ else
563
+ @user + ':' + @password
564
+ end
565
+ end
566
+
567
+ # Returns the user component (without Gem::URI decoding).
568
+ def user
569
+ @user
570
+ end
571
+
572
+ # Returns the password component (without Gem::URI decoding).
573
+ def password
574
+ @password
575
+ end
576
+
577
+ # Returns the user component after Gem::URI decoding.
578
+ def decoded_user
579
+ Gem::URI.decode_uri_component(@user) if @user
580
+ end
581
+
582
+ # Returns the password component after Gem::URI decoding.
583
+ def decoded_password
584
+ Gem::URI.decode_uri_component(@password) if @password
585
+ end
586
+
587
+ #
588
+ # Checks the host +v+ component for RFC2396 compliance
589
+ # and against the Gem::URI::Parser Regexp for :HOST.
590
+ #
591
+ # Can not have a registry or opaque component defined,
592
+ # with a host component defined.
593
+ #
594
+ def check_host(v)
595
+ return v unless v
596
+
597
+ if @opaque
598
+ raise InvalidURIError,
599
+ "can not set host with registry or opaque"
600
+ elsif parser.regexp[:HOST] !~ v
601
+ raise InvalidComponentError,
602
+ "bad component(expected host component): #{v}"
603
+ end
604
+
605
+ return true
606
+ end
607
+ private :check_host
608
+
609
+ # Protected setter for the host component +v+.
610
+ #
611
+ # See also Gem::URI::Generic.host=.
612
+ #
613
+ def set_host(v)
614
+ @host = v
615
+ end
616
+ protected :set_host
617
+
618
+ #
619
+ # == Args
620
+ #
621
+ # +v+::
622
+ # String
623
+ #
624
+ # == Description
625
+ #
626
+ # Public setter for the host component +v+
627
+ # (with validation).
628
+ #
629
+ # See also Gem::URI::Generic.check_host.
630
+ #
631
+ # == Usage
632
+ #
633
+ # require 'rubygems/vendor/uri/lib/uri'
634
+ #
635
+ # uri = Gem::URI.parse("http://my.example.com")
636
+ # uri.host = "foo.com"
637
+ # uri.to_s #=> "http://foo.com"
638
+ #
639
+ def host=(v)
640
+ check_host(v)
641
+ set_host(v)
642
+ v
643
+ end
644
+
645
+ # Extract the host part of the Gem::URI and unwrap brackets for IPv6 addresses.
646
+ #
647
+ # This method is the same as Gem::URI::Generic#host except
648
+ # brackets for IPv6 (and future IP) addresses are removed.
649
+ #
650
+ # uri = Gem::URI("http://[::1]/bar")
651
+ # uri.hostname #=> "::1"
652
+ # uri.host #=> "[::1]"
653
+ #
654
+ def hostname
655
+ v = self.host
656
+ v&.start_with?('[') && v.end_with?(']') ? v[1..-2] : v
657
+ end
658
+
659
+ # Sets the host part of the Gem::URI as the argument with brackets for IPv6 addresses.
660
+ #
661
+ # This method is the same as Gem::URI::Generic#host= except
662
+ # the argument can be a bare IPv6 address.
663
+ #
664
+ # uri = Gem::URI("http://foo/bar")
665
+ # uri.hostname = "::1"
666
+ # uri.to_s #=> "http://[::1]/bar"
667
+ #
668
+ # If the argument seems to be an IPv6 address,
669
+ # it is wrapped with brackets.
670
+ #
671
+ def hostname=(v)
672
+ v = "[#{v}]" if !(v&.start_with?('[') && v&.end_with?(']')) && v&.index(':')
673
+ self.host = v
674
+ end
675
+
676
+ #
677
+ # Checks the port +v+ component for RFC2396 compliance
678
+ # and against the Gem::URI::Parser Regexp for :PORT.
679
+ #
680
+ # Can not have a registry or opaque component defined,
681
+ # with a port component defined.
682
+ #
683
+ def check_port(v)
684
+ return v unless v
685
+
686
+ if @opaque
687
+ raise InvalidURIError,
688
+ "can not set port with registry or opaque"
689
+ elsif !v.kind_of?(Integer) && parser.regexp[:PORT] !~ v
690
+ raise InvalidComponentError,
691
+ "bad component(expected port component): #{v.inspect}"
692
+ end
693
+
694
+ return true
695
+ end
696
+ private :check_port
697
+
698
+ # Protected setter for the port component +v+.
699
+ #
700
+ # See also Gem::URI::Generic.port=.
701
+ #
702
+ def set_port(v)
703
+ v = v.empty? ? nil : v.to_i unless !v || v.kind_of?(Integer)
704
+ @port = v
705
+ end
706
+ protected :set_port
707
+
708
+ #
709
+ # == Args
710
+ #
711
+ # +v+::
712
+ # String
713
+ #
714
+ # == Description
715
+ #
716
+ # Public setter for the port component +v+
717
+ # (with validation).
718
+ #
719
+ # See also Gem::URI::Generic.check_port.
720
+ #
721
+ # == Usage
722
+ #
723
+ # require 'rubygems/vendor/uri/lib/uri'
724
+ #
725
+ # uri = Gem::URI.parse("http://my.example.com")
726
+ # uri.port = 8080
727
+ # uri.to_s #=> "http://my.example.com:8080"
728
+ #
729
+ def port=(v)
730
+ check_port(v)
731
+ set_port(v)
732
+ port
733
+ end
734
+
735
+ def check_registry(v) # :nodoc:
736
+ raise InvalidURIError, "can not set registry"
737
+ end
738
+ private :check_registry
739
+
740
+ def set_registry(v) #:nodoc:
741
+ raise InvalidURIError, "can not set registry"
742
+ end
743
+ protected :set_registry
744
+
745
+ def registry=(v)
746
+ raise InvalidURIError, "can not set registry"
747
+ end
748
+
749
+ #
750
+ # Checks the path +v+ component for RFC2396 compliance
751
+ # and against the Gem::URI::Parser Regexp
752
+ # for :ABS_PATH and :REL_PATH.
753
+ #
754
+ # Can not have a opaque component defined,
755
+ # with a path component defined.
756
+ #
757
+ def check_path(v)
758
+ # raise if both hier and opaque are not nil, because:
759
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
760
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
761
+ if v && @opaque
762
+ raise InvalidURIError,
763
+ "path conflicts with opaque"
764
+ end
765
+
766
+ # If scheme is ftp, path may be relative.
767
+ # See RFC 1738 section 3.2.2, and RFC 2396.
768
+ if @scheme && @scheme != "ftp"
769
+ if v && v != '' && parser.regexp[:ABS_PATH] !~ v
770
+ raise InvalidComponentError,
771
+ "bad component(expected absolute path component): #{v}"
772
+ end
773
+ else
774
+ if v && v != '' && parser.regexp[:ABS_PATH] !~ v &&
775
+ parser.regexp[:REL_PATH] !~ v
776
+ raise InvalidComponentError,
777
+ "bad component(expected relative path component): #{v}"
778
+ end
779
+ end
780
+
781
+ return true
782
+ end
783
+ private :check_path
784
+
785
+ # Protected setter for the path component +v+.
786
+ #
787
+ # See also Gem::URI::Generic.path=.
788
+ #
789
+ def set_path(v)
790
+ @path = v
791
+ end
792
+ protected :set_path
793
+
794
+ #
795
+ # == Args
796
+ #
797
+ # +v+::
798
+ # String
799
+ #
800
+ # == Description
801
+ #
802
+ # Public setter for the path component +v+
803
+ # (with validation).
804
+ #
805
+ # See also Gem::URI::Generic.check_path.
806
+ #
807
+ # == Usage
808
+ #
809
+ # require 'rubygems/vendor/uri/lib/uri'
810
+ #
811
+ # uri = Gem::URI.parse("http://my.example.com/pub/files")
812
+ # uri.path = "/faq/"
813
+ # uri.to_s #=> "http://my.example.com/faq/"
814
+ #
815
+ def path=(v)
816
+ check_path(v)
817
+ set_path(v)
818
+ v
819
+ end
820
+
821
+ #
822
+ # == Args
823
+ #
824
+ # +v+::
825
+ # String
826
+ #
827
+ # == Description
828
+ #
829
+ # Public setter for the query component +v+.
830
+ #
831
+ # == Usage
832
+ #
833
+ # require 'rubygems/vendor/uri/lib/uri'
834
+ #
835
+ # uri = Gem::URI.parse("http://my.example.com/?id=25")
836
+ # uri.query = "id=1"
837
+ # uri.to_s #=> "http://my.example.com/?id=1"
838
+ #
839
+ def query=(v)
840
+ return @query = nil unless v
841
+ raise InvalidURIError, "query conflicts with opaque" if @opaque
842
+
843
+ x = v.to_str
844
+ v = x.dup if x.equal? v
845
+ v.encode!(Encoding::UTF_8) rescue nil
846
+ v.delete!("\t\r\n")
847
+ v.force_encoding(Encoding::ASCII_8BIT)
848
+ raise InvalidURIError, "invalid percent escape: #{$1}" if /(%\H\H)/n.match(v)
849
+ v.gsub!(/(?!%\h\h|[!$-&(-;=?-_a-~])./n.freeze){'%%%02X' % $&.ord}
850
+ v.force_encoding(Encoding::US_ASCII)
851
+ @query = v
852
+ end
853
+
854
+ #
855
+ # Checks the opaque +v+ component for RFC2396 compliance and
856
+ # against the Gem::URI::Parser Regexp for :OPAQUE.
857
+ #
858
+ # Can not have a host, port, user, or path component defined,
859
+ # with an opaque component defined.
860
+ #
861
+ def check_opaque(v)
862
+ return v unless v
863
+
864
+ # raise if both hier and opaque are not nil, because:
865
+ # absoluteURI = scheme ":" ( hier_part | opaque_part )
866
+ # hier_part = ( net_path | abs_path ) [ "?" query ]
867
+ if @host || @port || @user || @path # userinfo = @user + ':' + @password
868
+ raise InvalidURIError,
869
+ "can not set opaque with host, port, userinfo or path"
870
+ elsif v && parser.regexp[:OPAQUE] !~ v
871
+ raise InvalidComponentError,
872
+ "bad component(expected opaque component): #{v}"
873
+ end
874
+
875
+ return true
876
+ end
877
+ private :check_opaque
878
+
879
+ # Protected setter for the opaque component +v+.
880
+ #
881
+ # See also Gem::URI::Generic.opaque=.
882
+ #
883
+ def set_opaque(v)
884
+ @opaque = v
885
+ end
886
+ protected :set_opaque
887
+
888
+ #
889
+ # == Args
890
+ #
891
+ # +v+::
892
+ # String
893
+ #
894
+ # == Description
895
+ #
896
+ # Public setter for the opaque component +v+
897
+ # (with validation).
898
+ #
899
+ # See also Gem::URI::Generic.check_opaque.
900
+ #
901
+ def opaque=(v)
902
+ check_opaque(v)
903
+ set_opaque(v)
904
+ v
905
+ end
906
+
907
+ #
908
+ # Checks the fragment +v+ component against the Gem::URI::Parser Regexp for :FRAGMENT.
909
+ #
910
+ #
911
+ # == Args
912
+ #
913
+ # +v+::
914
+ # String
915
+ #
916
+ # == Description
917
+ #
918
+ # Public setter for the fragment component +v+
919
+ # (with validation).
920
+ #
921
+ # == Usage
922
+ #
923
+ # require 'rubygems/vendor/uri/lib/uri'
924
+ #
925
+ # uri = Gem::URI.parse("http://my.example.com/?id=25#time=1305212049")
926
+ # uri.fragment = "time=1305212086"
927
+ # uri.to_s #=> "http://my.example.com/?id=25#time=1305212086"
928
+ #
929
+ def fragment=(v)
930
+ return @fragment = nil unless v
931
+
932
+ x = v.to_str
933
+ v = x.dup if x.equal? v
934
+ v.encode!(Encoding::UTF_8) rescue nil
935
+ v.delete!("\t\r\n")
936
+ v.force_encoding(Encoding::ASCII_8BIT)
937
+ v.gsub!(/(?!%\h\h|[!-~])./n){'%%%02X' % $&.ord}
938
+ v.force_encoding(Encoding::US_ASCII)
939
+ @fragment = v
940
+ end
941
+
942
+ #
943
+ # Returns true if Gem::URI is hierarchical.
944
+ #
945
+ # == Description
946
+ #
947
+ # Gem::URI has components listed in order of decreasing significance from left to right,
948
+ # see RFC3986 https://tools.ietf.org/html/rfc3986 1.2.3.
949
+ #
950
+ # == Usage
951
+ #
952
+ # require 'rubygems/vendor/uri/lib/uri'
953
+ #
954
+ # uri = Gem::URI.parse("http://my.example.com/")
955
+ # uri.hierarchical?
956
+ # #=> true
957
+ # uri = Gem::URI.parse("mailto:joe@example.com")
958
+ # uri.hierarchical?
959
+ # #=> false
960
+ #
961
+ def hierarchical?
962
+ if @path
963
+ true
964
+ else
965
+ false
966
+ end
967
+ end
968
+
969
+ #
970
+ # Returns true if Gem::URI has a scheme (e.g. http:// or https://) specified.
971
+ #
972
+ def absolute?
973
+ if @scheme
974
+ true
975
+ else
976
+ false
977
+ end
978
+ end
979
+ alias absolute absolute?
980
+
981
+ #
982
+ # Returns true if Gem::URI does not have a scheme (e.g. http:// or https://) specified.
983
+ #
984
+ def relative?
985
+ !absolute?
986
+ end
987
+
988
+ #
989
+ # Returns an Array of the path split on '/'.
990
+ #
991
+ def split_path(path)
992
+ path.split("/", -1)
993
+ end
994
+ private :split_path
995
+
996
+ #
997
+ # Merges a base path +base+, with relative path +rel+,
998
+ # returns a modified base path.
999
+ #
1000
+ def merge_path(base, rel)
1001
+
1002
+ # RFC2396, Section 5.2, 5)
1003
+ # RFC2396, Section 5.2, 6)
1004
+ base_path = split_path(base)
1005
+ rel_path = split_path(rel)
1006
+
1007
+ # RFC2396, Section 5.2, 6), a)
1008
+ base_path << '' if base_path.last == '..'
1009
+ while i = base_path.index('..')
1010
+ base_path.slice!(i - 1, 2)
1011
+ end
1012
+
1013
+ if (first = rel_path.first) and first.empty?
1014
+ base_path.clear
1015
+ rel_path.shift
1016
+ end
1017
+
1018
+ # RFC2396, Section 5.2, 6), c)
1019
+ # RFC2396, Section 5.2, 6), d)
1020
+ rel_path.push('') if rel_path.last == '.' || rel_path.last == '..'
1021
+ rel_path.delete('.')
1022
+
1023
+ # RFC2396, Section 5.2, 6), e)
1024
+ tmp = []
1025
+ rel_path.each do |x|
1026
+ if x == '..' &&
1027
+ !(tmp.empty? || tmp.last == '..')
1028
+ tmp.pop
1029
+ else
1030
+ tmp << x
1031
+ end
1032
+ end
1033
+
1034
+ add_trailer_slash = !tmp.empty?
1035
+ if base_path.empty?
1036
+ base_path = [''] # keep '/' for root directory
1037
+ elsif add_trailer_slash
1038
+ base_path.pop
1039
+ end
1040
+ while x = tmp.shift
1041
+ if x == '..'
1042
+ # RFC2396, Section 4
1043
+ # a .. or . in an absolute path has no special meaning
1044
+ base_path.pop if base_path.size > 1
1045
+ else
1046
+ # if x == '..'
1047
+ # valid absolute (but abnormal) path "/../..."
1048
+ # else
1049
+ # valid absolute path
1050
+ # end
1051
+ base_path << x
1052
+ tmp.each {|t| base_path << t}
1053
+ add_trailer_slash = false
1054
+ break
1055
+ end
1056
+ end
1057
+ base_path.push('') if add_trailer_slash
1058
+
1059
+ return base_path.join('/')
1060
+ end
1061
+ private :merge_path
1062
+
1063
+ #
1064
+ # == Args
1065
+ #
1066
+ # +oth+::
1067
+ # Gem::URI or String
1068
+ #
1069
+ # == Description
1070
+ #
1071
+ # Destructive form of #merge.
1072
+ #
1073
+ # == Usage
1074
+ #
1075
+ # require 'rubygems/vendor/uri/lib/uri'
1076
+ #
1077
+ # uri = Gem::URI.parse("http://my.example.com")
1078
+ # uri.merge!("/main.rbx?page=1")
1079
+ # uri.to_s # => "http://my.example.com/main.rbx?page=1"
1080
+ #
1081
+ def merge!(oth)
1082
+ t = merge(oth)
1083
+ if self == t
1084
+ nil
1085
+ else
1086
+ replace!(t)
1087
+ self
1088
+ end
1089
+ end
1090
+
1091
+ #
1092
+ # == Args
1093
+ #
1094
+ # +oth+::
1095
+ # Gem::URI or String
1096
+ #
1097
+ # == Description
1098
+ #
1099
+ # Merges two URIs.
1100
+ #
1101
+ # == Usage
1102
+ #
1103
+ # require 'rubygems/vendor/uri/lib/uri'
1104
+ #
1105
+ # uri = Gem::URI.parse("http://my.example.com")
1106
+ # uri.merge("/main.rbx?page=1")
1107
+ # # => "http://my.example.com/main.rbx?page=1"
1108
+ #
1109
+ def merge(oth)
1110
+ rel = parser.__send__(:convert_to_uri, oth)
1111
+
1112
+ if rel.absolute?
1113
+ #raise BadURIError, "both Gem::URI are absolute" if absolute?
1114
+ # hmm... should return oth for usability?
1115
+ return rel
1116
+ end
1117
+
1118
+ unless self.absolute?
1119
+ raise BadURIError, "both Gem::URI are relative"
1120
+ end
1121
+
1122
+ base = self.dup
1123
+
1124
+ authority = rel.userinfo || rel.host || rel.port
1125
+
1126
+ # RFC2396, Section 5.2, 2)
1127
+ if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query
1128
+ base.fragment=(rel.fragment) if rel.fragment
1129
+ return base
1130
+ end
1131
+
1132
+ base.query = nil
1133
+ base.fragment=(nil)
1134
+
1135
+ # RFC2396, Section 5.2, 4)
1136
+ if !authority
1137
+ base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path
1138
+ else
1139
+ # RFC2396, Section 5.2, 4)
1140
+ base.set_path(rel.path) if rel.path
1141
+ end
1142
+
1143
+ # RFC2396, Section 5.2, 7)
1144
+ base.set_userinfo(rel.userinfo) if rel.userinfo
1145
+ base.set_host(rel.host) if rel.host
1146
+ base.set_port(rel.port) if rel.port
1147
+ base.query = rel.query if rel.query
1148
+ base.fragment=(rel.fragment) if rel.fragment
1149
+
1150
+ return base
1151
+ end # merge
1152
+ alias + merge
1153
+
1154
+ # :stopdoc:
1155
+ def route_from_path(src, dst)
1156
+ case dst
1157
+ when src
1158
+ # RFC2396, Section 4.2
1159
+ return ''
1160
+ when %r{(?:\A|/)\.\.?(?:/|\z)}
1161
+ # dst has abnormal absolute path,
1162
+ # like "/./", "/../", "/x/../", ...
1163
+ return dst.dup
1164
+ end
1165
+
1166
+ src_path = src.scan(%r{[^/]*/})
1167
+ dst_path = dst.scan(%r{[^/]*/?})
1168
+
1169
+ # discard same parts
1170
+ while !dst_path.empty? && dst_path.first == src_path.first
1171
+ src_path.shift
1172
+ dst_path.shift
1173
+ end
1174
+
1175
+ tmp = dst_path.join
1176
+
1177
+ # calculate
1178
+ if src_path.empty?
1179
+ if tmp.empty?
1180
+ return './'
1181
+ elsif dst_path.first.include?(':') # (see RFC2396 Section 5)
1182
+ return './' + tmp
1183
+ else
1184
+ return tmp
1185
+ end
1186
+ end
1187
+
1188
+ return '../' * src_path.size + tmp
1189
+ end
1190
+ private :route_from_path
1191
+ # :startdoc:
1192
+
1193
+ # :stopdoc:
1194
+ def route_from0(oth)
1195
+ oth = parser.__send__(:convert_to_uri, oth)
1196
+ if self.relative?
1197
+ raise BadURIError,
1198
+ "relative Gem::URI: #{self}"
1199
+ end
1200
+ if oth.relative?
1201
+ raise BadURIError,
1202
+ "relative Gem::URI: #{oth}"
1203
+ end
1204
+
1205
+ if self.scheme != oth.scheme
1206
+ return self, self.dup
1207
+ end
1208
+ rel = Gem::URI::Generic.new(nil, # it is relative Gem::URI
1209
+ self.userinfo, self.host, self.port,
1210
+ nil, self.path, self.opaque,
1211
+ self.query, self.fragment, parser)
1212
+
1213
+ if rel.userinfo != oth.userinfo ||
1214
+ rel.host.to_s.downcase != oth.host.to_s.downcase ||
1215
+ rel.port != oth.port
1216
+
1217
+ if self.userinfo.nil? && self.host.nil?
1218
+ return self, self.dup
1219
+ end
1220
+
1221
+ rel.set_port(nil) if rel.port == oth.default_port
1222
+ return rel, rel
1223
+ end
1224
+ rel.set_userinfo(nil)
1225
+ rel.set_host(nil)
1226
+ rel.set_port(nil)
1227
+
1228
+ if rel.path && rel.path == oth.path
1229
+ rel.set_path('')
1230
+ rel.query = nil if rel.query == oth.query
1231
+ return rel, rel
1232
+ elsif rel.opaque && rel.opaque == oth.opaque
1233
+ rel.set_opaque('')
1234
+ rel.query = nil if rel.query == oth.query
1235
+ return rel, rel
1236
+ end
1237
+
1238
+ # you can modify `rel', but can not `oth'.
1239
+ return oth, rel
1240
+ end
1241
+ private :route_from0
1242
+ # :startdoc:
1243
+
1244
+ #
1245
+ # == Args
1246
+ #
1247
+ # +oth+::
1248
+ # Gem::URI or String
1249
+ #
1250
+ # == Description
1251
+ #
1252
+ # Calculates relative path from oth to self.
1253
+ #
1254
+ # == Usage
1255
+ #
1256
+ # require 'rubygems/vendor/uri/lib/uri'
1257
+ #
1258
+ # uri = Gem::URI.parse('http://my.example.com/main.rbx?page=1')
1259
+ # uri.route_from('http://my.example.com')
1260
+ # #=> #<Gem::URI::Generic /main.rbx?page=1>
1261
+ #
1262
+ def route_from(oth)
1263
+ # you can modify `rel', but can not `oth'.
1264
+ begin
1265
+ oth, rel = route_from0(oth)
1266
+ rescue
1267
+ raise $!.class, $!.message
1268
+ end
1269
+ if oth == rel
1270
+ return rel
1271
+ end
1272
+
1273
+ rel.set_path(route_from_path(oth.path, self.path))
1274
+ if rel.path == './' && self.query
1275
+ # "./?foo" -> "?foo"
1276
+ rel.set_path('')
1277
+ end
1278
+
1279
+ return rel
1280
+ end
1281
+
1282
+ alias - route_from
1283
+
1284
+ #
1285
+ # == Args
1286
+ #
1287
+ # +oth+::
1288
+ # Gem::URI or String
1289
+ #
1290
+ # == Description
1291
+ #
1292
+ # Calculates relative path to oth from self.
1293
+ #
1294
+ # == Usage
1295
+ #
1296
+ # require 'rubygems/vendor/uri/lib/uri'
1297
+ #
1298
+ # uri = Gem::URI.parse('http://my.example.com')
1299
+ # uri.route_to('http://my.example.com/main.rbx?page=1')
1300
+ # #=> #<Gem::URI::Generic /main.rbx?page=1>
1301
+ #
1302
+ def route_to(oth)
1303
+ parser.__send__(:convert_to_uri, oth).route_from(self)
1304
+ end
1305
+
1306
+ #
1307
+ # Returns normalized Gem::URI.
1308
+ #
1309
+ # require 'rubygems/vendor/uri/lib/uri'
1310
+ #
1311
+ # Gem::URI("HTTP://my.EXAMPLE.com").normalize
1312
+ # #=> #<Gem::URI::HTTP http://my.example.com/>
1313
+ #
1314
+ # Normalization here means:
1315
+ #
1316
+ # * scheme and host are converted to lowercase,
1317
+ # * an empty path component is set to "/".
1318
+ #
1319
+ def normalize
1320
+ uri = dup
1321
+ uri.normalize!
1322
+ uri
1323
+ end
1324
+
1325
+ #
1326
+ # Destructive version of #normalize.
1327
+ #
1328
+ def normalize!
1329
+ if path&.empty?
1330
+ set_path('/')
1331
+ end
1332
+ if scheme && scheme != scheme.downcase
1333
+ set_scheme(self.scheme.downcase)
1334
+ end
1335
+ if host && host != host.downcase
1336
+ set_host(self.host.downcase)
1337
+ end
1338
+ end
1339
+
1340
+ #
1341
+ # Constructs String from Gem::URI.
1342
+ #
1343
+ def to_s
1344
+ str = ''.dup
1345
+ if @scheme
1346
+ str << @scheme
1347
+ str << ':'
1348
+ end
1349
+
1350
+ if @opaque
1351
+ str << @opaque
1352
+ else
1353
+ if @host || %w[file postgres].include?(@scheme)
1354
+ str << '//'
1355
+ end
1356
+ if self.userinfo
1357
+ str << self.userinfo
1358
+ str << '@'
1359
+ end
1360
+ if @host
1361
+ str << @host
1362
+ end
1363
+ if @port && @port != self.default_port
1364
+ str << ':'
1365
+ str << @port.to_s
1366
+ end
1367
+ str << @path
1368
+ if @query
1369
+ str << '?'
1370
+ str << @query
1371
+ end
1372
+ end
1373
+ if @fragment
1374
+ str << '#'
1375
+ str << @fragment
1376
+ end
1377
+ str
1378
+ end
1379
+ alias to_str to_s
1380
+
1381
+ #
1382
+ # Compares two URIs.
1383
+ #
1384
+ def ==(oth)
1385
+ if self.class == oth.class
1386
+ self.normalize.component_ary == oth.normalize.component_ary
1387
+ else
1388
+ false
1389
+ end
1390
+ end
1391
+
1392
+ def hash
1393
+ self.component_ary.hash
1394
+ end
1395
+
1396
+ def eql?(oth)
1397
+ self.class == oth.class &&
1398
+ parser == oth.parser &&
1399
+ self.component_ary.eql?(oth.component_ary)
1400
+ end
1401
+
1402
+ =begin
1403
+
1404
+ --- Gem::URI::Generic#===(oth)
1405
+
1406
+ =end
1407
+ # def ===(oth)
1408
+ # raise NotImplementedError
1409
+ # end
1410
+
1411
+ =begin
1412
+ =end
1413
+
1414
+
1415
+ # Returns an Array of the components defined from the COMPONENT Array.
1416
+ def component_ary
1417
+ component.collect do |x|
1418
+ self.__send__(x)
1419
+ end
1420
+ end
1421
+ protected :component_ary
1422
+
1423
+ # == Args
1424
+ #
1425
+ # +components+::
1426
+ # Multiple Symbol arguments defined in Gem::URI::HTTP.
1427
+ #
1428
+ # == Description
1429
+ #
1430
+ # Selects specified components from Gem::URI.
1431
+ #
1432
+ # == Usage
1433
+ #
1434
+ # require 'rubygems/vendor/uri/lib/uri'
1435
+ #
1436
+ # uri = Gem::URI.parse('http://myuser:mypass@my.example.com/test.rbx')
1437
+ # uri.select(:userinfo, :host, :path)
1438
+ # # => ["myuser:mypass", "my.example.com", "/test.rbx"]
1439
+ #
1440
+ def select(*components)
1441
+ components.collect do |c|
1442
+ if component.include?(c)
1443
+ self.__send__(c)
1444
+ else
1445
+ raise ArgumentError,
1446
+ "expected of components of #{self.class} (#{self.class.component.join(', ')})"
1447
+ end
1448
+ end
1449
+ end
1450
+
1451
+ def inspect
1452
+ "#<#{self.class} #{self}>"
1453
+ end
1454
+
1455
+ #
1456
+ # == Args
1457
+ #
1458
+ # +v+::
1459
+ # Gem::URI or String
1460
+ #
1461
+ # == Description
1462
+ #
1463
+ # Attempts to parse other Gem::URI +oth+,
1464
+ # returns [parsed_oth, self].
1465
+ #
1466
+ # == Usage
1467
+ #
1468
+ # require 'rubygems/vendor/uri/lib/uri'
1469
+ #
1470
+ # uri = Gem::URI.parse("http://my.example.com")
1471
+ # uri.coerce("http://foo.com")
1472
+ # #=> [#<Gem::URI::HTTP http://foo.com>, #<Gem::URI::HTTP http://my.example.com>]
1473
+ #
1474
+ def coerce(oth)
1475
+ case oth
1476
+ when String
1477
+ oth = parser.parse(oth)
1478
+ else
1479
+ super
1480
+ end
1481
+
1482
+ return oth, self
1483
+ end
1484
+
1485
+ # Returns a proxy Gem::URI.
1486
+ # The proxy Gem::URI is obtained from environment variables such as http_proxy,
1487
+ # ftp_proxy, no_proxy, etc.
1488
+ # If there is no proper proxy, nil is returned.
1489
+ #
1490
+ # If the optional parameter +env+ is specified, it is used instead of ENV.
1491
+ #
1492
+ # Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.)
1493
+ # are examined, too.
1494
+ #
1495
+ # But http_proxy and HTTP_PROXY is treated specially under CGI environment.
1496
+ # It's because HTTP_PROXY may be set by Proxy: header.
1497
+ # So HTTP_PROXY is not used.
1498
+ # http_proxy is not used too if the variable is case insensitive.
1499
+ # CGI_HTTP_PROXY can be used instead.
1500
+ def find_proxy(env=ENV)
1501
+ raise BadURIError, "relative Gem::URI: #{self}" if self.relative?
1502
+ name = self.scheme.downcase + '_proxy'
1503
+ proxy_uri = nil
1504
+ if name == 'http_proxy' && env.include?('REQUEST_METHOD') # CGI?
1505
+ # HTTP_PROXY conflicts with *_proxy for proxy settings and
1506
+ # HTTP_* for header information in CGI.
1507
+ # So it should be careful to use it.
1508
+ pairs = env.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
1509
+ case pairs.length
1510
+ when 0 # no proxy setting anyway.
1511
+ proxy_uri = nil
1512
+ when 1
1513
+ k, _ = pairs.shift
1514
+ if k == 'http_proxy' && env[k.upcase] == nil
1515
+ # http_proxy is safe to use because ENV is case sensitive.
1516
+ proxy_uri = env[name]
1517
+ else
1518
+ proxy_uri = nil
1519
+ end
1520
+ else # http_proxy is safe to use because ENV is case sensitive.
1521
+ proxy_uri = env.to_hash[name]
1522
+ end
1523
+ if !proxy_uri
1524
+ # Use CGI_HTTP_PROXY. cf. libwww-perl.
1525
+ proxy_uri = env["CGI_#{name.upcase}"]
1526
+ end
1527
+ elsif name == 'http_proxy'
1528
+ if RUBY_ENGINE == 'jruby' && p_addr = ENV_JAVA['http.proxyHost']
1529
+ p_port = ENV_JAVA['http.proxyPort']
1530
+ if p_user = ENV_JAVA['http.proxyUser']
1531
+ p_pass = ENV_JAVA['http.proxyPass']
1532
+ proxy_uri = "http://#{p_user}:#{p_pass}@#{p_addr}:#{p_port}"
1533
+ else
1534
+ proxy_uri = "http://#{p_addr}:#{p_port}"
1535
+ end
1536
+ else
1537
+ unless proxy_uri = env[name]
1538
+ if proxy_uri = env[name.upcase]
1539
+ warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.', uplevel: 1
1540
+ end
1541
+ end
1542
+ end
1543
+ else
1544
+ proxy_uri = env[name] || env[name.upcase]
1545
+ end
1546
+
1547
+ if proxy_uri.nil? || proxy_uri.empty?
1548
+ return nil
1549
+ end
1550
+
1551
+ if self.hostname
1552
+ begin
1553
+ addr = IPSocket.getaddress(self.hostname)
1554
+ return nil if /\A127\.|\A::1\z/ =~ addr
1555
+ rescue SocketError
1556
+ end
1557
+ end
1558
+
1559
+ name = 'no_proxy'
1560
+ if no_proxy = env[name] || env[name.upcase]
1561
+ return nil unless Gem::URI::Generic.use_proxy?(self.hostname, addr, self.port, no_proxy)
1562
+ end
1563
+ Gem::URI.parse(proxy_uri)
1564
+ end
1565
+
1566
+ def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
1567
+ hostname = hostname.downcase
1568
+ dothostname = ".#{hostname}"
1569
+ no_proxy.scan(/([^:,\s]+)(?::(\d+))?/) {|p_host, p_port|
1570
+ if !p_port || port == p_port.to_i
1571
+ if p_host.start_with?('.')
1572
+ return false if hostname.end_with?(p_host.downcase)
1573
+ else
1574
+ return false if dothostname.end_with?(".#{p_host.downcase}")
1575
+ end
1576
+ if addr
1577
+ begin
1578
+ return false if IPAddr.new(p_host).include?(addr)
1579
+ rescue IPAddr::InvalidAddressError
1580
+ next
1581
+ end
1582
+ end
1583
+ end
1584
+ }
1585
+ true
1586
+ end
1587
+ end
1588
+ end