class WebSocket::Handshake::Base

@abstract Subclass and override to implement custom handshakes

Constants

Attributes

headers[R]
host[R]
path[R]
protocols[R]
query[R]
secure[R]
state[R]
version[R]

Public Class Methods

new(args = {}) click to toggle source

Initialize new WebSocket Handshake and set it's state to :new

# File lib/websocket/handshake/base.rb, line 15
def initialize(args = {})
  args.each do |k, v|
    value = begin
      v.dup
    rescue TypeError
      v
    end
    instance_variable_set("@#{k}", value)
  end

  @state = :new
  @handler = nil

  @data = String.new('')
  @headers ||= {}
  @protocols ||= []
end

Public Instance Methods

<<(data) click to toggle source

@abstract Add data to handshake

# File lib/websocket/handshake/base.rb, line 34
def <<(data)
  @data << data
end
default_port() click to toggle source

Return default port for protocol (80 for ws, 443 for wss)

# File lib/websocket/handshake/base.rb, line 70
def default_port
  secure ? 443 : 80
end
default_port?() click to toggle source

Check if provided port is a default one

# File lib/websocket/handshake/base.rb, line 75
def default_port?
  port == default_port
end
finished?() click to toggle source

Is parsing of data finished? @return [Boolena] True if request was completely parsed or error occured. False otherwise

# File lib/websocket/handshake/base.rb, line 47
def finished?
  @state == :finished || @state == :error
end
leftovers() click to toggle source

Data left from parsing. Sometimes data that doesn't belong to handshake are added - use this method to retrieve them. @return [String] String if some data are available. Nil otherwise

# File lib/websocket/handshake/base.rb, line 65
def leftovers
  (@leftovers.to_s.split("\n", reserved_leftover_lines + 1)[reserved_leftover_lines] || '').strip
end
port() click to toggle source
# File lib/websocket/handshake/base.rb, line 79
def port
  @port || default_port
end
should_respond?() click to toggle source

@abstract Should send data after parsing is finished?

# File lib/websocket/handshake/base.rb, line 59
def should_respond?
  raise NotImplementedError
end
to_s() click to toggle source

Return textual representation of handshake request or response @return [String] text of response

# File lib/websocket/handshake/base.rb, line 40
def to_s
  @handler ? @handler.to_s : ''
end
uri() click to toggle source

URI of request. @return [String] Full URI with protocol @example

@handshake.uri #=> "ws://example.com/path?query=true"
# File lib/websocket/handshake/base.rb, line 87
def uri
  uri =  String.new(secure ? 'wss://' : 'ws://')
  uri << host
  uri << ":#{port}" unless default_port?
  uri << path
  uri << "?#{query}" if query
  uri
end
valid?() click to toggle source

Is parsed data valid? @return [Boolean] False if some errors occured. Reason for error could be found in error method

# File lib/websocket/handshake/base.rb, line 53
def valid?
  finished? && @error.nil? && @handler && @handler.valid?
end

Private Instance Methods

error=(message) click to toggle source

Changes state to error and sets error message @param [String] message Error message to set

Calls superclass method
# File lib/websocket/handshake/base.rb, line 106
def error=(message)
  @state = :error
  super
end
parse_data() click to toggle source

Parse data imported to handshake and sets state to finished if necessary. @return [Boolean] True if finished parsing. False if not all data received yet.

# File lib/websocket/handshake/base.rb, line 115
def parse_data
  header, @leftovers = @data.split("\r\n\r\n", 2)
  return false unless @leftovers # The whole header has not been received yet.

  lines = header.split("\r\n")

  first_line = lines.shift
  parse_first_line(first_line)

  lines.each do |line|
    h = HEADER.match(line)
    next unless h # Skip any invalid headers
    key = h[1].strip.downcase
    val = h[2].strip
    # If the header is already set and refers to the websocket protocol, append the new value
    if @headers.key?(key) && key =~ /^(sec-)?websocket-protocol$/
      @headers[key] << ", #{val}"
    else
      @headers[key] = val
    end
  end

  @state = :finished
  true
end
reserved_leftover_lines() click to toggle source

Number of lines after header that should be handled as belonging to handshake. Any data after those lines will be handled as leftovers. @return [Integer] Number of lines

# File lib/websocket/handshake/base.rb, line 100
def reserved_leftover_lines
  0
end