Parent

Included Modules

HTTP::Client

Clients make requests and receive responses

Constants

BUFFER_SIZE

Input buffer size

Attributes

default_options[R]

Public Class Methods

new(default_options = {}) click to toggle source
# File lib/http/client.rb, line 16
def initialize(default_options = {})
  @default_options = HTTP::Options.new(default_options)
  @parser = HTTP::Response::Parser.new
  @socket = nil
end

Public Instance Methods

perform(req, options) click to toggle source

Perform a single (no follow) HTTP request

# File lib/http/client.rb, line 43
def perform(req, options)
  # 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 = options[:socket_class].open(req.socket_host, req.socket_port)
  @socket = start_tls(@socket, options) 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) click to toggle source

Read a chunk of the body

# 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 = {}) click to toggle source

Make an HTTP request

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.