Sha256: 93b2a34d3aa4119dd901e345e703d49e3c884b48c373d0d62e1b5c204fbd1f3d

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

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
          [
            %w[Upgrade websocket],
            %w[Connection Upgrade],
            ['Sec-WebSocket-Accept', signature]
          ] + protocol
        end

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

        def verify_key
          raise WebSocket::Error::Handshake::InvalidAuthentication unless key
          true
        end

        def key
          @handshake.headers['sec-websocket-key']
        end

        def protocol
          return [] unless @handshake.headers.key?('sec-websocket-protocol')
          protos = @handshake.headers['sec-websocket-protocol'].split(/ *, */) & @handshake.protocols
          [['Sec-WebSocket-Protocol', protos.first]]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/websocket-1.2.10/lib/websocket/handshake/handler/server04.rb
websocket-1.2.10 lib/websocket/handshake/handler/server04.rb