Skip to content

Commit

Permalink
Merge pull request #10346 from jasl/remove-typhoeus
Browse files Browse the repository at this point in the history
Rewrite the only place dependent on `typhoeus`
  • Loading branch information
igor-makarov committed Feb 6, 2021
2 parents 4949777 + 4a9a504 commit 592761b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ To install release candidates run `[sudo] gem install cocoapods --pre`
[dcvz](https://github.com/dcvz)
[#9935](https://github.com/CocoaPods/CocoaPods/pull/9935)

* Rewrite the only place dependent on `typhoeus`.
[Jun Jiang](https://github.com/jasl), [Igor Makarov](https://github.com/igor-makarov)
[#10346](https://github.com/CocoaPods/CocoaPods/pull/10346)

* Add a `--update-sources` option to `pod repo push` so one can ensure sources are up-to-date.
[Elton Gao](https://github.com/gyfelton)
[Justin Martin](https://github.com/justinseanmartin)
Expand Down
11 changes: 5 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ GEM
concurrent-ruby (1.1.7)
cork (0.3.0)
colored2 (~> 3.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crack (0.4.5)
rexml
danger (5.16.1)
claide (~> 1.0)
claide-plugins (>= 0.9.2)
Expand Down Expand Up @@ -184,7 +184,7 @@ GEM
gh_inspector (1.1.3)
git (1.7.0)
rchardet (~> 1.8)
hashdiff (0.3.1)
hashdiff (1.0.1)
httpclient (2.8.3)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -254,7 +254,6 @@ GEM
ruby-macho (1.4.0)
ruby-prof (0.15.2)
ruby-progressbar (1.10.1)
safe_yaml (1.0.4)
sawyer (0.8.2)
addressable (>= 2.3.5)
faraday (> 0.8, < 2.0)
Expand All @@ -276,10 +275,10 @@ GEM
tzinfo (1.2.9)
thread_safe (~> 0.1)
unicode-display_width (1.7.0)
webmock (2.3.1)
webmock (3.11.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
hashdiff (>= 0.4.0, < 2.0.0)
yard (0.9.12)

PLATFORMS
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods/command/spec/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def podspecs_to_lint
output_path = podspecs_tmp_dir + File.basename(path)
output_path.dirname.mkpath
begin
open(path) do |io|
OpenURI.open_uri(path) do |io|
output_path.open('w') { |f| f << io.read }
end
rescue => e
Expand Down
22 changes: 14 additions & 8 deletions lib/cocoapods/sources_manager.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'cocoapods-core/source'
require 'cocoapods/open-uri'
require 'netrc'
require 'set'
require 'rest'
Expand Down Expand Up @@ -70,15 +71,20 @@ def create_source_with_url(url)
# The URL of the source.
#
def cdn_url?(url)
if url =~ %r{^https?:\/\/}
require 'typhoeus'
return unless url =~ %r{^https?:\/\/}

response = Typhoeus.get(url + '/CocoaPods-version.yml', :netrc_file => Netrc.default_path, :netrc => :optional)
response.code == 200 && begin
response_hash = YAML.load(response.body) # rubocop:disable Security/YAMLLoad
response_hash.is_a?(Hash) && !Source::Metadata.new(response_hash).latest_cocoapods_version.nil?
end
end
uri_options = {}

netrc_info = Netrc.read
netrc_host = URI.parse(url).host
credentials = netrc_info[netrc_host]
uri_options[:http_basic_authentication] = credentials if credentials

response = OpenURI.open_uri(url.chomp('/') + '/CocoaPods-version.yml', uri_options)
response_hash = YAML.load(response.read) # rubocop:disable Security/YAMLLoad
response_hash.is_a?(Hash) && !Source::Metadata.new(response_hash).latest_cocoapods_version.nil?
rescue ::OpenURI::HTTPError, SocketError
return false
rescue => e
raise Informative, "Couldn't determine repo type for URL: `#{url}`: #{e}"
end
Expand Down
38 changes: 33 additions & 5 deletions spec/unit/sources_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require File.expand_path('../../spec_helper', __FILE__)
require 'webmock'

def set_up_test_repo_for_update
set_up_test_repo
Expand All @@ -24,12 +23,12 @@ def set_up_test_repo_for_update
- 1'.freeze

def stub_url_as_cdn(url)
WebMock.stub_request(:get, url + '/CocoaPods-version.yml').
WebMock.stub_request(:get, url.chomp('/') + '/CocoaPods-version.yml').
to_return(:status => 200, :headers => {}, :body => CDN_REPO_RESPONSE)
end

def stub_as_404(url)
WebMock.stub_request(:get, url + '/CocoaPods-version.yml').
WebMock.stub_request(:get, url.chomp('/') + '/CocoaPods-version.yml').
to_return(:status => 404, :headers => {}, :body => '')
end

Expand Down Expand Up @@ -105,7 +104,7 @@ module Pod
end

it 'raises informative exception on network error' do
Typhoeus.stubs(:get).with do
OpenURI.stubs(:open_uri).with do
raise StandardError, 'some network error'
end
@sources_manager.stubs(:source_with_url).returns(nil)
Expand Down Expand Up @@ -162,6 +161,19 @@ module Pod
@sources_manager.cdn_url?('https://github.com/cocoapods/specs').should == false
end

it 'netrc' do
WebMock.stub_request(:get, 'https://some_host.com/something/CocoaPods-version.yml').
with(:basic_auth => %w[user1 xxx]).
to_return(:status => 200, :body => CDN_REPO_RESPONSE)

netrc_file = temporary_directory + '.netrc'
File.open(netrc_file, 'w', 0o600) { |f| f.write("machine some_host.com\nlogin user1\npassword xxx\n") }

ENV['NETRC'] = temporary_directory.to_s
@sources_manager.cdn_url?('https://some_host.com/something').should == true
ENV.delete('NETRC')
end

it 'fake 200 response' do
HTML_RESPONSE = '<!doctype html>
<html>
Expand All @@ -178,10 +190,26 @@ module Pod
@sources_manager.cdn_url?('https://some_host.com/something').should == false
end

it 'redirect' do
it 'handles trailing slash' do
stub_url_as_cdn('http://some_host.com/something')
@sources_manager.cdn_url?('http://some_host.com/something').should == true
@sources_manager.cdn_url?('http://some_host.com/something/').should == true
end

it 'handles redirects' do
WebMock.stub_request(:get, 'http://some_host.com/something/CocoaPods-version.yml').
to_return(:status => 301, :body => '', :headers => { 'Location' => ['http://some_host.com'] })
WebMock.stub_request(:get, 'http://some_host.com').
to_return(:status => 200, :body => '', :headers => {})

@sources_manager.cdn_url?('http://some_host.com/something').should == false

WebMock.stub_request(:get, 'http://some_another_host.com/something/CocoaPods-version.yml').
to_return(:status => 301, :body => '', :headers => { 'Location' => ['http://some_another_host.com'] })
WebMock.stub_request(:get, 'http://some_another_host.com').
to_return(:status => 200, :body => CDN_REPO_RESPONSE)

@sources_manager.cdn_url?('http://some_another_host.com/something').should == true
end
end
end
Expand Down

0 comments on commit 592761b

Please sign in to comment.