Class: WebSocketRb::Wrapper::HandshakeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/web_socket_rb/wrapper/handshake_request.rb

Constant Summary collapse

HTTP_HEADER =

GET /chat HTTP/1.1

/^GET (\/[[:graph:]]*[\/[[:graph:]]]*) HTTP\/([[:digit:]]+\.[[:digit:]]+)$/
VERSION =
13
PROTOCOL =
'json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ HandshakeRequest

Returns a new instance of HandshakeRequest.



15
16
17
18
19
20
21
22
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 15

def initialize(request)
  @request = request
  @headers = {}
  @version = '1.1'
  @path    = '/'

  convert_request_to_hash
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 13

def headers
  @headers
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 13

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 13

def version
  @version
end

Instance Method Details

#keyObject



41
42
43
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 41

def key
  headers['Sec-WebSocket-Key'].to_s
end

#upgrade?Boolean

Verify if request contains upgrade to websocket demand

Returns:

  • (Boolean)


25
26
27
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 25

def upgrade?
  headers['Connection'] == 'Upgrade' && headers['Upgrade'] == 'websocket'
end

#valid_protocol?Boolean

Verify if request contains a valid protocol request

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 35

def valid_protocol?
  protocols = headers['Sec-WebSocket-Protocol'].to_s
  protocols = protocols.split(',').map(&:strip)
  protocols.empty? || protocols.include?(PROTOCOL)
end

#valid_version?Boolean

Verify if request contains a valid version of websocket protocol

Returns:

  • (Boolean)


30
31
32
# File 'lib/web_socket_rb/wrapper/handshake_request.rb', line 30

def valid_version?
  headers['Sec-WebSocket-Version'].to_i == VERSION
end