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

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

#connection_uri, #escape_uri, #port_string, #query_string, #request_uri, #split_header_value, #unescape_form, #unescape_uri

Constructor Details

#initialize(data = {}) ⇒ Socket


23
24
25
26
27
28
29
# File 'lib/excon/socket.rb', line 23

def initialize(data = {})
  @data = data
  @nonblock = data[:nonblock]
  @read_buffer = ''
  @eof = false
  connect
end

Instance Attribute Details

#dataObject

Returns the value of attribute data


7
8
9
# File 'lib/excon/socket.rb', line 7

def data
  @data
end

#remote_ipObject (readonly)

Returns the value of attribute remote_ip


19
20
21
# File 'lib/excon/socket.rb', line 19

def remote_ip
  @remote_ip
end

Instance Method Details

#legacy_readlineObject


66
67
68
69
70
71
72
73
74
# File 'lib/excon/socket.rb', line 66

def legacy_readline
   begin
    Timeout.timeout(@data[:read_timeout]) do
      @socket.readline
  end
  rescue Timeout::Error
  raise Excon::Errors::Timeout.new('read timeout reached')
  end
end

#local_addressObject


84
85
86
# File 'lib/excon/socket.rb', line 84

def local_address
  unpacked_sockaddr[1]
end

#local_portObject


88
89
90
# File 'lib/excon/socket.rb', line 88

def local_port
  unpacked_sockaddr[0]
end

#paramsObject


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

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

#params=(new_params) ⇒ Object


14
15
16
17
# File 'lib/excon/socket.rb', line 14

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


31
32
33
34
35
36
37
38
39
# File 'lib/excon/socket.rb', line 31

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

#readlineObject


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/excon/socket.rb', line 41

def readline
  return legacy_readline if RUBY_VERSION.to_f <= 1.8_7
  begin
    buffer = ''
    buffer << @socket.read_nonblock(1) while buffer[-1] != "\n"
    buffer
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable 
    if timeout_reached('read')
      raise_timeout_error('read')
    else
      retry
    end
  rescue OpenSSL::SSL::SSLError => e
    if e.message == 'read would block'
      if timeout_reached('read')
        raise_timeout_error('read')
      else
        retry
      end
    else
      raise(error)
    end
  end
end

#write(data) ⇒ Object


76
77
78
79
80
81
82
# File 'lib/excon/socket.rb', line 76

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