Skip to content

Commit

Permalink
Merge pull request #571 from abbeycode/issue9049
Browse files Browse the repository at this point in the history
Added user_agent argument to http methods
  • Loading branch information
dnkoutso authored Jul 30, 2019
2 parents 73b2ce6 + 3cefc3a commit 9ae4051
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 11 additions & 9 deletions lib/cocoapods-core/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 9ae4051

Please sign in to comment.