/
index.js
116 lines (94 loc) · 3.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var nativeHttps = require('https'),
nativeHttp = require('http'),
url = require('url'),
_ = require('underscore');
var maxRedirects = module.exports.maxRedirects = 5;
var protocols = {
https: nativeHttps,
http: nativeHttp
};
// Only use GETs on redirects
for (var protocol in protocols) {
// h is either our cloned http or https object
var h = function() {};
h.prototype = protocols[protocol];
h = new h();
module.exports[protocol] = h;
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);
var out = module.exports[proto].get(redirectUrl, redirectCallback(reqUrl, redirect), redirect);
// bubble errors that occur on the redirect back up to the initiating client request
// object, otherwise they wind up killing the process.
out.on("error", function(err) { clientRequest.emit("error", err) });
return out;
};
}
return clientRequest;
}
}(h);
// see https://github.com/joyent/node/blob/master/lib/http.js#L1623
h.get = function (h) {
return function (options, cb, redirectOptions) {
var req = h.request(options, cb, redirectOptions);
req.end();
return req;
};
}(h);
}