lib/faye/adapters/rack_adapter.rb in faye-1.1.3 vs lib/faye/adapters/rack_adapter.rb in faye-1.2.0

- old
+ new

@@ -31,11 +31,11 @@ @endpoint = @options[:mount] || DEFAULT_ENDPOINT @extensions = [] @endpoint_re = Regexp.new('^' + @endpoint.gsub(/\/$/, '') + '(/[^/]*)*(\\.[^\\.]+)?$') @server = Server.new(@options) - @static = StaticServer.new(ROOT, /\.(?:js|map)$/) + @static = StaticServer.new(File.join(ROOT, '..', 'build', 'client'), /\.(?: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) } @@ -108,11 +108,11 @@ return [400, TYPE_TEXT, ['Bad request']] end debug("Received message via HTTP #{request.request_method}: ?", json_msg) - message = MultiJson.load(json_msg) + message = parse_json(json_msg) jsonp = request.params['jsonp'] || JSONP_CALLBACK headers = request.get? ? TYPE_SCRIPT.dup : TYPE_JSON.dup origin = request.env['HTTP_ORIGIN'] callback = request.env['async.callback'] @@ -136,11 +136,10 @@ response = "/**/#{ jsonp }(#{ jsonp_escape(response) });" headers['Content-Disposition'] = 'attachment; filename=f.txt' end headers['Content-Length'] = response.bytesize.to_s unless request.env[HTTP_X_NO_CONTENT_LENGTH] - headers['Connection'] = 'close' debug('HTTP response: ?', response) send_response([200, headers, [response]], hijack, callback) end end @@ -194,11 +193,11 @@ ws.onmessage = lambda do |event| begin debug("Received message via WebSocket[#{ws.version}]: ?", event.data) - message = MultiJson.load(event.data) + message = parse_json(event.data) cid = Faye.client_id_from_messages(message) @server.close_socket(client_id, false) if client_id and cid and cid != client_id @server.open_socket(cid, ws, request) client_id = cid if cid @@ -241,9 +240,15 @@ 'Access-Control-Allow-Methods' => 'POST, GET', 'Access-Control-Allow-Origin' => '*', 'Access-Control-Max-Age' => '86400' } [200, headers, []] + end + + def parse_json(json) + data = MultiJson.load(json) + return data if Array === data or Hash === data + raise ArgumentError, 'JSON messages must contain an object or array' end def format_request(request) request.body.rewind string = "curl -X #{request.request_method.upcase}"