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.



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

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

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



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

def accepted
  @accepted
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



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

def extensions
  @extensions
end

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



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

attr :accepted

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



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

attr :extensions

Class Method Details

.defaultObject

Create a default server with permessage-deflate compression enabled.



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

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

Instance Method Details

#accept(headers) ⇒ Object

Negotiate client extension offers and yield accepted response headers.



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

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[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.



164
165
166
167
168
# File 'lib/protocol/websocket/extensions.rb', line 164

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.



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

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