Permalink
Browse files

Some cleanup after previous refactor

  • Loading branch information...
mcavage committed Mar 26, 2012
1 parent 2362067 commit 0a689b2d338cb824b5ae09c57c032789243ce56c
Showing with 27 additions and 11 deletions.
  1. +9 −2 lib/plugins/audit.js
  2. +15 −7 lib/response.js
  3. +3 −2 lib/server.js
View
@@ -26,6 +26,9 @@ function auditLogger(options) {
serializers: {
err: Logger.stdSerializers.err,
req: function auditRequestSerializer(req) {
+ if (!req)
+ return false;
+
return {
method: req.method,
url: req.url,
@@ -37,9 +40,12 @@ function auditLogger(options) {
};
},
res: function auditResponseSerializer(res) {
+ if (!res)
+ return false;
+
return {
statusCode: res.statusCode,
- headers: res.headers,
+ headers: res._headers,
trailer: res._trailer || false,
body: options.body === true ? res._body : undefined
};
@@ -66,9 +72,10 @@ function auditLogger(options) {
route: route,
secure: req.secure
};
+
return log.info(obj, '%s handled: %d',
(route ? route.name : 'no route'),
- res.code);
+ res.statusCode);
};
}
View
@@ -77,23 +77,30 @@ function httpDate(now) {
}
+function setContentLength(res, length) {
+ if (res.getHeader('Content-Length') === undefined &&
+ res.contentLength === undefined) {
+ res.setHeader('Content-Length', length);
+ }
+}
+
function formatBinary(req, res, body) {
if (!body) {
- res.setHeader('Content-Length', 0);
+ setContentLength(res, 0);
return null;
}
if (!Buffer.isBuffer(body))
body = new Buffer(body.toString());
- res.setHeader('Content-Length', body.length);
+ setContentLength(res, body.length);
return body;
}
function formatText(req, res, body) {
if (!body) {
- res.setHeader('Content-Length', 0);
+ setContentLength(res, 0);
return null;
}
@@ -105,14 +112,14 @@ function formatText(req, res, body) {
body = body.toString();
}
- res.setHeader('Content-Length', Buffer.byteLength(body));
+ setContentLength(res, Buffer.byteLength(body));
return body;
}
function formatJSON(req, res, body) {
if (!body) {
- res.setHeader('Content-Length', 0);
+ setContentLength(res, 0);
return null;
}
@@ -131,7 +138,8 @@ function formatJSON(req, res, body) {
body = body.toString('base64');
var data = JSON.stringify(body);
- res.setHeader('Content-Length', Buffer.byteLength(data));
+
+ setContentLength(res, Buffer.byteLength(data));
return data;
}
@@ -322,7 +330,7 @@ Response.prototype.defaultResponseHeaders = function defaultHeaders(data) {
this.setHeader('Connection', this.keepAlive ? 'Keep-Alive' : 'close');
if (!this.getHeader('Content-Length')) {
- if (this.contentLength) {
+ if (this.contentLength !== undefined) {
this.setHeader('Content-Length', this.contentLength);
} else if (data) {
if (Buffer.isBuffer(data)) {
View
@@ -121,6 +121,9 @@ function Server(options) {
this.responseTimeHeader = options.responseTimeHeader || 'X-Response-Time';
this.responseTimeFormatter = options.responseTimeFormatter;
+ var secure = false;
+ var self = this;
+
if (options.formatters) {
Object.keys(options.formatters).forEach(function (k) {
if (k.indexOf('/') === -1)
@@ -130,7 +133,6 @@ function Server(options) {
});
}
- var secure = false;
if (options.certificate && options.key) {
secure = true;
this.server = https.createServer({
@@ -141,7 +143,6 @@ function Server(options) {
this.server = http.createServer();
}
- var self = this;
this.server.on('error', function (err) {
self.log.trace({err: err}, 'error');
self.emit('error', err);

0 comments on commit 0a689b2

Please sign in to comment.