diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ee626bc9..7a4593989 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,11 @@ [Eric Amorde](https://github.com/amorde) [CocoaPods/CocoaPods#8785](https://github.com/CocoaPods/CocoaPods/issues/8785) +* Pass a non-browser user agent for social media validation + [Dov Frankel](https://github.com/abbeycode) + [#571](https://github.com/CocoaPods/Core/pull/571) + [CocoaPods#9053](https://github.com/CocoaPods/Cocoapods/pull/9053) + [CocoaPods#9049](https://github.com/CocoaPods/CocoaPods/issues/9049) ## 1.7.5 (2019-07-19) diff --git a/lib/cocoapods-core/http.rb b/lib/cocoapods-core/http.rb index da5c2a8b4..bf5f0b98b 100644 --- a/lib/cocoapods-core/http.rb +++ b/lib/cocoapods-core/http.rb @@ -8,11 +8,11 @@ module HTTP # # @return [string] # - def self.get_actual_url(url) + def self.get_actual_url(url, user_agent = nil) redirects = 0 loop do - response = perform_head_request(url) + response = perform_head_request(url, user_agent) if [301, 302, 303, 307, 308].include? response.status_code location = response.headers['location'].first @@ -38,12 +38,12 @@ def self.get_actual_url(url) # # @return [REST::response] # - def self.validate_url(url) + def self.validate_url(url, user_agent = nil) return nil unless url =~ /^#{URI.regexp}$/ begin - url = get_actual_url(url) - resp = perform_head_request(url) + url = get_actual_url(url, user_agent) + resp = perform_head_request(url, user_agent) rescue SocketError, URI::InvalidURIError, REST::Error, REST::Error::Connection resp = nil end @@ -59,17 +59,19 @@ def self.validate_url(url) # # @return [REST::response] # - def self.perform_head_request(url) + def self.perform_head_request(url, user_agent) require 'rest' - resp = ::REST.head(url, 'User-Agent' => USER_AGENT) + user_agent ||= USER_AGENT + + resp = ::REST.head(url, 'User-Agent' => user_agent) if resp.status_code >= 400 - resp = ::REST.get(url, 'User-Agent' => USER_AGENT, + resp = ::REST.get(url, 'User-Agent' => user_agent, 'Range' => 'bytes=0-0') if resp.status_code >= 400 - resp = ::REST.get(url, 'User-Agent' => USER_AGENT) + resp = ::REST.get(url, 'User-Agent' => user_agent) end end