Class: EventMachine::WebSocket::Handshake

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/em-websocket/handshake.rb

Overview

Resposible for creating the server handshake response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secure) ⇒ Handshake

Unfortunately drafts 75 & 76 require knowledge of whether the connection is being terminated as ws/wss in order to generate the correct handshake response


16
17
18
19
20
21
22
23
# File 'lib/em-websocket/handshake.rb', line 16

def initialize(secure)
  @parser = Http::Parser.new
  @secure = secure

  @parser.on_headers_complete = proc { |headers|
    @headers = Hash[headers.map { |k,v| [k.downcase, v] }]
  }
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser


11
12
13
# File 'lib/em-websocket/handshake.rb', line 11

def parser
  @parser
end

#protocol_versionObject (readonly)

Returns the value of attribute protocol_version


11
12
13
# File 'lib/em-websocket/handshake.rb', line 11

def protocol_version
  @protocol_version
end

Instance Method Details

#headersObject

Returns the WebSocket upgrade headers as a hash.

Keys are strings, unmodified from the request.


39
40
41
# File 'lib/em-websocket/handshake.rb', line 39

def headers
  @parser.headers
end

#headers_downcasedObject

The same as headers, except that the hash keys are downcased


45
46
47
# File 'lib/em-websocket/handshake.rb', line 45

def headers_downcased
  @headers
end

#originObject

Returns the WebSocket origin header if provided


66
67
68
# File 'lib/em-websocket/handshake.rb', line 66

def origin
  @headers["origin"] || @headers["sec-websocket-origin"] || nil
end

#pathObject

Returns the request path (excluding any query params)


51
52
53
# File 'lib/em-websocket/handshake.rb', line 51

def path
  @path
end

#queryObject


60
61
62
# File 'lib/em-websocket/handshake.rb', line 60

def query
  Hash[query_string.split('&').map { |c| c.split('=', 2) }]
end

#query_stringObject

Returns the query params as a string foo=bar&baz=…


56
57
58
# File 'lib/em-websocket/handshake.rb', line 56

def query_string
  @query_string
end

#receive_data(data) ⇒ Object


25
26
27
28
29
30
31
32
33
# File 'lib/em-websocket/handshake.rb', line 25

def receive_data(data)
  @parser << data

  if defined? @headers
    process(@headers, @parser.upgrade_data)
  end
rescue HTTP::Parser::Error => e
  fail(HandshakeError.new("Invalid HTTP header: #{e.message}"))
end

#secure?Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/em-websocket/handshake.rb', line 70

def secure?
  @secure
end