Class: UberS3::Connection::NetHttp

Inherits:
Adapter
  • Object
show all
Defined in:
lib/uber-s3/connection/net_http.rb

Instance Attribute Summary

Attributes inherited from Adapter

#access_key, #defaults, #http, #s3, #secret_access_key, #uri

Instance Method Summary collapse

Methods inherited from Adapter

#initialize

Constructor Details

This class inherits a constructor from UberS3::Connection::Adapter

Instance Method Details

#request(verb, url, headers = {}, body = nil) ⇒ 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
# File 'lib/uber-s3/connection/net_http.rb', line 6

def request(verb, url, headers={}, body=nil)
  if verb == :get
    # Support fetching compressed data
    headers['Accept-Encoding'] = 'gzip, deflate'
  end
  
  self.uri = URI.parse(url)

  # Init and open a HTTP connection
  http_connect! if http.nil? || !http.started?

  req_klass = instance_eval("Net::HTTP::"+verb.to_s.capitalize)
  req = req_klass.new(uri.to_s, headers)

  req.body = body if !body.nil? && !body.empty?

  # Make HTTP request
  retries = 2
  begin
    r = http.request(req)
  rescue EOFError, Errno::EPIPE
    # Something happened to our connection, lets try this again
    http_connect!
    retries -= 1
    retry if retries >= 0
  end

  # Auto-decode any gzipped objects
  if verb == :get && r.header['Content-Encoding'] == 'gzip'
    gz = Zlib::GzipReader.new(StringIO.new(r.body))
    response_body = gz.read
  else
    response_body = r.body
  end
  
  UberS3::Response.new({
    :status => r.code.to_i,
    :header => r.header.to_hash,
    :body   => response_body,
    :raw    => r
  })
end