Class: Funnel::Routing::Router

Inherits:
WebSocket::Connection
  • Object
show all
Defined in:
lib/funnel/routing/router.rb

Instance Method Summary collapse

Instance Method Details

#accept_connectionObject

TODO: add WebSocket-Protocol



45
46
47
48
49
50
51
52
53
# File 'lib/funnel/routing/router.rb', line 45

def accept_connection
  response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
  response << "Upgrade: WebSocket\r\n"
  response << "Connection: Upgrade\r\n"
  response << "WebSocket-Origin: #{origin}\r\n"
  response << "WebSocket-Location: ws://#{host}#{path}\r\n\r\n"
  
  send_data response
end

#defer_connectionObject

change the handler from this instance, to a new instance of the type defined by the routing map



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/funnel/routing/router.rb', line 57

def defer_connection
  handler = Routes.get_handler(path)

  unless handler
    Log.info "No route matches #{path}"
    close_connection and return
  end

  conn = handler.send(:new, @signature)
  conn.headers = @headers

  EM.instance_variable_get("@conns")[@signature] = conn
end

#is_flash_policy_request?(data) ⇒ Boolean

since we expect some client to be connecting via a flash socket, we need to handle the policy request if we detect ‘<’ char

Returns:

  • (Boolean)


78
79
80
# File 'lib/funnel/routing/router.rb', line 78

def is_flash_policy_request? data
  data[0] == ?<
end

#receive_data(data) ⇒ Object

parse headers, validate headers, accept connection, and change the default handler



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/funnel/routing/router.rb', line 8

def receive_data data
  
  if is_flash_policy_request?(data)
    send_data Funnel::Flash::Policy.response
    close_connection and return
  end

  @headers = WebSocket::Headers.parse(data)

  unless valid_connection?
    Log.info("Invalid WebSocket Connection")
    close_connection and return
  end

  unless valid_origin?
    Log.info("Invalid WebSocket Origin")
    close_connection and return
  end

  accept_connection
  defer_connection

end

#requested_protocolObject



71
72
73
# File 'lib/funnel/routing/router.rb', line 71

def requested_protocol
  @header[:websocket_protocol] || :default
end

#valid_connection?Boolean

is this a websocket connection?

Returns:

  • (Boolean)


33
34
35
# File 'lib/funnel/routing/router.rb', line 33

def valid_connection?
  @headers[:connection] =~ /upgrade/i && @headers[:upgrade] =~ /websocket/i
end

#valid_origin?Boolean

we can limit the availability to this service by setting the accepted_origins configuration

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/funnel/routing/router.rb', line 39

def valid_origin?
  accepted = Funnel::Configuration.get(:accepted_origins)
  accepted.empty? or accepted.include?(@headers[:origin])
end