Class: Protocol::WebSocket::Extensions::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/extensions.rb

Overview

Manages extensions on the client side, offering and accepting server responses.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extensions = []) ⇒ Client

Initialize a new client extension manager.



44
45
46
47
# File 'lib/protocol/websocket/extensions.rb', line 44

def initialize(extensions = [])
	@extensions = extensions
	@accepted = []
end

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



52
53
54
# File 'lib/protocol/websocket/extensions.rb', line 52

def accepted
  @accepted
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



50
51
52
# File 'lib/protocol/websocket/extensions.rb', line 50

def extensions
  @extensions
end

#The extensions accepted after negotiation.(extensionsacceptedafternegotiation.) ⇒ Object (readonly)



52
# File 'lib/protocol/websocket/extensions.rb', line 52

attr :accepted

#The list of extensions to offer.(listofextensionstooffer.) ⇒ Object (readonly)



50
# File 'lib/protocol/websocket/extensions.rb', line 50

attr :extensions

Class Method Details

.defaultObject

Create a default client with permessage-deflate compression enabled.



36
37
38
39
40
# File 'lib/protocol/websocket/extensions.rb', line 36

def self.default
	self.new([
		[Extension::Compression, {}]
	])
end

Instance Method Details

#accept(headers) ⇒ Object

Accept server extension responses and record the negotiated extensions.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/protocol/websocket/extensions.rb', line 76

def accept(headers)
	named = self.named
	
	# Each response header should map to at least one extension.
	Extensions.parse(headers) do |name, arguments|
		if extension = named.delete(name)
			klass, options = extension
			
			options = klass.accept(arguments, **options)
			
			@accepted << [klass, options]
		end
	end
	
	return @accepted
end

#apply(connection) ⇒ Object

Apply all accepted extensions to the given connection as a client.



95
96
97
98
99
# File 'lib/protocol/websocket/extensions.rb', line 95

def apply(connection)
	@accepted.each do |(klass, options)|
		klass.client(connection, **options)
	end
end

#namedObject

Build a lookup table of extensions keyed by their name.



56
57
58
59
60
# File 'lib/protocol/websocket/extensions.rb', line 56

def named
	@extensions.map do |extension|
		[extension.first::NAME, extension]
	end.to_h
end

#offerObject

Yield extension offer headers for each registered extension.



65
66
67
68
69
70
71
# File 'lib/protocol/websocket/extensions.rb', line 65

def offer
	@extensions.each do |extension, options|
		if header = extension.offer(**options)
			yield header
		end
	end
end