Class: HTTP::Client
- Inherits:
-
Object
- Object
- HTTP::Client
- Extended by:
- Forwardable
- Includes:
- Chainable
- Defined in:
- lib/http/client.rb
Overview
Clients make requests and receive responses
Constant Summary collapse
- KEEP_ALIVE =
"Keep-Alive".freeze
- CLOSE =
"close".freeze
- HTTP_OR_HTTPS_RE =
%r{^https?://}i
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(default_options = {}) ⇒ Client
constructor
A new instance of Client.
-
#perform(req, options) ⇒ Object
Perform a single (no follow) HTTP request.
-
#persistent? ⇒ Boolean
Whenever client is persistent.
-
#request(verb, uri, opts = {}) ⇒ Object
Make an HTTP request.
Methods included from Chainable
#accept, #auth, #basic_auth, #connect, #cookies, #default_headers, #default_headers=, #default_options, #default_options=, #delete, #follow, #get, #head, #headers, #options, #patch, #persistent, #post, #put, #timeout, #trace, #via
Constructor Details
Instance Method Details
#close ⇒ Object
85 86 87 88 89 |
# File 'lib/http/client.rb', line 85 def close @connection.close if @connection @connection = nil @state = :clean end |
#perform(req, options) ⇒ Object
Perform a single (no follow) HTTP request
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 |
# File 'lib/http/client.rb', line 54 def perform(req, ) verify_connection!(req.uri) @state = :dirty @connection ||= HTTP::Connection.new(req, ) unless @connection.failed_proxy_connect? @connection.send_request(req) @connection.read_headers! end 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 |
#persistent? ⇒ Boolean
Returns whenever client is persistent.
51 |
# File 'lib/http/client.rb', line 51 def_delegator :default_options, :persistent? |
#request(verb, uri, opts = {}) ⇒ Object
Make an HTTP request
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/http/client.rb', line 31 def request(verb, uri, opts = {}) opts = @default_options.merge(opts) uri = make_request_uri(uri, opts) headers = make_request_headers(opts) body = make_request_body(opts, headers) proxy = opts.proxy 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 |