Included Modules

Coolio::HttpClient

HTTP client class implemented as a subclass of Coolio::TCPSocket. Encodes requests and allows streaming consumption of the response. Response is parsed with a Ragel-generated whitelist parser which supports chunked HTTP encoding.

Example

loop = Coolio::Loop.default
client = Coolio::HttpClient.connect("www.google.com").attach
client.get('/search', query: {q: 'foobar'})
loop.run

Public Class Methods

connect(addr, port = 80, *args) click to toggle source

Connect to the given server, with port 80 as the default

# File lib/cool.io/http_client.rb, line 137
def self.connect(addr, port = 80, *args)
  super
end
new(socket) click to toggle source
# File lib/cool.io/http_client.rb, line 141
def initialize(socket)
  super

  @parser = HttpClientParser.new
  @parser_nbytes = 0

  @state = :response_header
  @data = ::IO::Buffer.new

  @response_header = HttpResponseHeader.new
  @chunk_header = HttpChunkHeader.new
end

Public Instance Methods

enable() click to toggle source

Enable the HttpClient if it has been disabled

# File lib/cool.io/http_client.rb, line 181
def enable
  super
  dispatch unless @data.empty?
end
on_body_data(data) click to toggle source

Called when part of the body has been read

# File lib/cool.io/http_client.rb, line 191
def on_body_data(data)
  STDOUT.write data
  STDOUT.flush
end
on_close() click to toggle source

called by close

# File lib/cool.io/http_client.rb, line 202
def on_close
  if @state != :finished and @state == :body
    on_request_complete
  end
end
on_error(reason) click to toggle source

Called when an error occurs dispatching the request

# File lib/cool.io/http_client.rb, line 209
def on_error(reason)
  close
  raise RuntimeError, reason
end
on_request_complete() click to toggle source

Called when the request has completed

# File lib/cool.io/http_client.rb, line 197
def on_request_complete
  @state == :finished ? close : @state = :finished
end
on_response_header(response_header) click to toggle source

Called when response header has been received

# File lib/cool.io/http_client.rb, line 187
def on_response_header(response_header)
end
request(method, path, options = {}) click to toggle source

Send an HTTP request and consume the response. Supports the following options:

head: {Key: Value}
  Specify an HTTP header, e.g. {'Connection': 'close'}

query: {Key: Value}
  Specify query string parameters (auto-escaped)

cookies: {Key: Value}
  Specify hash of cookies (auto-escaped)

body: String
  Specify the request body (you must encode it for now)
# File lib/cool.io/http_client.rb, line 169
def request(method, path, options = {})
  raise ArgumentError, "invalid request path" unless /^\// === path
  raise RuntimeError, "request already sent" if @requested

  @method, @path, @options = method, path, options
  @requested = true

  return unless @connected
  send_request
end

Protected Instance Methods

dispatch() click to toggle source

Response processing

# File lib/cool.io/http_client.rb, line 276
def dispatch
  while enabled? and case @state
    when :response_header
      parse_response_header
    when :chunk_header
      parse_chunk_header
    when :chunk_body
      process_chunk_body
    when :chunk_footer
      process_chunk_footer
    when :response_footer
      process_response_footer
    when :body
      process_body
    when :finished, :invalid
      break
    else raise RuntimeError, "invalid state: #{@state}"
    end
  end
on_connect() click to toggle source

Coolio callbacks

# File lib/cool.io/http_client.rb, line 222
def on_connect
  @connected = true
  send_request if @method and @path
end
on_read(data) click to toggle source
# File lib/cool.io/http_client.rb, line 227
def on_read(data)
  @data << data
  dispatch
end
send_request() click to toggle source

Request sending

# File lib/cool.io/http_client.rb, line 236
def send_request
  send_request_header
  send_request_body
end
send_request_body() click to toggle source
# File lib/cool.io/http_client.rb, line 268
def send_request_body
  write @options[:body] if @options[:body]
end
send_request_header() click to toggle source
# File lib/cool.io/http_client.rb, line 241
def send_request_header
  query   = @options[:query]
  head    = @options[:head] ? munge_header_keys(@options[:head]) : {}
  cookies = @options[:cookies]
  body    = @options[:body]

  # Set the Host header if it hasn't been specified already
  head['host'] ||= encode_host

  # Set the Content-Length if it hasn't been specified already and a body was given
  head['content-length'] ||= body ? body.length : 0

  # Set the User-Agent if it hasn't been specified
  head['user-agent'] ||= "Coolio #{Coolio::VERSION}"

  # Default to Connection: close
  head['connection'] ||= 'close'

  # Build the request
  request_header = encode_request(@method, @path, query)
  request_header << encode_headers(head)
  request_header << encode_cookies(cookies) if cookies
  request_header << CRLF

  write request_header
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.