Class: EventMachine::WebSocket::Handshake
- Inherits:
-
Object
- Object
- EventMachine::WebSocket::Handshake
- Includes:
- EM::Deferrable
- Defined in:
- lib/em-websocket/handshake.rb
Overview
Resposible for creating the server handshake response
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#protocol_version ⇒ Object
readonly
Returns the value of attribute protocol_version.
Instance Method Summary collapse
-
#headers ⇒ Object
Returns the WebSocket upgrade headers as a hash.
-
#headers_downcased ⇒ Object
The same as headers, except that the hash keys are downcased.
-
#initialize(secure) ⇒ Handshake
constructor
Unfortunately drafts 75 & 76 require knowledge of whether the connection is being terminated as ws/wss in order to generate the correct handshake response.
-
#origin ⇒ Object
Returns the WebSocket origin header if provided.
-
#path ⇒ Object
Returns the request path (excluding any query params).
- #query ⇒ Object
-
#query_string ⇒ Object
Returns the query params as a string foo=bar&baz=…
- #receive_data(data) ⇒ Object
- #secure? ⇒ Boolean
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
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
10 11 12 |
# File 'lib/em-websocket/handshake.rb', line 10 def parser @parser end |
#protocol_version ⇒ Object (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
#headers ⇒ Object
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_downcased ⇒ Object
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 |
#origin ⇒ Object
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 |
#path ⇒ Object
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 |
#query ⇒ Object
59 60 61 |
# File 'lib/em-websocket/handshake.rb', line 59 def query Hash[query_string.split('&').map { |c| c.split('=', 2) }] end |
#query_string ⇒ Object
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.}")) end |
#secure? ⇒ Boolean
69 70 71 |
# File 'lib/em-websocket/handshake.rb', line 69 def secure? @secure end |