Sha256: fc729ab094aa704b52cebefde0b328ea3030812719c2c16c4773a945cffa5a75

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

require 'digest/sha1'
require 'base64'

module WebSocket
  module Handshake
    module Handler
      class Server04 < Server

        # @see WebSocket::Handshake::Base#valid?
        def valid?
          super && verify_key
        end

        private

        # @see WebSocket::Handshake::Handler::Base#header_line
        def header_line
          "HTTP/1.1 101 Switching Protocols"
        end

        # @see WebSocket::Handshake::Handler::Base#handshake_keys
        def handshake_keys
          [
            ["Upgrade", "websocket"],
            ["Connection", "Upgrade"],
            ["Sec-WebSocket-Accept", signature]
          ]
        end

        # Signature of response, created from client request Sec-WebSocket-Key
        # @return [String] signature
        def signature
          return unless key = @handshake.headers['sec-websocket-key']
          string_to_sign = "#{key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
          Base64.encode64(Digest::SHA1.digest(string_to_sign)).chomp
        end

        def verify_key
          (@handshake.headers['sec-websocket-key'] ? true : raise(WebSocket::Error::Handshake::InvalidAuthentication))
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
websocket-1.1.4 lib/websocket/handshake/handler/server04.rb
websocket-1.1.3 lib/websocket/handshake/handler/server04.rb
websocket-1.1.2 lib/websocket/handshake/handler/server04.rb
websocket-1.1.1 lib/websocket/handshake/handler/server04.rb
websocket-1.1.0 lib/websocket/handshake/handler/server04.rb