aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/fetcher.rb
blob: 6f58ca02374107d2283feb76aa52d4e8948f224e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require "open3"

module Hbc
  class Fetcher
    TIMEOUT = 10

    def self.head(url)
      if url.to_s =~ %r{googlecode}
        googlecode_fake_head(url)
      else
        SystemCommand.run("/usr/bin/curl",
                          args: ["--max-time", TIMEOUT, "--silent", "--location", "--head", url]).stdout
      end
    end

    # google code does not properly respond to HTTP HEAD requests, like a jerk
    # this fakes a HEAD by doing a GET, taking the first 20 lines, then running away
    def self.googlecode_fake_head(url)
      command = "curl --max-time #{TIMEOUT} --verbose --location '#{url}' | head -n 20 > /dev/null"
      stderr = Open3.capture3(command)[1]
      stderr.split("\n").grep(%r{^< }).map { |line| line.sub(%r{^< }, "") }.join("\n")
    end
  end
end