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::ESCAPED, Utils::UNESCAPED

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, #valid_connection_keys, #valid_request_keys

Constructor Details

#initialize(data = {}) ⇒ Socket

Returns a new instance of Socket.



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

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.



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

def remote_ip
  @remote_ip
end

Instance Method Details

#local_addressObject



159
160
161
162
163
# File 'lib/excon/socket.rb', line 159

def local_address
  ::Socket.unpack_sockaddr_in(@socket.to_io.getsockname)[1]
rescue ArgumentError => e
  raise unless e.message == 'not an AF_INET/AF_INET6 sockaddr'
end

#local_portObject



153
154
155
156
157
# File 'lib/excon/socket.rb', line 153

def local_port
  ::Socket.unpack_sockaddr_in(@socket.to_io.getsockname)[0]
rescue ArgumentError => e
  raise unless e.message == 'not an AF_INET/AF_INET6 sockaddr'
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



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

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/excon/socket.rb', line 31

def read(max_length=nil)
  if @eof
    return max_length ? nil : ''
  elsif @nonblock
    begin
      if max_length
        until @read_buffer.length >= max_length
          @read_buffer << @socket.read_nonblock(max_length - @read_buffer.length)
        end
      else
        while true
          @read_buffer << @socket.read_nonblock(@data[:chunk_size])
        end
      end
    rescue OpenSSL::SSL::SSLError => error
      if error.message == 'read would block'
        if IO.select([@socket], nil, nil, @data[:read_timeout])
          retry
        else
          raise(Excon::Errors::Timeout.new("read timeout reached"))
        end
      else
        raise(error)
      end
    rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable
      if IO.select([@socket], nil, nil, @data[:read_timeout])
        retry
      else
        raise(Excon::Errors::Timeout.new("read timeout reached"))
      end
    rescue EOFError
      @eof = true
    end

    if max_length
      if @read_buffer.empty?
        nil # EOF met at beginning
      else
        @read_buffer.slice!(0, max_length)
      end
    else
      # read until EOFError, so return everything
      @read_buffer.slice!(0, @read_buffer.length)
    end
  else
    begin
      Timeout.timeout(@data[:read_timeout]) do
        @socket.read(max_length)
      end
    rescue Timeout::Error
      raise Excon::Errors::Timeout.new('read timeout reached')
    end
  end
end

#readlineObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/excon/socket.rb', line 86

def readline
  if @eof
    raise EOFError, 'end of file reached'
  else
    line = ''
    if @nonblock
      while char = read(1)
        line << char
        break if char == $/
      end
      raise EOFError, 'end of file reached' if line.empty?
    else
      begin
        Timeout.timeout(@data[:read_timeout]) do
          line = @socket.readline
        end
      rescue Timeout::Error
        raise Excon::Errors::Timeout.new('read timeout reached')
      end
    end
    line
  end
end

#write(data) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/excon/socket.rb', line 110

def write(data)
  if @nonblock
    if FORCE_ENC
      data.force_encoding('BINARY')
    end
    while true
      written = nil
      begin
        # I wish that this API accepted a start position, then we wouldn't
        # have to slice data when there is a short write.
        written = @socket.write_nonblock(data)
      rescue OpenSSL::SSL::SSLError, Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable => error
        if error.is_a?(OpenSSL::SSL::SSLError) && error.message != 'write would block'
          raise error
        else
          if IO.select(nil, [@socket], nil, @data[:write_timeout])
            retry
          else
            raise Excon::Errors::Timeout.new('write timeout reached')
          end
        end
      end

      # Fast, common case.
      break if written == data.size

      # This takes advantage of the fact that most ruby implementations
      # have Copy-On-Write strings. Thusly why requesting a subrange
      # of data, we actually don't copy data because the new string
      # simply references a subrange of the original.
      data = data[written, data.size]
    end
  else
    begin
      Timeout.timeout(@data[:write_timeout]) do
        @socket.write(data)
      end
    rescue Timeout::Error
      raise(Excon::Errors::Timeout.new('write timeout reached'))
    end
  end
end