Class: HTTP::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Chainable
Defined in:
lib/http/client.rb

Overview

Clients make requests and receive responses

Constant Summary collapse

CONNECTION =
"Connection".freeze
KEEP_ALIVE =
"Keep-Alive".freeze
CLOSE =
"close".freeze
HTTP_OR_HTTPS_RE =
%r{^https?://}i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chainable

#accept, #auth, #basic_auth, #connect, #default_headers, #default_headers=, #default_options=, #delete, #follow, #get, #head, #options, #patch, #persistent, #post, #put, #stream, #trace, #via, #with_cache, #with_headers

Constructor Details

#initialize(default_options = {}) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
# File 'lib/http/client.rb', line 26

def initialize(default_options = {})
  @default_options = HTTP::Options.new(default_options)
  @connection = nil
  @state = :clean
end

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



24
25
26
# File 'lib/http/client.rb', line 24

def default_options
  @default_options
end

Instance Method Details

#closeObject



97
98
99
100
101
# File 'lib/http/client.rb', line 97

def close
  @connection.close if @connection
  @connection = nil
  @state = :clean
end

#make_request(req, options) ⇒ Object



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
# File 'lib/http/client.rb', line 69

def make_request(req, options)
  verify_connection!(req.uri)

  @state = :dirty

  @connection ||= HTTP::Connection.new(req, options)
  @connection.send_request(req)
  @connection.read_headers!

  res = Response.new(
    @connection.status_code,
    @connection.http_version,
    @connection.headers,
    Response::Body.new(@connection),
    req.uri
  )

  @connection.finish_response if req.verb == :head
  @state = :clean

  res
rescue
  # On any exception we reset the conn. This is a safety measure, to ensure
  # we don't have conns in a bad state resulting in mixed requests/responses
  close if persistent?
  raise
end

#perform(req, options) ⇒ Object

Perform a single (no follow) HTTP request



58
59
60
61
62
# File 'lib/http/client.rb', line 58

def perform(req, options)
  options.cache.perform(req, options) do |r, opts|
    make_request(r, opts)
  end
end

#persistent?Boolean

Returns whenever client is persistent.

Returns:

  • (Boolean)

    whenever client is persistent

See Also:



67
# File 'lib/http/client.rb', line 67

def_delegator :default_options, :persistent?

#request(verb, uri, opts = {}) ⇒ Object

Make an HTTP request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/http/client.rb', line 33

def request(verb, uri, opts = {})
  opts    = @default_options.merge(opts)
  uri     = make_request_uri(uri, opts)
  headers = opts.headers
  proxy   = opts.proxy
  body    = make_request_body(opts, headers)

  # Tell the server to keep the conn open
  if default_options.persistent?
    headers[CONNECTION] = KEEP_ALIVE
  else
    headers[CONNECTION] = CLOSE
  end

  req = HTTP::Request.new(verb, uri, headers, proxy, body)
  res = perform req, opts

  return res unless opts.follow

  Redirector.new(opts.follow).perform req, res do |request|
    perform request, opts
  end
end