Class: Excon::Socket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/excon/socket.rb

Direct Known Subclasses

SSLSocket, UnixSocket

Constant Summary collapse

CONNECT_RETRY_EXCEPTION_CLASSES =
if defined?(IO::EINPROGRESSWaitWritable) # Ruby >= 2.1
  [Errno::EINPROGRESS, IO::EINPROGRESSWaitWritable]
else # Ruby <= 2.0
  [Errno::EINPROGRESS]
end
READ_RETRY_EXCEPTION_CLASSES =

Ruby >= 2.1

if defined?(IO::EAGAINWaitReadable) # Ruby >= 2.1
  [Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable, IO::EAGAINWaitReadable, IO::EWOULDBLOCKWaitReadable]
else # Ruby <= 2.0
  [Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable]
end
WRITE_RETRY_EXCEPTION_CLASSES =

Ruby >= 2.1

if defined?(IO::EAGAINWaitWritable) # Ruby >= 2.1
  [Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable, IO::EAGAINWaitWritable, IO::EWOULDBLOCKWaitWritable]
else # Ruby <= 2.0
  [Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable]
end
OPERATION_TO_TIMEOUT =

Maps a socket operation to a timeout property.

{
  :connect_read => :connect_timeout,
  :connect_write => :connect_timeout,
  :read => :read_timeout,
  :write => :write_timeout
}.freeze

Constants included from Utils

Utils::CONTROL, Utils::DELIMS, Utils::ESCAPED, Utils::NONASCII, Utils::UNESCAPED, Utils::UNWISE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#binary_encode, #connection_uri, #escape_uri, #headers_hash_to_s, #port_string, #query_string, #redact, #request_uri, #split_header_value, #unescape_form, #unescape_uri

Constructor Details

#initialize(data = {}) ⇒ Socket

Returns a new instance of Socket.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/excon/socket.rb', line 51

def initialize(data = {})
  @data = data
  @nonblock = data[:nonblock]
  @port ||= @data[:port] || 80
  @read_buffer = String.new
  @read_offset = 0
  @eof = false
  @backend_eof = false

  connect
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/excon/socket.rb', line 10

def data
  @data
end

#remote_ipObject (readonly)

Returns the value of attribute remote_ip.



46
47
48
# File 'lib/excon/socket.rb', line 46

def remote_ip
  @remote_ip
end

Instance Method Details

#local_addressObject



112
113
114
# File 'lib/excon/socket.rb', line 112

def local_address
  unpacked_sockaddr[1]
end

#local_portObject



116
117
118
# File 'lib/excon/socket.rb', line 116

def local_port
  unpacked_sockaddr[0]
end

#paramsObject



36
37
38
39
# File 'lib/excon/socket.rb', line 36

def params
  Excon.display_warning('Excon::Socket#params is deprecated use Excon::Socket#data instead.')
  @data
end

#params=(new_params) ⇒ Object



41
42
43
44
# File 'lib/excon/socket.rb', line 41

def params=(new_params)
  Excon.display_warning('Excon::Socket#params= is deprecated use Excon::Socket#data= instead.')
  @data = new_params
end

#read(max_length = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/excon/socket.rb', line 63

def read(max_length = nil)
  if @eof
    max_length ? nil : ''
  elsif @nonblock
    read_nonblock(max_length)
  else
    read_block(max_length)
  end
end

#readlineObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/excon/socket.rb', line 73

def readline
  if @nonblock
    result = String.new
    block = consume_read_buffer

    loop do
      idx = block.index("\n")

      if idx.nil?
        result << block
      else
        result << block[0..idx]
        rewind_read_buffer(block, idx)
        break
      end

      block = read_nonblock(@data[:chunk_size]) || raise(EOFError)
    end

    result
  else # nonblock/legacy
    begin
      Timeout.timeout(@data[:read_timeout]) do
        @socket.readline
      end
    rescue Timeout::Error
      raise Excon::Errors::Timeout.new('read timeout reached')
    end
  end
end

#write(data) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/excon/socket.rb', line 104

def write(data)
  if @nonblock
    write_nonblock(data)
  else
    write_block(data)
  end
end