Sha256: 2b2976148907297ae442a67bf8beb5314b2d5785003b9271be3990af1cf455db

Contents?: true

Size: 1.68 KB

Versions: 80

Compression:

Stored size: 1.68 KB

Contents

module WebSocket
  class Driver

    class Proxy
      include EventEmitter

      PORTS = {'ws' => 80, 'wss' => 443}

      attr_reader :status, :headers

      def initialize(client, origin, options)
        super()

        @client  = client
        @http    = HTTP::Response.new
        @socket  = client.instance_variable_get(:@socket)
        @origin  = URI.parse(@socket.url)
        @url     = URI.parse(origin)
        @options = options
        @state   = 0

        @headers = Headers.new
        @headers['Host'] = @origin.host + (@origin.port ? ":#{@origin.port}" : '')
        @headers['Connection'] = 'keep-alive'
        @headers['Proxy-Connection'] = 'keep-alive'

        if @url.user
          auth = Base64.strict_encode64([@url.user, @url.password] * ':')
          @headers['Proxy-Authorization'] = 'Basic ' + auth
        end
      end

      def set_header(name, value)
        return false unless @state == 0
        @headers[name] = value
        true
      end

      def start
        return false unless @state == 0
        @state = 1

        port    = @origin.port || PORTS[@origin.scheme]
        start   = "CONNECT #{@origin.host}:#{port} HTTP/1.1"
        headers = [start, @headers.to_s, '']

        @socket.write(headers.join("\r\n"))
        true
      end

      def parse(chunk)
        @http.parse(chunk)
        return unless @http.complete?

        @status  = @http.code
        @headers = Headers.new(@http.headers)

        if @status == 200
          emit(:connect, ConnectEvent.new)
        else
          message = "Can't establish a connection to the server at #{@socket.url}"
          emit(:error, ProtocolError.new(message))
        end
      end
    end

  end
end

Version data entries

80 entries across 52 versions & 10 rubygems

Version Path
kinetic_sdk-5.0.9 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.9 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.8 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.8 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.7 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.7 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.6 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.6 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.5 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.5 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/websocket-driver-0.7.1/lib/websocket/driver/proxy.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/websocket-driver-0.7.1/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.4 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.4 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.3 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.3 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.2 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.2 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.2.beta.4 gems/websocket-driver-0.6.5-java/lib/websocket/driver/proxy.rb
kinetic_sdk-5.0.2.beta.4 gems/websocket-driver-0.6.5/lib/websocket/driver/proxy.rb