Class: Excon::Socket

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

Direct Known Subclasses

SSLSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, proxy = nil) ⇒ Socket

Returns a new instance of Socket.



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

def initialize(params = {}, proxy = nil)
  @params, @proxy = params, proxy
  @read_buffer = ''
  @eof = false

  connect
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#connectObject



19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/excon/socket.rb', line 19

def connect
  @socket = nil
  exception = nil

  addrinfo = if @proxy
    ::Socket.getaddrinfo(@proxy[:host], @proxy[:port].to_i, ::Socket::Constants::AF_UNSPEC, ::Socket::Constants::SOCK_STREAM)
  else
    ::Socket.getaddrinfo(@params[:host], @params[:port].to_i, ::Socket::Constants::AF_UNSPEC, ::Socket::Constants::SOCK_STREAM)
  end

  addrinfo.each do |_, port, _, ip, a_family, s_type|
    # nonblocking connect
    begin
      sockaddr = ::Socket.sockaddr_in(port, ip)

      socket = ::Socket.new(a_family, s_type, 0)

      if @params[:nonblock]
        socket.connect_nonblock(sockaddr)
      else
        begin
          Timeout.timeout(@params[:connect_timeout]) do
            socket.connect(sockaddr)
          end
        rescue Timeout::Error
          raise Excon::Errors::Timeout.new('connect timeout reached')
        end
      end

      @socket = socket
      break
    rescue Errno::EINPROGRESS
      unless IO.select(nil, [socket], nil, @params[:connect_timeout])
        raise(Excon::Errors::Timeout.new("connect timeout reached"))
      end
      begin
        socket.connect_nonblock(sockaddr)

        @socket = socket
        break
      rescue Errno::EISCONN
        @socket = socket
        break
      rescue SystemCallError => exception
        socket.close
        next
      end
    rescue SystemCallError => exception
      socket.close
      next
    end
  end

  unless @socket
    # this will be our last encountered exception
    raise exception
  end
end

#read(max_length = nil) ⇒ Object



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/excon/socket.rb', line 78

def read(max_length=nil)
  return nil if @eof
  if @eof
    nil
  elsif @params[: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(CHUNK_SIZE)
        end
      end
    rescue OpenSSL::SSL::SSLError => error
      if error.message == 'read would block'
        if IO.select([@socket], nil, nil, @params[: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, @params[:read_timeout])
        retry
      else
        raise(Excon::Errors::Timeout.new("read timeout reached"))
      end
    rescue EOFError
      @eof = true
    end
    if max_length
      @read_buffer.slice!(0, max_length)
    else
      # read until EOFError, so return everything
      @read_buffer.slice!(0, @read_buffer.length)
    end
  else
    begin
      Timeout.timeout(@params[:read_timeout]) do
        @socket.read(max_length)
      end
    rescue Timeout::Error
      raise Excon::Errors::Timeout.new('read timeout reached')
    end
  end
end

#write(data) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/excon/socket.rb', line 129

def write(data)
  if @params[:nonblock]
    # We normally return from the return in the else block below, but
    # we guard that data is still something in case we get weird
    # values and String#[] returns nil. (This behavior has been observed
    # in the wild, so this is a simple defensive mechanism)
    while data
      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 => error
        if error.message == 'write would block'
          if IO.select(nil, [@socket], nil, @params[:write_timeout])
            retry
          else
            raise(Excon::Errors::Timeout.new("write timeout reached"))
          end
        else
          raise(error)
        end
      rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable
        if IO.select(nil, [@socket], nil, @params[:write_timeout])
          retry
        else
          raise(Excon::Errors::Timeout.new("write timeout reached"))
        end
      else
        # Fast, common case.
        # The >= seems weird, why would it have written MORE than we
        # requested. But we're getting some weird behavior when @socket
        # is an OpenSSL socket, where it seems like it's saying it wrote
        # more (perhaps due to SSL packet overhead?).
        #
        # Pretty weird, but this is a simple defensive mechanism.
        return 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
    end
  else
    begin
      Timeout.timeout(@params[:write_timeout]) do
        @socket.write(data)
      end
    rescue Timeout::Error
      Excon::Errors::Timeout.new('write timeout reached')
    end
  end
end