Skip to content

Commit

Permalink
Async-work before running action can break event-based streaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Nov 15, 2011
1 parent 1aefe96 commit 550f514
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 13 additions & 4 deletions lib/base_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ controller.BaseController.prototype = new (function () {
}

, _throwUndefinedFormatError = function () {
err = new errors.InternalServerError('Format not defined in response.formats.');
err = new errors.InternalServerError(
'Format not defined in response.formats.');
this.error(err);
};

Expand All @@ -408,13 +409,21 @@ controller.BaseController.prototype = new (function () {
* Wraps the action so befores and afters can be run
*/
this._handleAction = function (action) {
var _this = this;
var self = this;
// Wrap the actual action-handling in a callback to use as the 'last'
// method in the async chain of before-filters
var callback = function () {
_this[action].apply(_this, [_this.request, _this.response, _this.params]);
self[action].apply(self, [self.request, self.response, self.params]);
};
_execFilters.apply(this, [action, 'before', callback]);
// Running filters asynchronously breaks handlers that depend on
// setting listeners on the request before the next tick -- only
// run them if necessary
if (this.beforeFilters.length) {
_execFilters.apply(this, [action, 'before', callback]);
}
else {
callback();
}
};

// Public methods
Expand Down
6 changes: 4 additions & 2 deletions lib/geddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,19 @@ geddy.mixin(geddy, new (function () {
// the URL and the query-string to produce a Grand Unified Params object
urlParams = url.parse(req.url, true).query
geddy.mixin(params, urlParams);
// If it's a plain form-post, parse the request-body into
// If it's a plain form-post, save the request-body, and parse it into
// params as well
if ((req.method == 'POST' || req.method == 'PUT') &&
(req.headers['content-type'].indexOf('form-urlencoded') > -1)) {
(req.headers['content-type'].indexOf('form-urlencoded') > -1 ||
req.headers['content-type'].indexOf('application/json') > -1)) {
req.addListener('data', function (data) {
body += data.toString();
});
// Handle the request once it's finished
req.addListener('end', function () {
bodyParams = querystring.parse(body);
geddy.mixin(params, bodyParams);
req.body = body;
finish('parseBody');
});
}
Expand Down

0 comments on commit 550f514

Please sign in to comment.