Curl: Rename :status to :status_code

The return hash from `#curl_http_content_headers_and_checksum`
contains a `:status`, which is the status code of the last response.
This string value comes from `#parse_curl_response`, where the key is
`:status_code` instead.

Aligning these keys technically allows us to pass either of these
hashes to the `#url_protected_by_*` methods, as both contain
`:status_code` and `:headers` in the expected format.
This commit is contained in:
Sam Ford 2022-05-25 13:31:54 -04:00
parent 1d1474a03f
commit 7b23bc64e5
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
2 changed files with 12 additions and 12 deletions

View File

@ -14,7 +14,7 @@ describe "Utils::Curl" do
details[:normal][:no_cookie] = { details[:normal][:no_cookie] = {
url: "https://www.example.com/", url: "https://www.example.com/",
final_url: nil, final_url: nil,
status: "403", status_code: "403",
headers: { headers: {
"age" => "123456", "age" => "123456",
"cache-control" => "max-age=604800", "cache-control" => "max-age=604800",
@ -35,7 +35,7 @@ describe "Utils::Curl" do
} }
details[:normal][:ok] = Marshal.load(Marshal.dump(details[:normal][:no_cookie])) details[:normal][:ok] = Marshal.load(Marshal.dump(details[:normal][:no_cookie]))
details[:normal][:ok][:status] = "200" details[:normal][:ok][:status_code] = "200"
details[:normal][:single_cookie] = Marshal.load(Marshal.dump(details[:normal][:no_cookie])) details[:normal][:single_cookie] = Marshal.load(Marshal.dump(details[:normal][:no_cookie]))
details[:normal][:single_cookie][:headers]["set-cookie"] = "a_cookie=for_testing" details[:normal][:single_cookie][:headers]["set-cookie"] = "a_cookie=for_testing"
@ -52,7 +52,7 @@ describe "Utils::Curl" do
details[:cloudflare][:single_cookie] = { details[:cloudflare][:single_cookie] = {
url: "https://www.example.com/", url: "https://www.example.com/",
final_url: nil, final_url: nil,
status: "403", status_code: "403",
headers: { headers: {
"date" => "Wed, 1 Jan 2020 01:23:45 GMT", "date" => "Wed, 1 Jan 2020 01:23:45 GMT",
"content-type" => "text/plain; charset=UTF-8", "content-type" => "text/plain; charset=UTF-8",

View File

@ -205,7 +205,7 @@ module Utils
sig { params(details: T::Hash[Symbol, T.untyped]).returns(T::Boolean) } sig { params(details: T::Hash[Symbol, T.untyped]).returns(T::Boolean) }
def url_protected_by_cloudflare?(details) def url_protected_by_cloudflare?(details)
return false if details[:headers].blank? return false if details[:headers].blank?
return false unless [403, 503].include?(details[:status].to_i) return false unless [403, 503].include?(details[:status_code].to_i)
set_cookie_header = Array(details[:headers]["set-cookie"]) set_cookie_header = Array(details[:headers]["set-cookie"])
has_cloudflare_cookie_header = set_cookie_header.compact.any? do |cookie| has_cloudflare_cookie_header = set_cookie_header.compact.any? do |cookie|
@ -228,7 +228,7 @@ module Utils
sig { params(details: T::Hash[Symbol, T.untyped]).returns(T::Boolean) } sig { params(details: T::Hash[Symbol, T.untyped]).returns(T::Boolean) }
def url_protected_by_incapsula?(details) def url_protected_by_incapsula?(details)
return false if details[:headers].blank? return false if details[:headers].blank?
return false if details[:status].to_i != 403 return false if details[:status_code].to_i != 403
set_cookie_header = Array(details[:headers]["set-cookie"]) set_cookie_header = Array(details[:headers]["set-cookie"])
set_cookie_header.compact.any? { |cookie| cookie.match?(/^(visid_incap|incap_ses)_/i) } set_cookie_header.compact.any? { |cookie| cookie.match?(/^(visid_incap|incap_ses)_/i) }
@ -255,7 +255,7 @@ module Utils
next next
end end
next unless http_status_ok?(secure_details[:status]) next unless http_status_ok?(secure_details[:status_code])
hash_needed = true hash_needed = true
user_agents = [user_agent] user_agents = [user_agent]
@ -273,20 +273,20 @@ module Utils
use_homebrew_curl: use_homebrew_curl, use_homebrew_curl: use_homebrew_curl,
user_agent: user_agent, user_agent: user_agent,
) )
break if http_status_ok?(details[:status]) break if http_status_ok?(details[:status_code])
end end
unless details[:status] unless details[:status_code]
# Hack around https://github.com/Homebrew/brew/issues/3199 # Hack around https://github.com/Homebrew/brew/issues/3199
return if MacOS.version == :el_capitan return if MacOS.version == :el_capitan
return "The #{url_type} #{url} is not reachable" return "The #{url_type} #{url} is not reachable"
end end
unless http_status_ok?(details[:status]) unless http_status_ok?(details[:status_code])
return if url_protected_by_cloudflare?(details) || url_protected_by_incapsula?(details) return if url_protected_by_cloudflare?(details) || url_protected_by_incapsula?(details)
return "The #{url_type} #{url} is not reachable (HTTP status code #{details[:status]})" return "The #{url_type} #{url} is not reachable (HTTP status code #{details[:status_code]})"
end end
if url.start_with?("https://") && Homebrew::EnvConfig.no_insecure_redirect? && if url.start_with?("https://") && Homebrew::EnvConfig.no_insecure_redirect? &&
@ -296,7 +296,7 @@ module Utils
return unless secure_details return unless secure_details
return if !http_status_ok?(details[:status]) || !http_status_ok?(secure_details[:status]) return if !http_status_ok?(details[:status_code]) || !http_status_ok?(secure_details[:status_code])
etag_match = details[:etag] && etag_match = details[:etag] &&
details[:etag] == secure_details[:etag] details[:etag] == secure_details[:etag]
@ -397,7 +397,7 @@ module Utils
{ {
url: url, url: url,
final_url: final_url, final_url: final_url,
status: status_code, status_code: status_code,
headers: headers, headers: headers,
etag: etag, etag: etag,
content_length: content_length, content_length: content_length,