Skip to content
Snippets Groups Projects
Commit 12d4226b authored by Markus Reiter's avatar Markus Reiter
Browse files

Remove `_checkurl`.

parent 1cae6dd6
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,6 @@ require "hbc/container"
require "hbc/download"
require "hbc/download_strategy"
require "hbc/exceptions"
require "hbc/fetcher"
require "hbc/installer"
require "hbc/locations"
require "hbc/macos"
......@@ -28,7 +27,6 @@ require "hbc/system_command"
require "hbc/topological_hash"
require "hbc/underscore_supporting_uri"
require "hbc/url"
require "hbc/url_checker"
require "hbc/utils"
require "hbc/verify"
require "hbc/version"
......
......@@ -27,7 +27,6 @@ require "hbc/cli/zap"
require "hbc/cli/abstract_internal_command"
require "hbc/cli/internal_audit_modified_casks"
require "hbc/cli/internal_appcast_checkpoint"
require "hbc/cli/internal_checkurl"
require "hbc/cli/internal_dump"
require "hbc/cli/internal_help"
require "hbc/cli/internal_stanza"
......
module Hbc
class CLI
class InternalCheckurl < AbstractInternalCommand
def run
casks_to_check = args.empty? ? Hbc.all : args.map(&CaskLoader.public_method(:load))
casks_to_check.each do |cask|
odebug "Checking URL for Cask #{cask}"
checker = UrlChecker.new(cask)
checker.run
puts checker.summary
end
end
def self.help
"checks for bad Cask URLs"
end
end
end
end
require "open3"
module Hbc
class Fetcher
TIMEOUT = 10
def self.head(url)
if url.to_s =~ /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(/^< /).map { |line| line.sub(/^< /, "") }.join("\n")
end
end
end
require "hbc/checkable"
require "hbc/fetcher"
module Hbc
class UrlChecker
attr_accessor :cask, :response_status, :headers
include Checkable
def initialize(cask, fetcher = Fetcher)
@cask = cask
@fetcher = fetcher
@headers = {}
end
def summary_header
"url check result for #{cask}"
end
def run
_get_data_from_request
return if errors?
_check_response_status
end
HTTP_RESPONSES = [
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"HTTP/1.1 302 Found",
].freeze
OK_RESPONSES = {
"http" => HTTP_RESPONSES,
"https" => HTTP_RESPONSES,
"ftp" => ["OK"],
}.freeze
def _check_response_status
ok = OK_RESPONSES[cask.url.scheme]
return if ok.include?(@response_status)
add_error "unexpected http response, expecting #{ok.map(&:to_s).join(" or ")}, got #{@response_status}"
end
def _get_data_from_request
response = @fetcher.head(cask.url)
if response.empty?
add_error "timeout while requesting #{cask.url}"
return
end
response_lines = response.split("\n").map(&:chomp)
case cask.url.scheme
when "http", "https" then
@response_status = response_lines.grep(/^HTTP/).last
if @response_status.respond_to?(:strip)
@response_status.strip!
unless response_lines.index(@response_status).nil?
http_headers = response_lines[(response_lines.index(@response_status) + 1)..-1]
http_headers.each do |line|
header_name, header_value = line.split(": ")
@headers[header_name] = header_value
end
end
end
when "ftp" then
@response_status = "OK"
response_lines.each do |line|
header_name, header_value = line.split(": ")
@headers[header_name] = header_value
end
else
add_error "unknown scheme for #{cask.url}"
end
end
end
end
describe Hbc::UrlChecker, :cask do
describe "request processing" do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/basic-cask.rb") }
let(:checker) { Hbc::UrlChecker.new(cask) }
before(:each) do
allow(Hbc::Fetcher).to receive(:head).and_return(response)
checker.run
end
context "with an empty response" do
let(:response) { "" }
it "adds an error" do
expect(checker.errors).to include("timeout while requesting #{cask.url}")
end
end
context "with a valid http response" do
let(:response) {
<<-EOS.undent
HTTP/1.1 200 OK
Content-Type: application/x-apple-diskimage
ETag: "b4208f3e84967be4b078ecaa03fba941"
Content-Length: 23726161
Last-Modified: Sun, 12 Aug 2012 21:17:21 GMT
EOS
}
it "properly populates the response code and headers" do
expect(checker.errors).to be_empty
expect(checker.response_status).to eq("HTTP/1.1 200 OK")
expect(checker.headers).to eq(
"Content-Type" => "application/x-apple-diskimage",
"ETag" => '"b4208f3e84967be4b078ecaa03fba941"',
"Content-Length" => "23726161",
"Last-Modified" => "Sun, 12 Aug 2012 21:17:21 GMT",
)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment