Class: HTTP::Client
Overview
Clients make requests and receive responses
Constant Summary collapse
- BUFFER_SIZE =
Input buffer size
4096
Instance Attribute Summary collapse
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
Instance Method Summary collapse
- #body(opts, headers) ⇒ Object
- #format_response(method, response, option) ⇒ Object
-
#initialize(default_options = {}) ⇒ Client
constructor
A new instance of Client.
- #perform(request, options) ⇒ Object
-
#request(method, uri, options = {}) ⇒ Object
Make an HTTP request.
Methods included from Chainable
#accept, #connect, #default_callbacks, #default_callbacks=, #default_headers, #default_headers=, #default_options=, #delete, #get, #head, #on, #options, #patch, #post, #put, #stream, #trace, #via, #with_follow, #with_headers, #with_response
Constructor Details
Instance Attribute Details
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
12 13 14 |
# File 'lib/http/client.rb', line 12 def @default_options end |
Instance Method Details
#body(opts, headers) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/http/client.rb', line 18 def body(opts, headers) if opts.body body = opts.body elsif opts.form headers['Content-Type'] ||= 'application/x-www-form-urlencoded' body = URI.encode_www_form(opts.form) end end |
#format_response(method, response, option) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/http/client.rb', line 96 def format_response(method, response, option) case option when :auto, NilClass if method == :head response else HTTP::Response::BodyDelegator.new(response, response.parse_body) end when :object response when :parsed_body HTTP::Response::BodyDelegator.new(response, response.parse_body) when :body HTTP::Response::BodyDelegator.new(response) else raise ArgumentError, "invalid response type: #{option}" end end |
#perform(request, options) ⇒ Object
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 |
# File 'lib/http/client.rb', line 55 def perform(request, ) parser = HTTP::Response::Parser.new uri = request.uri socket = [:socket_class].open(uri.host, uri.port) # TODO: proxy support if uri.is_a?(URI::HTTPS) if [:ssl_context] == nil context = OpenSSL::SSL::SSLContext.new else context = [:ssl_context] end socket = [:ssl_socket_class].new(socket, context) socket.connect end request.stream socket begin parser << socket.readpartial(BUFFER_SIZE) until parser.headers rescue IOError, Errno::ECONNRESET, Errno::EPIPE => ex raise IOError, "problem making HTTP request: #{ex}" end response = HTTP::Response.new(parser.status_code, parser.http_version, parser.headers) do if !parser.finished? || (@body_remaining && @body_remaining > 0) chunk = parser.chunk || begin parser << socket.readpartial(BUFFER_SIZE) parser.chunk || "" end @body_remaining -= chunk.length if @body_remaining @body_remaining = nil if @body_remaining && @body_remaining < 1 chunk end end @body_remaining = Integer(response['Content-Length']) if response['Content-Length'] response end |
#request(method, uri, options = {}) ⇒ Object
Make an HTTP request
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/http/client.rb', line 28 def request(method, uri, = {}) opts = @default_options.merge() host = URI.parse(uri).host opts.headers["Host"] = host headers = opts.headers proxy = opts.proxy method_body = body(opts, headers) if opts.params && !opts.params.empty? uri="#{uri}?#{URI.encode_www_form(opts.params)}" end request = HTTP::Request.new method, uri, headers, proxy, method_body opts.callbacks[:request].each { |c| c.call(request) } response = perform request, opts if opts.follow response = Redirector.new(opts.follow).perform request, response do |req| perform req, opts end end opts.callbacks[:response].each { |c| c.call(response) } format_response method, response, opts.response end |