follow-redirects 0.0.3 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of follow-redirects might be problematic. Click here for more details.
- package/README.md +87 -32
- package/create.js +162 -0
- package/http.js +1 -0
- package/https.js +1 -0
- package/index.js +4 -109
- package/package.json +34 -9
- package/.npmignore +0 -15
- package/test/index.js +0 -82
package/README.md
CHANGED
@@ -1,35 +1,23 @@
|
|
1
|
-
|
2
|
-
HTTP redirects painlessly. It does not modify the native modules but
|
3
|
-
instead offers its own http/https modules which inherit from the native
|
4
|
-
modules. If you want to automatically follow redirects, all you need to
|
5
|
-
do is replace:
|
1
|
+
## Follow Redirects
|
6
2
|
|
7
|
-
|
8
|
-
var http = require('http');
|
9
|
-
```
|
10
|
-
|
11
|
-
by
|
12
|
-
|
13
|
-
```javascript
|
14
|
-
var http = require('follow-redirects').http;
|
15
|
-
```
|
3
|
+
Drop in replacement for Nodes `http` and `https` that automatically follows redirects.
|
16
4
|
|
17
|
-
|
5
|
+
[](https://travis-ci.org/olalonde/follow-redirects)
|
6
|
+
[](https://coveralls.io/r/olalonde/follow-redirects?branch=master)
|
7
|
+
[](https://codeclimate.com/github/olalonde/follow-redirects)
|
8
|
+
[](https://david-dm.org/olalonde/follow-redirects)
|
9
|
+
[](https://david-dm.org/olalonde/follow-redirects#info=devDependencies)
|
18
10
|
|
19
|
-
|
11
|
+
[](https://nodei.co/npm/follow-redirects/)
|
20
12
|
|
21
|
-
#
|
13
|
+
`follow-redirects` provides [request](https://nodejs.org/api/http.html#http_http_request_options_callback) and [get](https://nodejs.org/api/http.html#http_http_get_options_callback)
|
14
|
+
methods that behave identically to those found on the native [http](https://nodejs.org/api/http.html#http_http_request_options_callback) and [https](https://nodejs.org/api/https.html#https_https_request_options_callback)
|
15
|
+
modules, with the exception that they will seamlessly follow redirects.
|
22
16
|
|
23
17
|
```javascript
|
24
|
-
|
25
18
|
var http = require('follow-redirects').http;
|
26
19
|
var https = require('follow-redirects').https;
|
27
20
|
|
28
|
-
/*
|
29
|
-
* http and https are just like Node.js' http and https modules except
|
30
|
-
* that they follow redirects seamlessly.
|
31
|
-
*/
|
32
|
-
|
33
21
|
http.get('http://bit.ly/900913', function (res) {
|
34
22
|
res.on('data', function (chunk) {
|
35
23
|
console.log(chunk);
|
@@ -37,21 +25,88 @@ http.get('http://bit.ly/900913', function (res) {
|
|
37
25
|
}).on('error', function (err) {
|
38
26
|
console.error(err);
|
39
27
|
});
|
28
|
+
```
|
29
|
+
|
30
|
+
By default the number of redirects is limited to 5, but you can modify that globally or per request.
|
40
31
|
|
41
|
-
|
42
|
-
|
43
|
-
*/
|
32
|
+
```javascript
|
33
|
+
require('follow-redirects').maxRedirects = 10; // Has global affect (be careful!)
|
44
34
|
|
45
35
|
https.request({
|
46
36
|
host: 'bitly.com',
|
47
37
|
path: '/UHfDGO',
|
48
|
-
maxRedirects: 3
|
38
|
+
maxRedirects: 3 // per request setting
|
39
|
+
}, function (res) {/* ... */});
|
40
|
+
```
|
41
|
+
|
42
|
+
You can inspect the redirection chain from the `fetchedUrls` array on the `response`.
|
43
|
+
The array is populated in reverse order, so the original url you requested will be the
|
44
|
+
last element, while the final redirection point will be at index 0.
|
45
|
+
|
46
|
+
```javascript
|
47
|
+
https.request({
|
48
|
+
host: 'bitly.com',
|
49
|
+
path: '/UHfDGO',
|
49
50
|
}, function (res) {
|
50
|
-
res.
|
51
|
-
|
52
|
-
});
|
53
|
-
}).on('error', function (err) {
|
54
|
-
console.error(err);
|
51
|
+
console.log(res.fetchedUrls);
|
52
|
+
// [ 'http://duckduckgo.com/robots.txt', 'http://bitly.com/UHfDGO' ]
|
55
53
|
});
|
54
|
+
```
|
55
|
+
|
56
|
+
## Browserify Usage
|
57
|
+
|
58
|
+
Due to the way `XMLHttpRequest` works, the `browserify` versions of `http` and `https` already follow redirects.
|
59
|
+
If you are *only* targetting the browser, then this library has little value for you. If you want to write cross
|
60
|
+
platform code for node and the browser, `follow-redirects` provides a great solution for making the native node
|
61
|
+
modules behave the same as they do in browserified builds in the browser. To avoid bundling unnecessary code
|
62
|
+
you should tell browserify to swap out `follow-redirects` with the standard modules when bundling.
|
63
|
+
To make this easier, you need to change how you require the modules:
|
64
|
+
|
65
|
+
```javascript
|
66
|
+
var http = require('follow-redirects/http');
|
67
|
+
var https = require('follow-redirects/https');
|
68
|
+
```
|
69
|
+
|
70
|
+
You can then replace `follow-redirects` in your browserify configuration like so:
|
71
|
+
|
72
|
+
```javascript
|
73
|
+
"browser": {
|
74
|
+
"follow-redirects/http" : "http",
|
75
|
+
"follow-redirects/https" : "https"
|
76
|
+
}
|
77
|
+
```
|
56
78
|
|
79
|
+
The `browserify-http` module has not kept pace with node development, and no long behaves identically to the native
|
80
|
+
module when running in the browser. If you are experiencing problems, you may want to check out
|
81
|
+
[browserify-http-2](https://www.npmjs.com/package/http-browserify-2). It is more actively maintained and
|
82
|
+
attempts to address a few of the shortcomings of `browserify-http`. In that case, your browserify config should
|
83
|
+
look something like this:
|
84
|
+
|
85
|
+
```javascript
|
86
|
+
"browser": {
|
87
|
+
"follow-redirects/http" : "browserify-http-2/http",
|
88
|
+
"follow-redirects/https" : "browserify-http-2/https"
|
89
|
+
}
|
57
90
|
```
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Pull Requests are always welcome. Please [file an issue](https://github.com/olalonde/follow-redirects/issues)
|
95
|
+
detailing your proposal before you invest your valuable time. Additional features and bug fixes should be accompanied
|
96
|
+
by tests. You can run the test suite locally with a simple `npm test` command.
|
97
|
+
|
98
|
+
## Debug Logging
|
99
|
+
|
100
|
+
`follow-redirects` uses the excellent [debug](https://www.npmjs.com/package/debug) for logging. To turn on logging
|
101
|
+
set the environment variable `DEBUG=follow-redirects` for debug output from just this module. When running the test
|
102
|
+
suite it is sometimes advantageous to set `DEBUG=*` to see output from the express server as well.
|
103
|
+
|
104
|
+
## Authors
|
105
|
+
|
106
|
+
Olivier Lalonde (olalonde@gmail.com)
|
107
|
+
|
108
|
+
James Talmage (james@talmage.io)
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
MIT: [http://olalonde.mit-license.org](http://olalonde.mit-license.org)
|
package/create.js
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
'use strict';
|
2
|
+
var url = require('url');
|
3
|
+
var debug = require('debug')('follow-redirects');
|
4
|
+
var assert = require('assert');
|
5
|
+
var consume = require('stream-consume');
|
6
|
+
|
7
|
+
module.exports = function(_nativeProtocols) {
|
8
|
+
var nativeProtocols = {};
|
9
|
+
|
10
|
+
var publicApi = {
|
11
|
+
maxRedirects: 5
|
12
|
+
};
|
13
|
+
|
14
|
+
for (var p in _nativeProtocols) {
|
15
|
+
/* istanbul ignore else */
|
16
|
+
if (_nativeProtocols.hasOwnProperty(p)) {
|
17
|
+
// http://www.ietf.org/rfc/rfc2396.txt - Section 3.1
|
18
|
+
assert(/^[A-Z][A-Z\+\-\.]*$/i.test(p), JSON.stringify(p) + ' is not a valid scheme name');
|
19
|
+
generateWrapper(p, _nativeProtocols[p]);
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
return publicApi;
|
24
|
+
|
25
|
+
function execute(options) {
|
26
|
+
var clientRequest;
|
27
|
+
var fetchedUrls = [];
|
28
|
+
|
29
|
+
return (clientRequest = cb());
|
30
|
+
|
31
|
+
function cb(res) {
|
32
|
+
// skip the redirection logic on the first call.
|
33
|
+
if (res) {
|
34
|
+
var fetchedUrl = url.format(options);
|
35
|
+
fetchedUrls.unshift(fetchedUrl);
|
36
|
+
|
37
|
+
if (!isRedirect(res)) {
|
38
|
+
res.fetchedUrls = fetchedUrls;
|
39
|
+
return options.userCallback(res);
|
40
|
+
}
|
41
|
+
|
42
|
+
// we are going to follow the redirect, but in node 0.10 we must first attach a data listener
|
43
|
+
// to consume the stream and send the 'end' event
|
44
|
+
consume(res);
|
45
|
+
|
46
|
+
// need to use url.resolve() in case location is a relative URL
|
47
|
+
var redirectUrl = url.resolve(fetchedUrl, res.headers.location);
|
48
|
+
debug('redirecting to', redirectUrl);
|
49
|
+
|
50
|
+
// clean all the properties related to the old url away, and copy from the redirect url
|
51
|
+
wipeUrlProps(options);
|
52
|
+
extend(options, url.parse(redirectUrl));
|
53
|
+
}
|
54
|
+
|
55
|
+
if (fetchedUrls.length > options.maxRedirects) {
|
56
|
+
var err = new Error('Max redirects exceeded.');
|
57
|
+
return forwardError(err);
|
58
|
+
}
|
59
|
+
|
60
|
+
options.nativeProtocol = nativeProtocols[options.protocol];
|
61
|
+
options.defaultRequest = defaultMakeRequest;
|
62
|
+
|
63
|
+
var req = (options.makeRequest || defaultMakeRequest)(options, cb, res);
|
64
|
+
|
65
|
+
if (res) {
|
66
|
+
req.on('error', forwardError);
|
67
|
+
}
|
68
|
+
return req;
|
69
|
+
}
|
70
|
+
|
71
|
+
function defaultMakeRequest(options, cb, res) {
|
72
|
+
if (res) {
|
73
|
+
// This is a redirect, so use only GET methods
|
74
|
+
options.method = 'GET';
|
75
|
+
}
|
76
|
+
|
77
|
+
var req = options.nativeProtocol.request(options, cb);
|
78
|
+
|
79
|
+
if (res) {
|
80
|
+
// We leave the user to call `end` on the first request
|
81
|
+
req.end();
|
82
|
+
}
|
83
|
+
|
84
|
+
return req;
|
85
|
+
}
|
86
|
+
|
87
|
+
// bubble errors that occur on the redirect back up to the initiating client request
|
88
|
+
// object, otherwise they wind up killing the process.
|
89
|
+
function forwardError (err) {
|
90
|
+
clientRequest.emit('error', err);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
function generateWrapper (scheme, nativeProtocol) {
|
95
|
+
var wrappedProtocol = scheme + ':';
|
96
|
+
var H = function() {};
|
97
|
+
H.prototype = nativeProtocols[wrappedProtocol] = nativeProtocol;
|
98
|
+
H = new H();
|
99
|
+
publicApi[scheme] = H;
|
100
|
+
|
101
|
+
H.request = function(options, callback) {
|
102
|
+
return execute(parseOptions(options, callback, wrappedProtocol));
|
103
|
+
};
|
104
|
+
|
105
|
+
// see https://github.com/joyent/node/blob/master/lib/http.js#L1623
|
106
|
+
H.get = function(options, callback) {
|
107
|
+
options = parseOptions(options, callback, wrappedProtocol);
|
108
|
+
var req = execute(options);
|
109
|
+
req.end();
|
110
|
+
return req;
|
111
|
+
};
|
112
|
+
}
|
113
|
+
|
114
|
+
// returns a safe copy of options (or a parsed url object if options was a string).
|
115
|
+
// validates that the supplied callback is a function
|
116
|
+
function parseOptions (options, callback, wrappedProtocol) {
|
117
|
+
assert.equal(typeof callback, 'function', 'callback must be a function');
|
118
|
+
if ('string' === typeof options) {
|
119
|
+
options = url.parse(options);
|
120
|
+
options.maxRedirects = publicApi.maxRedirects;
|
121
|
+
} else {
|
122
|
+
options = extend({
|
123
|
+
maxRedirects: publicApi.maxRedirects,
|
124
|
+
protocol: wrappedProtocol
|
125
|
+
}, options);
|
126
|
+
}
|
127
|
+
assert.equal(options.protocol, wrappedProtocol, 'protocol mismatch');
|
128
|
+
options.protocol = wrappedProtocol;
|
129
|
+
options.userCallback = callback;
|
130
|
+
|
131
|
+
debug('options', options);
|
132
|
+
return options;
|
133
|
+
}
|
134
|
+
};
|
135
|
+
|
136
|
+
// copies source's own properties onto destination and returns destination
|
137
|
+
function extend(destination, source) {
|
138
|
+
for (var i in source) {
|
139
|
+
if (source.hasOwnProperty(i)) {
|
140
|
+
destination[i] = source[i];
|
141
|
+
}
|
142
|
+
}
|
143
|
+
return destination;
|
144
|
+
}
|
145
|
+
|
146
|
+
// to redirect the result must have
|
147
|
+
// a statusCode between 300-399
|
148
|
+
// and a `Location` header
|
149
|
+
function isRedirect (res) {
|
150
|
+
return (res.statusCode >= 300 && res.statusCode <= 399 &&
|
151
|
+
'location' in res.headers);
|
152
|
+
}
|
153
|
+
|
154
|
+
// nulls all url related properties on the object.
|
155
|
+
// required on node <10
|
156
|
+
function wipeUrlProps(options) {
|
157
|
+
for (var i = 0, l = urlProps.length; i < l; ++i) {
|
158
|
+
options[urlProps[i]] = null;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
var urlProps = ['protocol', 'slashes', 'auth', 'host', 'port', 'hostname',
|
162
|
+
'hash', 'search', 'query', 'pathname', 'path', 'href'];
|
package/http.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require('./').http;
|
package/https.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require('./').https;
|
package/index.js
CHANGED
@@ -1,109 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
var maxRedirects = module.exports.maxRedirects = 5;
|
7
|
-
|
8
|
-
var protocols = {
|
9
|
-
https: nativeHttps,
|
10
|
-
http: nativeHttp
|
11
|
-
};
|
12
|
-
|
13
|
-
// Only use GETs on redirects
|
14
|
-
for (var protocol in protocols) {
|
15
|
-
// h is either our cloned http or https object
|
16
|
-
var h = function() {};
|
17
|
-
h.prototype = protocols[protocol];
|
18
|
-
h = new h();
|
19
|
-
|
20
|
-
module.exports[protocol] = h;
|
21
|
-
|
22
|
-
h.request = function (h) {
|
23
|
-
return function (options, callback, redirectOptions) {
|
24
|
-
|
25
|
-
redirectOptions = redirectOptions || {};
|
26
|
-
|
27
|
-
var max = (typeof options === 'object' && 'maxRedirects' in options) ? options.maxRedirects : exports.maxRedirects;
|
28
|
-
|
29
|
-
var redirect = _.extend({
|
30
|
-
count: 0,
|
31
|
-
max: max,
|
32
|
-
clientRequest: null,
|
33
|
-
userCallback: callback
|
34
|
-
}, redirectOptions);
|
35
|
-
|
36
|
-
//console.log(redirect.count);
|
37
|
-
//console.log(redirect.max);
|
38
|
-
/**
|
39
|
-
* Emit error if too many redirects
|
40
|
-
*/
|
41
|
-
if (redirect.count > redirect.max) {
|
42
|
-
var err = new Error('Max redirects exceeded. To allow more redirects, pass options.maxRedirects property.');
|
43
|
-
redirect.clientRequest.emit('error', err);
|
44
|
-
return redirect.clientRequest;
|
45
|
-
}
|
46
|
-
|
47
|
-
redirect.count++;
|
48
|
-
|
49
|
-
/**
|
50
|
-
* Parse URL from options
|
51
|
-
*/
|
52
|
-
var reqUrl;
|
53
|
-
if (typeof options === 'string') {
|
54
|
-
reqUrl = options;
|
55
|
-
}
|
56
|
-
else {
|
57
|
-
reqUrl = url.format(_.extend({ protocol: protocol }, options));
|
58
|
-
}
|
59
|
-
|
60
|
-
/*
|
61
|
-
* Build client request
|
62
|
-
*/
|
63
|
-
var clientRequest = h.__proto__.request(options, redirectCallback(reqUrl, redirect));
|
64
|
-
|
65
|
-
// Save user's clientRequest so we can emit errors later
|
66
|
-
if (!redirect.clientRequest) redirect.clientRequest = clientRequest;
|
67
|
-
|
68
|
-
/**
|
69
|
-
* ClientRequest callback for redirects
|
70
|
-
*/
|
71
|
-
function redirectCallback (reqUrl, redirect) {
|
72
|
-
return function (res) {
|
73
|
-
// status must be 300-399 for redirects
|
74
|
-
if (res.statusCode < 300 || res.statusCode > 399) {
|
75
|
-
//console.log('[' + res.statusCode + '] callback user on url ' + reqUrl);
|
76
|
-
return redirect.userCallback(res);
|
77
|
-
}
|
78
|
-
|
79
|
-
// no `Location:` header => nowhere to redirect
|
80
|
-
if (!('location' in res.headers)) {
|
81
|
-
//console.log('[no location header] callback user on url ' + reqUrl);
|
82
|
-
return redirect.userCallback(res);
|
83
|
-
}
|
84
|
-
|
85
|
-
// save the original clientRequest to our redirectOptions so we can emit errors later
|
86
|
-
|
87
|
-
// need to use url.resolve() in case location is a relative URL
|
88
|
-
var redirectUrl = url.resolve(reqUrl, res.headers['location']);
|
89
|
-
// we need to call the right api (http vs https) depending on protocol
|
90
|
-
var proto = url.parse(redirectUrl).protocol;
|
91
|
-
proto = proto.substr(0, proto.length - 1);
|
92
|
-
//console.log('Redirecting from ' + reqUrl + ' to ' + redirectUrl);
|
93
|
-
return module.exports[proto].get(redirectUrl, redirectCallback(reqUrl, redirect), redirect);
|
94
|
-
};
|
95
|
-
}
|
96
|
-
|
97
|
-
return clientRequest;
|
98
|
-
}
|
99
|
-
}(h);
|
100
|
-
|
101
|
-
// see https://github.com/joyent/node/blob/master/lib/http.js#L1623
|
102
|
-
h.get = function (h) {
|
103
|
-
return function (options, cb, redirectOptions) {
|
104
|
-
var req = h.request(options, cb, redirectOptions);
|
105
|
-
req.end();
|
106
|
-
return req;
|
107
|
-
};
|
108
|
-
}(h);
|
109
|
-
}
|
1
|
+
module.exports = require('./create')({
|
2
|
+
'http': require('http'),
|
3
|
+
'https': require('https')
|
4
|
+
});
|
package/package.json
CHANGED
@@ -1,16 +1,23 @@
|
|
1
1
|
{
|
2
2
|
"name": "follow-redirects",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.7",
|
4
4
|
"description": "HTTP and HTTPS modules that follow redirects.",
|
5
5
|
"main": "index.js",
|
6
6
|
"scripts": {
|
7
|
-
"test": "
|
7
|
+
"test": "npm run cover && npm run lint && npm run style",
|
8
|
+
"lint": "jshint *.js test/*.js test/**/*.js",
|
9
|
+
"style": "jscs *.js && jscs test/*.js test/**/*.js --config=test/.jscsrc",
|
10
|
+
"cover": "BLUEBIRD_DEBUG=1 istanbul cover ./node_modules/.bin/_mocha",
|
11
|
+
"debug": "BLUEBIRD_DEBUG=1 mocha"
|
8
12
|
},
|
9
13
|
"repository": {
|
10
14
|
"type": "git",
|
11
15
|
"url": "git@github.com:olalonde/follow-redirects.git"
|
12
16
|
},
|
13
|
-
"homepage": "",
|
17
|
+
"homepage": "https://github.com/olalonde/follow-redirects",
|
18
|
+
"bugs": {
|
19
|
+
"url": "https://github.com/olalonde/follow-redirects/issues"
|
20
|
+
},
|
14
21
|
"keywords": [
|
15
22
|
"http",
|
16
23
|
"https",
|
@@ -21,15 +28,33 @@
|
|
21
28
|
"utility"
|
22
29
|
],
|
23
30
|
"author": {
|
24
|
-
"name": "Olivier Lalonde"
|
25
|
-
|
26
|
-
|
31
|
+
"name": "Olivier Lalonde",
|
32
|
+
"email": "olalonde@gmail.com",
|
33
|
+
"url": "http://www.syskall.com"
|
27
34
|
},
|
35
|
+
"contributors": [
|
36
|
+
"James Talmage <james@talmage.io>"
|
37
|
+
],
|
38
|
+
"files": [
|
39
|
+
"index.js",
|
40
|
+
"create.js",
|
41
|
+
"http.js",
|
42
|
+
"https.js"
|
43
|
+
],
|
28
44
|
"dependencies": {
|
29
|
-
"
|
45
|
+
"debug": "^2.2.0",
|
46
|
+
"stream-consume": "^0.1.0"
|
30
47
|
},
|
31
48
|
"devDependencies": {
|
32
|
-
"
|
49
|
+
"bluebird": "^2.9.30",
|
50
|
+
"concat-stream": "^1.5.0",
|
51
|
+
"coveralls": "^2.11.2",
|
52
|
+
"express": "^4.13.0",
|
53
|
+
"istanbul": "^0.3.17",
|
54
|
+
"jscs": "^1.13.1",
|
55
|
+
"jshint": "^2.8.0",
|
56
|
+
"mocha": "^2.2.5",
|
57
|
+
"semver": "~4.3.6"
|
33
58
|
},
|
34
|
-
"license": "
|
59
|
+
"license": "MIT"
|
35
60
|
}
|
package/.npmignore
DELETED
package/test/index.js
DELETED
@@ -1,82 +0,0 @@
|
|
1
|
-
var https = require('../').https,
|
2
|
-
http = require('../').http,
|
3
|
-
nativeHttps = require('https'),
|
4
|
-
nativeHttp = require('http');
|
5
|
-
|
6
|
-
require('colors');
|
7
|
-
|
8
|
-
var urls = [
|
9
|
-
'http://bit.ly/900913'
|
10
|
-
/*,
|
11
|
-
{
|
12
|
-
type: 'https',
|
13
|
-
host: 'bitly.com',
|
14
|
-
path: '/UHfDGO',
|
15
|
-
maxRedirects: 10
|
16
|
-
}
|
17
|
-
*/
|
18
|
-
];
|
19
|
-
|
20
|
-
require('../').maxRedirects = 6;
|
21
|
-
|
22
|
-
|
23
|
-
var libs = {
|
24
|
-
http: {
|
25
|
-
//native: nativeHttp,
|
26
|
-
follow: http
|
27
|
-
},
|
28
|
-
https: {
|
29
|
-
//native: nativeHttps,
|
30
|
-
follow: https
|
31
|
-
}
|
32
|
-
};
|
33
|
-
|
34
|
-
urls.forEach(function (url) {
|
35
|
-
|
36
|
-
var proto = 'http';
|
37
|
-
if (typeof url === 'string' && url.substr(0, 5) === 'https') {
|
38
|
-
proto = 'https';
|
39
|
-
}
|
40
|
-
else if (url.type === 'https') {
|
41
|
-
proto = 'https';
|
42
|
-
}
|
43
|
-
for (var key in libs[proto]) {
|
44
|
-
var lib = libs[proto][key];
|
45
|
-
/**
|
46
|
-
* Test .get
|
47
|
-
*/
|
48
|
-
console.log((proto + '.' + 'get(' + url + ')').blue);
|
49
|
-
lib.get(url, function(res) {
|
50
|
-
//console.log('statusCode: ', res.statusCode);
|
51
|
-
//console.log('headers: ', res.headers);
|
52
|
-
|
53
|
-
res.on('data', function(d) {
|
54
|
-
console.log(('Data received ').red);
|
55
|
-
console.log(d.toString());
|
56
|
-
});
|
57
|
-
|
58
|
-
}).on('error', function(e) {
|
59
|
-
console.error(e);
|
60
|
-
});
|
61
|
-
|
62
|
-
/**
|
63
|
-
* Test .request
|
64
|
-
*/
|
65
|
-
console.log((proto + '.' + 'request(' + url + ')').blue);
|
66
|
-
var request = http.request;
|
67
|
-
var req = request(url, function(res) {
|
68
|
-
//console.log('STATUS: ' + res.statusCode);
|
69
|
-
//console.log('HEADERS: ' + JSON.stringify(res.headers));
|
70
|
-
res.setEncoding('utf8');
|
71
|
-
res.on('data', function (chunk) {
|
72
|
-
console.log('BODY: ' + chunk);
|
73
|
-
});
|
74
|
-
});
|
75
|
-
|
76
|
-
req.on('error', function(e) {
|
77
|
-
console.log('problem with request: ' + e.message);
|
78
|
-
});
|
79
|
-
|
80
|
-
req.end();
|
81
|
-
};
|
82
|
-
});
|