class WebSocket::Handshake::Base
@abstract Subclass and override to implement custom handshakes
Constants
- HEADER
Attributes
Public Class Methods
Public Instance Methods
@abstract Add data to handshake
# File lib/websocket/handshake/base.rb, line 20 def <<(data) raise NotImplementedError end
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 40 def finished? @state == :finished || @state == :error end
Recreate inspect as to_s was overwritten
# File lib/websocket/handshake/base.rb, line 32 def inspect vars = self.instance_variables.map { |v| "#{v}=#{instance_variable_get(v).inspect}" }.join(', ') insp = "#{self.class}:0x%08x" % (self.__id__ * 2) "<#{insp} #{vars}>" end
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 58 def leftovers (@leftovers.to_s.split("\n", reserved_leftover_lines + 1)[reserved_leftover_lines] || "").strip end
@abstract Should send data after parsing is finished?
# File lib/websocket/handshake/base.rb, line 52 def should_respond? raise NotImplementedError end
Return textual representation of handshake request or response @return [String] text of response
# File lib/websocket/handshake/base.rb, line 26 def to_s @handler ? @handler.to_s : '' end
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 66 def uri uri = secure ? 'wss://' : 'ws://' uri << host uri << ":#{port}" if port uri << path uri << "?#{query}" if query uri end
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 46 def valid? finished? && @error == nil && @handler && @handler.valid? end
Private Instance Methods
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 94 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) @headers[h[1].strip.downcase] = h[2].strip if h end @state = :finished true end
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 79 def reserved_leftover_lines 0 end
Changes state to error and sets error message @param [String] message Error message to set
# File lib/websocket/handshake/base.rb, line 85 def set_error(message) @state = :error super end