Class: Faye::WebSocket::Protocol8Parser::Handshake
- Inherits:
-
Object
- Object
- Faye::WebSocket::Protocol8Parser::Handshake
- Defined in:
- lib/faye/util/web_socket/protocol8_parser.rb
Instance Method Summary collapse
- #complete? ⇒ Boolean
-
#initialize(uri) ⇒ Handshake
constructor
A new instance of Handshake.
- #parse(data) ⇒ Object
- #request_data ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(uri) ⇒ Handshake
Returns a new instance of Handshake.
40 41 42 43 44 45 |
# File 'lib/faye/util/web_socket/protocol8_parser.rb', line 40 def initialize(uri) @uri = uri @key = Base64.encode64((1..16).map { rand(255).chr } * '').strip @accept = Base64.encode64(Digest::SHA1.digest(@key + GUID)).strip @buffer = [] end |
Instance Method Details
#complete? ⇒ Boolean
75 76 77 |
# File 'lib/faye/util/web_socket/protocol8_parser.rb', line 75 def complete? @buffer[-4..-1] == [0x0D, 0x0A, 0x0D, 0x0A] end |
#parse(data) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/faye/util/web_socket/protocol8_parser.rb', line 61 def parse(data) = [] complete = false data.each_byte do |byte| if complete << byte else @buffer << byte complete ||= complete? end end end |
#request_data ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/faye/util/web_socket/protocol8_parser.rb', line 47 def request_data hostname = @uri.host + (@uri.port ? ":#{@uri.port}" : '') handshake = "GET #{@uri.path}#{@uri.query ? '?' : ''}#{@uri.query} HTTP/1.1\r\n" handshake << "Host: #{hostname}\r\n" handshake << "Upgrade: websocket\r\n" handshake << "Connection: Upgrade\r\n" handshake << "Sec-WebSocket-Key: #{@key}\r\n" handshake << "Sec-WebSocket-Version: 8\r\n" handshake << "\r\n" handshake end |
#valid? ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/faye/util/web_socket/protocol8_parser.rb', line 79 def valid? data = Faye.encode(@buffer) response = Net::HTTPResponse.read_new(Net::BufferedIO.new(StringIO.new(data))) return false unless response.code.to_i == 101 upgrade, connection = response['Upgrade'], response['Connection'] upgrade and upgrade =~ /^websocket$/i and connection and connection.split(/\s*,\s*/).include?('Upgrade') and response['Sec-WebSocket-Accept'] == @accept end |