Class: HTTPAccess2::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/rss-client/http-access2.rb

Overview

HTTPAccess2::Session – manage http session with one site.

One or more TCP sessions with the site may be created.
Only 1 TCP session is live at the same time.

Defined Under Namespace

Classes: BadResponse, Error, InvalidState, KeepAliveDisconnected

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest, user_agent, from) ⇒ Session

Returns a new instance of Session.



1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
# File 'lib/rss-client/http-access2.rb', line 1611

def initialize(dest, user_agent, from)
  @dest = dest
  @src = Site.new
  @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

  @ssl_config = nil
  @ssl_peer_cert = nil

  @user_agent = user_agent
  @from = from
  @state = :INIT

  @requests = []

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

  @socket = nil
end

Instance Attribute Details

#connect_retryObject

Returns the value of attribute connect_retry.



1603
1604
1605
# File 'lib/rss-client/http-access2.rb', line 1603

def connect_retry
  @connect_retry
end

#connect_timeoutObject

These session parameters are not used now…



1602
1603
1604
# File 'lib/rss-client/http-access2.rb', line 1602

def connect_timeout
  @connect_timeout
end

#debug_devObject

Device for dumping log for debugging



1599
1600
1601
# File 'lib/rss-client/http-access2.rb', line 1599

def debug_dev
  @debug_dev
end

#destObject (readonly)

Destination site



1592
1593
1594
# File 'lib/rss-client/http-access2.rb', line 1592

def dest
  @dest
end

#proxyObject

Proxy site



1594
1595
1596
# File 'lib/rss-client/http-access2.rb', line 1594

def proxy
  @proxy
end

#read_block_sizeObject

Returns the value of attribute read_block_size.



1606
1607
1608
# File 'lib/rss-client/http-access2.rb', line 1606

def read_block_size
  @read_block_size
end

#receive_timeoutObject

Returns the value of attribute receive_timeout.



1605
1606
1607
# File 'lib/rss-client/http-access2.rb', line 1605

def receive_timeout
  @receive_timeout
end

#requested_versionObject

Requested protocol version



1597
1598
1599
# File 'lib/rss-client/http-access2.rb', line 1597

def requested_version
  @requested_version
end

#send_timeoutObject

Returns the value of attribute send_timeout.



1604
1605
1606
# File 'lib/rss-client/http-access2.rb', line 1604

def send_timeout
  @send_timeout
end

#socket_syncObject

Boolean value for Socket#sync



1595
1596
1597
# File 'lib/rss-client/http-access2.rb', line 1595

def socket_sync
  @socket_sync
end

#srcObject (readonly)

Source site



1593
1594
1595
# File 'lib/rss-client/http-access2.rb', line 1593

def src
  @src
end

#ssl_configObject

Returns the value of attribute ssl_config.



1608
1609
1610
# File 'lib/rss-client/http-access2.rb', line 1608

def ssl_config
  @ssl_config
end

#ssl_peer_certObject (readonly)

Returns the value of attribute ssl_peer_cert.



1609
1610
1611
# File 'lib/rss-client/http-access2.rb', line 1609

def ssl_peer_cert
  @ssl_peer_cert
end

Instance Method Details

#closeObject



1671
1672
1673
1674
1675
1676
1677
# File 'lib/rss-client/http-access2.rb', line 1671

def close
  if !@socket.nil? and !@socket.closed?
    @socket.flush rescue nil  # try to rescue OpenSSL::SSL::SSLError: cf. #120
    @socket.close
  end
  @state = :INIT
end

#closed?Boolean

Returns:

  • (Boolean)


1679
1680
1681
# File 'lib/rss-client/http-access2.rb', line 1679

def closed?
  @state == :INIT
end

#eof?Boolean

Returns:

  • (Boolean)


1713
1714
1715
1716
1717
1718
1719
1720
1721
# File 'lib/rss-client/http-access2.rb', line 1713

def eof?
  if !@content_length.nil?
    @content_length == 0
  elsif @readbuf.length > 0
    false
  else
    @socket.closed? or @socket.eof?
  end
end

#get_data(&block) ⇒ Object



1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
# File 'lib/rss-client/http-access2.rb', line 1723

def get_data(&block)
  begin
    read_header() if @state == :META
    return nil if @state != :DATA
    unless @state == :DATA
      raise InvalidState.new('state != DATA')
    end
    data = nil
    if block
      while true
        begin
          timeout(@receive_timeout) do
            data = read_body()
          end
        rescue TimeoutError
          raise
        end
        block.call(data) if data
        break if eof?
      end
      data = nil      # Calling with block returns nil.
    else
      begin
        timeout(@receive_timeout) do
          data = read_body()
        end
      rescue TimeoutError
        raise
      end
    end
  rescue
    close
    raise
  end
  if eof?
    if @next_connection
      @state = :WAIT
    else
      close
    end
  end
  data
end

#get_header(&block) ⇒ Object



1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
# File 'lib/rss-client/http-access2.rb', line 1697

def get_header(&block)
  begin
    read_header() if @state == :META
  rescue
    close
    raise
  end
  if block
    @headers.each do |line|
      block.call(line)
    end
  else
    @headers
  end
end

#get_statusObject



1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
# File 'lib/rss-client/http-access2.rb', line 1683

def get_status
  version = status = reason = nil
  begin
    if @state != :META
      raise RuntimeError.new("get_status must be called at the beginning of a session.")
    end
    version, status, reason = read_header()
  rescue
    close
    raise
  end
  return version, status, reason
end

#query(req) ⇒ Object

Send a request to the server



1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
# File 'lib/rss-client/http-access2.rb', line 1643

def query(req)
  connect() if @state == :INIT
  begin
    timeout(@send_timeout) 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
    close
    raise KeepAliveDisconnected.new
  rescue
    if SSLEnabled and $!.is_a?(OpenSSL::SSL::SSLError)
      raise KeepAliveDisconnected.new
    elsif $!.is_a?(TimeoutError)
      close
      raise
    else
      raise
    end
  end

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