Skip to content

Commit

Permalink
Fix uncontrolled crash when "this.uri" is an invalid URI
Browse files Browse the repository at this point in the history
  • Loading branch information
naholyr committed Jun 7, 2012
1 parent 70b99ff commit 500e790
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ Request.prototype.init = function (options) {
}
}

if (!self.uri.host || !self.uri.pathname) {
// Invalid URI: it may generate lot of bad errors, like "TypeError: Cannot call method 'indexOf' of undefined" in CookieJar
// Detect and reject it as soon as possible
var faultyUri = url.format(self.uri)
var message = 'Invalid URI "' + faultyUri + '"'
if (Object.keys(options).length === 0) {
// No option ? This can be the sign of a redirect
// As this is a case where the user cannot do anything (he didn't call request directly with this URL)
// he should be warned that it can be caused by a redirection (can save some hair)
message += '. This can be caused by a crappy redirection.'
}
self.emit('error', new Error(message))
return // This error was fatal
}

self._redirectsFollowed = self._redirectsFollowed || 0
self.maxRedirects = (self.maxRedirects !== undefined) ? self.maxRedirects : 10
self.followRedirect = (self.followRedirect !== undefined) ? self.followRedirect : true
Expand Down

0 comments on commit 500e790

Please sign in to comment.