Method: FTW::WebSocket::Rack#initialize

Defined in:
lib/ftw/websocket/rack.rb

#initialize(rack_env) ⇒ Rack

Create a new websocket rack helper… thing.

Parameters:

  • rack_env

    the ‘env’ bit given to your Rack application



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ftw/websocket/rack.rb', line 31

def initialize(rack_env)
  @env = rack_env
  @handshake_errors = []

  # RFC6455 section 4.2.1 bullet 3
  expect_equal("websocket", @env["HTTP_UPGRADE"],
               "The 'Upgrade' header must be set to 'websocket'")
  # RFC6455 section 4.2.1 bullet 4
  # Firefox uses a multivalued 'Connection' header, that appears like this:
  #   Connection: keep-alive, Upgrade
  # So we have to split this multivalue field. 
  expect_equal(true,
               @env["HTTP_CONNECTION"].split(/, +/).include?("Upgrade"),
               "The 'Connection' header must be set to 'Upgrade'")
  # RFC6455 section 4.2.1 bullet 6
  expect_equal("13", @env["HTTP_SEC_WEBSOCKET_VERSION"],
               "Sec-WebSocket-Version must be set to 13")

  # RFC6455 section 4.2.1 bullet 5
  @key = @env["HTTP_SEC_WEBSOCKET_KEY"] 

  @parser = FTW::WebSocket::Parser.new
end