Class: CurlDownloadStrategy

Inherits:
AbstractFileDownloadStrategy show all
Includes:
Utils::Curl
Defined in:
download_strategy.rb

Overview

Strategy for downloading files using curl.

Instance Attribute Summary collapse

Attributes inherited from AbstractDownloadStrategy

#cache, #cached_location, #source_modified_time, #url

Instance Method Summary collapse

Methods included from Utils::Curl

clear_path_cache, curl, curl_args, curl_check_http_content, curl_download, curl_executable, curl_headers, curl_http_content_headers_and_checksum, curl_output, curl_path, curl_response_follow_redirections, curl_response_last_location, curl_supports_tls13?, curl_with_workarounds, http_status_ok?, parse_curl_output, url_protected_by_cloudflare?, url_protected_by_incapsula?

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Methods included from Kernel

#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #with_env, #with_homebrew_path

Methods inherited from AbstractFileDownloadStrategy

#basename, #cached_location, #symlink_location, #temporary_path

Methods inherited from AbstractDownloadStrategy

#basename, #quiet!, #quiet?, #shutup!, #stage

Methods included from Context

current, current=, #debug?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(url, name, version, **meta) ⇒ CurlDownloadStrategy

Returns a new instance of CurlDownloadStrategy.



384
385
386
387
388
389
390
391
392
393
394
395
# File 'download_strategy.rb', line 384

def initialize(url, name, version, **meta)
  @try_partial = true
  @mirrors = meta.fetch(:mirrors, [])

  # Merge `:header` with `:headers`.
  if (header = meta.delete(:header))
    meta[:headers] ||= []
    meta[:headers] << header
  end

  super
end

Instance Attribute Details

#mirrorsObject (readonly)



382
383
384
# File 'download_strategy.rb', line 382

def mirrors
  @mirrors
end

Instance Method Details

#clear_cacheObject



461
462
463
464
# File 'download_strategy.rb', line 461

def clear_cache
  super
  rm_rf(temporary_path)
end

#fetch(timeout: nil) ⇒ Object

Download and cache the file at AbstractFileDownloadStrategy#cached_location.



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'download_strategy.rb', line 400

def fetch(timeout: nil)
  end_time = Time.now + timeout if timeout

  download_lock = LockFile.new(temporary_path.basename)
  download_lock.lock

  urls = [url, *mirrors]

  begin
    url = urls.shift

    if (domain = Homebrew::EnvConfig.artifact_domain)
      url = url.sub(%r{^https?://#{GitHubPackages::URL_DOMAIN}/}o, "#{domain.chomp("/")}/")
    end

    ohai "Downloading #{url}"

    use_cached_location = cached_location.exist?
    use_cached_location = false if version.respond_to?(:latest?) && version.latest?

    resolved_url, _, last_modified, _, is_redirection = begin
      resolve_url_basename_time_file_size(url, timeout: Utils::Timer.remaining!(end_time))
    rescue ErrorDuringExecution
      raise unless use_cached_location
    end

    # Authorization is no longer valid after redirects
    meta[:headers]&.delete_if { |header| header.start_with?("Authorization") } if is_redirection

    # The cached location is no longer fresh if Last-Modified is after the file's timestamp
    use_cached_location = false if cached_location.exist? && last_modified && last_modified > cached_location.mtime

    if use_cached_location
      puts "Already downloaded: #{cached_location}"
    else
      begin
        _fetch(url:, resolved_url:, timeout: Utils::Timer.remaining!(end_time))
      rescue ErrorDuringExecution
        raise CurlDownloadStrategyError, url
      end
      ignore_interrupts do
        cached_location.dirname.mkpath
        temporary_path.rename(cached_location)
        symlink_location.dirname.mkpath
      end
    end

    FileUtils.ln_s cached_location.relative_path_from(symlink_location.dirname), symlink_location, force: true
  rescue CurlDownloadStrategyError
    raise if urls.empty?

    puts "Trying a mirror..."
    retry
  rescue Timeout::Error => e
    raise Timeout::Error, "Timed out downloading #{self.url}: #{e}"
  end
ensure
  download_lock&.unlock
  download_lock&.path&.unlink
end

#resolved_time_file_size(timeout: nil) ⇒ Object



466
467
468
469
# File 'download_strategy.rb', line 466

def resolved_time_file_size(timeout: nil)
  _, _, time, file_size = resolve_url_basename_time_file_size(url, timeout:)
  [time, file_size]
end