Class: Async::WebSocket::UpgradeRequest

Inherits:
Protocol::HTTP::Request
  • Object
show all
Includes:
Protocol::WebSocket::Headers
Defined in:
lib/async/websocket/upgrade_request.rb

Overview

This is required for HTTP/1.x to upgrade the connection to the WebSocket protocol.

Defined Under Namespace

Classes: Wrapper

Instance Method Summary collapse

Constructor Details

#initialize(request, protocols: [], version: 13, &block) ⇒ UpgradeRequest

Returns a new instance of UpgradeRequest.



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

def initialize(request, protocols: [], version: 13, &block)
	@key = Nounce.generate_key
	
	headers = ::Protocol::HTTP::Headers[request.headers]
	
	headers.add(SEC_WEBSOCKET_KEY, @key)
	headers.add(SEC_WEBSOCKET_VERSION, String(version))
	
	if protocols.any?
		headers.add(SEC_WEBSOCKET_PROTOCOL, protocols.join(','))
	end
	
	super(request.scheme, request.authority, ::Protocol::HTTP::Methods::GET, request.path, nil, headers, nil, PROTOCOL)
end

Instance Method Details

#call(connection) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/async/websocket/upgrade_request.rb', line 91

def call(connection)
	response = super
	
	if accept_digest = response.headers[SEC_WEBSOCKET_ACCEPT]&.first
		expected_accept_digest = Nounce.accept_digest(@key)
		
		unless accept_digest and accept_digest == expected_accept_digest
			raise ProtocolError, "Invalid accept digest, expected #{expected_accept_digest.inspect}, got #{accept_digest.inspect}!"
		end
	end
	
	return Wrapper.new(response)
end