From 0e77b69dc00a049c3fd00816b6ca572bc21faabc Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Tue, 13 Sep 2011 14:49:21 -0700 Subject: [PATCH] Server after chain support, and minor metrics --- lib/messages/search_response.js | 2 ++ lib/server.js | 32 +++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/messages/search_response.js b/lib/messages/search_response.js index 0d24139..55fc5fe 100644 --- a/lib/messages/search_response.js +++ b/lib/messages/search_response.js @@ -24,6 +24,7 @@ function SearchResponse(options) { this.attributes = options.attributes ? options.attributes.slice() : []; this.notAttributes = []; + this.sentEntries = 0; } util.inherits(SearchResponse, LDAPResult); module.exports = SearchResponse; @@ -76,6 +77,7 @@ SearchResponse.prototype.send = function(entry) { this.log.debug('%s: sending: %j', this.connection.ldap.id, entry.json); this.connection.write(entry.toBer()); + this.sentEntries++; } catch (e) { this.log.warn('%s failure to write message %j: %s', this.connection.ldap.id, this.json, e.toString()); diff --git a/lib/server.js b/lib/server.js index ec8682d..7cafdc6 100644 --- a/lib/server.js +++ b/lib/server.js @@ -300,6 +300,7 @@ function Server(options) { c.parser.on('message', function(req) { req.connection = c; req.logId = c.ldap.id + '::' + req.messageID; + req.startTime = new Date().getTime(); if (log.isDebugEnabled()) log.debug('%s: message received: req=%j', c.ldap.id, req.json); @@ -325,18 +326,32 @@ function Server(options) { res.requestDN = req.dn; var chain = self._getHandlerChain(req); + //.concat(this._postChain || []); var i = 0; return function(err) { function sendError(err) { res.status = err.code || errors.LDAP_OPERATIONS_ERROR; - res.matchedDN = req.suffix.toString(); + res.matchedDN = req.suffix ? req.suffix.toString() : ''; res.errorMessage = err.message || ''; return res.end(); } - if (err) - return sendError(err); + function after() { + if (!self._postChain || !self._postChain.length) + return; + + function next() {} // stub out next for the post chain + + self._postChain.forEach(function(c) { + c.call(self, req, res, next); + }); + } + + if (err) { + sendError(err); + return after(); + } try { var next = arguments.callee; @@ -346,6 +361,7 @@ function Server(options) { if (req.protocolOp === Protocol.LDAP_REQ_BIND && res.status === 0) c.ldap.bindDN = req.dn; + return after(); } catch (e) { if (!e.stack) e.stack = e.toString(); @@ -611,6 +627,16 @@ Server.prototype.unbind = function() { }; +Server.prototype.after = function() { + if (!this._postChain) + this._postChain = []; + + var self = this; + mergeFunctionArgs(arguments).forEach(function(h) { + self._postChain.push(h); + }); +}; + // All these just reexpose the requisite net.Server APIs Server.prototype.listen = function(port, host, callback) { if (!port)