lib/faye/adapters/rack_adapter.rb in faye-0.8.1 vs lib/faye/adapters/rack_adapter.rb in faye-0.8.2

- old
+ new

@@ -7,11 +7,11 @@ def_delegators "@server.engine", :bind, :unbind ASYNC_RESPONSE = [-1, {}, []].freeze DEFAULT_ENDPOINT = '/bayeux' - SCRIPT_PATH = File.join(ROOT, 'faye-browser-min.js') + SCRIPT_PATH = 'faye-browser-min.js' TYPE_JSON = {'Content-Type' => 'application/json'} TYPE_SCRIPT = {'Content-Type' => 'text/javascript'} TYPE_TEXT = {'Content-Type' => 'text/plain'} @@ -23,13 +23,17 @@ 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)?$') + @endpoint_re = Regexp.new('^' + @endpoint + '(/[^/]+)*(\\.[^\\.]+)?$') @server = Server.new(@options) + @static = StaticServer.new(ROOT, /\.(?:js|map)$/) + @static.map(File.basename(@endpoint) + '.js', SCRIPT_PATH) + @static.map('client.js', SCRIPT_PATH) + return unless extensions = @options[:extensions] [*extensions].each { |extension| add_extension(extension) } end def add_extension(extension) @@ -73,39 +77,22 @@ 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' + # 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(request) + end + + return @static.call(env) if @static =~ request.path_info return handle_websocket(env) if Faye::WebSocket.websocket?(env) return handle_eventsource(env) if Faye::EventSource.eventsource?(env) handle_request(request) end private - - def serve_client_script(env) - @client_script ||= File.read(SCRIPT_PATH) - @client_digest ||= Digest::SHA1.hexdigest(@client_script) - @client_mtime ||= File.mtime(SCRIPT_PATH) - - headers = TYPE_SCRIPT.dup - ims = env['HTTP_IF_MODIFIED_SINCE'] - - headers['Content-Length'] = @client_script.bytesize.to_s unless env[HTTP_X_NO_CONTENT_LENGTH] - headers['ETag'] = @client_digest - headers['Last-Modified'] = @client_mtime.httpdate - - if env['HTTP_IF_NONE_MATCH'] == @client_digest - [304, headers, ['']] - elsif ims and @client_mtime <= Time.httpdate(ims) - [304, headers, ['']] - else - [200, headers, [@client_script]] - end - end def handle_request(request) json_msg = message_from_request(request) message = Yajl::Parser.parse(json_msg) jsonp = request.params['jsonp'] || JSONP_CALLBACK