Class: WebSocket::Frame::Handler::Handler75

### Home Classes Methods

### In Files

- lib/websocket/frame/handler/handler75.rb

### Parent

WebSocket::Frame::Handler::Base

### Methods

- #decode_frame
- #encode_frame
- #supported_frames

### 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::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::Frame::Handler::Handler75

### Public Instance Methods

decode_frame() click to toggle source

@see WebSocket::Frame::Handler::Base#decode_frame

    # File lib/websocket/frame/handler/handler75.rb, line 25
    def decode_frame
      return if @frame.data.size == 0

      pointer = 0
      frame_type = @frame.data.getbyte(pointer)
      pointer += 1

      if (frame_type & 0x80) == 0x80
        # If the high-order bit of the /frame type/ byte is set
        length = 0

        loop do
          return unless @frame.data.getbyte(pointer)
          b = @frame.data.getbyte(pointer)
          pointer += 1
          b_v = b & 0x7F
          length = length * 128 + b_v
          break unless (b & 0x80) == 0x80
        end

        raise WebSocket::Error::Frame::TooLong if length > ::WebSocket.max_frame_size

        unless @frame.data.getbyte(pointer + length - 1) == nil
          # Straight from spec - I'm sure this isn't crazy...
          # 6. Read /length/ bytes.
          # 7. Discard the read bytes.
          @frame.instance_variable_set '@data', @frame.data[(pointer + length)..-1]

          # If the /frame type/ is 0xFF and the /length/ was 0, then close
          if length == 0
            @frame.class.new(version: @frame.version, type: :close, decoded: true)
          end
        end
      else
        # If the high-order bit of the /frame type/ byte is _not_ set

        raise WebSocket::Error::Frame::Invalid if @frame.data.getbyte(0) != 0x00

        # Addition to the spec to protect against malicious requests
        raise WebSocket::Error::Frame::TooLong if @frame.data.size > ::WebSocket.max_frame_size

        msg = @frame.data.slice!(/\A\x00[^\xff]*\xff/)
        if msg
          msg.gsub!(/\A\x00|\xff\z/, '')
          msg.force_encoding('UTF-8')
          @frame.class.new(version: @frame.version, type: :text, data: msg, decoded: true)
        end
      end
    end

encode_frame() click to toggle source

@see WebSocket::Frame::Handler::Base#encode_frame

    # File lib/websocket/frame/handler/handler75.rb, line 13
    def encode_frame
      case @frame.type
        when :close then "\xff\x00"
        when :text then
          ary = ["\x00", @frame.data, "\xff"]
          ary.map { |s| s.encode('UTF-8', 'UTF-8', invalid: :replace) }
          ary.join
        else raise WebSocket::Error::Frame::UnknownFrameType
      end
    end

supported_frames() click to toggle source

@see WebSocket::Frame::Base#supported_frames

    # File lib/websocket/frame/handler/handler75.rb, line 8
    def supported_frames
      [:text, :close]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.