lib/faye/adapters/rack_adapter.rb in faye-0.5.2 vs lib/faye/adapters/rack_adapter.rb in faye-0.5.3
- old
+ new
@@ -1,6 +1,7 @@
require 'rubygems'
+require 'json'
require 'rack'
require 'thin'
require Faye::ROOT + '/thin_extensions'
module Faye
@@ -59,11 +60,11 @@
if request.path_info =~ /\.js$/
return [200, TYPE_SCRIPT, File.new(SCRIPT_PATH)]
end
begin
- json_msg = request.post? ? request.body.read : request.params['message']
+ json_msg = message_from_request(request)
message = JSON.parse(json_msg)
jsonp = request.params['jsonp'] || JSONP_CALLBACK
type = request.get? ? TYPE_SCRIPT : TYPE_JSON
callback = env['async.callback']
body = DeferredBody.new
@@ -84,9 +85,20 @@
[400, TYPE_TEXT, 'Bad request']
end
end
private
+
+ def message_from_request(request)
+ if request.post?
+ content_type = request.env['CONTENT_TYPE'].split(';').first
+ content_type == 'application/json' ?
+ request.body.read :
+ request.params['message']
+ else
+ request.params['message']
+ end
+ end
def handle_upgrade(request)
socket = Faye::WebSocket.new(request)
socket.onmessage = lambda do |message|