Class: Excon::Connection
- Inherits:
-
Object
- Object
- Excon::Connection
- Defined in:
- lib/excon/connection.rb
Constant Summary collapse
- CR_NL =
"\r\n"
- HTTP_1_1 =
" HTTP/1.1\r\n"
- FORCE_ENC =
String.respond_to?(:force_encoding)
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
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.
-
#request(params, &block) ⇒ Object
Sends the supplied request to the destination host.
- #reset ⇒ 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/excon/connection.rb', line 19 def initialize(url, params = {}) uri = URI.parse(url) @connection = { :headers => {}, :host => uri.host, :path => uri.path, :port => uri.port, :query => uri.query, :scheme => uri.scheme }.merge!(params) @socket_key = '' << @connection[:host] << ':' << @connection[:port].to_s 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 |
Instance Method Details
#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
44 45 46 47 48 49 50 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 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/excon/connection.rb', line 44 def request(params, &block) begin # connection has defaults, merge in new params to override params = @connection.merge(params) params[:headers] = @connection[:headers].merge(params[:headers] || {}) params[:headers]['Host'] ||= '' << params[:host] << ':' << params[:port].to_s # if path is empty or doesn't start with '/', insert one unless params[:path][0, 1] == '/' params[:path].insert(0, '/') end # start with "METHOD /path" request = params[:method].to_s.upcase << ' ' << params[:path] # add query to path, if there is one case params[:query] when String request << '?' << params[:query] when Hash request << '?' for key, values in params[:query] case values when nil request << key.to_s << '&' when Array for value in values request << key.to_s << '=' << CGI.escape(value.to_s) << '&' end else request << key.to_s << '=' << CGI.escape(values.to_s) << '&' end end request.chop! # remove trailing '&' end # finish first line with "HTTP/1.1\r\n" request << HTTP_1_1 # calculate content length and set to handle non-ascii params[:headers]['Content-Length'] = case params[:body] when File params[:body].binmode File.size(params[:body]) when String params[:body].force_encoding('BINARY') if FORCE_ENC params[:body].length else 0 end # add headers to request for key, values in params[:headers] for value in [*values] request << key.to_s << ': ' << value.to_s << CR_NL end end # add additional "\r\n" to indicate end of headers request << CR_NL # write out the request, sans body socket.write(request) # write out the body if params[:body] if params[:body].is_a?(String) socket.write(params[:body]) else while chunk = params[:body].read(CHUNK_SIZE) socket.write(chunk) end end end # read the response response = Excon::Response.parse(socket, params, &block) if response.headers['Connection'] == 'close' reset end response rescue => socket_error reset raise(Excon::Errors::SocketError.new(socket_error)) end if params.has_key?(:expects) && ![*params[:expects]].include?(response.status) reset raise(Excon::Errors.status_error(params, response)) else response end rescue => request_error if params.has_key?(:idempotent) && (request_error.is_a?(Excon::Errors::SocketError) || (request_error.is_a?(Excon::Errors::HTTPStatusError) && response.status != 404)) retries_remaining ||= 4 retries_remaining -= 1 if retries_remaining > 0 retry else raise(request_error) end else raise(request_error) end end |
#reset ⇒ Object
155 156 157 |
# File 'lib/excon/connection.rb', line 155 def reset (old_socket = sockets.delete(@socket_key)) && old_socket.close end |