From e76e1d3ce4bf65b3c7f56e09dd5f51b79538df18 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 28 Oct 2021 17:42:36 +0900 Subject: [PATCH] Downloader: retry when RFC 2616 noncompliant dates [ci skip] zlib.net rarely returns the current time in RFC 2616 noncompliant format in the response header, and the checksum does not match in that case (maybe creating the tarball on the fly?). --- tool/downloader.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tool/downloader.rb b/tool/downloader.rb index e9ea00c4c4..d3a9f75637 100644 --- a/tool/downloader.rb +++ b/tool/downloader.rb @@ -211,9 +211,15 @@ class Downloader $stdout.print "downloading #{name} ... " $stdout.flush end + mtime = nil + options = options.merge(http_options(file, since.nil? ? true : since)) begin data = with_retry(10) do - url.read(options.merge(http_options(file, since.nil? ? true : since))) + data = url.read(options) + if mtime = data.meta["last-modified"] + mtime = Time.httpdate(mtime) + end + data end rescue OpenURI::HTTPError => http_error if http_error.message =~ /^304 / # 304 Not Modified @@ -237,16 +243,13 @@ class Downloader end raise end - mtime = nil dest = (cache_save && cache && !cache.exist? ? cache : file) dest.parent.mkpath dest.open("wb", 0600) do |f| f.write(data) f.chmod(mode_for(data)) - mtime = data.meta["last-modified"] end if mtime - mtime = httpdate(mtime) dest.utime(mtime, mtime) end if $VERBOSE @@ -328,7 +331,7 @@ class Downloader times = 0 begin block.call - rescue Errno::ETIMEDOUT, SocketError, OpenURI::HTTPError, Net::ReadTimeout, Net::OpenTimeout => e + rescue Errno::ETIMEDOUT, SocketError, OpenURI::HTTPError, Net::ReadTimeout, Net::OpenTimeout, ArgumentError => e raise if e.is_a?(OpenURI::HTTPError) && e.message !~ /^50[023] / # retry only 500, 502, 503 for http error times += 1 if times <= max_times