ably 1.1.4.rc → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/ably.gemspec CHANGED
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency 'eventmachine', '~> 1.2.6'
22
22
  spec.add_runtime_dependency 'em-http-request', '~> 1.1'
23
- spec.add_runtime_dependency 'statesman', '~> 1.0.0'
24
- spec.add_runtime_dependency 'faraday', '~> 1.0'
25
- spec.add_runtime_dependency 'typhoeus', '~> 1.4'
23
+ spec.add_runtime_dependency 'statesman', '~> 7.4'
24
+ spec.add_runtime_dependency 'faraday', '>= 0.12', '< 2.0.0'
25
+ spec.add_runtime_dependency 'excon', '~> 0.55'
26
26
 
27
27
  if RUBY_VERSION.match(/^1\./)
28
28
  spec.add_runtime_dependency 'json', '< 2.0'
@@ -33,20 +33,20 @@ Gem::Specification.new do |spec|
33
33
  spec.add_runtime_dependency 'msgpack', '>= 1.3.0'
34
34
  spec.add_runtime_dependency 'addressable', '>= 2.0.0'
35
35
 
36
- spec.add_development_dependency 'rake', '~> 13.0'
36
+ spec.add_development_dependency 'rake', '~> 11.3'
37
37
  spec.add_development_dependency 'redcarpet', '~> 3.3'
38
- spec.add_development_dependency 'rspec', '~> 3.10.0' # version lock, see config.around(:example, :event_machine) in event_machine_helper.rb
38
+ spec.add_development_dependency 'rspec', '~> 3.3.0' # version lock, see config.around(:example, :event_machine) in event_machine_helper.rb
39
39
  spec.add_development_dependency 'rspec-retry', '~> 0.6'
40
40
  spec.add_development_dependency 'yard', '~> 0.9'
41
41
  spec.add_development_dependency 'rspec-instafail', '~> 1.0'
42
- spec.add_development_dependency 'bundler', '~> 2.2.1'
42
+ spec.add_development_dependency 'bundler', '>= 1.3.0'
43
43
 
44
44
  if RUBY_VERSION.match(/^1\./)
45
45
  spec.add_development_dependency 'public_suffix', '~> 1.4.6' # Later versions do not support Ruby 1.9
46
46
  spec.add_development_dependency 'webmock', '2.2'
47
47
  spec.add_development_dependency 'parallel_tests', '~> 2.9.0'
48
48
  else
49
- spec.add_development_dependency 'webmock', '~> 2.2'
49
+ spec.add_development_dependency 'webmock', '~> 3.11'
50
50
  spec.add_development_dependency 'coveralls'
51
51
  spec.add_development_dependency 'parallel_tests', '~> 2.22'
52
52
  if !RUBY_VERSION.match(/^2\.[0123]/)
data/lib/ably/auth.rb CHANGED
@@ -103,7 +103,6 @@ module Ably
103
103
  end
104
104
 
105
105
  if has_client_id? && !token_creatable_externally? && !token_option
106
- raise ArgumentError, 'client_id cannot be provided without a complete API key or means to authenticate. An API key is needed to automatically authenticate with Ably and obtain a token' unless api_key_present?
107
106
  @client_id = ensure_utf_8(:client_id, client_id) if client_id
108
107
  end
109
108
 
@@ -377,7 +376,7 @@ module Ably
377
376
  # True when Token Auth is being used to authenticate with Ably
378
377
  def using_token_auth?
379
378
  return options[:use_token_auth] if options.has_key?(:use_token_auth)
380
- !!(token_option || current_token_details || has_client_id? || token_creatable_externally?)
379
+ !!(token_option || current_token_details || token_creatable_externally?)
381
380
  end
382
381
 
383
382
  def client_id
@@ -408,6 +407,17 @@ module Ably
408
407
  end
409
408
  end
410
409
 
410
+ # Extra headers that may be used during authentication
411
+ #
412
+ # @return [Hash] headers
413
+ def extra_auth_headers
414
+ if client_id && using_basic_auth?
415
+ { 'X-Ably-ClientId' => Base64.urlsafe_encode64(client_id) }
416
+ else
417
+ {}
418
+ end
419
+ end
420
+
411
421
  # Auth params used in URI endpoint for Realtime connections
412
422
  # Will reauthorize implicitly if required and capable
413
423
  #
@@ -79,6 +79,10 @@ module Ably
79
79
  # @return [Hash]
80
80
  attr_reader :options
81
81
 
82
+ # Properties of a channel and its state
83
+ # @return [{Ably::Realtime::Channel::ChannelProperties}]
84
+ attr_reader :properties
85
+
82
86
  # When a channel failure occurs this attribute contains the Ably Exception
83
87
  # @return [Ably::Models::ErrorInfo,Ably::Exceptions::BaseAblyException]
84
88
  attr_reader :error_reason
@@ -88,11 +92,6 @@ module Ably
88
92
  # @api private
89
93
  attr_reader :manager
90
94
 
91
- # Serial number assigned to this channel when it was attached
92
- # @return [Integer]
93
- # @api private
94
- attr_reader :attached_serial
95
-
96
95
  # Initialize a new Channel object
97
96
  #
98
97
  # @param client [Ably::Rest::Client]
@@ -112,6 +111,7 @@ module Ably
112
111
  @state = STATE(state_machine.current_state)
113
112
  @manager = ChannelManager.new(self, client.connection)
114
113
  @push = PushChannel.new(self)
114
+ @properties = ChannelProperties.new(self)
115
115
 
116
116
  setup_event_handlers
117
117
  setup_presence
@@ -292,7 +292,7 @@ module Ably
292
292
  error = Ably::Exceptions::InvalidRequest.new('option :until_attach is invalid as the channel is not attached' )
293
293
  return Ably::Util::SafeDeferrable.new_and_fail_immediately(logger, error)
294
294
  end
295
- options[:from_serial] = attached_serial
295
+ options[:from_serial] = properties.attach_serial
296
296
  end
297
297
 
298
298
  async_wrap(callback) do
@@ -319,11 +319,6 @@ module Ably
319
319
  @error_reason = nil
320
320
  end
321
321
 
322
- # @api private
323
- def set_attached_serial(serial)
324
- @attached_serial = serial
325
- end
326
-
327
322
  # @api private
328
323
  def update_options(channel_options)
329
324
  @options = channel_options.clone.freeze
@@ -372,3 +367,4 @@ end
372
367
  require 'ably/realtime/channel/channel_manager'
373
368
  require 'ably/realtime/channel/channel_state_machine'
374
369
  require 'ably/realtime/channel/push_channel'
370
+ require 'ably/realtime/channel/channel_properties'
@@ -37,7 +37,7 @@ module Ably::Realtime
37
37
  # library, such as returning to attached whne detach has failed
38
38
  if attached_protocol_message
39
39
  update_presence_sync_state_following_attached attached_protocol_message
40
- channel.set_attached_serial attached_protocol_message.channel_serial
40
+ channel.properties.set_attach_serial(attached_protocol_message.channel_serial)
41
41
  end
42
42
  end
43
43
 
@@ -76,7 +76,7 @@ module Ably::Realtime
76
76
  update_presence_sync_state_following_attached protocol_message
77
77
  end
78
78
 
79
- channel.set_attached_serial protocol_message.channel_serial
79
+ channel.properties.set_attach_serial(protocol_message.channel_serial)
80
80
  end
81
81
 
82
82
  # Handle DETACED messages, see #RTL13 for server-initated detaches
@@ -0,0 +1,24 @@
1
+ module Ably::Realtime
2
+ class Channel
3
+ # Represents properties of a channel and its state
4
+ class ChannelProperties
5
+ # {Ably::Realtime::Channel} this object associated with
6
+ # @return [Ably::Realtime::Channel]
7
+ attr_reader :channel
8
+
9
+ # Contains the last channelSerial received in an ATTACHED ProtocolMesage for the channel, see RTL15a
10
+ #
11
+ # @return [String]
12
+ attr_reader :attach_serial
13
+
14
+ def initialize(channel)
15
+ @channel = channel
16
+ end
17
+
18
+ # @api private
19
+ def set_attach_serial(attach_serial)
20
+ @attach_serial = attach_serial
21
+ end
22
+ end
23
+ end
24
+ end
@@ -65,6 +65,10 @@ module Ably
65
65
  # @return [String,Nil]
66
66
  attr_reader :recover
67
67
 
68
+ # Additional parameters to be sent in the querystring when initiating a realtime connection
69
+ # @return [Hash]
70
+ attr_reader :transport_params
71
+
68
72
  def_delegators :auth, :client_id, :auth_options
69
73
  def_delegators :@rest_client, :encoders
70
74
  def_delegators :@rest_client, :use_tls?, :protocol, :protocol_binary?
@@ -82,6 +86,7 @@ module Ably
82
86
  # @option options [Boolean] :echo_messages If false, prevents messages originating from this connection being echoed back on the same connection
83
87
  # @option options [String] :recover When a recover option is specified a connection inherits the state of a previous connection that may have existed under a different instance of the Realtime library, please refer to the API documentation for further information on connection state recovery
84
88
  # @option options [Boolean] :auto_connect By default as soon as the client library is instantiated it will connect to Ably. You can optionally set this to false and explicitly connect.
89
+ # @option options [Hash] :transport_params Additional parameters to be sent in the querystring when initiating a realtime connection. Keys are Strings, values are Stringifiable(a value must respond to #to_s)
85
90
  #
86
91
  # @option options [Integer] :channel_retry_timeout (15 seconds). When a channel becomes SUSPENDED, after this delay in seconds, the channel will automatically attempt to reattach if the connection is CONNECTED
87
92
  # @option options [Integer] :disconnected_retry_timeout (15 seconds). When the connection enters the DISCONNECTED state, after this delay in seconds, if the state is still DISCONNECTED, the client library will attempt to reconnect automatically
@@ -109,6 +114,9 @@ module Ably
109
114
  end
110
115
  end
111
116
 
117
+ @transport_params = options.delete(:transport_params).to_h.each_with_object({}) do |(key, value), acc|
118
+ acc[key.to_s] = value.to_s
119
+ end
112
120
  @rest_client = Ably::Rest::Client.new(options.merge(realtime_client: self))
113
121
  @echo_messages = rest_client.options.fetch(:echo_messages, true) == false ? false : true
114
122
  @queue_messages = rest_client.options.fetch(:queue_messages, true) == false ? false : true
@@ -431,10 +431,10 @@ module Ably
431
431
  client.auth.auth_params.tap do |auth_deferrable|
432
432
  auth_deferrable.callback do |auth_params|
433
433
  url_params = auth_params.merge(
434
- format: client.protocol,
435
- echo: client.echo_messages,
436
- v: Ably::PROTOCOL_VERSION,
437
- lib: client.rest_client.lib_version_id,
434
+ 'format' => client.protocol,
435
+ 'echo' => client.echo_messages,
436
+ 'v' => Ably::PROTOCOL_VERSION,
437
+ 'lib' => client.rest_client.lib_version_id,
438
438
  )
439
439
 
440
440
  # Use native websocket heartbeats if possible, but allow Ably protocol heartbeats
@@ -445,6 +445,7 @@ module Ably
445
445
  end
446
446
 
447
447
  url_params['clientId'] = client.auth.client_id if client.auth.has_client_id?
448
+ url_params.merge!(client.transport_params)
448
449
 
449
450
  if connection_resumable?
450
451
  url_params.merge! resume: key, connection_serial: serial
@@ -1,3 +1,5 @@
1
+ require 'openssl'
2
+
1
3
  module Ably::Realtime
2
4
  class Connection
3
5
  # EventMachine WebSocket transport
@@ -16,10 +18,13 @@ module Ably::Realtime
16
18
  )
17
19
  include Ably::Modules::StateEmitter
18
20
 
21
+ attr_reader :host
22
+
19
23
  def initialize(connection, url)
20
24
  @connection = connection
21
25
  @state = STATE.Initialized
22
26
  @url = url
27
+ @host = URI.parse(url).hostname
23
28
 
24
29
  setup_event_handlers
25
30
  end
@@ -49,7 +54,7 @@ module Ably::Realtime
49
54
  # Required {http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection EventMachine::Connection} interface
50
55
  def connection_completed
51
56
  change_state STATE.Connected
52
- start_tls if client.use_tls?
57
+ start_tls(tls_opts) if client.use_tls?
53
58
  driver.start
54
59
  end
55
60
 
@@ -77,6 +82,51 @@ module Ably::Realtime
77
82
  send_data(data)
78
83
  end
79
84
 
85
+ # TLS verification support, original implementation by Mislav Marohnić:
86
+ #
87
+ # https://github.com/lostisland/faraday/commit/63cf47c95b573539f047c729bd9ad67560bc83ff
88
+ def ssl_verify_peer(cert_string)
89
+ cert = nil
90
+ begin
91
+ cert = OpenSSL::X509::Certificate.new(cert_string)
92
+ rescue OpenSSL::X509::CertificateError => e
93
+ disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate: #{e.message}"
94
+ return false
95
+ end
96
+
97
+ @last_seen_cert = cert
98
+
99
+ if certificate_store.verify(@last_seen_cert)
100
+ begin
101
+ certificate_store.add_cert(@last_seen_cert)
102
+ rescue OpenSSL::X509::StoreError => e
103
+ unless e.message == 'cert already in hash table'
104
+ disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate: #{e.message}"
105
+ return false
106
+ end
107
+ end
108
+ true
109
+ else
110
+ disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate"
111
+ false
112
+ end
113
+ end
114
+
115
+ def ssl_handshake_completed
116
+ unless OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
117
+ disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate"
118
+ false
119
+ else
120
+ true
121
+ end
122
+ end
123
+
124
+ def certificate_store
125
+ @certificate_store ||= OpenSSL::X509::Store.new.tap do |store|
126
+ store.set_default_paths
127
+ end
128
+ end
129
+
80
130
  # True if socket connection is ready to be released
81
131
  # i.e. it is not currently connecting or connected
82
132
  def ready_for_release?
@@ -106,6 +156,12 @@ module Ably::Realtime
106
156
  @connection
107
157
  end
108
158
 
159
+ def disconnect_with_reason(reason)
160
+ client.logger.error { "WebsocketTransport: Disconnecting due to error: #{reason}" }
161
+ @reason_closed = reason
162
+ disconnect
163
+ end
164
+
109
165
  def reason_closed
110
166
  @reason_closed
111
167
  end
@@ -214,6 +270,16 @@ module Ably::Realtime
214
270
  end
215
271
  )
216
272
  end
273
+
274
+ # TLS options to pass to EventMachine::Connection#start_tls
275
+ #
276
+ # See https://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection#start_tls-instance_method
277
+ def tls_opts
278
+ {
279
+ sni_hostname: host,
280
+ verify_peer: true,
281
+ }
282
+ end
217
283
  end
218
284
  end
219
285
  end
@@ -278,28 +278,14 @@ module Ably::Realtime
278
278
 
279
279
  # Return the presence messages history for the channel
280
280
  #
281
- # Once attached to a channel, you can retrieve presence message history on the channel before the
282
- # channel was attached with the option <tt>until_attach: true</tt>. This is very useful for
283
- # developers who wish to capture new presence events as well as retrieve historical presence state with
284
- # the guarantee that no presence history has been missed.
285
- #
286
281
  # @param (see Ably::Rest::Presence#history)
287
282
  # @option options (see Ably::Rest::Presence#history)
288
- # @option options [Boolean] :until_attach When true, request for history will be limited only to messages published before the associated channel was attached. The associated channel must be attached.
289
283
  #
290
284
  # @yield [Ably::Models::PaginatedResult<Ably::Models::PresenceMessage>] First {Ably::Models::PaginatedResult page} of {Ably::Models::PresenceMessage} objects accessible with {Ably::Models::PaginatedResult#items #items}.
291
285
  #
292
286
  # @return [Ably::Util::SafeDeferrable]
293
287
  #
294
288
  def history(options = {}, &callback)
295
- if options.delete(:until_attach)
296
- unless channel.attached?
297
- error = Ably::Exceptions::InvalidRequest.new('option :until_attach is invalid as the channel is not attached')
298
- return Ably::Util::SafeDeferrable.new_and_fail_immediately(logger, error)
299
- end
300
- options[:from_serial] = channel.attached_serial
301
- end
302
-
303
289
  async_wrap(callback) do
304
290
  rest_presence.history(options.merge(async_blocking_operations: true))
305
291
  end
@@ -3,9 +3,6 @@ require 'json'
3
3
  require 'logger'
4
4
  require 'uri'
5
5
 
6
- require 'typhoeus'
7
- require 'typhoeus/adapters/faraday'
8
-
9
6
  require 'ably/rest/middleware/exceptions'
10
7
 
11
8
  module Ably
@@ -205,8 +202,6 @@ module Ably
205
202
  @http_defaults = HTTP_DEFAULTS.dup
206
203
  options.each do |key, val|
207
204
  if http_key = key[/^http_(.+)/, 1]
208
- # Typhoeus converts decimal durations to milliseconds, so 0.0001 timeout is treated as 0 (no timeout)
209
- val = 0.001 if val.kind_of?(Numeric) && (val > 0) && (val < 0.001)
210
205
  @http_defaults[http_key.to_sym] = val if val && @http_defaults.has_key?(http_key.to_sym)
211
206
  end
212
207
  end
@@ -348,14 +343,14 @@ module Ably
348
343
  #
349
344
  # @return [Ably::Models::HttpPaginatedResponse<>]
350
345
  def request(method, path, params = {}, body = nil, headers = {}, options = {})
351
- raise "Method #{method.to_s.upcase} not supported" unless [:get, :put, :post].include?(method.to_sym)
346
+ raise "Method #{method.to_s.upcase} not supported" unless %i(get put patch post delete).include?(method.to_sym)
352
347
 
353
348
  response = case method.to_sym
354
- when :get
349
+ when :get, :delete
355
350
  reauthorize_on_authorization_failure do
356
351
  send_request(method, path, params, headers: headers)
357
352
  end
358
- when :post
353
+ when :post, :patch, :put
359
354
  path_with_params = Addressable::URI.new
360
355
  path_with_params.query_values = params || {}
361
356
  query = path_with_params.query
@@ -576,10 +571,9 @@ module Ably
576
571
  end
577
572
  unless options[:send_auth_header] == false
578
573
  request.headers[:authorization] = auth.auth_header
579
- if options[:headers]
580
- options[:headers].map do |key, val|
581
- request.headers[key] = val
582
- end
574
+
575
+ options[:headers].to_h.merge(auth.extra_auth_headers).map do |key, val|
576
+ request.headers[key] = val
583
577
  end
584
578
  end
585
579
  end.tap do
@@ -671,7 +665,7 @@ module Ably
671
665
  }
672
666
  end
673
667
 
674
- # Return a Faraday middleware stack to initiate the Faraday::RackBuilder with
668
+ # Return a Faraday middleware stack to initiate the Faraday::Connection with
675
669
  #
676
670
  # @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/
677
671
  def middleware
@@ -683,8 +677,8 @@ module Ably
683
677
 
684
678
  setup_incoming_middleware builder, logger, fail_if_unsupported_mime_type: true
685
679
 
686
- # Set Faraday's HTTP adapter with support for HTTP/2
687
- builder.adapter :typhoeus, http_version: :httpv2_0
680
+ # Set Faraday's HTTP adapter
681
+ builder.adapter :excon
688
682
  end
689
683
  end
690
684
 
@@ -7,10 +7,7 @@ module Ably
7
7
  class FailIfUnsupportedMimeType < Faraday::Response::Middleware
8
8
  def on_complete(env)
9
9
  unless env.response_headers['Ably-Middleware-Parsed'] == true
10
- # Ignore empty body with success status code for no body response
11
- return if env.body.to_s.empty? && env.status == 204
12
-
13
- unless (500..599).include?(env.status)
10
+ unless (500..599).include?(env.status)
14
11
  raise Ably::Exceptions::InvalidResponseBody,
15
12
  "Content Type #{env.response_headers['Content-Type']} is not supported by this client library"
16
13
  end
data/lib/ably/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Ably
2
- VERSION = '1.1.4.rc'
2
+ VERSION = '1.1.4'
3
3
  PROTOCOL_VERSION = '1.1'
4
4
 
5
5
  # Allow a variant to be configured for all instances of this client library