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



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

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.



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

def parser
  @parser
end

#protocol_versionObject (readonly)

Returns the value of attribute protocol_version.



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

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.



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

def headers
  @parser.headers
end

#headers_downcasedObject

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



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

def headers_downcased
  @headers
end

#originObject

Returns the WebSocket origin header if provided



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

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

#pathObject

Returns the request path (excluding any query params)



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

def path
  @parser.request_path
end

#queryObject



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

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

#query_stringObject

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



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

def query_string
  @parser.query_string
end

#receive_data(data) ⇒ Object



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

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)


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

def secure?
  @secure
end