Method: Excon::Connection#request

Defined in:
lib/excon/connection.rb

#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] :path appears after 'scheme://host:port/'
  @option params [Hash]   :query appended to the 'scheme://host:port/path/' in the form of '?key=value'


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/excon/connection.rb', line 214

def request(params={}, &block)
  params = validate_params(:request, params)
  # @data has defaults, merge in new params to override
  datum = @data.merge(params)
  datum[:headers] = @data[:headers].merge(datum[:headers] || {})

  if datum[:user] || datum[:password]
    user, pass = Utils.unescape_form(datum[:user].to_s), Utils.unescape_form(datum[:password].to_s)
    datum[:headers]['Authorization'] ||= 'Basic ' + ["#{user}:#{pass}"].pack('m').delete(Excon::CR_NL)
  end

  if datum[:scheme] == UNIX
    datum[:headers]['Host']   = ''
  else
    datum[:headers]['Host']   ||= datum[:host] + port_string(datum)
  end
  datum[:retries_remaining] ||= datum[:retry_limit]

  # if path is empty or doesn't start with '/', insert one
  unless datum[:path][0, 1] == '/'
    datum[:path] = datum[:path].dup.insert(0, '/')
  end

  if block_given?
    Excon.display_warning('Excon requests with a block are deprecated, pass :response_block instead.')
    datum[:response_block] = Proc.new
  end

  datum[:connection] = self

  datum[:stack] = datum[:middlewares].map do |middleware|
    lambda {|stack| middleware.new(stack)}
  end.reverse.inject(self) do |middlewares, middleware|
    middleware.call(middlewares)
  end
  datum = datum[:stack].request_call(datum)

  unless datum[:pipeline]
    datum = response(datum)

    if datum[:persistent]
      if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Connection') == 0 }
        if datum[:response][:headers][key].casecmp('close') == 0
          reset
        end
      end
    else
      reset
    end

    Excon::Response.new(datum[:response])
  else
    datum
  end
rescue => error
  reset
  datum[:error] = error
  if datum[:stack]
    datum[:stack].error_call(datum)
  else
    raise error
  end
end