lib/faye/adapters/rack_adapter.rb in faye-0.8.0 vs lib/faye/adapters/rack_adapter.rb in faye-0.8.1
- old
+ new
@@ -13,10 +13,15 @@
TYPE_JSON = {'Content-Type' => 'application/json'}
TYPE_SCRIPT = {'Content-Type' => 'text/javascript'}
TYPE_TEXT = {'Content-Type' => 'text/plain'}
+ # 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 = 'HTTP_X_NO_CONTENT_LENGTH'
+
def initialize(app = nil, options = nil)
@app = app if app.respond_to?(:call)
@options = [app, options].grep(Hash).first || {}
@endpoint = @options[:mount] || DEFAULT_ENDPOINT
@@ -86,10 +91,11 @@
@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, ['']]
@@ -115,10 +121,11 @@
headers['Cache-Control'] = 'no-cache, no-store' if request.get?
@server.process(message, false) do |replies|
response = Faye.to_json(replies)
response = "#{ jsonp }(#{ response });" if request.get?
+ headers['Content-Length'] = response.bytesize.to_s unless request.env[HTTP_X_NO_CONTENT_LENGTH]
debug 'Returning ?', response
callback.call [200, headers, [response]]
end
ASYNC_RESPONSE
@@ -132,10 +139,10 @@
client_id = nil
ws.onmessage = lambda do |event|
begin
message = Yajl::Parser.parse(event.data)
- client_id = [message].flatten[0]['clientId']
+ client_id = Faye.client_id_from_messages(message)
debug "Received via WebSocket[#{ws.version}]: ?", message
@server.open_socket(client_id, ws)
@server.process(message, false) do |replies|