Class: Excon::Connection
- Inherits:
-
Object
- Object
- Excon::Connection
- Defined in:
- lib/excon/connection.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
Instance Method Summary collapse
-
#initialize(url, params = {}) ⇒ Connection
constructor
Initializes a new Connection instance @param [String] url The destination URL @param [Hash<Symbol, >] params One or more optional params @option params [String] :body Default text to be sent over a socket.
- #inspect ⇒ Object
-
#request(params, &block) ⇒ Object
Sends the supplied request to the destination host.
- #reset ⇒ Object
- #retry_limit ⇒ Object
- #retry_limit=(new_retry_limit) ⇒ Object
Constructor Details
#initialize(url, params = {}) ⇒ Connection
Initializes a new Connection instance
@param [String] url The destination URL
@param [Hash<Symbol, >] params One or more optional params
@option params [String] :body Default text to be sent over a socket. Only used if :body absent in Connection#request params
@option params [Hash<Symbol, String>] :headers The default headers to supply in a request. Only used if params[:headers] is not supplied to Connection#request
@option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
@option params [String] :path Default path; appears after 'scheme://host:port/'. Only used if params[:path] is not supplied to Connection#request
@option params [Fixnum] :port The port on which to connect, to the destination host
@option params [Hash] :query Default query; appended to the 'scheme://host:port/path/' in the form of '?key=value'. Will only be used if params[:query] is not supplied to Connection#request
@option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
@option params [String] :proxy Proxy server; e.g. 'http://myproxy.com:8888'
@option params [Fixnum] :retry_limit Set how many times we'll retry a failed request. (Default 4)
@option params [Class] :instrumentor Responds to #instrument as in ActiveSupport::Notifications
@option params [String] :instrumentor_name Name prefix for #instrument events. Defaults to 'excon'
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 56 57 58 59 60 61 62 |
# File 'lib/excon/connection.rb', line 19 def initialize(url, params = {}) uri = URI.parse(url) @connection = Excon.defaults.merge({ :host => uri.host, :host_port => '' << uri.host << ':' << uri.port.to_s, :path => uri.path, :port => uri.port.to_s, :query => uri.query, :scheme => uri.scheme, }).merge!(params) # merge does not deep-dup, so make sure headers is not the original @connection[:headers] = @connection[:headers].dup @proxy = nil if @connection[:scheme] == HTTPS && (ENV.has_key?('https_proxy') || ENV.has_key?('HTTPS_PROXY')) @proxy = setup_proxy(ENV['https_proxy'] || ENV['HTTPS_PROXY']) elsif (ENV.has_key?('http_proxy') || ENV.has_key?('HTTP_PROXY')) @proxy = setup_proxy(ENV['http_proxy'] || ENV['HTTP_PROXY']) elsif @connection.has_key?(:proxy) @proxy = setup_proxy(@connection[:proxy]) end if @proxy @connection[:headers]['Proxy-Connection'] ||= 'Keep-Alive' # https credentials happen in handshake if @connection[:scheme] == 'http' && (@proxy[:user] || @proxy[:password]) auth = ['' << @proxy[:user].to_s << ':' << @proxy[:password].to_s].pack('m').delete(Excon::CR_NL) @connection[:headers]['Proxy-Authorization'] = 'Basic ' << auth end end if ENV.has_key?('EXCON_DEBUG') || ENV.has_key?('EXCON_STANDARD_INSTRUMENTOR') @connection[:instrumentor] = Excon::StandardInstrumentor end # Use Basic Auth if url contains a login if uri.user || uri.password @connection[:headers]['Authorization'] ||= 'Basic ' << ['' << uri.user.to_s << ':' << uri.password.to_s].pack('m').delete(Excon::CR_NL) end @socket_key = '' << @connection[:host_port] reset end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/excon/connection.rb', line 3 def connection @connection end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
3 4 5 |
# File 'lib/excon/connection.rb', line 3 def proxy @proxy end |
Instance Method Details
#inspect ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/excon/connection.rb', line 149 def inspect vars = instance_variables.inject({}) do |accum, var| accum.merge!(var.to_sym => instance_variable_get(var)) end if vars[:'@connection'][:headers].has_key?('Authorization') vars[:'@connection'] = vars[:'@connection'].dup vars[:'@connection'][:headers] = vars[:'@connection'][:headers].dup vars[:'@connection'][:headers]['Authorization'] = REDACTED end inspection = '#<Excon::Connection:' inspection << (object_id << 1).to_s(16) vars.each do |key, value| inspection << ' ' << key.to_s << '=' << value.inspect end inspection << '>' inspection end |
#request(params, &block) ⇒ Object
Sends the supplied request to the destination host.
@yield [chunk] @see Response#self.parse
@param [Hash<Symbol, >] params One or more optional params, override defaults set in Connection.new
@option params [String] :body text to be sent over a socket
@option params [Hash<Symbol, String>] :headers The default headers to supply in a request
@option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
@option params [String] :path appears after 'scheme://host:port/'
@option params [Fixnum] :port The port on which to connect, to the destination host
@option params [Hash] :query appended to the 'scheme://host:port/path/' in the form of '?key=value'
@option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/excon/connection.rb', line 74 def request(params, &block) # connection has defaults, merge in new params to override params = @connection.merge(params) params[:host_port] = '' << params[:host] << ':' << params[:port].to_s params[:headers] = @connection[:headers].merge(params[:headers] || {}) params[:headers]['Host'] ||= '' << params[:host_port] # if path is empty or doesn't start with '/', insert one unless params[:path][0, 1] == '/' params[:path].insert(0, '/') end if block_given? $stderr.puts("Excon requests with a block are deprecated, pass :response_block instead (#{caller.first})") params[:response_block] = Proc.new end if params.has_key?(:instrumentor) if (retries_remaining ||= params[:retry_limit]) < params[:retry_limit] event_name = "#{params[:instrumentor_name]}.retry" else event_name = "#{params[:instrumentor_name]}.request" end response = params[:instrumentor].instrument(event_name, params) do request_kernel(params) end params[:instrumentor].instrument("#{params[:instrumentor_name]}.response", response.attributes) response else request_kernel(params) end rescue => request_error if params[:idempotent] && [Excon::Errors::Timeout, Excon::Errors::SocketError, Excon::Errors::HTTPStatusError].any? {|ex| request_error.kind_of? ex } retries_remaining ||= params[:retry_limit] retries_remaining -= 1 if retries_remaining > 0 retry else if params.has_key?(:instrumentor) params[:instrumentor].instrument("#{params[:instrumentor_name]}.error", :error => request_error) end raise(request_error) end else if params.has_key?(:instrumentor) params[:instrumentor].instrument("#{params[:instrumentor_name]}.error", :error => request_error) end raise(request_error) end end |
#reset ⇒ Object
126 127 128 |
# File 'lib/excon/connection.rb', line 126 def reset (old_socket = sockets.delete(@socket_key)) && old_socket.close end |
#retry_limit ⇒ Object
144 145 146 147 |
# File 'lib/excon/connection.rb', line 144 def retry_limit $stderr.puts("Excon::Connection#retry_limit is deprecated, pass :retry_limit to the initializer (#{caller.first})") @connection[:retry_limit] ||= DEFAULT_RETRY_LIMIT end |
#retry_limit=(new_retry_limit) ⇒ Object
139 140 141 142 |
# File 'lib/excon/connection.rb', line 139 def retry_limit=(new_retry_limit) $stderr.puts("Excon::Connection#retry_limit= is deprecated, pass :retry_limit to the initializer (#{caller.first})") @connection[:retry_limit] = new_retry_limit end |