Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user_agent argument to http methods #571

Merged
merged 5 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@dnkoutso dnkoutso Jul 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also set that as the default?

def self.perform_head_request(url, user_agent = USER_AGENT)
  # code
end

Copy link
Contributor Author

@abbeycode abbeycode Jul 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did at first, but realized this method is private to http.rb, so it becomes easier to pass nil at the call site(s) and then substitute it here (we could also expose the USER_AGENT default value as public, if you'd prefer).


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