PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rubygems/remote_fetcher.rb

https://gitlab.com/MichelZuniga/ruby
Ruby | 394 lines | 238 code | 91 blank | 65 comment | 29 complexity | 71aab137d0fdedb9487b8d26bb3321b3 MD5 | raw file
  1. require 'rubygems'
  2. require 'rubygems/request'
  3. require 'rubygems/uri_formatter'
  4. require 'rubygems/user_interaction'
  5. require 'rubygems/request/connection_pools'
  6. require 'resolv'
  7. ##
  8. # RemoteFetcher handles the details of fetching gems and gem information from
  9. # a remote source.
  10. class Gem::RemoteFetcher
  11. include Gem::UserInteraction
  12. ##
  13. # A FetchError exception wraps up the various possible IO and HTTP failures
  14. # that could happen while downloading from the internet.
  15. class FetchError < Gem::Exception
  16. ##
  17. # The URI which was being accessed when the exception happened.
  18. attr_accessor :uri
  19. def initialize(message, uri)
  20. super message
  21. @uri = uri
  22. end
  23. def to_s # :nodoc:
  24. "#{super} (#{uri})"
  25. end
  26. end
  27. ##
  28. # A FetchError that indicates that the reason for not being
  29. # able to fetch data was that the host could not be contacted
  30. class UnknownHostError < FetchError
  31. end
  32. @fetcher = nil
  33. ##
  34. # Cached RemoteFetcher instance.
  35. def self.fetcher
  36. @fetcher ||= self.new Gem.configuration[:http_proxy]
  37. end
  38. ##
  39. # Initialize a remote fetcher using the source URI and possible proxy
  40. # information.
  41. #
  42. # +proxy+
  43. # * [String]: explicit specification of proxy; overrides any environment
  44. # variable setting
  45. # * nil: respect environment variables (HTTP_PROXY, HTTP_PROXY_USER,
  46. # HTTP_PROXY_PASS)
  47. # * <tt>:no_proxy</tt>: ignore environment variables and _don't_ use a proxy
  48. #
  49. # +dns+: An object to use for DNS resolution of the API endpoint.
  50. # By default, use Resolv::DNS.
  51. def initialize(proxy=nil, dns=Resolv::DNS.new)
  52. require 'net/http'
  53. require 'stringio'
  54. require 'time'
  55. require 'uri'
  56. Socket.do_not_reverse_lookup = true
  57. @proxy = proxy
  58. @pools = {}
  59. @pool_lock = Mutex.new
  60. @cert_files = Gem::Request.get_cert_files
  61. @dns = dns
  62. end
  63. ##
  64. # Given a source at +uri+, calculate what hostname to actually
  65. # connect to query the data for it.
  66. def api_endpoint(uri)
  67. host = uri.host
  68. begin
  69. res = @dns.getresource "_rubygems._tcp.#{host}",
  70. Resolv::DNS::Resource::IN::SRV
  71. rescue Resolv::ResolvError
  72. uri
  73. else
  74. URI.parse "#{uri.scheme}://#{res.target}#{uri.path}"
  75. end
  76. end
  77. ##
  78. # Given a name and requirement, downloads this gem into cache and returns the
  79. # filename. Returns nil if the gem cannot be located.
  80. #--
  81. # Should probably be integrated with #download below, but that will be a
  82. # larger, more emcompassing effort. -erikh
  83. def download_to_cache dependency
  84. found, _ = Gem::SpecFetcher.fetcher.spec_for_dependency dependency
  85. return if found.empty?
  86. spec, source = found.max_by { |(s,_)| s.version }
  87. download spec, source.uri.to_s
  88. end
  89. ##
  90. # Moves the gem +spec+ from +source_uri+ to the cache dir unless it is
  91. # already there. If the source_uri is local the gem cache dir copy is
  92. # always replaced.
  93. def download(spec, source_uri, install_dir = Gem.dir)
  94. cache_dir =
  95. if Dir.pwd == install_dir then # see fetch_command
  96. install_dir
  97. elsif File.writable? install_dir then
  98. File.join install_dir, "cache"
  99. else
  100. File.join Gem.user_dir, "cache"
  101. end
  102. gem_file_name = File.basename spec.cache_file
  103. local_gem_path = File.join cache_dir, gem_file_name
  104. FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir
  105. # Always escape URI's to deal with potential spaces and such
  106. # It should also be considered that source_uri may already be
  107. # a valid URI with escaped characters. e.g. "{DESede}" is encoded
  108. # as "%7BDESede%7D". If this is escaped again the percentage
  109. # symbols will be escaped.
  110. unless source_uri.is_a?(URI::Generic)
  111. begin
  112. source_uri = URI.parse(source_uri)
  113. rescue
  114. source_uri = URI.parse(URI.const_defined?(:DEFAULT_PARSER) ?
  115. URI::DEFAULT_PARSER.escape(source_uri.to_s) :
  116. URI.escape(source_uri.to_s))
  117. end
  118. end
  119. scheme = source_uri.scheme
  120. # URI.parse gets confused by MS Windows paths with forward slashes.
  121. scheme = nil if scheme =~ /^[a-z]$/i
  122. # REFACTOR: split this up and dispatch on scheme (eg download_http)
  123. # REFACTOR: be sure to clean up fake fetcher when you do this... cleaner
  124. case scheme
  125. when 'http', 'https', 's3' then
  126. unless File.exist? local_gem_path then
  127. begin
  128. verbose "Downloading gem #{gem_file_name}"
  129. remote_gem_path = source_uri + "gems/#{gem_file_name}"
  130. self.cache_update_path remote_gem_path, local_gem_path
  131. rescue Gem::RemoteFetcher::FetchError
  132. raise if spec.original_platform == spec.platform
  133. alternate_name = "#{spec.original_name}.gem"
  134. verbose "Failed, downloading gem #{alternate_name}"
  135. remote_gem_path = source_uri + "gems/#{alternate_name}"
  136. self.cache_update_path remote_gem_path, local_gem_path
  137. end
  138. end
  139. when 'file' then
  140. begin
  141. path = source_uri.path
  142. path = File.dirname(path) if File.extname(path) == '.gem'
  143. remote_gem_path = correct_for_windows_path(File.join(path, 'gems', gem_file_name))
  144. FileUtils.cp(remote_gem_path, local_gem_path)
  145. rescue Errno::EACCES
  146. local_gem_path = source_uri.to_s
  147. end
  148. verbose "Using local gem #{local_gem_path}"
  149. when nil then # TODO test for local overriding cache
  150. source_path = if Gem.win_platform? && source_uri.scheme &&
  151. !source_uri.path.include?(':') then
  152. "#{source_uri.scheme}:#{source_uri.path}"
  153. else
  154. source_uri.path
  155. end
  156. source_path = Gem::UriFormatter.new(source_path).unescape
  157. begin
  158. FileUtils.cp source_path, local_gem_path unless
  159. File.identical?(source_path, local_gem_path)
  160. rescue Errno::EACCES
  161. local_gem_path = source_uri.to_s
  162. end
  163. verbose "Using local gem #{local_gem_path}"
  164. else
  165. raise ArgumentError, "unsupported URI scheme #{source_uri.scheme}"
  166. end
  167. local_gem_path
  168. end
  169. ##
  170. # File Fetcher. Dispatched by +fetch_path+. Use it instead.
  171. def fetch_file uri, *_
  172. Gem.read_binary correct_for_windows_path uri.path
  173. end
  174. ##
  175. # HTTP Fetcher. Dispatched by +fetch_path+. Use it instead.
  176. def fetch_http uri, last_modified = nil, head = false, depth = 0
  177. fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
  178. response = request uri, fetch_type, last_modified
  179. case response
  180. when Net::HTTPOK, Net::HTTPNotModified then
  181. response.uri = uri if response.respond_to? :uri
  182. head ? response : response.body
  183. when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
  184. Net::HTTPTemporaryRedirect then
  185. raise FetchError.new('too many redirects', uri) if depth > 10
  186. location = URI.parse response['Location']
  187. if https?(uri) && !https?(location)
  188. raise FetchError.new("redirecting to non-https resource: #{location}", uri)
  189. end
  190. fetch_http(location, last_modified, head, depth + 1)
  191. else
  192. raise FetchError.new("bad response #{response.message} #{response.code}", uri)
  193. end
  194. end
  195. alias :fetch_https :fetch_http
  196. ##
  197. # Downloads +uri+ and returns it as a String.
  198. def fetch_path(uri, mtime = nil, head = false)
  199. uri = URI.parse uri unless URI::Generic === uri
  200. raise ArgumentError, "bad uri: #{uri}" unless uri
  201. unless uri.scheme
  202. raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}"
  203. end
  204. data = send "fetch_#{uri.scheme}", uri, mtime, head
  205. if data and !head and uri.to_s =~ /\.gz$/
  206. begin
  207. data = Gem.gunzip data
  208. rescue Zlib::GzipFile::Error
  209. raise FetchError.new("server did not return a valid file", uri.to_s)
  210. end
  211. end
  212. data
  213. rescue FetchError
  214. raise
  215. rescue Timeout::Error
  216. raise UnknownHostError.new('timed out', uri.to_s)
  217. rescue IOError, SocketError, SystemCallError => e
  218. if e.message =~ /getaddrinfo/
  219. raise UnknownHostError.new('no such name', uri.to_s)
  220. else
  221. raise FetchError.new("#{e.class}: #{e}", uri.to_s)
  222. end
  223. end
  224. def fetch_s3(uri, mtime = nil, head = false)
  225. public_uri = sign_s3_url(uri)
  226. fetch_https public_uri, mtime, head
  227. end
  228. ##
  229. # Downloads +uri+ to +path+ if necessary. If no path is given, it just
  230. # passes the data.
  231. def cache_update_path uri, path = nil, update = true
  232. mtime = path && File.stat(path).mtime rescue nil
  233. data = fetch_path(uri, mtime)
  234. if data == nil # indicates the server returned 304 Not Modified
  235. return Gem.read_binary(path)
  236. end
  237. if update and path
  238. open(path, 'wb') do |io|
  239. io.flock(File::LOCK_EX)
  240. io.write data
  241. end
  242. end
  243. data
  244. end
  245. ##
  246. # Returns the size of +uri+ in bytes.
  247. def fetch_size(uri) # TODO: phase this out
  248. response = fetch_path(uri, nil, true)
  249. response['content-length'].to_i
  250. end
  251. def correct_for_windows_path(path)
  252. if path[0].chr == '/' && path[1].chr =~ /[a-z]/i && path[2].chr == ':'
  253. path = path[1..-1]
  254. else
  255. path
  256. end
  257. end
  258. ##
  259. # Performs a Net::HTTP request of type +request_class+ on +uri+ returning
  260. # a Net::HTTP response object. request maintains a table of persistent
  261. # connections to reduce connect overhead.
  262. def request(uri, request_class, last_modified = nil)
  263. proxy = proxy_for @proxy, uri
  264. pool = pools_for(proxy).pool_for uri
  265. request = Gem::Request.new uri, request_class, last_modified, pool
  266. request.fetch do |req|
  267. yield req if block_given?
  268. end
  269. end
  270. def https?(uri)
  271. uri.scheme.downcase == 'https'
  272. end
  273. protected
  274. # we have our own signing code here to avoid a dependency on the aws-sdk gem
  275. # fortunately, a simple GET request isn't too complex to sign properly
  276. def sign_s3_url(uri, expiration = nil)
  277. require 'base64'
  278. require 'openssl'
  279. unless uri.user && uri.password
  280. raise FetchError.new("credentials needed in s3 source, like s3://key:secret@bucket-name/", uri.to_s)
  281. end
  282. expiration ||= s3_expiration
  283. canonical_path = "/#{uri.host}#{uri.path}"
  284. payload = "GET\n\n\n#{expiration}\n#{canonical_path}"
  285. digest = OpenSSL::HMAC.digest('sha1', uri.password, payload)
  286. # URI.escape is deprecated, and there isn't yet a replacement that does quite what we want
  287. signature = Base64.encode64(digest).gsub("\n", '').gsub(/[\+\/=]/) { |c| BASE64_URI_TRANSLATE[c] }
  288. URI.parse("https://#{uri.host}.s3.amazonaws.com#{uri.path}?AWSAccessKeyId=#{uri.user}&Expires=#{expiration}&Signature=#{signature}")
  289. end
  290. def s3_expiration
  291. (Time.now + 3600).to_i # one hour from now
  292. end
  293. BASE64_URI_TRANSLATE = { '+' => '%2B', '/' => '%2F', '=' => '%3D' }.freeze
  294. private
  295. def proxy_for proxy, uri
  296. Gem::Request.proxy_uri(proxy || Gem::Request.get_proxy_from_env(uri.scheme))
  297. end
  298. def pools_for proxy
  299. @pool_lock.synchronize do
  300. @pools[proxy] ||= Gem::Request::ConnectionPools.new proxy, @cert_files
  301. end
  302. end
  303. end