You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: AWS::Core::Http::NetHttpHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/http/net_http_handler.rb

Overview

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.

Defined Under Namespace

Classes: TruncatedBodyError

Constant Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionPool

Returns a connection pool constructed from the given options. Calling this method twice with the same options will return the same pool.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :proxy_uri (URI::HTTP, String)

    A proxy to send requests through. Formatted like 'http://proxy.com:123'.

  • :http_continue_timeout (Float) — default: nil

    The number of seconds to wait for a 100-continue response before sending the request body. This option has no effect unless the request has "Expect" header set to "100-continue". Defaults to nil which disables this behaviour. This value can safely be set per-request on the session yeidled by #session_for.

  • :http_idle_timeout (Float) — default: 15

    The number of seconds a connection is allowed to sit idble before it is considered stale. Stale connections are closed and removed from the pool before making a request.

  • :http_open_timeout (Float) — default: 15

    The number of seconds to wait when opening a HTTP session before rasing a Timeout::Error.

  • :http_read_timeout (Integer) — default: 60

    The default number of seconds to wait for response data. This value can safely be set per-request on the session yeidled by #session_for.

  • :http_wire_trace (Boolean) — default: false

    When true, HTTP debug output will be sent to the :logger.

  • :logger (Logger)

    Where debug output is sent. Defaults to nil when :http_wire_trace is false. Defaults to Logger.new($stdout) when :http_wire_trace is true.

  • :ssl_verify_peer (Boolean) — default: true

    When true, SSL peer certificates are verified when establishing a connection.

  • :ssl_ca_file (String)

    Full path to the SSL certificate authority bundle file that should be used when verifying peer certificates. If you do not pass :ssl_ca_file or :ssl_ca_path the the system default will be used if available.

  • :ssl_ca_path (String)

    Full path of the directory that contains the unbundled SSL certificate authority files# for verifying peer certificates. If you do not pass :ssl_ca_file or :ssl_ca_path the the system default will be used if available.

  • :ssl_cert_store (String)


37
38
39
40
# File 'lib/aws/core/http/net_http_handler.rb', line 37

def initialize options = {}
  @pool = options[:connection_pool] || ConnectionPool.new(options)
  @verify_content_length = options[:verify_response_body_content_length]
end

Instance Attribute Details

#poolConnectionPool (readonly)

Returns:

  • (ConnectionPool)


43
44
45
# File 'lib/aws/core/http/net_http_handler.rb', line 43

def pool
  @pool
end

Instance Method Details

#handle(request, response, &read_block) ⇒ nil

Given a populated request object and an empty response object, this method will make the request and them populate the response.

Parameters:

Returns:

  • (nil)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aws/core/http/net_http_handler.rb', line 51

def handle request, response, &read_block
  retry_possible = true

  begin

    @pool.session_for(request.endpoint) do |http|

      http.read_timeout = request.read_timeout
      http.continue_timeout = request.continue_timeout if
        http.respond_to?(:continue_timeout=)

      exp_length = nil
      act_length = 0
      http.request(build_net_http_request(request)) do |net_http_resp|
        response.status = net_http_resp.code.to_i
        response.headers = net_http_resp.to_hash
        exp_length = determine_expected_content_length(response)
        if block_given? and response.status < 300
          net_http_resp.read_body do |data|
            begin
              act_length += data.bytesize
              yield data unless data.empty?
            ensure
              retry_possible = false
            end
          end
        else
          response.body = net_http_resp.read_body
          act_length += response.body.bytesize unless response.body.nil?
        end
      end
      run_check = exp_length && request.http_method != "HEAD" && @verify_content_length
      if run_check && act_length != exp_length
        raise TruncatedBodyError, 'content-length does not match'
      end
    end

  rescue *NETWORK_ERRORS => error
    raise error unless retry_possible
    response.network_error = error
  end
  nil
end