Class/Module Index [+]

Quicksearch

WebSocket::Handshake::Server

Construct or parse a server WebSocket handshake.

@example

handshake = WebSocket::Handshake::Server.new

# Parse client request
@handshake << <<EOF
GET /demo HTTP/1.1\r
Upgrade: websocket\r
Connection: Upgrade\r
Host: example.com\r
Sec-WebSocket-Origin: http://example.com\r
Sec-WebSocket-Version: 13\r
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r
\r
EOF

# All data received?
@handshake.finished?

# No parsing errors?
@handshake.valid?

# Create response
@handshake.to_s # HTTP/1.1 101 Switching Protocols
                # Upgrade: websocket
                # Connection: Upgrade
                # Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Constants

PATH

Public Class Methods

new(args = {}) click to toggle source

Initialize new WebSocket Server

@param [Hash] args Arguments for server

@option args [Boolean] :secure If true then server will use wss:// protocol

@example

Websocket::Handshake::Server.new(secure: true)
# File lib/websocket/handshake/server.rb, line 42
def initialize(args = {})
  super
  @secure = !!args[:secure]
end

Public Instance Methods

<<(data) click to toggle source

Add text of request from Client. This method will parse content immediately and update version, state and error(if neccessary)

@param [String] data Data to add

@example

@handshake << <<EOF
GET /demo HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: example.com
Sec-WebSocket-Origin: http://example.com
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

EOF
# File lib/websocket/handshake/server.rb, line 62
def <<(data)
  @data << data
  set_version if parse_data
end
from_hash(hash) click to toggle source

Parse the request from hash @param hash Hash to import data @option hash [Hash] :headers HTTP headers of request, downcased @option hash [String] :path Path for request(without host and query string) @option hash [String] :query Query string for request @option hash [String] :body Body of request(if exists)

@example

@handshake.from_hash(hash)
# File lib/websocket/handshake/server.rb, line 114
def from_hash(hash)
  @headers = hash[:headers] || {}
  @path = hash[:path] || '/'
  @query = hash[:query] || ''
  @leftovers = hash[:body]

  set_version
  @state = :finished
end
from_rack(env) click to toggle source

Parse the request from a rack environment @param env Rack Environment

@example

@handshake.from_rack(env)
# File lib/websocket/handshake/server.rb, line 73
def from_rack(env)
  @headers = env.select do |key, value|
    key =~ /\AHTTP_/
  end.reduce({}) do |memo, tuple|
    key, value = tuple
    memo[key.gsub(/\AHTTP_/, '').gsub('_', '-').downcase] = value
    memo
  end

  @path      = env['REQUEST_PATH']
  @query     = env['QUERY_STRING']

  set_version

  # Passenger is blocking on read
  # Unicorn doesn't support readpartial
  # Maybe someone is providing even plain string?
  # Better safe than sorry...
  if @version == 76
    input = env['rack.input']
    @leftovers =  if input.respond_to?(:readpartial)
                    input.readpartial
                  elsif input.respond_to?(:read)
                    input.read
                  else
                    input.to_s
                  end
  end

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

Host of server according to client header @return [String] host

# File lib/websocket/handshake/server.rb, line 132
def host
  @headers['host'].to_s.split(':')[0].to_s
end
port() click to toggle source

Port of server according to client header @return [String] port

# File lib/websocket/handshake/server.rb, line 138
def port
  @headers['host'].to_s.split(':')[1]
end
should_respond?() click to toggle source

Should send content to client after finished parsing? @return [Boolean] true

# File lib/websocket/handshake/server.rb, line 126
def should_respond?
  true
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.