Sha256: 169a113efcd587456338b2f5783331c14fe0cd68849de1d80bdbe63831f20a57

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

require 'digest/md5'

module Faye
  class WebSocket
    
    class Draft76Parser < Draft75Parser
      def version
        'draft-76'
      end
      
      def handshake_response
        request = @socket.request
        
        key1   = request.env['HTTP_SEC_WEBSOCKET_KEY1']
        value1 = number_from_key(key1) / spaces_in_key(key1)
        
        key2   = request.env['HTTP_SEC_WEBSOCKET_KEY2']
        value2 = number_from_key(key2) / spaces_in_key(key2)
        
        hash = Digest::MD5.digest(big_endian(value1) +
                                  big_endian(value2) +
                                  request.body.read)
        
        upgrade =  "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
        upgrade << "Upgrade: WebSocket\r\n"
        upgrade << "Connection: Upgrade\r\n"
        upgrade << "Sec-WebSocket-Origin: #{request.env['HTTP_ORIGIN']}\r\n"
        upgrade << "Sec-WebSocket-Location: #{@socket.url}\r\n"
        upgrade << "\r\n"
        upgrade << hash
        upgrade
      end
      
    private
      
      def number_from_key(key)
        key.scan(/[0-9]/).join('').to_i(10)
      end
      
      def spaces_in_key(key)
        key.scan(/ /).size
      end
      
      def big_endian(number)
        string = ''
        [24,16,8,0].each do |offset|
          string << (number >> offset & 0xFF).chr
        end
        string
      end
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
faye-0.6.8 lib/faye/util/web_socket/draft76_parser.rb
faye-0.6.7 lib/faye/util/web_socket/draft76_parser.rb
faye-0.6.6 lib/faye/util/web_socket/draft76_parser.rb
faye-0.6.5 lib/faye/util/web_socket/draft76_parser.rb