Class: Wands::UpgradeRequest

Inherits:
Object
  • Object
show all
Includes:
Protocol::WebSocket::Headers
Defined in:
lib/wands/upgrade_request.rb

Overview

The request is used to upgrade the HTTP connection to a WebSocket connection.

Constant Summary collapse

TEMPLATE =

The Host header is required in HTTP 1.1.

"GET / HTTP/1.1\nHost: <%= @host %>:<%= @port %>\nConnection: Upgrade\nUpgrade: websocket\nSec-WebSocket-Version: 13\nSec-WebSocket-Key: <%= @key %>\n\n"
ERB =
ERB.new(TEMPLATE).freeze

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ UpgradeRequest

Returns a new instance of UpgradeRequest.



25
26
27
28
29
# File 'lib/wands/upgrade_request.rb', line 25

def initialize(host, port)
  @host = host
  @port = port
  @key = Nounce.generate_key
end

Instance Method Details

#to_sObject



31
32
33
# File 'lib/wands/upgrade_request.rb', line 31

def to_s
  ERB.result(binding).gsub(/\r?\n/, "\r\n")
end

#verify(response) ⇒ Object



35
36
37
38
# File 'lib/wands/upgrade_request.rb', line 35

def verify(response)
  accept_digest = response.header[SEC_WEBSOCKET_ACCEPT].first
  accept_digest == Nounce.accept_digest(@key) || raise(ResponseException.new("Invalid accept digest", response))
end