Skip to content

Commit 668f545

Browse files
committed
Skip routing when req.url is not set
1 parent 7bc5f1a commit 668f545

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ unreleased
44
* Add debug message when loading view engine
55
* Remove usage of `res._headers` private field
66
- Improves compatibility with Node.js 8 nightly
7+
* Skip routing when `req.url` is not set
78
* Use `statuses` instead of `http` module for status messages
89
910
- Allow colors in workers

lib/router/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,8 @@ proto.handle = function handle(req, res, out) {
137137

138138
debug('dispatching %s %s', req.method, req.url);
139139

140-
var search = 1 + req.url.indexOf('?');
141-
var pathlength = search ? search - 1 : req.url.length;
142-
var fqdn = req.url[0] !== '/' && 1 + req.url.substr(0, pathlength).indexOf('://');
143-
var protohost = fqdn ? req.url.substr(0, req.url.indexOf('/', 2 + fqdn)) : '';
144140
var idx = 0;
141+
var protohost = getProtohost(req.url) || ''
145142
var removed = '';
146143
var slashAdded = false;
147144
var paramcalled = {};
@@ -293,7 +290,7 @@ proto.handle = function handle(req, res, out) {
293290
req.url = protohost + req.url.substr(protohost.length + removed.length);
294291

295292
// Ensure leading slash
296-
if (!fqdn && req.url[0] !== '/') {
293+
if (!protohost && req.url[0] !== '/') {
297294
req.url = '/' + req.url;
298295
slashAdded = true;
299296
}
@@ -526,6 +523,23 @@ function getPathname(req) {
526523
}
527524
}
528525

526+
// Get get protocol + host for a URL
527+
function getProtohost(url) {
528+
if (typeof url !== 'string' || url.length === 0 || url[0] === '/') {
529+
return undefined
530+
}
531+
532+
var searchIndex = url.indexOf('?')
533+
var pathLength = searchIndex !== -1
534+
? searchIndex
535+
: url.length
536+
var fqdnIndex = url.substr(0, pathLength).indexOf('://')
537+
538+
return fqdnIndex !== -1
539+
? url.substr(0, url.indexOf('/', 3 + fqdnIndex))
540+
: undefined
541+
}
542+
529543
// get type for error message
530544
function gettype(obj) {
531545
var type = typeof obj;

test/Router.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ describe('Router', function(){
5353
router.handle({ url: '', method: 'GET' }, {}, done);
5454
});
5555

56+
it('should handle missing URL', function (done) {
57+
var router = new Router()
58+
59+
router.use(function (req, res) {
60+
throw new Error('should not be called')
61+
})
62+
63+
router.handle({ method: 'GET' }, {}, done)
64+
})
65+
5666
it('should not stack overflow with many registered routes', function(done){
5767
var handler = function(req, res){ res.end(new Error('wrong handler')) };
5868
var router = new Router();

0 commit comments

Comments
 (0)