Sha256: 6d2a2e152f8304df152381599807257404e13698574b78205e1d71d8b7ecd30a

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 KB

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 getbyte(index)
        if defined?(@masking_key) && @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 = ''
        data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
        count.times do |i|
          data << getbyte(start_index + i)
        end
        data
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
em-websocket-0.5.3 lib/em-websocket/masking04.rb
em-websocket-0.5.2 lib/em-websocket/masking04.rb
sonixlabs-em-websocket-0.5.1.5 lib/em-websocket/masking04.rb
sonixlabs-em-websocket-0.5.1.4 lib/em-websocket/masking04.rb
sonixlabs-em-websocket-0.5.1.3 lib/em-websocket/masking04.rb
sonixlabs-em-websocket-0.5.1.2 lib/em-websocket/masking04.rb
sonixlabs-em-websocket-0.5.1.1 lib/em-websocket/masking04.rb
em-websocket-0.5.1 lib/em-websocket/masking04.rb
em-websocket-0.5.0 lib/em-websocket/masking04.rb