Class: Protocol::WebSocket::Extensions::Server

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

Overview

Manages extensions on the server side, negotiating client offers and applying the agreed extensions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extensions) ⇒ Server

Initialize a new server extension manager.



114
115
116
117
# File 'lib/protocol/websocket/extensions.rb', line 114

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

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



122
123
124
# File 'lib/protocol/websocket/extensions.rb', line 122

def accepted
  @accepted
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



120
121
122
# File 'lib/protocol/websocket/extensions.rb', line 120

def extensions
  @extensions
end

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



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

attr :accepted

#The list of supported extensions.(listofsupportedextensions.) ⇒ Object (readonly)



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

attr :extensions

Class Method Details

.defaultObject

Create a default server with permessage-deflate compression enabled.



106
107
108
109
110
# File 'lib/protocol/websocket/extensions.rb', line 106

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

Instance Method Details

#accept(headers) ⇒ Object

Negotiate client extension offers and yield accepted response headers.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/protocol/websocket/extensions.rb', line 137

def accept(headers)
	extensions = []
	
	named = self.named
	response = []
	
	# Each response header should map to at least one extension.
	Extensions.parse(headers) do |name, arguments|
		if extension = named[name]
			klass, options = extension
			
			if result = klass.negotiate(arguments, **options)
				header, options = result
				
				# The extension is accepted and no further offers will be considered:
				named.delete(name)
				
				yield header if block_given?
				
				@accepted << [klass, options]
			end
		end
	end
	
	return @accepted
end

#apply(connection) ⇒ Object

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



166
167
168
169
170
# File 'lib/protocol/websocket/extensions.rb', line 166

def apply(connection)
	@accepted.reverse_each do |(klass, options)|
		klass.server(connection, **options)
	end
end

#namedObject

Build a lookup table of extensions keyed by their name.



126
127
128
129
130
# File 'lib/protocol/websocket/extensions.rb', line 126

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