Class: Faye::RackAdapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/faye/adapters/rack_adapter.rb

Constant Summary collapse

ASYNC_RESPONSE =
[-1, {}, []].freeze
DEFAULT_ENDPOINT =
'/bayeux'
SCRIPT_PATH =
File.join(ROOT, 'faye-browser-min.js')
TYPE_JSON =
{'Content-Type' => 'application/json'}
TYPE_SCRIPT =
{'Content-Type' => 'text/javascript'}
TYPE_TEXT =
{'Content-Type' => 'text/plain'}
HTTP_X_NO_CONTENT_LENGTH =

This header is passed by Rack::Proxy during testing. Rack::Proxy seems to set content-length for you, and setting it in here really slows the tests down. Better suggestions welcome.

'HTTP_X_NO_CONTENT_LENGTH'

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary

Attributes included from Logging

#log_level

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(app = nil, options = nil) ⇒ RackAdapter

Returns a new instance of RackAdapter.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/faye/adapters/rack_adapter.rb', line 23

def initialize(app = nil, options = nil)
  @app      = app if app.respond_to?(:call)
  @options  = [app, options].grep(Hash).first || {}
  
  @endpoint    = @options[:mount] || DEFAULT_ENDPOINT
  @endpoint_re = Regexp.new('^' + @endpoint + '(/[^/]*)*(\\.js)?$')
  @server      = Server.new(@options)
  
  return unless extensions = @options[:extensions]
  [*extensions].each { |extension| add_extension(extension) }
end

Instance Method Details

#add_extension(extension) ⇒ Object



35
36
37
# File 'lib/faye/adapters/rack_adapter.rb', line 35

def add_extension(extension)
  @server.add_extension(extension)
end

#call(env) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/faye/adapters/rack_adapter.rb', line 68

def call(env)
  Faye.ensure_reactor_running!
  request = Rack::Request.new(env)
  
  unless request.path_info =~ @endpoint_re
    env['faye.client'] = get_client
    return @app ? @app.call(env) :
                  [404, TYPE_TEXT, ["Sure you're not looking for #{@endpoint} ?"]]
  end
  
  return serve_client_script(env) if request.path_info =~ /\.js$/
  return handle_options(request)  if env['REQUEST_METHOD'] == 'OPTIONS'
  return handle_websocket(env)    if Faye::WebSocket.websocket?(env)
  return handle_eventsource(env)  if Faye::EventSource.eventsource?(env)
  
  handle_request(request)
end

#get_clientObject



43
44
45
# File 'lib/faye/adapters/rack_adapter.rb', line 43

def get_client
  @client ||= Client.new(@server)
end

#listen(port, ssl_options = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/faye/adapters/rack_adapter.rb', line 47

def listen(port, ssl_options = nil)
  Faye::WebSocket.load_adapter('thin')
  handler = Rack::Handler.get('thin')
  handler.run(self, :Port => port) do |s|
    if ssl_options
      s.ssl = true
      s.ssl_options = {
        :private_key_file => ssl_options[:key],
        :cert_chain_file  => ssl_options[:cert]
      }
    end
    @thin_server = s
  end
end

#remove_extension(extension) ⇒ Object



39
40
41
# File 'lib/faye/adapters/rack_adapter.rb', line 39

def remove_extension(extension)
  @server.remove_extension(extension)
end

#stopObject



62
63
64
65
66
# File 'lib/faye/adapters/rack_adapter.rb', line 62

def stop
  return unless @thin_server
  @thin_server.stop
  @thin_server = nil
end