Class: HTTP::Connection

### Home Classes Methods

### In Files

- lib/http/connection.rb

### Parent

Object

### Methods

- ::new
- #close
- #expired?
- #failed_proxy_connect?
- #finish_response
- #keep_alive?
- #read_headers!
- #readpartial
- #send_request

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Cache
- HTTP::Cache::Headers
- HTTP::Cache::NullCache
- HTTP::Cache::NullLogger
- HTTP::CacheError
- HTTP::Chainable
- HTTP::Client
- HTTP::Connection
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::InvalidHeaderNameError
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::Caching
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Caching
- HTTP::Response::IoBody
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::Response::StringBody
- HTTP::ResponseError
- HTTP::StateError
- HTTP::Timeout
- HTTP::Timeout::Global
- HTTP::Timeout::Null
- HTTP::Timeout::PerOperation
- HTTP::TimeoutError
- HTTP::URI
- Object

No matching classes.

# HTTP::Connection

A connection to the HTTP server

### Constants

BUFFER_SIZE

  Attempt to read this much data

HTTP_1_0

  HTTP/1.0

HTTP_1_1

  HTTP/1.1

### Public Class Methods

new(req, options) click to toggle source

@param [HTTP::Request] req @param [HTTP::Options] options

    # File lib/http/connection.rb, line 23
    def initialize(req, options)
      @persistent           = options.persistent?
      @keep_alive_timeout   = options[:keep_alive_timeout].to_f
      @pending_request      = false
      @pending_response     = false
      @failed_proxy_connect = false

      @parser = Response::Parser.new

      @socket = options[:timeout_class].new(options[:timeout_options])
      @socket.connect(options[:socket_class], req.socket_host, req.socket_port)

      send_proxy_connect_request(req)
      start_tls(req, options)
      reset_timer
    end

### Public Instance Methods

close() click to toggle source

Close the connection @return [void]

    # File lib/http/connection.rb, line 118
    def close
      @socket.close unless @socket.closed?

      @pending_response = false
      @pending_request  = false
    end

expired?() click to toggle source

Whether our connection has expired @return [Boolean]

    # File lib/http/connection.rb, line 133
    def expired?
      !@conn_expires_at || @conn_expires_at < Time.now
    end

failed_proxy_connect?() click to toggle source

@return [Boolean] whenever proxy connect failed

    # File lib/http/connection.rb, line 50
    def failed_proxy_connect?
      @failed_proxy_connect
    end

finish_response() click to toggle source

Callback for when we've reached the end of a response @return [void]

    # File lib/http/connection.rb, line 106
    def finish_response
      close unless keep_alive?

      @parser.reset
      @socket.reset_counter if @socket.respond_to?(:reset_counter)
      reset_timer

      @pending_response = false
    end

keep_alive?() click to toggle source

Whether we're keeping the conn alive @return [Boolean]

    # File lib/http/connection.rb, line 127
    def keep_alive?
      !!@keep_alive && !@socket.closed?
    end

read_headers!() click to toggle source

Reads data from socket up until headers are loaded @return [void]

    # File lib/http/connection.rb, line 96
    def read_headers!
      read_more BUFFER_SIZE until @parser.headers
      set_keep_alive
    rescue IOError, Errno::ECONNRESET, Errno::EPIPE => e
      return if e.is_a?(EOFError) && @parser.headers
      raise IOError, "problem making HTTP request: #{e}"
    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/connection.rb, line 77
    def readpartial(size = BUFFER_SIZE)
      return unless @pending_response

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

      chunk = @parser.chunk

      finish_response if finished

      chunk.to_s
    end

send_request(req) click to toggle source

Send a request to the server

@param [Request] req Request to send to the server @return [nil]

    # File lib/http/connection.rb, line 58
    def send_request(req)
      if @pending_response
        fail StateError, "Tried to send a request while one is pending already. Make sure you read off the body."
      elsif @pending_request
        fail StateError, "Tried to send a request while a response is pending. Make sure you've fully read the body from the request."
      end

      @pending_request = true

      req.stream @socket

      @pending_response = true
      @pending_request  = false
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.