Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix bug when using the unbound function fixes #1

  • Loading branch information...
commit 60842d8cf9eecd20842b11b76e059e0b2037aaf4 1 parent d93402f
@olalonde authored
Showing with 99 additions and 95 deletions.
  1. +84 −81 index.js
  2. +1 −1  package.json
  3. +14 −13 test/index.js
View
165 index.js
@@ -19,88 +19,91 @@ for (var protocol in protocols) {
module.exports[protocol] = h;
- h.request = function (options, callback, redirectOptions) {
-
- redirectOptions = redirectOptions || {};
-
- var max = (typeof options === 'object' && 'maxRedirects' in options) ? options.maxRedirects : exports.maxRedirects;
-
- var redirect = _.extend({
- count: 0,
- max: max,
- clientRequest: null,
- userCallback: callback
- }, redirectOptions);
-
- //console.log(redirect.count);
- //console.log(redirect.max);
- /**
- * Emit error if too many redirects
- */
- if (redirect.count > redirect.max) {
- var err = new Error('Max redirects exceeded. To allow more redirects, pass options.maxRedirects property.');
- redirect.clientRequest.emit('error', err);
- return redirect.clientRequest;
+ h.request = function (h) {
+ return function (options, callback, redirectOptions) {
+
+ redirectOptions = redirectOptions || {};
+
+ var max = (typeof options === 'object' && 'maxRedirects' in options) ? options.maxRedirects : exports.maxRedirects;
+
+ var redirect = _.extend({
+ count: 0,
+ max: max,
+ clientRequest: null,
+ userCallback: callback
+ }, redirectOptions);
+
+ //console.log(redirect.count);
+ //console.log(redirect.max);
+ /**
+ * Emit error if too many redirects
+ */
+ if (redirect.count > redirect.max) {
+ var err = new Error('Max redirects exceeded. To allow more redirects, pass options.maxRedirects property.');
+ redirect.clientRequest.emit('error', err);
+ return redirect.clientRequest;
+ }
+
+ redirect.count++;
+
+ /**
+ * Parse URL from options
+ */
+ var reqUrl;
+ if (typeof options === 'string') {
+ reqUrl = options;
+ }
+ else {
+ reqUrl = url.format(_.extend({ protocol: protocol }, options));
+ }
+
+ /*
+ * Build client request
+ */
+ var clientRequest = h.__proto__.request(options, redirectCallback(reqUrl, redirect));
+
+ // Save user's clientRequest so we can emit errors later
+ if (!redirect.clientRequest) redirect.clientRequest = clientRequest;
+
+ /**
+ * ClientRequest callback for redirects
+ */
+ function redirectCallback (reqUrl, redirect) {
+ return function (res) {
+ // status must be 300-399 for redirects
+ if (res.statusCode < 300 || res.statusCode > 399) {
+ //console.log('[' + res.statusCode + '] callback user on url ' + reqUrl);
+ return redirect.userCallback(res);
+ }
+
+ // no `Location:` header => nowhere to redirect
+ if (!('location' in res.headers)) {
+ //console.log('[no location header] callback user on url ' + reqUrl);
+ return redirect.userCallback(res);
+ }
+
+ // save the original clientRequest to our redirectOptions so we can emit errors later
+
+ // need to use url.resolve() in case location is a relative URL
+ var redirectUrl = url.resolve(reqUrl, res.headers['location']);
+ // we need to call the right api (http vs https) depending on protocol
+ var proto = url.parse(redirectUrl).protocol;
+ proto = proto.substr(0, proto.length - 1);
+ //console.log('Redirecting from ' + reqUrl + ' to ' + redirectUrl);
+ return module.exports[proto].get(redirectUrl, redirectCallback(reqUrl, redirect), redirect);
+ };
+ }
+
+ return clientRequest;
}
-
- redirect.count++;
-
- /**
- * Parse URL from options
- */
- var reqUrl;
- if (typeof options === 'string') {
- reqUrl = options;
- }
- else {
- reqUrl = url.format(_.extend({ protocol: protocol }, options));
- }
-
- /*
- * Build client request
- */
- var clientRequest = this.__proto__.request(options, redirectCallback(reqUrl, redirect));
-
- // Save user's clientRequest so we can emit errors later
- if (!redirect.clientRequest) redirect.clientRequest = clientRequest;
-
- /**
- * ClientRequest callback for redirects
- */
- function redirectCallback (reqUrl, redirect) {
- return function (res) {
- // status must be 300-399 for redirects
- if (res.statusCode < 300 || res.statusCode > 399) {
- //console.log('[' + res.statusCode + '] callback user on url ' + reqUrl);
- return redirect.userCallback(res);
- }
-
- // no `Location:` header => nowhere to redirect
- if (!('location' in res.headers)) {
- //console.log('[no location header] callback user on url ' + reqUrl);
- return redirect.userCallback(res);
- }
-
- // save the original clientRequest to our redirectOptions so we can emit errors later
-
- // need to use url.resolve() in case location is a relative URL
- var redirectUrl = url.resolve(reqUrl, res.headers['location']);
- // we need to call the right api (http vs https) depending on protocol
- var proto = url.parse(redirectUrl).protocol;
- proto = proto.substr(0, proto.length - 1);
- //console.log('Redirecting from ' + reqUrl + ' to ' + redirectUrl);
- return module.exports[proto].get(redirectUrl, redirectCallback(reqUrl, redirect), redirect);
- };
- }
-
- return clientRequest;
- }
+ }(h);
// see https://github.com/joyent/node/blob/master/lib/http.js#L1623
- h.get = function (options, cb, redirectOptions) {
- var req = this.request(options, cb, redirectOptions);
- req.end();
- return req;
- };
-
+ h.get = function (h) {
+ return function (options, cb, redirectOptions) {
+ var req = h.request(options, cb, redirectOptions);
+ req.end();
+ return req;
+ };
+ }(h);
}
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "follow-redirects",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "HTTP and HTTPS modules that follow redirects.",
"main": "index.js",
"scripts": {
View
27 test/index.js
@@ -62,20 +62,21 @@ urls.forEach(function (url) {
/**
* Test .request
*/
- //console.log((proto + '.' + 'request(' + url + ')').blue);
- //var req = http.request(url, function(res) {
- ////console.log('STATUS: ' + res.statusCode);
- ////console.log('HEADERS: ' + JSON.stringify(res.headers));
- //res.setEncoding('utf8');
- //res.on('data', function (chunk) {
- //console.log('BODY: ' + chunk);
- //});
- //});
+ console.log((proto + '.' + 'request(' + url + ')').blue);
+ var request = http.request;
+ var req = request(url, function(res) {
+ //console.log('STATUS: ' + res.statusCode);
+ //console.log('HEADERS: ' + JSON.stringify(res.headers));
+ res.setEncoding('utf8');
+ res.on('data', function (chunk) {
+ console.log('BODY: ' + chunk);
+ });
+ });
- //req.on('error', function(e) {
- //console.log('problem with request: ' + e.message);
- //});
+ req.on('error', function(e) {
+ console.log('problem with request: ' + e.message);
+ });
- //req.end();
+ req.end();
};
});
Please sign in to comment.
Something went wrong with that request. Please try again.