Class: EventMachine::AblyHttpRequest::HttpConnection

Inherits:
Object
  • Object
show all
Includes:
Connectify, HTTPMethods, Socksify
Defined in:
lib/em-http/http_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTPMethods

#delete, #get, #head, #options, #patch, #post, #put

Constructor Details

#initializeHttpConnection

Returns a new instance of HttpConnection.



190
191
192
193
# File 'lib/em-http/http_connection.rb', line 190

def initialize
  @deferred = true
  @middleware = []
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



187
188
189
# File 'lib/em-http/http_connection.rb', line 187

def conn
  @conn
end

#connoptsObject

Returns the value of attribute connopts.



188
189
190
# File 'lib/em-http/http_connection.rb', line 188

def connopts
  @connopts
end

#deferredObject (readonly)

Returns the value of attribute deferred.



187
188
189
# File 'lib/em-http/http_connection.rb', line 187

def deferred
  @deferred
end

#errorObject

Returns the value of attribute error.



188
189
190
# File 'lib/em-http/http_connection.rb', line 188

def error
  @error
end

#uriObject

Returns the value of attribute uri.



188
189
190
# File 'lib/em-http/http_connection.rb', line 188

def uri
  @uri
end

Instance Method Details

#activate_connection(client) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/em-http/http_connection.rb', line 200

def activate_connection(client)
  begin
    EventMachine.bind_connect(@connopts.bind, @connopts.bind_port,
                              @connopts.host, @connopts.port,
                              HttpStubConnection) do |conn|
      post_init

      @deferred = false
      @conn = conn

      conn.parent = self
      conn.pending_connect_timeout = @connopts.connect_timeout
      conn.comm_inactivity_timeout = @connopts.inactivity_timeout
    end

    finalize_request(client)
  rescue EventMachine::ConnectionError => e
    #
    # Currently, this can only fire on initial connection setup
    # since #connect is a synchronous method. Hence, rescue the exception,
    # and return a failed deferred which fail any client request at next
    # tick.  We fail at next tick to keep a consistent API when the newly
    # created HttpClient is failed. This approach has the advantage to
    # remove a state check of @deferred_status after creating a new
    # HttpRequest. The drawback is that users may setup a callback which we
    # know won't be used.
    #
    # Once there is async-DNS, then we'll iterate over the outstanding
    # client requests and fail them in order.
    #
    # Net outcome: failed connection will invoke the same ConnectionError
    # message on the connection deferred, and on the client deferred.
    #
    EM.next_tick{client.close(e.message)}
  end
end

#connection_completedObject



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/em-http/http_connection.rb', line 312

def connection_completed
  @peer = @conn.get_peername

  if @connopts.socks_proxy?
    socksify(client.req.uri.hostname, client.req.uri.inferred_port, *@connopts.proxy[:authorization]) { start }
  elsif @connopts.connect_proxy?
    connectify(client.req.uri.hostname, client.req.uri.inferred_port, *@connopts.proxy[:authorization]) { start }
  else
    start
  end
end

#finalize_request(c) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'lib/em-http/http_connection.rb', line 243

def finalize_request(c)
  @conn.callback { c.connection_completed }

  middleware.each do |m|
    c.callback(&m.method(:response)) if m.respond_to?(:response)
  end

  @clients.push c
end

#middlewareObject



253
254
255
# File 'lib/em-http/http_connection.rb', line 253

def middleware
  [HttpRequest.middleware, @middleware].flatten
end

#peerObject



299
300
301
# File 'lib/em-http/http_connection.rb', line 299

def peer
  Socket.unpack_sockaddr_in(@peer)[1] rescue nil
end

#post_initObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/em-http/http_connection.rb', line 257

def post_init
  @clients = []
  @pending = []

  @p = Http::Parser.new
  @p.header_value_type = :mixed
  @p.on_headers_complete = proc do |h|
    if client
      if @p.status_code == 100
        client.send_request_body
        @p.reset!
      else
        client.parse_response_header(h, @p.http_version, @p.status_code)
        :reset if client.req.no_body?
      end
    else
      # if we receive unexpected data without a pending client request
      # reset the parser to avoid firing any further callbacks and close
      # the connection because we're processing invalid HTTP
      @p.reset!
      unbind
      :stop
    end
  end

  @p.on_body = proc do |b|
    client.on_body_data(b)
  end

  @p.on_message_complete = proc do
    if !client.continue?
      c = @clients.shift
      c.state = :finished
      c.on_request_complete
    end
  end
end

#receive_data(data) ⇒ Object



303
304
305
306
307
308
309
310
# File 'lib/em-http/http_connection.rb', line 303

def receive_data(data)
  begin
    @p << data
  rescue HTTP::Parser::Error => e
    c = @clients.shift
    c.nil? ? unbind(e.message) : c.on_error(e.message)
  end
end

#redirect(client, new_location) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/em-http/http_connection.rb', line 329

def redirect(client, new_location)
  old_location = client.req.uri
  new_location = client.req.set_uri(new_location)

  if client.req.keepalive
    # Application requested a keep-alive connection but one of the requests
    # hits a cross-origin redirect. We need to open a new connection and
    # let both connections proceed simultaneously.
    if old_location.origin != new_location.origin
      conn = HttpConnection.new
      client.conn = conn
      conn.connopts = @connopts
      conn.connopts.https = new_location.scheme == "https"
      conn.uri = client.req.uri
      conn.activate_connection(client)

    # If the redirect is a same-origin redirect on a keep-alive request
    # then immidiately dispatch the request over existing connection.
    else
      @clients.push client
      client.connection_completed
    end
  else
    # If connection is not keep-alive the unbind will fire and we'll
    # reconnect using the same connection object.
    @pending.push client
  end
end

#send_data(data) ⇒ Object



389
390
391
# File 'lib/em-http/http_connection.rb', line 389

def send_data(data)
  @conn.send_data data
end

#setup_request(method, options = {}, c = nil) ⇒ Object



237
238
239
240
241
# File 'lib/em-http/http_connection.rb', line 237

def setup_request(method, options = {}, c = nil)
  c ||= HttpClient.new(self, ::AblyHttpRequest::HttpClientOptions.new(@uri, options, method))
  @deferred ? activate_connection(c) : finalize_request(c)
  c
end

#startObject



324
325
326
327
# File 'lib/em-http/http_connection.rb', line 324

def start
  @conn.start_tls(@connopts.tls) if client && client.req.ssl?
  @conn.succeed
end

#stream_data(io, opts = {}) ⇒ Object



397
398
399
# File 'lib/em-http/http_connection.rb', line 397

def stream_data(io, opts = {})
  EventMachine::AblyHttpRequest::IOStreamer.new(self, io, opts)
end

#stream_file_data(filename, args = {}) ⇒ Object



393
394
395
# File 'lib/em-http/http_connection.rb', line 393

def stream_file_data(filename, args = {})
  @conn.stream_file_data filename, args
end

#unbind(reason = nil) ⇒ Object Also known as: close



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/em-http/http_connection.rb', line 358

def unbind(reason = nil)
  @clients.map { |c| c.unbind(reason) }

  if r = @pending.shift
    @clients.push r

    r.reset!
    @p.reset!

    begin
      @conn.set_deferred_status :unknown

      if @connopts.proxy
        @conn.reconnect(@connopts.host, @connopts.port)
      else
        @conn.reconnect(r.req.host, r.req.port)
      end

      @conn.pending_connect_timeout = @connopts.connect_timeout
      @conn.comm_inactivity_timeout = @connopts.inactivity_timeout
      @conn.callback { r.connection_completed }
    rescue EventMachine::ConnectionError => e
      @clients.pop.close(e.message)
    end
  else
    @deferred = true
    @conn.close_connection
  end
end

#use(klass, *args, &block) ⇒ Object



295
296
297
# File 'lib/em-http/http_connection.rb', line 295

def use(klass, *args, &block)
  @middleware << klass.new(*args, &block)
end