Class: Coolio::Http

Inherits:
HttpClient
  • Object
show all
Defined in:
lib/cool.io/http.rb,
lib/cool.io/http/payload.rb

Direct Known Subclasses

HttpFiber

Defined Under Namespace

Modules: Payload Classes: Response

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Http

Returns a new instance of Http.



47
48
49
50
# File 'lib/cool.io/http.rb', line 47

def initialize socket
  super
  @http_data = []
end

Class Method Details

.connect(host, port, ssl = false) ⇒ Object



37
38
39
40
41
# File 'lib/cool.io/http.rb', line 37

def self.connect host, port, ssl=false
  http = super(host, port)
  http.extend(Coolio::SSL) if ssl
  http
end

.request(opts = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cool.io/http.rb', line 15

def self.request opts={}, &block
  method  = opts[:method]  || opts['method']  || :get
  url     = opts[:url]     || opts['url']
  payload = opts[:payload] || opts['payload'] || {}
  headers = opts[:headers] || opts['headers'] || {}
  query   = opts[:query]   || opts['query']   || {}
  loop    = opts[:loop]    || opts['loop']    || Coolio::Loop.default

  uri  = URI.parse(url)
  path = if uri.path.strip.empty? then '/' else uri.path end
  q    = (uri.query || '').split('&').inject({}){ |r, i|
           k, v = i.split('=')
           r[k] = v
           r}.merge(query.inject({}){ |r, (k, v)| r[k.to_s] = v.to_s; r })
  p    = Payload.generate(payload)

  connect(uri.host, uri.port, uri.scheme.downcase == 'https').attach(loop).
    request(method.to_s.upcase, path, :query => q,
                                      :head  => p.headers.merge(headers),
                                      :body  => p.read, &block)
end

Instance Method Details

#on_body_data(data) ⇒ Object



61
62
63
# File 'lib/cool.io/http.rb', line 61

def on_body_data data
  @http_data << data
end

#on_connectObject



72
73
74
75
# File 'lib/cool.io/http.rb', line 72

def on_connect
  ssl_client_start if respond_to?(:ssl_socket)
  super
end

#on_request_completeObject



65
66
67
68
69
70
# File 'lib/cool.io/http.rb', line 65

def on_request_complete
  super
  @http_callback.call(Response.new(@http_data.join,
                                   @http_response_header,
                                   @http_response_header.http_status.to_i))
end

#on_response_header(response_header) ⇒ Object



57
58
59
# File 'lib/cool.io/http.rb', line 57

def on_response_header response_header
  @http_response_header = response_header
end

#request(method, path, opts = {}, &block) ⇒ Object



52
53
54
55
# File 'lib/cool.io/http.rb', line 52

def request method, path, opts={}, &block
  @http_callback = block
  super
end

#ssl?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cool.io/http.rb', line 43

def ssl?
  false
end