forked from restify/node-restify
/
server.js
1247 lines (1060 loc) · 37 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 Mark Cavage, Inc. All rights reserved.
'use strict';
var domain = require('domain');
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var https = require('https');
var util = require('util');
var _ = require('lodash');
var assert = require('assert-plus');
var errors = require('restify-errors');
var mime = require('mime');
var once = require('once');
var semver = require('semver');
var spdy = require('spdy');
var uuid = require('uuid');
var vasync = require('vasync');
var dtrace = require('./dtrace');
var formatters = require('./formatters');
var shallowCopy = require('./utils').shallowCopy;
var upgrade = require('./upgrade');
var deprecationWarnings = require('./deprecationWarnings');
// Ensure these are loaded
require('./request');
require('./response');
///--- Globals
var sprintf = util.format;
var maxSatisfying = semver.maxSatisfying;
var ResourceNotFoundError = errors.ResourceNotFoundError;
var PROXY_EVENTS = [
'clientError',
'close',
'connection',
'error',
'listening',
'secureConnection'
];
///--- Helpers
/**
* helper function that returns true if the request was closed or aborted.
* @private
* @function _reqClosed
* @param {Object} req the request object
* @returns {Boolean}
*/
function _reqClosed(req) {
return (req._connectionState === 'close' ||
req._connectionState === 'aborted');
}
/**
* Verify and flatten a nested array of request handlers.
*
* @private
* @function argumentsToChain
* @throws {TypeError}
* @param {Array} handlers - pass through of funcs from server.[method]
* @returns {Array}
*/
function argumentsToChain(handlers) {
assert.array(handlers, 'handlers');
var chain = [];
// A recursive function for unwinding a nested array of handlers into a
// single chain.
function process(array) {
for (var i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
// Recursively call on nested arrays
process(array[i]);
continue;
}
// If an element of the array isn't an array, ensure it is a
// handler function and then push it onto the chain of handlers
assert.func(array[i], 'handler');
chain.push(array[i]);
}
return chain;
}
// Return the chain, note that if `handlers` is an empty array, this will
// return an empty array.
return process(handlers);
}
/**
* merge optional formatters with the default formatters to create a single
* formatters object. the passed in optional formatters object looks like:
* formatters: {
* 'application/foo': function formatFoo(req, res, body) {...}
* }
* @private
* @function mergeFormatters
* @param {Object} fmt user specified formatters object
* @returns {Object}
*/
function mergeFormatters(fmt) {
var arr = [];
var obj = {};
function addFormatter(src, k) {
assert.func(src[k], 'formatter');
var q = 1.0; // RFC 2616 sec14 - The default value is q=1
var t = k;
if (k.indexOf(';') !== -1) {
var tmp = k.split(/\s*;\s*/);
t = tmp[0];
if (tmp[1].indexOf('q=') !== -1) {
q = parseFloat(tmp[1].split('=')[1]);
}
}
if (k.indexOf('/') === -1) {
k = mime.lookup(k);
}
obj[t] = src[k];
arr.push({
q: q,
t: t
});
}
Object.keys(formatters).forEach(addFormatter.bind(this, formatters));
Object.keys(fmt || {}).forEach(addFormatter.bind(this, fmt || {}));
arr = arr.sort(function (a, b) {
return (b.q - a.q);
}).map(function (a) {
return (a.t);
});
return ({
formatters: obj,
acceptable: arr
});
}
/**
* attaches ifError function attached to the `next` function in handler chain.
* uses a closure to maintain ref to next.
*
* @private
* @deprecated since 5.x
* @function ifError
* @param {Function} n the next function
* @returns {Function}
*/
function ifError(n) {
/**
* @throws will throw if an error is passed in.
* @private
* @function _ifError
* @param {Object} err an error object
* @returns {undefined}
*/
function _ifError(err) {
if (err) {
err._restify_next = n;
throw err;
}
}
return (_ifError);
}
/**
* returns true if the router generated a 404 for an options request.
*
* TODO: this is relevant for CORS only. Should move this out eventually to a
* userland middleware? This also seems a little like overreach, as there is no
* option to opt out of this behavior today.
* @private
* @function optionsError
* @param {Object} err an error object
* @param {Object} req the request object
* @param {Object} res the response object
* @returns {Boolean}
*/
function optionsError(err, req, res) {
return (
err.statusCode === 404 &&
req.method === 'OPTIONS' &&
req.url === '*'
);
}
/**
* map an Error's .name property into the actual event name that is emitted
* by the restify server object.
* @function
* @private errEvtNameFromError
* @param {Object} err an error object
* @returns {String} an event name to emit
*/
function errEvtNameFromError(err) {
if (err.name === 'ResourceNotFoundError') {
// remap the name for router errors
return 'NotFound';
} else if (err.name === 'InvalidVersionError') {
// remap the name for router errors
return 'VersionNotAllowed';
} else {
return err.name.replace(/Error$/, '');
}
}
///--- API
/**
* Creates a new Server.
* @public
* @class
* @param {Object} options an options object
*/
function Server(options) {
assert.object(options, 'options');
assert.object(options.log, 'options.log');
assert.object(options.router, 'options.router');
assert.string(options.name, 'options.name');
var self = this;
EventEmitter.call(this);
this.before = [];
this.chain = [];
this.log = options.log;
this.name = options.name;
this.handleUncaughtExceptions = options.handleUncaughtExceptions || false;
this.router = options.router;
this.routes = {};
this.secure = false;
this.socketio = options.socketio || false;
this._once = (options.strictNext === false) ? once : once.strict;
this.versions = options.versions || options.version || [];
this._inflightRequests = 0;
var fmt = mergeFormatters(options.formatters);
this.acceptable = fmt.acceptable;
this.formatters = fmt.formatters;
if (options.spdy) {
this.spdy = true;
this.server = spdy.createServer(options.spdy);
} else if ((options.cert || options.certificate) && options.key) {
this.ca = options.ca;
this.certificate = options.certificate || options.cert;
this.key = options.key;
this.passphrase = options.passphrase || null;
this.secure = true;
this.server = https.createServer({
ca: self.ca,
cert: self.certificate,
key: self.key,
passphrase: self.passphrase,
rejectUnauthorized: options.rejectUnauthorized,
requestCert: options.requestCert,
ciphers: options.ciphers
});
} else if (options.httpsServerOptions) {
this.server = https.createServer(options.httpsServerOptions);
} else {
this.server = http.createServer();
}
this.router.on('mount', this.emit.bind(this, 'mount'));
if (!options.handleUpgrades && PROXY_EVENTS.indexOf('upgrade') === -1) {
PROXY_EVENTS.push('upgrade');
}
PROXY_EVENTS.forEach(function (e) {
self.server.on(e, self.emit.bind(self, e));
});
// Now the things we can't blindly proxy
this.server.on('checkContinue', function onCheckContinue(req, res) {
if (self.listeners('checkContinue').length > 0) {
self.emit('checkContinue', req, res);
return;
}
if (!options.noWriteContinue) {
res.writeContinue();
}
self._setupRequest(req, res);
self._handle(req, res, true);
});
if (options.handleUpgrades) {
this.server.on('upgrade', function onUpgrade(req, socket, head) {
req._upgradeRequest = true;
var res = upgrade.createResponse(req, socket, head);
self._setupRequest(req, res);
self._handle(req, res);
});
}
this.server.on('request', function onRequest(req, res) {
self.emit('request', req, res);
if (options.socketio && (/^\/socket\.io.*/).test(req.url)) {
return;
}
self._setupRequest(req, res);
self._handle(req, res);
});
this.__defineGetter__('maxHeadersCount', function () {
return (self.server.maxHeadersCount);
});
this.__defineSetter__('maxHeadersCount', function (c) {
self.server.maxHeadersCount = c;
return (c);
});
this.__defineGetter__('url', function () {
if (self.socketPath) {
return ('http://' + self.socketPath);
}
var addr = self.address();
var str = '';
if (self.spdy) {
str += 'spdy://';
} else if (self.secure) {
str += 'https://';
} else {
str += 'http://';
}
if (addr) {
str += addr.family === 'IPv6' ?
'[' + addr.address + ']' : addr.address;
str += ':';
str += addr.port;
} else {
str += '169.254.0.1:0000';
}
return (str);
});
// print deprecation messages based on server configuration
deprecationWarnings(self);
}
util.inherits(Server, EventEmitter);
module.exports = Server;
/**
* Returns the server address. Wraps node's address().
* @public
* @function address
* @returns {String}
*/
Server.prototype.address = function address() {
return (this.server.address());
};
/**
* Gets the server up and listening. Wraps node's listen().
*
* You can call like:
* server.listen(80)
* server.listen(80, '127.0.0.1')
* server.listen('/tmp/server.sock')
*
* @public
* @function listen
* @throws {TypeError}
* @param {Function} callback optionally get notified when listening.
* @returns {undefined}
*/
Server.prototype.listen = function listen() {
var args = Array.prototype.slice.call(arguments);
return (this.server.listen.apply(this.server, args));
};
/**
* Shuts down this server, and invokes callback (optionally) when done.
* Wraps node's close().
* @public
* @function close
* @param {Function} callback optional callback to invoke when done.
* @returns {undefined}
*/
Server.prototype.close = function close(callback) {
if (callback) {
assert.func(callback, 'callback');
}
this.server.once('close', function onClose() {
return (callback ? callback() : false);
});
return (this.server.close());
};
/**
* Returns the number of currently inflight requests.
* @public
* @function inflightRequests
* @returns {number}
*/
Server.prototype.inflightRequests = function inflightRequests() {
var self = this;
return (self._inflightRequests);
};
// Register all the routing methods
/**
* Mounts a chain on the given path against this HTTP verb
*
* @public
* @function del, get, head, opts, post, put, patch
* @param {String | Object} opts if string, the URL to handle.
* if options, the URL to handle, at minimum.
* @returns {Route} the newly created route.
*/
[
'del',
'get',
'head',
'opts',
'post',
'put',
'patch'
].forEach(function (method) {
Server.prototype[method] = function (opts) {
if (opts instanceof RegExp || typeof (opts) === 'string') {
opts = {
path: opts
};
} else if (typeof (opts) === 'object') {
opts = shallowCopy(opts);
} else {
throw new TypeError('path (string) required');
}
if (arguments.length < 2) {
throw new TypeError('handler (function) required');
}
var chain = [];
var route;
var self = this;
function addHandler(h) {
assert.func(h, 'handler');
chain.push(h);
}
if (method === 'del') {
method = 'DELETE';
}
if (method === 'opts') {
method = 'OPTIONS';
}
opts.method = method.toUpperCase();
opts.versions = opts.versions || opts.version || self.versions;
if (!Array.isArray(opts.versions)) {
opts.versions = [opts.versions];
}
if (!opts.name) {
opts.name = method + '-' + (opts.path || opts.url);
if (opts.versions.length > 0) {
opts.name += '-' + opts.versions.join('--');
}
opts.name = opts.name.replace(/\W/g, '').toLowerCase();
if (this.router.mounts[opts.name]) { // GH-401
opts.name += uuid.v4().substr(0, 7);
}
}
if (!(route = this.router.mount(opts))) {
return (false);
}
this.chain.forEach(addHandler);
// We accept both a variable number of handler functions, a
// variable number of nested arrays of handler functions, or a mix
// of both
argumentsToChain(Array.prototype.slice.call(arguments, 1))
.forEach(addHandler);
this.routes[route] = chain;
return (route);
};
});
/**
* Minimal port of the functionality offered by Express.js Route Param
* Pre-conditions
* @link http://expressjs.com/guide.html#route-param%20pre-conditions
*
* This basically piggy-backs on the `server.use` method. It attaches a
* new middleware function that only fires if the specified parameter exists
* in req.params
*
* Exposes an API:
* server.param("user", function (req, res, next) {
* // load the user's information here, always making sure to call next()
* });
*
* @public
* @function param
* @param {String} name The name of the URL param to respond to
* @param {Function} fn The middleware function to execute
* @returns {Object} returns self
*/
Server.prototype.param = function param(name, fn) {
this.use(function _param(req, res, next) {
if (req.params && req.params[name]) {
fn.call(this, req, res, next, req.params[name], name);
} else {
next();
}
});
return (this);
};
/**
* Piggy-backs on the `server.use` method. It attaches a new middleware
* function that only fires if the specified version matches the request.
*
* Note that if the client does not request a specific version, the middleware
* function always fires. If you don't want this set a default version with a
* pre handler on requests where the client omits one.
*
* Exposes an API:
* server.versionedUse("version", function (req, res, next, ver) {
* // do stuff that only applies to routes of this API version
* });
*
* @public
* @function versionedUse
* @param {String|Array} versions the version(s) the URL to respond to
* @param {Function} fn the middleware function to execute, the
* fourth parameter will be the selected
* version
* @returns {undefined}
*/
Server.prototype.versionedUse = function versionedUse(versions, fn) {
if (!Array.isArray(versions)) {
versions = [versions];
}
assert.arrayOfString(versions, 'versions');
versions.forEach(function (v) {
if (!semver.valid(v)) {
throw new TypeError('%s is not a valid semver', v);
}
});
this.use(function _versionedUse(req, res, next) {
var ver;
if (req.version() === '*' ||
(ver = maxSatisfying(versions,
req.version()) || false)) {
fn.call(this, req, res, next, ver);
} else {
next();
}
});
return (this);
};
/**
* Removes a route from the server.
* You pass in the route 'blob' you got from a mount call.
* @public
* @function rm
* @throws {TypeError} on bad input.
* @param {String} route the route name.
* @returns {Boolean} true if route was removed, false if not.
*/
Server.prototype.rm = function rm(route) {
var r = this.router.unmount(route);
if (r && this.routes[r]) {
delete this.routes[r];
}
return (r);
};
/**
* Installs a list of handlers to run _before_ the "normal" handlers of all
* routes.
*
* You can pass in any combination of functions or array of functions.
* @public
* @function use
* @param {...*} handlers - A variable number of handler functions * and/or a
* variable number of nested arrays of handler functions
* @returns {Object} returns self
*/
Server.prototype.use = function use() {
var self = this;
var handlers = Array.prototype.slice.call(arguments);
argumentsToChain(handlers).forEach(function (h) {
self.chain.push(h);
});
return (this);
};
/**
* Gives you hooks to run _before_ any routes are located. This gives you
* a chance to intercept the request and change headers, etc., that routing
* depends on. Note that req.params will _not_ be set yet.
* @public
* @function pre
* @param {...*} handlers - A variable number of handler functions * and/or a
* variable number of nested arrays of handler functions
* @returns {Object} returns self
*/
Server.prototype.pre = function pre() {
var self = this;
var handlers = Array.prototype.slice.call(arguments);
argumentsToChain(handlers).forEach(function (h) {
self.before.push(h);
});
return (this);
};
/**
* toString() the server for easy reading/output.
* @public
* @function toString
* @returns {String}
*/
Server.prototype.toString = function toString() {
var LINE_FMT = '\t%s: %s\n';
var SUB_LINE_FMT = '\t\t%s: %s\n';
var self = this;
var str = '';
function handlersToString(arr) {
var s = '[' + arr.map(function (b) {
return (b.name || 'function');
}).join(', ') + ']';
return (s);
}
str += sprintf(LINE_FMT, 'Accepts', this.acceptable.join(', '));
str += sprintf(LINE_FMT, 'Name', this.name);
str += sprintf(LINE_FMT, 'Pre', handlersToString(this.before));
str += sprintf(LINE_FMT, 'Router', this.router.toString());
str += sprintf(LINE_FMT, 'Routes', '');
Object.keys(this.routes).forEach(function (k) {
var handlers = handlersToString(self.routes[k]);
str += sprintf(SUB_LINE_FMT, k, handlers);
});
str += sprintf(LINE_FMT, 'Secure', this.secure);
str += sprintf(LINE_FMT, 'Url', this.url);
str += sprintf(LINE_FMT, 'Version', Array.isArray(this.versions) ?
this.versions.join() :
this.versions);
return (str);
};
///--- Private methods
/**
* upon receivng a request, route the request, then run the chain of handlers.
* @private
* @function _handle
* @param {Object} req the request object
* @param {Object} res the response object
* @returns {undefined}
*/
Server.prototype._handle = function _handle(req, res) {
var self = this;
// increment number of requests
self._inflightRequests++;
// emit 'pre' event before we run the pre handlers
self.emit('pre', req, res);
function routeAndRun() {
self._route(req, res, function (route, context) {
// emit 'routed' event after the req has been routed
self.emit('routed', req, res, route);
req.context = req.params = context;
req.route = route.spec;
var r = route ? route.name : null;
var chain = self.routes[r];
self._run(req, res, route, chain, function done(e) {
self._finishReqResCycle(req, res, route, e);
});
});
}
// run pre() handlers first before routing and running
if (self.before.length > 0) {
self._run(req, res, null, self.before, function (err) {
// Like with regular handlers, if we are provided an error, we
// should abort the middleware chain and fire after events.
if (err === false || err instanceof Error) {
self._finishReqResCycle(req, res, null, err);
return;
}
routeAndRun();
});
} else {
routeAndRun();
}
};
/**
* look into the router, find the route object that should match this request.
* if a route cannot be found, fire error events then flush the error out.
* @private
* @function _route
* @param {Object} req the request object
* @param {Object} res the response object
* @param {String} [name] name of the route
* @param {Function} cb callback function
* @returns {undefined}
*/
Server.prototype._route = function _route(req, res, name, cb) {
var self = this;
// helper function to, when on router error, emit error events and then
// flush the err.
var errResponse = function errResponse(err) {
return self._emitErrorEvents(req, res, null, err, function () {
res.send(err);
return self._finishReqResCycle(req, res, null, err);
});
};
if (typeof (name) === 'function') {
cb = name;
name = null;
return this.router.find(req, res, function onRoute(err, route, ctx) {
var r = route ? route.name : null;
if (err) {
// TODO: if its a 404 for OPTION method (likely a CORS
// preflight), return OK. This should move into userland.
if (optionsError(err, req, res)) {
res.send(200);
return self._finishReqResCycle(req, res, null, null);
} else {
return errResponse(err);
}
} else if (!r || !self.routes[r]) {
err = new ResourceNotFoundError(req.path());
return errResponse(err);
} else {
// else no err, continue
return cb(route, ctx);
}
});
} else {
return this.router.get(name, req, function (err, route, ctx) {
if (err) {
return errResponse(err);
} else {
// else no err, continue
return cb(route, ctx);
}
});
}
};
/*
* `cb()` is called when execution is complete. "completion" can occur when:
* 1) all functions in handler chain have been executed
* 2) users invoke `next(false)`. this indicates the chain should stop
* executing immediately.
* 3) users invoke `next(err)`. this is sugar for calling res.send(err) and
* firing any error events. after error events are done firing, it will also
* stop execution.
*
* The goofy checks in next() are to make sure we fire the DTrace
* probes after an error might have been sent, as in a handler
* return next(new Error) is basically shorthand for sending an
* error via res.send(), so we do that before firing the dtrace
* probe (namely so the status codes get updated in the
* response).
*
* there are two important closure variables in this function as a result of
* the way `next()` is currently implemented. `next()` assumes logic is sync,
* and automatically calls cb() when a route is considered complete. however,
* for case #3, emitted error events are async and serial. this means the
* automatic invocation of cb() cannot occur:
*
* 1) `emittingErrors` - this boolean is set to true when the server is still
* emitting error events. this var is used to avoid automatic invocation of
* cb(), which is delayed until all async error events are fired.
* 2) `done` - when next is invoked with a value of `false`, or handler if
*
* @private
* @function _run
* @param {Object} req the request object
* @param {Object} res the response object
* @param {Object} route the route object
* @param {Array} chain array of handler functions
* @param {Function} cb callback function
* @emits redirect
* @returns {undefined}
*/
Server.prototype._run = function _run(req, res, route, chain, cb) {
var i = -1;
var id = dtrace.nextId();
req._dtraceId = id;
if (!req._anonFuncCount) {
// Counter used to keep track of anonymous functions. Used when a
// handler function is anonymous. This ensures we're using a
// monotonically increasing int for anonymous handlers through out the
// the lifetime of this request
req._anonFuncCount = 0;
}
var log = this.log;
var self = this;
var handlerName = null;
var emittingErrors = false;
cb = self._once(cb);
// attach a listener for 'close' and 'aborted' events, this will let us set
// a flag so that we can stop processing the request if the client closes
// the connection (or we lose the connection).
function _requestClose() {
req._connectionState = 'close';
}
function _requestAborted() {
req._connectionState = 'aborted';
}
req.once('close', _requestClose);
req.once('aborted', _requestAborted);
// attach a listener for the response's 'redirect' event
res.on('redirect', function (redirectLocation) {
self.emit('redirect', redirectLocation);
});
function next(arg) {
// default value of done determined by whether or not there is another
// function in the chain and/or if req was not already closed. we will
// consume the value of `done` after dealing with any passed in values
// of `arg`.
var done = false;
if (typeof arg !== 'undefined') {
if (arg instanceof Error) {
// the emitting of the error events are async, so we can not
// complete this invocation of run() until it returns. set a
// flag so that the automatic invocation of cb() at the end of
// this function is bypassed.
emittingErrors = true;
// set the done flag - allows us to stop execution of handler
// chain now that an error has occurred.
done = true;
// now emit error events in serial and async
self._emitErrorEvents(req, res, route, arg,
function emitErrorsDone() {
res.send(arg);
return cb(arg);
});
} else if (typeof (arg) === 'string') { // GH-193, allow redirect
if (req._rstfy_chained_route) {
var _e = new errors.InternalError();
log.error({
err: _e
}, 'Multiple next("chain") calls not ' +
'supported');
res.send(_e);
return (false);
}
// Stop running the rest of this route since we're redirecting.
// do this instead of setting done since the route technically
// isn't complete yet.
return self._route(req, res, arg, function (r, ctx) {
req.context = req.params = ctx;
req.route = r.spec;
var _c = chain.slice(0, i + 1);
function _uniq(fn) {
return (_c.indexOf(fn) === -1);
}
var _routes = self.routes[r.name] || [];
var _chain = _routes.filter(_uniq);
req._rstfy_chained_route = true;
// Need to fire DTrace done for previous handler here too.
if ((i + 1) > 0 && chain[i] && !chain[i]._skip) {
req.endHandlerTimer(handlerName);
}
self._run(req, res, r, _chain, cb);
});
} else if (arg === false) {
done = true;
}
}
// Fire DTrace done for the previous handler.
if ((i + 1) > 0 && chain[i] && !chain[i]._skip) {
req.endHandlerTimer(handlerName);
}
// Run the next handler up
if (!done && chain[++i] && !_reqClosed(req)) {
if (chain[i]._skip) {
return (next());
}
if (log.trace()) {
log.trace('running %s', chain[i].name || '?');
}
req._currentRoute = (route !== null ? route.name : 'pre');
handlerName = (chain[i].name ||
('handler-' + req._anonFuncCount++));
req._currentHandler = handlerName;
req.startHandlerTimer(handlerName);
var n = self._once(next);
// support ifError only if domains are on
if (self.handleUncaughtExceptions === true) {
n.ifError = ifError(n);
}
return (chain[i].call(self, req, res, n));
}
// if we have reached this last section of next(), then we are 'done'
// with this route.
dtrace._rstfy_probes['route-done'].fire(function () {
return ([
self.name,
route !== null ? route.name : 'pre',