From 793ea17749a9a12d9d76b07dc10e47a7e9d3c201 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Fri, 4 Jun 2021 13:12:01 -0400 Subject: [PATCH] Strategy: Create constant for HTTP separator --- Library/Homebrew/livecheck/strategy.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index 3a1f2a423f..b69de957cb 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -19,7 +19,15 @@ module Homebrew # the middle of this range. Strategies with a priority of 0 (or lower) # are ignored. DEFAULT_PRIORITY = 5 - private_constant :DEFAULT_PRIORITY + + # HTTP response head(s) and body are typically separated by a double + # `CRLF` (whereas HTTP header lines are separated by a single `CRLF`). + # In rare cases, this can also be a double newline (`\n\n`). + HTTP_HEAD_BODY_SEPARATOR = "\r\n\r\n" + + # The `#strategies` method expects `Strategy` constants to be strategies, + # so constants we create need to be private for this to work properly. + private_constant :DEFAULT_PRIORITY, :HTTP_HEAD_BODY_SEPARATOR # Creates and/or returns a `@strategies` `Hash`, which maps a snake # case strategy name symbol (e.g. `:page_match`) to the associated @@ -166,11 +174,11 @@ module Homebrew max_iterations = 5 iterations = 0 output = output.lstrip - while output.match?(%r{\AHTTP/[\d.]+ \d+}) && output.include?("\r\n\r\n") + 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("\r\n\r\n") + head_text, _, output = output.partition(HTTP_HEAD_BODY_SEPARATOR) output = output.lstrip location = head_text[/^Location:\s*(.*)$/i, 1]