Class: Buildr::Transports::HTTP

Inherits:
Transport show all
Defined in:
lib/core/transports.rb

Instance Attribute Summary

Attributes inherited from Transport

#base_path, #options, #uri

Instance Method Summary collapse

Methods inherited from Transport

#mkpath, perform, #upload

Constructor Details

#initialize(url, options) ⇒ HTTP

Returns a new instance of HTTP.



233
234
235
236
# File 'lib/core/transports.rb', line 233

def initialize(url, options)
  super
  @http = Net::HTTP.start(@uri.host, @uri.port)
end

Instance Method Details

#closeObject



293
294
295
296
# File 'lib/core/transports.rb', line 293

def close()
  @http.finish
  @http = nil
end

#download(path, target = nil, &block) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/core/transports.rb', line 238

def download(path, target = nil, &block)
  puts "Requesting #{path} from #{@uri}" if Rake.application.options.trace
  @http.request_get(@base_path + path) do |response|
    case response
    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)

    when Net::HTTPOK
      puts "Downloading #{@uri}/#{path}" if verbose
      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.
            response.read_body do |chunk|
              write[chunk]
              digester << chunk
              progress << chunk 
            end
            # Check server digests before approving the download.
            digester.each do |type, hexdigest|
              @http.request_get("#{@base_path}#{path}.#{type.to_s.downcase}") do |response|
                if Net::HTTPOK === response
                  puts "Checking signature from #{@uri}/#{path}" if Rake.application.options.trace
                  fail "Checksum failure for #{@uri}/#{path}: #{type.to_s.upcase} digest on server did not match downloaded file" unless
                    response.read_body.split.first == hexdigest
                end
              end
            end
          end
      
          if target
            # If download breaks we end up with a partial file which is
            # 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
          else
            download[ block ]
          end

        end
      end
    when Net::HTTPNotFound
      raise NotFound
    else
      fail "Failed to download #{@uri}/#{path}: #{response.message}"
    end
  end
end