Class: WebSocket::Driver::Proxy

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/websocket/driver/proxy.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventEmitter

#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener

Constructor Details

#initialize(client, origin, options) ⇒ Proxy

Returns a new instance of Proxy.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/websocket/driver/proxy.rb', line 11

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers


9
10
11
# File 'lib/websocket/driver/proxy.rb', line 9

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status


9
10
11
# File 'lib/websocket/driver/proxy.rb', line 9

def status
  @status
end

Instance Method Details

#parse(chunk) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/websocket/driver/proxy.rb', line 51

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

#set_header(name, value) ⇒ Object


33
34
35
36
37
# File 'lib/websocket/driver/proxy.rb', line 33

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

#startObject


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/websocket/driver/proxy.rb', line 39

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