lib/core/transports.rb in buildr-0.21.0 vs lib/core/transports.rb in buildr-0.22.0

- old
+ new

@@ -1,5 +1,6 @@ +require "cgi" require "net/http" require "net/ssh" require "net/sftp" require "uri" require "uri/sftp" @@ -112,13 +113,13 @@ @base_path = @uri.path || "/" @base_path += "/" unless @base_path[-1] == ?/ @options = options || {} end - # Downloads a file from the specified path, relative to the - # server URI. Downloads to either the target file, or by - # calling the block with each chunk of the file. + # Downloads a file from the specified path, relative to the server URI. + # Downloads to either the target file, or by calling the block with each + # chunk of the file. Returns the file's modified timestamp, or now. # # For example: # Transports.perform("http://server/libs") do |http| # http.download("my_project/test.jar", "test.jar") # http.download("my_project/readme") { |text| $stdout.write text } @@ -270,20 +271,28 @@ super @http = Net::HTTP.start(@uri.host, @uri.port) end def download(path, target = nil, &block) - puts "Requesting #{path} from #{@uri}" if Rake.application.options.trace - @http.request_get(@base_path + path) do |response| + puts "Requesting #{@uri}/#{path} " if Rake.application.options.trace + last_modified = File.stat(target).mtime.utc if target && File.exist?(target) + headers = {} + headers["If-Modified-Since"] = CGI.rfc1123_date(last_modified) if last_modified + @http.request_get(@base_path + path, headers) do |response| case response + when Net::HTTPNotModified + # No modification, nothing to do. + puts "Not modified since last download" if Rake.application.options.trace + when Net::HTTPRedirection # Try to download from the new URI, handle relative redirects. - puts "Redirected" if Rake.application.options.trace - Transports.download(@uri + URI.parse(response["location"]), target, @options) + puts "Redirected to #{response['Location']}" if Rake.application.options.trace + last_modified = Transports.download(@uri + URI.parse(response["location"]), target, @options) when Net::HTTPOK puts "Downloading #{@uri}/#{path}" if verbose + last_modified = Time.parse(response["Last-Modified"] || "") with_progress_bar path.split("/").last, response.content_length do |progress| with_digests(@options[:digests]) do |digester| download = proc do |write| # Read the body of the page and write it out. @@ -309,11 +318,12 @@ # worse than not having a file at all, so download to temporary # file and then move over. temp = Tempfile.new(File.basename(target)) download[ proc { |chunk| temp.write chunk } ] temp.close - File.move temp.path, target if target + File.move temp.path, target + File.utime last_modified, last_modified, target else download[ block ] end end @@ -322,9 +332,10 @@ raise NotFound else fail "Failed to download #{@uri}/#{path}: #{response.message}" end end + last_modified end def close() @http.finish @http = nil