Sha256: 4fdefb84e0a3dc1adc497a2cea1489bbc0a5fef43d6817eca855a3696b19ed42

Contents?: true

Size: 992 Bytes

Versions: 4

Compression:

Stored size: 992 Bytes

Contents

# frozen_string_literal: true

require "erb"
require "protocol/websocket/headers"
require_relative "response_exception"

module Wands
  # The request is used to upgrade the HTTP connection to a WebSocket connection.
  class UpgradeRequest
    include ::Protocol::WebSocket::Headers

    # The Host header is required in HTTP 1.1.
    TEMPLATE = <<~REQUEST
      GET / HTTP/1.1
      Host: <%= @host %>:<%= @port %>
      Connection: Upgrade
      Upgrade: websocket
      Sec-WebSocket-Version: 13
      Sec-WebSocket-Key: <%= @key %>

    REQUEST

    ERB = ERB.new(TEMPLATE).freeze

    def initialize(host, port)
      @host = host
      @port = port
      @key = Nounce.generate_key
    end

    def to_s
      ERB.result(binding).gsub(/\r?\n/, "\r\n")
    end

    def verify(response)
      accept_digest = response.header[SEC_WEBSOCKET_ACCEPT].first
      accept_digest == Nounce.accept_digest(@key) || raise(ResponseException.new("Invalid accept digest", response))
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wands-0.4.0 lib/wands/upgrade_request.rb
wands-0.3.0 lib/wands/upgrade_request.rb
wands-0.2.0 lib/wands/upgrade_request.rb
wands-0.1.0 lib/wands/upgrade_request.rb