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 =
'faye-browser-min.js'
TYPE_JSON =
{'Content-Type' => 'application/json; charset=utf-8'}
TYPE_SCRIPT =
{'Content-Type' => 'text/javascript; charset=utf-8'}
TYPE_TEXT =
{'Content-Type' => 'text/plain; charset=utf-8'}
VALID_JSONP_CALLBACK =
/^[a-z_\$][a-z0-9_\$]*(\.[a-z_\$][a-z0-9_\$]*)*$/i
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::LOG_LEVELS

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RackAdapter.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/faye/adapters/rack_adapter.rb', line 25

def initialize(app = nil, options = nil, &block)
  @app     = app if app.respond_to?(:call)
  @options = [app, options].grep(Hash).first || {}

  ::WebSocket::Driver.validate_options(@options, [:engine, :mount, :ping, :timeout, :extensions, :websocket_extensions])

  @endpoint    = @options[:mount] || DEFAULT_ENDPOINT
  @extensions  = []
  @endpoint_re = Regexp.new('^' + @endpoint.gsub(/\/$/, '') + '(/[^/]*)*(\\.[^\\.]+)?$')
  @server      = Server.new(@options)

  @static = StaticServer.new(ROOT, /\.(?:js|map)$/)
  @static.map(File.basename(@endpoint) + '.js', SCRIPT_PATH)
  @static.map('client.js', SCRIPT_PATH)

  if extensions = @options[:extensions]
    [*extensions].each { |extension| add_extension(extension) }
  end

  if websocket_extensions = @options[:websocket_extensions]
    [*websocket_extensions].each { |ext| add_websocket_extension(ext) }
  end

  block.call(self) if block
end

Instance Method Details

#add_extension(extension) ⇒ Object



55
56
57
# File 'lib/faye/adapters/rack_adapter.rb', line 55

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

#add_websocket_extension(extension) ⇒ Object



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

def add_websocket_extension(extension)
  @extensions << extension
end

#call(env) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/faye/adapters/rack_adapter.rb', line 75

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 @static.call(env) if @static =~ request.path_info

  # http://groups.google.com/group/faye-users/browse_thread/thread/4a01bb7d25d3636a
  if env['REQUEST_METHOD'] == 'OPTIONS' or env['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'POST'
    return handle_options
  end

  return handle_websocket(request)   if Faye::WebSocket.websocket?(env)
  return handle_eventsource(request) if Faye::EventSource.eventsource?(env)

  handle_request(request)
end

#closeObject



67
68
69
# File 'lib/faye/adapters/rack_adapter.rb', line 67

def close
  @server.close
end

#get_clientObject



71
72
73
# File 'lib/faye/adapters/rack_adapter.rb', line 71

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

#listen(*args) ⇒ Object



51
52
53
# File 'lib/faye/adapters/rack_adapter.rb', line 51

def listen(*args)
  raise 'The listen() method is deprecated - see https://github.com/faye/faye-websocket-ruby#running-your-socket-application for information on running your Faye server'
end

#remove_extension(extension) ⇒ Object



59
60
61
# File 'lib/faye/adapters/rack_adapter.rb', line 59

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