Sha256: 828a08dc2ebb5aff297146d9f7a601171591d0031367331afd2d1b921dc065d9

Contents?: true

Size: 981 Bytes

Versions: 4

Compression:

Stored size: 981 Bytes

Contents

module EventMachine
  module WebSocket
    class MaskedString < String
      # Read a 4 bit XOR mask - further requested bytes will be unmasked
      def read_mask
        if respond_to?(:encoding) && encoding.name != "ASCII-8BIT"
          raise "MaskedString only operates on BINARY strings"
        end
        raise "Too short" if bytesize < 4 # TODO - change
        @masking_key = String.new(self[0..3])
      end

      # Removes the mask, behaves like a normal string again
      def unset_mask
        @masking_key = nil
      end

      def slice_mask
        slice!(0, 4)
      end

      def getbyte(index)
        if @masking_key
          masked_char = super
          masked_char ? masked_char ^ @masking_key.getbyte(index % 4) : nil
        else
          super
        end
      end

      def getbytes(start_index, count)
        data = ''
        count.times do |i|
          data << getbyte(start_index + i)
        end
        data
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
em-websocket-0.3.6 lib/em-websocket/masking04.rb
em-websocket-0.3.5 lib/em-websocket/masking04.rb
em-websocket-0.3.2 lib/em-websocket/masking04.rb
em-websocket-0.3.1 lib/em-websocket/masking04.rb