Skip to content

Commit

Permalink
Improve logging of requests with no message data.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jul 15, 2012
1 parent cc4b4d5 commit 66d3f1f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
26 changes: 23 additions & 3 deletions javascript/adapters/node_adapter.js
Expand Up @@ -144,6 +144,7 @@ Faye.NodeAdapter = Faye.Class({
? {message: data}
: querystring.parse(data);

request.body = data;
self._callWithParams(request, response, params);
});

Expand All @@ -157,10 +158,11 @@ Faye.NodeAdapter = Faye.Class({

ws.onmessage = function(event) {
try {
self.debug('Received message via WebSocket[' + ws.version + ']: ?', event.data);

var message = JSON.parse(event.data);
clientId = Faye.clientIdFromMessages(message);

self.debug('Received via WebSocket[' + ws.version + ']: ?', message);
self._server.openSocket(clientId, ws);

self._server.process(message, false, function(replies) {
Expand Down Expand Up @@ -192,15 +194,21 @@ Faye.NodeAdapter = Faye.Class({
},

_callWithParams: function(request, response, params) {
if (!params.message) {
this.error('Received request with no message: ?', this._formatRequest(request));
return this._returnError(response);
}

try {
this.debug('Received message via HTTP ' + request.method + ': ?', params.message);

var message = JSON.parse(params.message),
jsonp = params.jsonp || Faye.JSONP_CALLBACK,
isGet = (request.method === 'GET'),
type = isGet ? this.TYPE_SCRIPT : this.TYPE_JSON,
headers = Faye.extend({}, type),
origin = request.headers.origin;

this.debug('Received ?: ?', request.method, message);
if (isGet) this._server.flushConnection(message);

if (origin) headers['Access-Control-Allow-Origin'] = origin;
Expand All @@ -212,7 +220,7 @@ Faye.NodeAdapter = Faye.Class({
headers['Content-Length'] = new Buffer(body, 'utf8').length.toString();
headers['Connection'] = 'close';

this.debug('Returning ?', body);
this.debug('HTTP response: ?', body);
response.writeHead(200, headers);
response.write(body);
response.end();
Expand All @@ -223,6 +231,18 @@ Faye.NodeAdapter = Faye.Class({
}
},

_formatRequest: function(request) {
var method = request.method.toUpperCase(),
string = 'curl -X ' + method;

string += " 'http://" + request.headers.host + request.url + "'";
if (method === 'POST') {
string += " -H 'Content-Type: " + request.headers['content-type'] + "'";
string += " -d '" + request.body + "'";
}
return string;
},

_handleOptions: function(request, response) {
var headers = {
'Access-Control-Allow-Origin': '*',
Expand Down
25 changes: 21 additions & 4 deletions lib/faye/adapters/rack_adapter.rb
Expand Up @@ -94,14 +94,19 @@ def call(env)
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
Expand All @@ -112,7 +117,7 @@ def handle_request(request)
response = "#{ jsonp }(#{ response });" if request.get?
headers['Content-Length'] = response.bytesize.to_s unless request.env[HTTP_X_NO_CONTENT_LENGTH]
headers['Connection'] = 'close'
debug 'Returning ?', response
debug 'HTTP response: ?', response
callback.call [200, headers, [response]]
end

Expand All @@ -128,10 +133,11 @@ def handle_websocket(env)

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|
Expand Down Expand Up @@ -181,6 +187,17 @@ def message_from_request(request)
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' => '*',
Expand Down

0 comments on commit 66d3f1f

Please sign in to comment.