diff --git a/lib/app/request.js b/lib/app/request.js index 68932ca12..487d58278 100644 --- a/lib/app/request.js +++ b/lib/app/request.js @@ -100,6 +100,8 @@ module.exports = function request( /* address, body, cb */ ) { var clientRes = new MockClientResponse(); clientRes.on('finish', function() { + // console.log('clientRes finished. Headers:',clientRes.headers); + // Only dump the buffer if a callback was supplied if (cb) { clientRes.body = Buffer.concat(clientRes._readableState.buffer).toString(); diff --git a/lib/router/index.js b/lib/router/index.js index 76b059eeb..a4b91cdc5 100644 --- a/lib/router/index.js +++ b/lib/router/index.js @@ -175,7 +175,7 @@ Router.prototype.route = function(req, res) { return res.send(400, err && err.stack); } - console.log('Ran cookie parser'); + // console.log('Ran cookie parser'); // console.log('res.writeHead= ',res.writeHead); // Load session (if relevant) @@ -184,9 +184,9 @@ Router.prototype.route = function(req, res) { return res.send(400, err && err.stack); } // console.log('res is now:\n',res); - console.log('\n\n'); - console.log('Ran session middleware. Now req.session= ',req.session); - console.log('req.sessionID= ',req.sessionID); + // console.log('\n\n'); + // console.log('Ran session middleware. Now req.session= ',req.session); + // console.log('req.sessionID= ',req.sessionID); sails.log.silly('Handling virtual request :: Running virtual body parser...'); bodyParser(req,res, function (err) { @@ -421,7 +421,7 @@ function parseCookies (req, res, next){ // TODO: this line of code does not need to be run for every request req._sails.log.verbose('Parsing cookie:',req.headers.cookie); - console.log('Parsing cookie:',req.headers.cookie); + // console.log('Parsing cookie:',req.headers.cookie); var _cookieParser = Express.cookieParser(req._sails && req._sails.config.session && req._sails.config.session.secret); // Run the middleware diff --git a/lib/router/res.js b/lib/router/res.js index 3b1b150ae..b61fd61b3 100644 --- a/lib/router/res.js +++ b/lib/router/res.js @@ -35,7 +35,7 @@ module.exports = function buildResponse (req, _res) { // Ensure res.headers and res.locals exist. // res = _.merge(res, {locals: {}, headers: {}}, _res); - res = _.extend(res, {locals: {}, headers: {}}); + res = _.extend(res, {locals: {}, headers: {}, _headers: {}}); res = _.extend(res, _res); // Now that we're sure `res` is a Transform stream, we'll handle the two different @@ -101,7 +101,7 @@ module.exports = function buildResponse (req, _res) { // res.writeHead() is wrapped in closure by the `on-header` module, // but it still needs the underlying impl res.writeHead = function ( /* statusCode, [reasonPhrase], headers */) { - console.log('res.writeHead(%s)', Array.prototype.slice.call(arguments)); + // console.log('\n\n• res.writeHead(%s)', Array.prototype.slice.call(arguments)); var statusCode = +arguments[0]; var reasonPhrase = (function(){ if (arguments[2] && _.isString(arguments[1])) { @@ -122,6 +122,8 @@ module.exports = function buildResponse (req, _res) { // Set status code res.statusCode = statusCode; + // Ensure `._headers` have been merged into `.headers` + _.extend(res.headers, res._headers); if (newHeaders) { if (!_.isObject(newHeaders)) { @@ -131,6 +133,16 @@ module.exports = function buildResponse (req, _res) { _.extend(res.headers, newHeaders); } + // Set status code and headers on the `_clientRes` stream so they are accessible + // to the provider of that stream. + // (this has to happen in `send()` because the code/headers might have just changed) + if (res._clientRes) { + // console.log('Setting headers on clientRes- res.headers = ',res.headers); + res._clientRes.headers = res.headers; + res._clientRes.statusCode = res.statusCode; + } + + // Flag headers as written. headersWritten = true; @@ -139,20 +151,21 @@ module.exports = function buildResponse (req, _res) { var prevSetHeader = res.setHeader; res.setHeader = function (){ - console.log('called res.setHeader() w/ args=',arguments); + // console.log('called res.setHeader() w/ args=',arguments); prevSetHeader.apply(res, Array.prototype.slice.call(arguments)); }; // Wrap res.write() and res.end() to get them to call writeHead() var prevWrite = res.write; res.write = function (){ - res.writeHead(res.statusCode, res._headers); - console.log('res.write():: res.headers=',res.headers); + res.writeHead(res.statusCode, _.extend(res._headers,res.headers)); + // console.log('res.write():: called writeHead with headers=',_.extend(res._headers,res.headers)); prevWrite.apply(res, Array.prototype.slice.call(arguments)); }; var prevEnd = res.end; res.end = function (){ - res.writeHead(res.statusCode, res._headers); + res.writeHead(res.statusCode, _.extend(res._headers,res.headers)); + // console.log('res.end():: called writeHead with headers=',_.extend(res._headers,res.headers)); prevEnd.apply(res, Array.prototype.slice.call(arguments)); }; @@ -186,6 +199,7 @@ module.exports = function buildResponse (req, _res) { req._sails.log.error(e); return; } + // TODO: use debug() console.error(e); return; } @@ -195,7 +209,7 @@ module.exports = function buildResponse (req, _res) { res.charset = res.charset || 'utf-8'; // Ensure headers are set - res.headers = _.extend(res.headers || {}, res._headers || {}); + _.extend(res.headers, res._headers); // Ensure statusCode is set // (override `this.statusCode` if `statusCode` argument specified) @@ -205,20 +219,10 @@ module.exports = function buildResponse (req, _res) { if (res._clientCallback) { // Manually plug in res.body. - res.body = args.other; - res._clientRes.body = res.body; // (but don't include body if it is empty) - if (!res.body) delete res.body; - if (!res._clientRes.body) delete res._clientRes.body; - - // Set status code and headers on the `_clientRes` stream so they are accessible - // to the provider of that stream. - // (this has to happen in `send()` because the code/headers might have just changed) - if (res._clientRes) { - - // TODO: try `res.emit('header')` to trigger .on('header') listeners - res._clientRes.headers = res.headers; - res._clientRes.statusCode = res.statusCode; + if (res.body) { + res.body = args.other; + res._clientRes.body = res.body; } // End the `res` stream (which will in turn end the `res._clientRes` stream) @@ -226,8 +230,14 @@ module.exports = function buildResponse (req, _res) { return; } + // // Otherwise, the hook using the interpreter must have provided us with a `res._clientRes` stream, // so we'll need to serialize everything to work w/ that stream. + // + + // console.log('\n---\nwriting to clientRes stream...'); + // console.log('res.headers =>',res.headers); + // console.log('res._headers =>',res._headers); // Write body to `res` stream if (args.other) { @@ -238,7 +248,7 @@ module.exports = function buildResponse (req, _res) { try { toWrite = JSON.stringify(args.other); - // original method: + // original way: // toWrite = util.inspect(toWrite); } catch(e) { @@ -246,11 +256,12 @@ module.exports = function buildResponse (req, _res) { 'Failed to stringify specified JSON response body :: ' + util.inspect(args.other) + '\nError:\n' + util.inspect(e) ); - console.log('failed to stringify!'); + // console.log('failed to stringify!'); if (req._sails && req._sails.log && req._sails.log.error) { req._sails.log.error(failedToStringify); } else { + // todo: use debug() console.error(failedToStringify); } toWrite = failedStringify.message; @@ -260,14 +271,6 @@ module.exports = function buildResponse (req, _res) { res.write(toWrite); } - // Set status code and headers on the `_clientRes` stream so they are accessible - // to the provider of that stream. - // (this has to happen in `send()` because the code/headers might have just changed) - if (res._clientRes) { - res._clientRes.headers = res.headers; - res._clientRes.statusCode = res.statusCode; - } - // End the `res` stream. res.end(); };