Class: HTTP::Client

### Home Classes Methods

### In Files

- lib/http/client.rb

### Parent

Object

### Methods

- ::new
- #perform
- #readpartial
- #request

### Included Modules

- HTTP::Chainable

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Chainable
- HTTP::Client
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::ResponseError
- HTTP::StateError
- Object

No matching classes.

# 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 17
    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 44
    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, uri.host, options) if uri.is_a?(URI::HTTPS) && !req.using_proxy?

      req.stream @socket

      read_headers!

      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

@return [String] data chunk @return [Nil] when no more data left

    # File lib/http/client.rb, line 73
    def readpartial(size = BUFFER_SIZE)
      return unless @socket

      begin
        read_more size
        finished = @parser.finished?
      rescue EOFError
        finished = true
      end

      chunk = @parser.chunk

      finish_response if finished

      chunk.to_s
    end

request(verb, uri, opts = {}) click to toggle source

Make an HTTP request

    # File lib/http/client.rb, line 24
    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.