Class: HTTP::Client

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

Overview

Clients make requests and receive responses

Constant Summary collapse

BUFFER_SIZE =

Input buffer size

4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chainable

#accept, #connect, #default_callbacks, #default_callbacks=, #default_headers, #default_headers=, #default_options=, #delete, #get, #head, #on, #options, #patch, #post, #put, #trace, #via, #with_follow, #with_headers, #with_response

Constructor Details

#initialize(default_options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/http/client.rb', line 12

def initialize(default_options = {})
  @default_options = Options.new(default_options)
end

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



10
11
12
# File 'lib/http/client.rb', line 10

def default_options
  @default_options
end

Instance Method Details

#body(opts, headers) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/http/client.rb', line 16

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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/http/client.rb', line 104

def format_response(method, response, option)
  case option
  when :auto, NilClass
    method == :head ? response : response.parse_body
  when :object
    response
  when :parsed_body
    response.parse_body
  when :body
    response.body
  else raise ArgumentError, "invalid response type: #{option}"
  end
end

#perform(request, options) ⇒ Object



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
95
96
97
98
99
100
101
102
# File 'lib/http/client.rb', line 60

def perform(request, options)
  parser = Http::Response::Parser.new
  uri, proxy = request.uri, request.proxy
  socket = options[:socket_class].open(uri.host, uri.port) # TODO: proxy support

  if uri.is_a?(URI::HTTPS)
    if options[:ssl_context] == nil
      context = OpenSSL::SSL::SSLContext.new
    else
      context = options[:ssl_context]
    end
    socket = options[: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
    # TODO: handle errors
    raise "zomg IO troubles: #{$!.message}"
  end

  response = Http::Response.new(parser.status_code, parser.http_version, parser.headers) do
    if @body_remaining and @body_remaining > 0
      chunk = parser.chunk
      unless chunk
        parser << socket.readpartial(BUFFER_SIZE)
        chunk = parser.chunk
        return unless chunk
      end

      @body_remaining -= chunk.length
      @body_remaining = nil if @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



26
27
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
54
55
56
57
58
# File 'lib/http/client.rb', line 26

def request(method, uri, options = {})
  opts = @default_options.merge(options)
  host = URI.parse(uri).host
  opts.headers["Host"] = host
  headers = opts.headers
  proxy = opts.proxy

  method_body = body(opts, headers)
  request = Request.new method, uri, headers, proxy, method_body

  if opts.follow
    code = 302
    while code == 302 or code == 301
      # if the uri isn't fully formed complete it
      if not uri.match /\./
        uri = "http://#{host}#{uri}"
      end
      host = URI.parse(uri).host
      opts.headers["Host"] = host
      method_body = body(opts, headers)
      request = Request.new method, uri, headers, proxy, method_body
      response = perform request, opts
      code = response.code
      uri = response.headers["Location"]
    end
  end

  opts.callbacks[:request].each { |c| c.call(request) }
  response = perform request, opts
  opts.callbacks[:response].each { |c| c.call(response) }

  format_response method, response, opts.response
end