Class: HTTPX::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Callbacks, Loggable, Registry
Defined in:
lib/httpx/connection.rb

Overview

The Connection can be watched for IO events.

It contains the io object to read/write from, and knows what to do when it can.

It defers connecting until absolutely necessary. Connection should be triggered from the IO selector (until then, any request will be queued).

A connection boots up its parser after connection is established. All pending requests will be redirected there after connection.

A connection can be prevented from closing by the parser, that is, if there are pending requests. This will signal that the connection was prematurely closed, due to a possible number of conditions:

  • Remote peer closed the connection (“Connection: close”);

  • Remote peer doesn’t support pipelining;

A connection may also route requests for a different host for which the io was connected to, provided that the IP is the same and the port and scheme as well. This will allow to share the same socket to send HTTP/2 requests to different hosts.

Defined Under Namespace

Classes: HTTP1, HTTP2

Constant Summary collapse

BUFFER_SIZE =
1 << 14

Constants included from Loggable

Loggable::COLORS

Constants included from Registry

Registry::Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#emit, #on, #once

Methods included from Loggable

#log, #log_exception

Methods included from Registry

extended, included

Constructor Details

#initialize(type, uri, options) ⇒ Connection

Returns a new instance of Connection.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/httpx/connection.rb', line 49

def initialize(type, uri, options)
  @type = type
  @origins = [uri.origin]
  @origin = URI(uri.origin)
  @options = Options.new(options)
  @window_size = @options.window_size
  @read_buffer = Buffer.new(BUFFER_SIZE)
  @write_buffer = Buffer.new(BUFFER_SIZE)
  @pending = []
  on(:error, &method(:on_error))
  if @options.io
    # if there's an already open IO, get its
    # peer address, and force-initiate the parser
    transition(:already_open)
    @io = IO.registry(@type).new(@origin, nil, @options)
    parser
  else
    transition(:idle)
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



47
48
49
# File 'lib/httpx/connection.rb', line 47

def options
  @options
end

#originObject (readonly)

Returns the value of attribute origin.



47
48
49
# File 'lib/httpx/connection.rb', line 47

def origin
  @origin
end

#pendingObject (readonly)

Returns the value of attribute pending.



47
48
49
# File 'lib/httpx/connection.rb', line 47

def pending
  @pending
end

#stateObject (readonly)

Returns the value of attribute state.



47
48
49
# File 'lib/httpx/connection.rb', line 47

def state
  @state
end

Instance Method Details

#addressesObject



76
77
78
# File 'lib/httpx/connection.rb', line 76

def addresses
  @io && @io.addresses
end

#addresses=(addrs) ⇒ Object

this is a semi-private method, to be used by the resolver to initiate the io object.



72
73
74
# File 'lib/httpx/connection.rb', line 72

def addresses=(addrs)
  @io ||= IO.registry(@type).new(@origin, addrs, @options) # rubocop:disable Naming/MemoizedInstanceVariableName
end

#callObject



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/httpx/connection.rb', line 180

def call
  case @state
  when :closed
    return
  when :closing
    dwrite
    transition(:closed)
    emit(:close)
  when :open
    consume
  end
  nil
end

#closeObject



160
161
162
163
# File 'lib/httpx/connection.rb', line 160

def close
  @parser.close if @parser
  transition(:closing)
end

#coalescable?(connection) ⇒ Boolean

coalescable connections need to be mergeable! but internally, #mergeable? is called before #coalescable?

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/httpx/connection.rb', line 94

def coalescable?(connection)
  if @io.protocol == "h2" && @origin.scheme == "https"
    @io.verify_hostname(connection.origin.host)
  else
    @origin == connection.origin
  end
end

#connecting?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/httpx/connection.rb', line 138

def connecting?
  @state == :idle
end

#inflight?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/httpx/connection.rb', line 142

def inflight?
  @parser && !@parser.empty? && !@write_buffer.empty?
end

#interestsObject



146
147
148
149
150
# File 'lib/httpx/connection.rb', line 146

def interests
  return :w if @state == :idle

  :rw
end

#match?(uri, options) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/httpx/connection.rb', line 80

def match?(uri, options)
  return false if @state == :closing || @state == :closed

  (@origins.include?(uri.origin) || match_altsvcs?(uri)) && @options == options
end

#match_altsvcs?(uri) ⇒ Boolean

checks if this is connection is an alternative service of uri

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'lib/httpx/connection.rb', line 131

def match_altsvcs?(uri)
  AltSvc.cached_altsvc(@origin).any? do |altsvc|
    origin = altsvc["origin"]
    origin.altsvc_match?(uri.origin)
  end
end

#merge(connection) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/httpx/connection.rb', line 102

def merge(connection)
  @origins += connection.instance_variable_get(:@origins)
  pending = connection.instance_variable_get(:@pending)
  pending.each do |req, args|
    send(req, args)
  end
end

#mergeable?(connection) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/httpx/connection.rb', line 86

def mergeable?(connection)
  return false if @state == :closing || @state == :closed || !@io

  !(@io.addresses & connection.addresses).empty? && @options == connection.options
end

#purge_pendingObject



121
122
123
124
125
126
127
# File 'lib/httpx/connection.rb', line 121

def purge_pending
  [@parser.pending, @pending].each do |pending|
    pending.reject! do |request, *args|
      yield(request, args)
    end
  end
end

#resetObject



165
166
167
168
169
# File 'lib/httpx/connection.rb', line 165

def reset
  transition(:closing)
  transition(:closed)
  emit(:close)
end

#send(request) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/httpx/connection.rb', line 171

def send(request)
  if @parser && !@write_buffer.full?
    request.headers["alt-used"] = @origin.authority if match_altsvcs?(request.uri)
    parser.send(request)
  else
    @pending << request
  end
end

#timeoutObject



194
195
196
197
198
199
200
# File 'lib/httpx/connection.rb', line 194

def timeout
  return @timeout if defined?(@timeout)

  return @options.timeout.connect_timeout if @state == :idle

  @options.timeout.operation_timeout
end

#to_ioObject



152
153
154
155
156
157
158
# File 'lib/httpx/connection.rb', line 152

def to_io
  case @state
  when :idle
    transition(:open)
  end
  @io.to_io
end

#unmerge(connection) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/httpx/connection.rb', line 110

def unmerge(connection)
  @origins -= connection.instance_variable_get(:@origins)
  purge_pending do |request|
    request.uri.origin == connection.origin && begin
      request.transition(:idle)
      connection.send(request)
      true
    end
  end
end