class HTTPClient::Session

Manages a HTTP session with a Site.

Deprecated. just for backward compatibility

Constants

BadResponse
RS
StatusParseRegexp

Attributes

connect_retry[RW]
connect_timeout[RW]
debug_dev[RW]

Device for dumping log for debugging

dest[R]

Destination site

last_used[R]
protocol_retry_count[RW]
proxy[RW]

Proxy site

read_block_size[RW]
receive_timeout[RW]
requested_version[RW]

Requested protocol version

send_timeout[RW]
socket_local[RW]
socket_sync[RW]

Boolean value for Socket#sync

ssl_config[RW]
ssl_peer_cert[R]
test_loopback_http_response[RW]
transparent_gzip_decompression[RW]

Public Class Methods

new(client, dest, agent_name, from) click to toggle source
# File lib/httpclient/session.rb, line 567
def initialize(client, dest, agent_name, from)
  @client = client
  @dest = dest
  @invalidated = false
  @proxy = nil
  @socket_sync = true
  @requested_version = nil

  @debug_dev = nil

  @connect_timeout = nil
  @connect_retry = 1
  @send_timeout = nil
  @receive_timeout = nil
  @read_block_size = nil
  @protocol_retry_count = 5

  @ssl_config = nil
  @ssl_peer_cert = nil

  @test_loopback_http_response = nil
  @socket_local = Site::EMPTY

  @agent_name = agent_name
  @from = from
  @state = :INIT

  @requests = []

  @status = nil
  @reason = nil
  @headers = []

  @socket = nil
  @readbuf = nil

  @transparent_gzip_decompression = false
  @last_used = nil
end

Public Instance Methods

close() click to toggle source
# File lib/httpclient/session.rb, line 641
def close
  if !@socket.nil? and !@socket.closed?
    # @socket.flush may block when it the socket is already closed by
    # foreign host and the client runs under MT-condition.
    @socket.close
  end
  @state = :INIT
end
closed?() click to toggle source
# File lib/httpclient/session.rb, line 650
def closed?
  @state == :INIT
end
eof?() click to toggle source
# File lib/httpclient/session.rb, line 675
def eof?
  if !@content_length.nil?
    @content_length == 0
  else
    @socket.closed? or @socket.eof?
  end
end
get_body(&block) click to toggle source
# File lib/httpclient/session.rb, line 683
def get_body(&block)
  begin
    read_header if @state == :META
    return nil if @state != :DATA
    if @gzipped and @transparent_gzip_decompression
      # zlib itself has a functionality to decompress gzip stream.
      # - zlib 1.2.5 Manual
      #   http://www.zlib.net/manual.html#Advanced
      # > windowBits can also be greater than 15 for optional gzip decoding. Add 32 to
      # > windowBits to enable zlib and gzip decoding with automatic header detection,
      # > or add 16 to decode only the gzip format
      inflate_stream = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
      original_block = block
      block = Proc.new { |buf|
        original_block.call(inflate_stream.inflate(buf))
      }
    end
    if @chunked
      read_body_chunked(&block)
    elsif @content_length
      read_body_length(&block)
    else
      read_body_rest(&block)
    end
  rescue
    close
    raise
  end
  if eof?
    if @next_connection
      @state = :WAIT
    else
      close
    end
  end
  nil
end
get_header() click to toggle source
# File lib/httpclient/session.rb, line 662
def get_header
  begin
    if @state != :META
      raise RuntimeError.new("get_status must be called at the beginning of a session")
    end
    read_header
  rescue
    close
    raise
  end
  [@version, @status, @reason, @headers]
end
invalidate() click to toggle source
# File lib/httpclient/session.rb, line 654
def invalidate
  @invalidated = true
end
invalidated?() click to toggle source
# File lib/httpclient/session.rb, line 658
def invalidated?
  @invalidated
end
query(req) click to toggle source

Send a request to the server

# File lib/httpclient/session.rb, line 608
def query(req)
  connect if @state == :INIT
  # Use absolute URI (not absolute path) iif via proxy AND not HTTPS.
  req.header.request_absolute_uri = !@proxy.nil? and !https?(@dest)
  begin
    timeout(@send_timeout, SendTimeoutError) do
      set_header(req)
      req.dump(@socket)
      # flush the IO stream as IO::sync mode is false
      @socket.flush unless @socket_sync
    end
  rescue Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE, IOError
    # JRuby can raise IOError instead of ECONNRESET for now
    close
    raise KeepAliveDisconnected.new(self)
  rescue HTTPClient::TimeoutError
    close
    raise
  rescue
    close
    if SSLEnabled and $!.is_a?(OpenSSL::SSL::SSLError)
      raise KeepAliveDisconnected.new(self)
    else
      raise
    end
  end

  @state = :META if @state == :WAIT
  @next_connection = nil
  @requests.push(req)
  @last_used = Time.now
end