Class: HTTPX::TCP

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/httpx/io/tcp.rb

Direct Known Subclasses

SSL, TLS, UNIX

Constant Summary

Constants included from Loggable

Loggable::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log, #log_exception

Constructor Details

#initialize(origin, addresses, options) ⇒ TCP

Returns a new instance of TCP.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/httpx/io/tcp.rb', line 16

def initialize(origin, addresses, options)
  @state = :idle
  @hostname = origin.host
  @options = Options.new(options)
  @fallback_protocol = @options.fallback_protocol
  @port = origin.port
  @interests = :w
  if @options.io
    @io = case @options.io
          when Hash
            @options.io[origin.authority]
          else
            @options.io
    end
    raise Error, "Given IO objects do not match the request authority" unless @io

    _, _, _, @ip = @io.addr
    @addresses ||= [@ip]
    @ip_index = @addresses.size - 1
    @keep_open = true
    @state = :connected
  else
    @addresses = addresses.map { |addr| addr.is_a?(IPAddr) ? addr : IPAddr.new(addr) }
  end
  @ip_index = @addresses.size - 1
  @io ||= build_socket
end

Instance Attribute Details

#addressesObject (readonly)

Returns the value of attribute addresses.



12
13
14
# File 'lib/httpx/io/tcp.rb', line 12

def addresses
  @addresses
end

#interestsObject (readonly)

Returns the value of attribute interests.



12
13
14
# File 'lib/httpx/io/tcp.rb', line 12

def interests
  @interests
end

#ipObject (readonly) Also known as: host

Returns the value of attribute ip.



12
13
14
# File 'lib/httpx/io/tcp.rb', line 12

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/httpx/io/tcp.rb', line 12

def port
  @port
end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/httpx/io/tcp.rb', line 12

def state
  @state
end

Instance Method Details

#closeObject



152
153
154
155
156
157
158
159
160
# File 'lib/httpx/io/tcp.rb', line 152

def close
  return if @keep_open || closed?

  begin
    @io.close
  ensure
    transition(:closed)
  end
end

#closed?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/httpx/io/tcp.rb', line 166

def closed?
  @state == :idle || @state == :closed
end

#connectObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/httpx/io/tcp.rb', line 52

def connect
  return unless closed?

  if @io.closed?
    transition(:idle)
    @io = build_socket
  end
  try_connect
rescue Errno::EHOSTUNREACH => e
  raise e if @ip_index <= 0

  @ip_index -= 1
  retry
rescue Errno::ETIMEDOUT => e
  raise ConnectTimeoutError.new(@options.timeout[:connect_timeout], e.message) if @ip_index <= 0

  @ip_index -= 1
  retry
end

#connected?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/httpx/io/tcp.rb', line 162

def connected?
  @state == :connected
end

#inspectObject

:nocov:



171
172
173
# File 'lib/httpx/io/tcp.rb', line 171

def inspect
  "#<#{self.class}: #{@ip}:#{@port} (state: #{@state})>"
end

#protocolObject



48
49
50
# File 'lib/httpx/io/tcp.rb', line 48

def protocol
  @fallback_protocol
end

#read(size, buffer) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/httpx/io/tcp.rb', line 89

def read(size, buffer)
  @io.read_nonblock(size, buffer)
  log { "READ: #{buffer.bytesize} bytes..." }
  buffer.bytesize
rescue ::IO::WaitReadable
  buffer.clear
  0
rescue EOFError
  nil
end

#to_ioObject



44
45
46
# File 'lib/httpx/io/tcp.rb', line 44

def to_io
  @io.to_io
end

#write(buffer) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/httpx/io/tcp.rb', line 100

def write(buffer)
  siz = @io.write_nonblock(buffer)
  log { "WRITE: #{siz} bytes..." }
  buffer.shift!(siz)
  siz
rescue ::IO::WaitWritable
  0
rescue EOFError
  nil
end