Module: ImageSize::URIReader

Extended by:
HTTPChunkyReader
Defined in:
lib/image_size/uri_reader.rb

Overview

:nodoc:

Defined Under Namespace

Modules: HTTPChunkyReader Classes: BodyReader, RangeReader

Class Method Summary collapse

Methods included from HTTPChunkyReader

chunk_range_header

Methods included from ChunkyReader

#[], #chunk_size

Methods included from Reader

#fetch, open_with_uri, #unpack, #unpack1

Class Method Details

.open(uri, max_redirects = 5) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/image_size/uri_reader.rb', line 72

def open(uri, max_redirects = 5)
  http = nil
  (max_redirects + 1).times do
    unless http && http.address == uri.host && http.port == uri.port
      http.finish if http

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true if uri.scheme == 'https'
      http.start
    end

    response = http.request_get(uri.request_uri, chunk_range_header(0)) do |response_with_unread_body|
      case response_with_unread_body
      when Net::HTTPOK
        return yield BodyReader.new(response_with_unread_body)
      end
    end

    case response
    when Net::HTTPRedirection
      uri += response['location']
    when Net::HTTPPartialContent
      return yield RangeReader.new(http, uri.request_uri, response.body)
    when Net::HTTPRequestedRangeNotSatisfiable
      return yield StringReader.new('')
    else
      raise "Unexpected response: #{response}"
    end
  end

  raise "Too many redirects: #{uri}"
ensure
  http.finish if http.started?
end