Class: WebSocket::ClientHandshake

Inherits:
Http::Request
  • Object
show all
Defined in:
lib/websocket/client_handshake.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accept_token_for(websocket_key) ⇒ Object



7
8
9
# File 'lib/websocket/client_handshake.rb', line 7

def self.accept_token_for(websocket_key)
  Base64.encode64(Digest::SHA1.digest(websocket_key.strip + GUID)).strip
end

Instance Method Details

#accept_responseObject



31
32
33
# File 'lib/websocket/client_handshake.rb', line 31

def accept_response
  ServerHandshake.new(response_headers)
end

#errorsObject



11
12
13
# File 'lib/websocket/client_handshake.rb', line 11

def errors
  @errors ||= []
end

#response_headersObject



35
36
37
38
39
40
41
42
# File 'lib/websocket/client_handshake.rb', line 35

def response_headers
  websocket_key = headers['Sec-Websocket-Key']
  {
    'Upgrade'    => 'websocket',
    'Connection' => 'Upgrade',
    'Sec-WebSocket-Accept' => ClientHandshake.accept_token_for(websocket_key)
  }
end

#to_dataObject



44
45
46
47
48
49
50
51
# File 'lib/websocket/client_handshake.rb', line 44

def to_data
  data = "#{method.to_s.upcase} #{uri.path} HTTP/#{version}#{CRLF}"
  @headers.each do |field, value|
    data << "#{field}: #{value}#{CRLF}"
  end
  data << CRLF
  data
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/websocket/client_handshake.rb', line 15

def valid?
  if headers['Upgrade'].downcase != 'websocket'
    errors << 'Connection upgrade is not for websocket'
    return false
  end

  # Careful: Http gem changes header capitalization,
  # so Sec-WebSocket-Version becomes Sec-Websocket-Version
  if headers['Sec-Websocket-Version'].to_i != PROTOCOL_VERSION
    errors << "Protocol version not supported '#{headers['Sec-Websocket-Version']}'"
    return false
  end

  return true
end