forked from hapijs/hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eran Hammer
committed
Feb 27, 2013
1 parent
7d51148
commit 22ad77f
Showing
4 changed files
with
136 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Load modules | ||
|
||
var Route = require('./route'); | ||
var Utils = require('./utils'); | ||
|
||
|
||
// Declare internals | ||
|
||
var internals = {}; | ||
|
||
|
||
module.exports = internals.Router = function (server) { | ||
|
||
this.server = server; | ||
this.table = {}; // Array per HTTP method, including * for catch-all | ||
|
||
this.notfound = new Route({ | ||
method: 'notfound', | ||
path: '/{p*}', | ||
config: { | ||
auth: { mode: 'none' }, // In case defaults are set otherwise | ||
handler: 'notFound' | ||
} | ||
}, server); | ||
|
||
if (server.settings.cors) { | ||
this.cors = new Route({ | ||
path: '/{p*}', | ||
method: 'options', | ||
config: { | ||
auth: { mode: 'none' }, // In case defaults are set otherwise | ||
handler: function (request) { | ||
|
||
request.reply({}); | ||
} | ||
} | ||
}, server); | ||
} | ||
}; | ||
|
||
|
||
internals.Router.prototype.route = function (request) { | ||
|
||
// Lookup route | ||
|
||
var method = (request.method === 'head' ? 'get' : request.method); | ||
|
||
var routes = this.table[method] || []; | ||
for (var i = 0, il = routes.length; i<il;++i) { | ||
var route = routes[i]; | ||
if (route.match(request)) { | ||
return route; | ||
} | ||
} | ||
|
||
// CORS | ||
|
||
if (method === 'options' && | ||
this.cors) { | ||
|
||
return this.cors; | ||
} | ||
|
||
// * | ||
|
||
routes = this.table['*'] || []; | ||
for (i = 0, il = routes.length; i < il; ++i) { | ||
var route = routes[i]; | ||
if (route.match(request)) { | ||
return route; | ||
} | ||
} | ||
|
||
// Not found | ||
|
||
return this.notfound; | ||
}; | ||
|
||
|
||
internals.Router.prototype.add = function (configs) { | ||
|
||
var self = this; | ||
|
||
Utils.assert(configs, 'Routes configs must exist'); | ||
|
||
configs = (configs instanceof Array ? configs : [configs]); | ||
|
||
var methods = {}; | ||
configs.forEach(function (config) { | ||
|
||
var route = new Route(config, self.server); // Do no use config beyond this point, use route members | ||
|
||
self.table[route.method] = self.table[route.method] || []; | ||
|
||
// Check for existing route with same fingerprint | ||
|
||
methods[route.method] = true; | ||
self.table[route.method].forEach(function (existing) { | ||
|
||
Utils.assert(route.fingerprint !== existing.fingerprint, 'New route: ' + route.path + ' conflicts with existing: ' + existing.path); | ||
}); | ||
|
||
self.table[route.method].push(route); | ||
}); | ||
|
||
Object.keys(methods).forEach(function (method) { | ||
|
||
self.table[method].sort(Route.sort); | ||
}); | ||
}; | ||
|
||
|
||
internals.Router.prototype.routingTable = function () { | ||
|
||
var self = this; | ||
|
||
var table = []; | ||
Object.keys(this.table).forEach(function (method) { | ||
|
||
self.table[method].forEach(function (route) { | ||
|
||
table.push(route); | ||
}); | ||
}); | ||
|
||
return table; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters