Class: HTTP::Client
Overview
Clients make requests and receive responses
Constant Summary collapse
- BUFFER_SIZE =
Input buffer size
16_384
Instance Attribute Summary collapse
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
Instance Method Summary collapse
-
#initialize(default_options = {}) ⇒ Client
constructor
A new instance of Client.
-
#perform(req, options) ⇒ Object
Perform a single (no follow) HTTP request.
-
#readpartial(size = BUFFER_SIZE) ⇒ Object
Read a chunk of the body.
-
#request(verb, uri, opts = {}) ⇒ Object
Make an HTTP request.
Methods included from Chainable
#accept, #auth, #connect, #default_callbacks, #default_callbacks=, #default_headers, #default_headers=, #default_options=, #delete, #follow, #get, #head, #options, #patch, #post, #put, #stream, #trace, #via, #with_headers
Constructor Details
Instance Attribute Details
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
14 15 16 |
# File 'lib/http/client.rb', line 14 def @default_options end |
Instance Method Details
#perform(req, options) ⇒ Object
Perform a single (no follow) HTTP request
43 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 |
# File 'lib/http/client.rb', line 43 def perform(req, ) # finish previous response if client was re-used # TODO: this is pretty wrong, as socket shoud be part of response # connection, so that re-use of client will not break multiple # chunked responses finish_response uri = req.uri # TODO: keep-alive support @socket = [:socket_class].open(req.socket_host, req.socket_port) @socket = start_tls(@socket, ) if uri.is_a?(URI::HTTPS) req.stream @socket begin read_more BUFFER_SIZE until @parser.headers rescue IOError, Errno::ECONNRESET, Errno::EPIPE => ex raise IOError, "problem making HTTP request: #{ex}" end body = Response::Body.new(self) res = Response.new(@parser.status_code, @parser.http_version, @parser.headers, body, uri) finish_response if :head == req.verb res end |
#readpartial(size = BUFFER_SIZE) ⇒ Object
Read a chunk of the body
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/http/client.rb', line 73 def readpartial(size = BUFFER_SIZE) return unless @socket read_more size chunk = @parser.chunk finish_response if @parser.finished? chunk.to_s end |
#request(verb, uri, opts = {}) ⇒ Object
Make an HTTP request
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/http/client.rb', line 23 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) req = HTTP::Request.new(verb, uri, headers, proxy, body) res = perform req, opts if opts.follow res = Redirector.new(opts.follow).perform req, res do |request| perform request, opts end end res end |