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

Try getting page content with both headers #11517

Merged
merged 1 commit into from
Jun 15, 2021
Merged
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
69 changes: 36 additions & 33 deletions Library/Homebrew/livecheck/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,46 +179,49 @@ def self.page_headers(url)
def self.page_content(url)
original_url = url

stdout, stderr, status = curl_with_workarounds(
*PAGE_CONTENT_CURL_ARGS, url,
**DEFAULT_CURL_OPTIONS
)

unless status.success?
/^(?<error_msg>curl: \(\d+\) .+)/ =~ stderr
return {
messages: [error_msg.presence || "cURL failed without an error"],
}
end

# stdout contains the header information followed by the page content.
# We use #scrub here to avoid "invalid byte sequence in UTF-8" errors.
output = stdout.scrub
stderr = nil
[:default, :browser].each do |user_agent|
stdout, stderr, status = curl_with_workarounds(
*PAGE_CONTENT_CURL_ARGS, url,
**DEFAULT_CURL_OPTIONS,
user_agent: user_agent
)
next unless status.success?

# Separate the head(s)/body and identify the final URL (after any
# redirections)
max_iterations = 5
iterations = 0
output = output.lstrip
while output.match?(%r{\AHTTP/[\d.]+ \d+}) && output.include?(HTTP_HEAD_BODY_SEPARATOR)
iterations += 1
raise "Too many redirects (max = #{max_iterations})" if iterations > max_iterations
# stdout contains the header information followed by the page content.
# We use #scrub here to avoid "invalid byte sequence in UTF-8" errors.
output = stdout.scrub

head_text, _, output = output.partition(HTTP_HEAD_BODY_SEPARATOR)
# Separate the head(s)/body and identify the final URL (after any
# redirections)
max_iterations = 5
iterations = 0
output = output.lstrip
while output.match?(%r{\AHTTP/[\d.]+ \d+}) && output.include?(HTTP_HEAD_BODY_SEPARATOR)
iterations += 1
raise "Too many redirects (max = #{max_iterations})" if iterations > max_iterations

head_text, _, output = output.partition(HTTP_HEAD_BODY_SEPARATOR)
output = output.lstrip

location = head_text[/^Location:\s*(.*)$/i, 1]
next if location.blank?
location = head_text[/^Location:\s*(.*)$/i, 1]
next if location.blank?

location.chomp!
# Convert a relative redirect URL to an absolute URL
location = URI.join(url, location) unless location.match?(PageMatch::URL_MATCH_REGEX)
final_url = location
end

location.chomp!
# Convert a relative redirect URL to an absolute URL
location = URI.join(url, location) unless location.match?(PageMatch::URL_MATCH_REGEX)
final_url = location
data = { content: output }
data[:final_url] = final_url if final_url.present? && final_url != original_url
return data
end

data = { content: output }
data[:final_url] = final_url if final_url.present? && final_url != original_url
data
/^(?<error_msg>curl: \(\d+\) .+)/ =~ stderr
{
messages: [error_msg.presence || "cURL failed without an error"],
}
end
end
end
Expand Down