Class: WebSocket::Driver::Proxy

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

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.



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

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'] = Driver.host_header(@origin)
  @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.



7
8
9
# File 'lib/websocket/driver/proxy.rb', line 7

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/websocket/driver/proxy.rb', line 7

def status
  @status
end

Instance Method Details

#parse(chunk) ⇒ Object



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

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



31
32
33
34
35
# File 'lib/websocket/driver/proxy.rb', line 31

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

#startObject



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

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