Sha256: 2f8d98c68b96232500b03d3a30fa7ac5effc989a711312108e1bc206bc828e95

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

require 'digest/sha1'
require 'base64'

module EventMachine
  module WebSocket
    module Handshake04
      def handshake
        # Required
        unless key = request['sec-websocket-key']
          raise HandshakeError, "Sec-WebSocket-Key header is required"
        end
        
        # Optional
        origin = request['sec-websocket-origin']
        protocols = request['sec-websocket-protocol']
        extensions = request['sec-websocket-extensions']
        
        string_to_sign = "#{key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
        signature = Base64.encode64(Digest::SHA1.digest(string_to_sign)).chomp
        
        upgrade = ["HTTP/1.1 101 Switching Protocols"]
        upgrade << "Upgrade: websocket"
        upgrade << "Connection: Upgrade"
        upgrade << "Sec-WebSocket-Accept: #{signature}"
        
        # TODO: Support Sec-WebSocket-Protocol
        # TODO: Sec-WebSocket-Extensions
        
        debug [:upgrade_headers, upgrade]
        
        return upgrade.join("\r\n") + "\r\n\r\n"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
em-websocket-0.3.8 lib/em-websocket/handshake04.rb
em-websocket-0.3.7 lib/em-websocket/handshake04.rb
em-websocket-0.3.6 lib/em-websocket/handshake04.rb
em-websocket-0.3.5 lib/em-websocket/handshake04.rb
em-websocket-0.3.2 lib/em-websocket/handshake04.rb
em-websocket-0.3.1 lib/em-websocket/handshake04.rb
em-websocket-0.3.0 lib/em-websocket/handshake04.rb