Protocol::WebSocketSourceProtocolWebSocketExtensionsClient

class Client

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

Definitions

def self.default

Create a default client with permessage-deflate compression enabled.

Signature

returns Client

A new client with the default compression extension.

Implementation

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

def initialize(extensions = [])

Initialize a new client extension manager.

Signature

parameter extensions Array

The list of extensions to offer, each as [klass, options].

Implementation

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

attr :extensions

Signature

attribute Array

The list of extensions to offer.

attr :accepted

Signature

attribute Array

The extensions accepted after negotiation.

def named

Build a lookup table of extensions keyed by their name.

Signature

returns Hash

A hash mapping extension names to their [klass, options] pairs.

Implementation

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

def offer

Yield extension offer headers for each registered extension.

Signature

yields {|header| ...}

Each offer header string.

parameter header Array(String)

The extension offer header tokens.

Implementation

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

def accept(headers)

Accept server extension responses and record the negotiated extensions.

Signature

parameter headers Array(String)

The Sec-WebSocket-Extensions response header values.

returns Array

The accepted extensions as [klass, options] pairs.

Implementation

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

def apply(connection)

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

Signature

parameter connection Connection

The WebSocket connection to configure.

Implementation

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