Class: Async::WebSocket::UpgradeResponse

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

Overview

The response from the server back to the client for negotiating HTTP/1.x WebSockets.

Instance Method Summary collapse

Constructor Details

#initialize(request, headers = nil, protocol: nil, &block) ⇒ UpgradeResponse

Returns a new instance of UpgradeResponse.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/async/websocket/upgrade_response.rb', line 17

def initialize(request, headers = nil, protocol: nil, &block)
  headers = ::Protocol::HTTP::Headers[headers]
  
  if accept_nounce = request.headers[SEC_WEBSOCKET_KEY]&.first
    headers.add(SEC_WEBSOCKET_ACCEPT, Nounce.accept_digest(accept_nounce))
    
    if protocol
      headers.add(SEC_WEBSOCKET_PROTOCOL, protocol)
    end
    
    body = Async::HTTP::Body::Hijack.wrap(request, &block)
    
    super(request.version, 101, headers, body, PROTOCOL)
  else
    super(request.version, 400, headers)
  end
end