class AWS::Core::Http::NetHttpHandler

NetHttpHandler

This is the default HTTP handler for the aws-sdk gem. It uses Ruby's Net::HTTP to make requests. It uses persistent connections and a connection pool.

Constants

NETWORK_ERRORS

@private

Attributes

pool[R]

@return [Net::HTTP::ConnectionPool]

Public Class Methods

new(options = {}) click to toggle source

(see Net::HTTP::ConnectionPool.new)

# File lib/aws/core/http/net_http_handler.rb, line 41
def initialize options = {}
  @pool = Net::HTTP::ConnectionPool.new(options)
end

Public Instance Methods

handle(request, response, &read_block) click to toggle source

Given a populated request object and an empty response object, this method will make the request and them populate the response. @param [Request] request @param [Response] response @return [nil]

# File lib/aws/core/http/net_http_handler.rb, line 54
def handle request, response, &read_block

  options = {}
  options[:port] = request.port
  options[:ssl] = request.use_ssl?
  options[:proxy_uri] = request.proxy_uri
  options[:ssl_verify_peer] = request.ssl_verify_peer?
  options[:ssl_ca_file] = request.ssl_ca_file if request.ssl_ca_file
  options[:ssl_ca_path] = request.ssl_ca_path if request.ssl_ca_path

  begin

    connection = pool.connection_for(request.host, options)
    connection.read_timeout = request.read_timeout

    connection.request(build_net_http_request(request)) do |http_resp|
      response.status = http_resp.code.to_i
      response.headers = http_resp.to_hash
      if block_given? and response.status < 300
        http_resp.read_body(&read_block)
      else
        response.body = http_resp.read_body
      end
    end

  rescue *NETWORK_ERRORS
    response.network_error = true
  end

  nil

end

Protected Instance Methods

build_net_http_request(request) click to toggle source

Given an AWS::Core::HttpRequest, this method translates it into a Net::HTTPRequest (Get, Put, Post, Head or Delete). @param [Request] request @return [Net::HTTPRequest]

# File lib/aws/core/http/net_http_handler.rb, line 93
def build_net_http_request request

  # Net::HTTP adds a content-type header automatically unless its set
  # and this messes with request signature signing.  Also, it expects
  # all header values to be strings (it call strip on them).
  headers = { 'content-type' => '' }
  request.headers.each_pair do |key,value|
    headers[key] = value.to_s
  end

  request_class = case request.http_method
    when 'GET'    then Net::HTTP::Get
    when 'PUT'    then Net::HTTP::Put
    when 'POST'   then Net::HTTP::Post
    when 'HEAD'   then Net::HTTP::Head
    when 'DELETE' then Net::HTTP::Delete
    else raise "unsupported http method: #{request.http_method}"
    end

  net_http_req = request_class.new(request.uri, headers)
  net_http_req.body_stream = request.body_stream
  net_http_req

end