class WebSocketRb::Wrapper::HandshakeRequest

Constants

HTTP_HEADER
PROTOCOL
VERSION

Attributes

headers[R]
path[R]
version[R]

Public Class Methods

new(request) click to toggle source
# 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

Public Instance Methods

key() click to toggle source
# File lib/web_socket_rb/wrapper/handshake_request.rb, line 41
def key
  headers['Sec-WebSocket-Key'].to_s
end
upgrade?() click to toggle source

Verify if request contains upgrade to websocket demand

# File lib/web_socket_rb/wrapper/handshake_request.rb, line 25
def upgrade?
  headers['Connection'] == 'Upgrade' && headers['Upgrade'] == 'websocket'
end
valid_protocol?() click to toggle source

Verify if request contains a valid protocol request

# 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?() click to toggle source

Verify if request contains a valid version of websocket protocol

# File lib/web_socket_rb/wrapper/handshake_request.rb, line 30
def valid_version?
  headers['Sec-WebSocket-Version'].to_i == VERSION
end

Private Instance Methods

convert_request_to_hash() click to toggle source

Read each line of request and store it into Hash as key-val pair of all parsed headers

# File lib/web_socket_rb/wrapper/handshake_request.rb, line 49
def convert_request_to_hash
  until (line = @request.gets.strip).empty?
    if !!(line =~ HTTP_HEADER)
      _, @path, @version = line.match(HTTP_HEADER).to_a
    else
      key, val = line.split(':', 2).map(&:strip)
      @headers.store(key, val)
    end
  end
end