From 2722fbe30e20c53665357f7a71bbcf378990302e Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Fri, 22 Apr 2022 13:01:01 -0400 Subject: [PATCH] #parse_curl_output: add max_iterations parameter In cases where there may be more than five responses in `curl` output to parse, we need to be able to control the `max_iterations` of the `while` loop in `#parse_curl_output` to properly parse all the responses. For example, if we pass `--max-redirs 5` to `curl` and there are exactly five redirections before the final response, the output would contain a total of six responses and `#parse_curl_output` wouldn't properly handle this (it would give a `Too many redirects` error). `max_iterations` should be the maximum number of redirections + 1 (to account for any final response after the redirections), so we need to be able to override this value when necessary. --- Library/Homebrew/utils/curl.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index 20792f25a5..07a65d8bd9 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -393,13 +393,17 @@ module Utils # `:status_code`, `:status_text`, and `:headers`. # @param output [String] The output text from `curl` containing HTTP # responses, body content, or both. + # @param max_iterations [Integer] The maximum number of iterations for the + # `while` loop that parses HTTP response text. This should correspond to + # the maximum number of requests in the output. If `curl`'s `--max-redirs` + # option is used, `max_iterations` should be `max-redirs + 1`, to + # account for any final response after the redirections. # @return [Hash] A hash containing an array of response hashes and the body # content, if found. - sig { params(output: String).returns(T::Hash[Symbol, T.untyped]) } - def parse_curl_output(output) + sig { params(output: String, max_iterations: Integer).returns(T::Hash[Symbol, T.untyped]) } + def parse_curl_output(output, max_iterations: 5) responses = [] - max_iterations = 5 iterations = 0 output = output.lstrip while output.match?(%r{\AHTTP/[\d.]+ \d+}) && output.include?(HTTP_RESPONSE_BODY_SEPARATOR)