class Async::WebSocket::UpgradeRequest

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

Public Class Methods

new(request, protocols: [], version: 13, &block) click to toggle source
Calls superclass method
# 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

Public Instance Methods

call(connection) click to toggle source
Calls superclass method
# 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