Class: URI::HTTP
- Inherits:
-
Object
- Object
- URI::HTTP
- Defined in:
- lib/buildr/override/core/transports.rb
Overview
:nodoc:
Instance Method Summary collapse
Instance Method Details
#read(options = nil, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/buildr/override/core/transports.rb', line 6 def read( = nil, &block) ||= {} user = self.user || [:username] password = self.password || [:password] connect do |http| trace "Requesting #{self}" headers = { 'If-Modified-Since' => CGI.rfc1123_date([:modified].utc) } if [:modified] request = Net::HTTP::Get.new(request_uri.empty? ? '/' : request_uri, headers) request.basic_auth user, password if user http.request request do |response| case response when Net::HTTPNotModified # No modification, nothing to do. trace 'Not modified since last download' return nil when Net::HTTPRedirection # Try to download from the new URI, handle relative redirects. trace "Redirected to #{response['Location']}" rself = self + URI.parse(response['Location']) rself.user, rself.password = user, password return rself.read(, &block) when Net::HTTPOK info "Downloading #{self}" result = nil [:progress], path.split('/').last, response.content_length do |progress| if block response.read_body do |chunk| block.call chunk progress << chunk end else result = '' response.read_body do |chunk| result << chunk progress << chunk end end end return result when Net::HTTPNotFound raise NotFoundError, "Looking for #{self} and all I got was a 404!" else raise RuntimeError, "Failed to download #{self}: #{response.}" end end end end |