mikeal/request — pull request #265 by naholyr (2012-06-07 13:15:08+00:00)
Title: uncaughtException when redirected to invalid URI

We recently faced a case of uncaught exception: when a website redirects (using `Location` header) we end up with an ugly `Uncaught Exception TypeError: Cannot call method 'indexOf' of undefined`.

Here is the stack trace:
```
TypeError: Cannot call method 'indexOf' of undefined
at /…/node_modules/request/vendor/cookie/jar.js:51:19
at Array.filter (native)
at CookieJar.get (/…/node_modules/request/vendor/cookie/jar.js:50:23)
at Request.request (/…/node_modules/request/main.js:156:29)
at request (/…/node_modules/request/main.js:570:5)
at ClientRequest. (/…/node_modules/request/main.js:385:9)
at ClientRequest.g (events.js:156:14)
at ClientRequest.emit (events.js:67:17)
at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1256:7)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:91:29)
```

And the real-life case:
```
$ curl -i 'http://indicecorporativo.com/component/flexbanner/?task=click&bannerid=1' HTTP/1.1 303 See Other
Date: Thu, 07 Jun 2012 13:06:40 GMT
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Server: Nginx / Varnish
X-Powered-By: PHP/5.2.17
Set-Cookie: b8ff2450d98af17e3cc82ed18c538162=687e411a3bbe6a967d8f88a99e22ea09; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Location: http://
Vary: Accept-Encoding
```

Boom.

This PR will check the URI before going further, and if it's invalid it will emit an "error" event, which will make it catchable.