Class: WebSocket::Handshake::Server

### Home Classes Methods

### In Files

- lib/websocket/handshake/server.rb

### Parent

WebSocket::Handshake::Base

### Methods

- ::new
- #<<
- #from_hash
- #from_rack
- #host
- #port
- #should_respond?

### Class/Module Index

Quicksearch

- WebSocket
- WebSocket::Error
- WebSocket::Error::Frame
- WebSocket::Error::Frame::ControlFramePayloadTooLong
- WebSocket::Error::Frame::DataFrameInsteadContinuation
- WebSocket::Error::Frame::FragmentedControlFrame
- WebSocket::Error::Frame::Invalid
- WebSocket::Error::Frame::InvalidPayloadEncoding
- WebSocket::Error::Frame::MaskTooShort
- WebSocket::Error::Frame::ReservedBitUsed
- WebSocket::Error::Frame::TooLong
- WebSocket::Error::Frame::UnexpectedContinuationFrame
- WebSocket::Error::Frame::UnknownCloseCode
- WebSocket::Error::Frame::UnknownFrameType
- WebSocket::Error::Frame::UnknownOpcode
- WebSocket::Error::Frame::UnknownVersion
- WebSocket::Error::Handshake
- WebSocket::Error::Handshake::GetRequestRequired
- WebSocket::Error::Handshake::InvalidAuthentication
- WebSocket::Error::Handshake::InvalidHeader
- WebSocket::Error::Handshake::InvalidStatusCode
- WebSocket::Error::Handshake::NoHostProvided
- WebSocket::Error::Handshake::UnknownVersion
- WebSocket::ExceptionHandler
- WebSocket::ExceptionHandler::ClassMethods
- WebSocket::Frame
- WebSocket::Frame::Base
- WebSocket::Frame::Data
- WebSocket::Frame::Handler
- WebSocket::Frame::Handler::Base
- WebSocket::Frame::Handler::Handler03
- WebSocket::Frame::Handler::Handler04
- WebSocket::Frame::Handler::Handler05
- WebSocket::Frame::Handler::Handler07
- WebSocket::Frame::Handler::Handler75
- WebSocket::Frame::Incoming
- WebSocket::Frame::Incoming::Client
- WebSocket::Frame::Incoming::Server
- WebSocket::Frame::Outgoing
- WebSocket::Frame::Outgoing::Client
- WebSocket::Frame::Outgoing::Server
- WebSocket::Handshake
- WebSocket::Handshake::Base
- WebSocket::Handshake::Client
- WebSocket::Handshake::Handler
- WebSocket::Handshake::Handler::Base
- WebSocket::Handshake::Handler::Client
- WebSocket::Handshake::Handler::Client01
- WebSocket::Handshake::Handler::Client04
- WebSocket::Handshake::Handler::Client11
- WebSocket::Handshake::Handler::Client75
- WebSocket::Handshake::Handler::Client76
- WebSocket::Handshake::Handler::Server
- WebSocket::Handshake::Handler::Server04
- WebSocket::Handshake::Handler::Server75
- WebSocket::Handshake::Handler::Server76
- WebSocket::Handshake::Server

No matching classes.

# 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
    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
    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.