lib/faye/adapters/rack_adapter.rb in faye-0.8.2 vs lib/faye/adapters/rack_adapter.rb in faye-0.8.3
- old
+ new
@@ -92,28 +92,34 @@
end
private
def handle_request(request)
- json_msg = message_from_request(request)
+ unless json_msg = message_from_request(request)
+ error 'Received request with no message: ?', format_request(request)
+ return [400, TYPE_TEXT, ['Bad request']]
+ end
+
+ debug "Received message via HTTP #{request.request_method}: ?", json_msg
+
message = Yajl::Parser.parse(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']
- debug 'Received ?: ?', request.env['REQUEST_METHOD'], json_msg
@server.flush_connection(message) if request.get?
headers['Access-Control-Allow-Origin'] = origin if origin
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
+ headers['Connection'] = 'close'
+ debug 'HTTP response: ?', response
callback.call [200, headers, [response]]
end
ASYNC_RESPONSE
rescue => e
@@ -125,14 +131,15 @@
ws = Faye::WebSocket.new(env, nil, :ping => @options[:ping])
client_id = nil
ws.onmessage = lambda do |event|
begin
+ debug "Received message via WebSocket[#{ws.version}]: ?", event.data
+
message = Yajl::Parser.parse(event.data)
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|
ws.send(Faye.to_json(replies)) if ws
end
@@ -176,9 +183,20 @@
if content_type.split(';').first == 'application/json'
request.body.read
else
CGI.parse(request.body.read)['message'][0]
end
+ end
+
+ def format_request(request)
+ request.body.rewind
+ string = "curl -X #{request.request_method.upcase}"
+ string << " '#{request.url}'"
+ if request.post?
+ string << " -H 'Content-Type: #{request.env['CONTENT_TYPE']}'"
+ string << " -d '#{request.body.read}'"
+ end
+ string
end
def handle_options(request)
headers = {
'Access-Control-Allow-Origin' => '*',