This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+135
View File
@@ -0,0 +1,135 @@
## Follow Redirects
Drop-in replacement for Nodes `http` and `https` that automatically follows redirects.
[![npm version](https://badge.fury.io/js/follow-redirects.svg)](https://www.npmjs.com/package/follow-redirects)
[![Build Status](https://travis-ci.org/olalonde/follow-redirects.svg?branch=master)](https://travis-ci.org/olalonde/follow-redirects)
[![Coverage Status](https://coveralls.io/repos/olalonde/follow-redirects/badge.svg?branch=master)](https://coveralls.io/r/olalonde/follow-redirects?branch=master)
[![Code Climate](https://codeclimate.com/github/olalonde/follow-redirects/badges/gpa.svg)](https://codeclimate.com/github/olalonde/follow-redirects)
[![Dependency Status](https://david-dm.org/olalonde/follow-redirects.svg)](https://david-dm.org/olalonde/follow-redirects)
[![devDependency Status](https://david-dm.org/olalonde/follow-redirects/dev-status.svg)](https://david-dm.org/olalonde/follow-redirects#info=devDependencies)
[![NPM](https://nodei.co/npm/follow-redirects.png?downloads=true)](https://nodei.co/npm/follow-redirects/)
`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)
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)
modules, with the exception that they will seamlessly follow redirects.
```javascript
var http = require('follow-redirects').http;
var https = require('follow-redirects').https;
http.get('http://bit.ly/900913', function (response) {
response.on('data', function (chunk) {
console.log(chunk);
});
}).on('error', function (err) {
console.error(err);
});
```
You can inspect the final redirected URL through the `responseUrl` property on the `response`.
If no redirection happened, `responseUrl` is the original request URL.
```javascript
https.request({
host: 'bitly.com',
path: '/UHfDGO',
}, function (response) {
console.log(response.responseUrl);
// 'http://duckduckgo.com/robots.txt'
});
```
## Options
### Global options
Global options are set directly on the `follow-redirects` module:
```javascript
var followRedirects = require('follow-redirects');
followRedirects.maxRedirects = 10;
```
The following global options are supported:
- `maxRedirects` (default: `21`) sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
### Per-request options
Per-request options are set by passing an `options` object:
```javascript
var url = require('url');
var followRedirects = require('follow-redirects');
var options = url.parse('http://bit.ly/900913');
options.maxRedirects = 10;
http.request(options);
```
In addition to the [standard HTTP](https://nodejs.org/api/http.html#http_http_request_options_callback) and [HTTPS options](https://nodejs.org/api/https.html#https_https_request_options_callback),
the following per-request options are supported:
- `followRedirects` (default: `true`) whether redirects should be followed.
- `maxRedirects` (default: `21`) sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
- `agents` (default: `undefined`) sets the `agent` option per protocol, since HTTP and HTTPS use different agents. Example value: `{ http: new http.Agent(), https: new https.Agent() }`
## Browserify Usage
Due to the way `XMLHttpRequest` works, the `browserify` versions of `http` and `https` already follow redirects.
If you are *only* targeting the browser, then this library has little value for you. If you want to write cross
platform code for node and the browser, `follow-redirects` provides a great solution for making the native node
modules behave the same as they do in browserified builds in the browser. To avoid bundling unnecessary code
you should tell browserify to swap out `follow-redirects` with the standard modules when bundling.
To make this easier, you need to change how you require the modules:
```javascript
var http = require('follow-redirects/http');
var https = require('follow-redirects/https');
```
You can then replace `follow-redirects` in your browserify configuration like so:
```javascript
"browser": {
"follow-redirects/http" : "http",
"follow-redirects/https" : "https"
}
```
The `browserify-http` module has not kept pace with node development, and no long behaves identically to the native
module when running in the browser. If you are experiencing problems, you may want to check out
[browserify-http-2](https://www.npmjs.com/package/http-browserify-2). It is more actively maintained and
attempts to address a few of the shortcomings of `browserify-http`. In that case, your browserify config should
look something like this:
```javascript
"browser": {
"follow-redirects/http" : "browserify-http-2/http",
"follow-redirects/https" : "browserify-http-2/https"
}
```
## Contributing
Pull Requests are always welcome. Please [file an issue](https://github.com/olalonde/follow-redirects/issues)
detailing your proposal before you invest your valuable time. Additional features and bug fixes should be accompanied
by tests. You can run the test suite locally with a simple `npm test` command.
## Debug Logging
`follow-redirects` uses the excellent [debug](https://www.npmjs.com/package/debug) for logging. To turn on logging
set the environment variable `DEBUG=follow-redirects` for debug output from just this module. When running the test
suite it is sometimes advantageous to set `DEBUG=*` to see output from the express server as well.
## Authors
- Olivier Lalonde (olalonde@gmail.com)
- James Talmage (james@talmage.io)
- [Ruben Verborgh](https://ruben.verborgh.org/)
## License
MIT: [http://olalonde.mit-license.org](http://olalonde.mit-license.org)
+1
View File
@@ -0,0 +1 @@
module.exports = require('./').http;
+1
View File
@@ -0,0 +1 @@
module.exports = require('./').https;
+185
View File
@@ -0,0 +1,185 @@
'use strict';
var url = require('url');
var assert = require('assert');
var http = require('http');
var https = require('https');
var Writable = require('stream').Writable;
var debug = require('debug')('follow-redirects');
var nativeProtocols = {'http:': http, 'https:': https};
var schemes = {};
var exports = module.exports = {
maxRedirects: 21
};
// RFC7231§4.2.1: Of the request methods defined by this specification,
// the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.
var safeMethods = {GET: true, HEAD: true, OPTIONS: true, TRACE: true};
// Create handlers that pass events from native requests
var eventHandlers = Object.create(null);
['abort', 'aborted', 'error'].forEach(function (event) {
eventHandlers[event] = function (arg) {
this._redirectable.emit(event, arg);
};
});
// An HTTP(S) request that can be redirected
function RedirectableRequest(options, responseCallback) {
// Initialize the request
Writable.call(this);
this._options = options;
this._redirectCount = 0;
// Attach a callback if passed
if (responseCallback) {
this.on('response', responseCallback);
}
// React to responses of native requests
var self = this;
this._onNativeResponse = function (response) {
self._processResponse(response);
};
// Perform the first request
this._performRequest();
}
RedirectableRequest.prototype = Object.create(Writable.prototype);
// Executes the next native request (initial or redirect)
RedirectableRequest.prototype._performRequest = function () {
// If specified, use the agent corresponding to the protocol
// (HTTP and HTTPS use different types of agents)
var protocol = this._options.protocol;
if (this._options.agents) {
this._options.agent = this._options.agents[schemes[protocol]];
}
// Create the native request
var nativeProtocol = nativeProtocols[this._options.protocol];
var request = this._currentRequest =
nativeProtocol.request(this._options, this._onNativeResponse);
this._currentUrl = url.format(this._options);
// Set up event handlers
request._redirectable = this;
for (var event in eventHandlers) {
if (event) {
request.on(event, eventHandlers[event]);
}
}
// The first request is explicitly ended in RedirectableRequest#end
if (this._currentResponse) {
request.end();
}
};
// Processes a response from the current native request
RedirectableRequest.prototype._processResponse = function (response) {
// RFC7231§6.4: The 3xx (Redirection) class of status code indicates
// that further action needs to be taken by the user agent in order to
// fulfill the request. If a Location header field is provided,
// the user agent MAY automatically redirect its request to the URI
// referenced by the Location field value,
// even if the specific status code is not understood.
var location = response.headers.location;
if (location && this._options.followRedirects !== false &&
response.statusCode >= 300 && response.statusCode < 400) {
// RFC7231§6.4: A client SHOULD detect and intervene
// in cyclical redirections (i.e., "infinite" redirection loops).
if (++this._redirectCount > this._options.maxRedirects) {
return this.emit('error', new Error('Max redirects exceeded.'));
}
// RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates
// that the target resource resides temporarily under a different URI
// and the user agent MUST NOT change the request method
// if it performs an automatic redirection to that URI.
if (response.statusCode !== 307) {
// RFC7231§6.4: Automatic redirection needs to done with
// care for methods not known to be safe […],
// since the user might not wish to redirect an unsafe request.
if (!(this._options.method in safeMethods)) {
this._options.method = 'GET';
}
}
// Perform the redirected request
var redirectUrl = url.resolve(this._currentUrl, location);
debug('redirecting to', redirectUrl);
Object.assign(this._options, url.parse(redirectUrl));
this._currentResponse = response;
this._performRequest();
} else {
// The response is not a redirect; return it as-is
response.responseUrl = this._currentUrl;
return this.emit('response', response);
}
};
// Aborts the current native request
RedirectableRequest.prototype.abort = function () {
this._currentRequest.abort();
};
// Ends the current native request
RedirectableRequest.prototype.end = function (data, encoding, callback) {
this._currentRequest.end(data, encoding, callback);
};
// Flushes the headers of the current native request
RedirectableRequest.prototype.flushHeaders = function () {
this._currentRequest.flushHeaders();
};
// Sets the noDelay option of the current native request
RedirectableRequest.prototype.setNoDelay = function (noDelay) {
this._currentRequest.setNoDelay(noDelay);
};
// Sets the socketKeepAlive option of the current native request
RedirectableRequest.prototype.setSocketKeepAlive = function (enable, initialDelay) {
this._currentRequest.setSocketKeepAlive(enable, initialDelay);
};
// Sets the timeout option of the current native request
RedirectableRequest.prototype.setTimeout = function (timeout, callback) {
this._currentRequest.setTimeout(timeout, callback);
};
// Writes buffered data to the current native request
RedirectableRequest.prototype._write = function (chunk, encoding, callback) {
this._currentRequest.write(chunk, encoding, callback);
};
// Export a redirecting wrapper for each native protocol
Object.keys(nativeProtocols).forEach(function (protocol) {
var scheme = schemes[protocol] = protocol.substr(0, protocol.length - 1);
var nativeProtocol = nativeProtocols[protocol];
var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
// Executes an HTTP request, following redirects
wrappedProtocol.request = function (options, callback) {
if (typeof options === 'string') {
options = url.parse(options);
options.maxRedirects = exports.maxRedirects;
} else {
options = Object.assign({
maxRedirects: exports.maxRedirects,
protocol: protocol
}, options);
}
assert.equal(options.protocol, protocol, 'protocol mismatch');
debug('options', options);
return new RedirectableRequest(options, callback);
};
// Executes a GET request, following redirects
wrappedProtocol.get = function (options, callback) {
var request = wrappedProtocol.request(options, callback);
request.end();
return request;
};
});
+140
View File
@@ -0,0 +1,140 @@
{
"_args": [
[
{
"raw": "follow-redirects@1.0.0",
"scope": null,
"escapedName": "follow-redirects",
"name": "follow-redirects",
"rawSpec": "1.0.0",
"spec": "1.0.0",
"type": "version"
},
"c:\\xampp\\htdocs\\laravel\\node_modules\\axios"
]
],
"_from": "follow-redirects@1.0.0",
"_id": "follow-redirects@1.0.0",
"_inCache": true,
"_location": "/follow-redirects",
"_nodeVersion": "6.7.0",
"_npmOperationalInternal": {
"host": "packages-12-west.internal.npmjs.com",
"tmp": "tmp/follow-redirects-1.0.0.tgz_1477238992406_0.5941755524836481"
},
"_npmUser": {
"name": "rubenverborgh",
"email": "ruben.verborgh@gmail.com"
},
"_npmVersion": "3.10.3",
"_phantomChildren": {},
"_requested": {
"raw": "follow-redirects@1.0.0",
"scope": null,
"escapedName": "follow-redirects",
"name": "follow-redirects",
"rawSpec": "1.0.0",
"spec": "1.0.0",
"type": "version"
},
"_requiredBy": [
"/axios"
],
"_resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.0.0.tgz",
"_shasum": "8e34298cbd2e176f254effec75a1c78cc849fd37",
"_shrinkwrap": null,
"_spec": "follow-redirects@1.0.0",
"_where": "c:\\xampp\\htdocs\\laravel\\node_modules\\axios",
"author": {
"name": "Olivier Lalonde",
"email": "olalonde@gmail.com",
"url": "http://www.syskall.com"
},
"bugs": {
"url": "https://github.com/olalonde/follow-redirects/issues"
},
"contributors": [
{
"name": "James Talmage",
"email": "james@talmage.io"
},
{
"name": "Ruben Verborgh",
"email": "ruben@verborgh.org",
"url": "https://ruben.verborgh.org/"
}
],
"dependencies": {
"debug": "^2.2.0"
},
"description": "HTTP and HTTPS modules that follow redirects.",
"devDependencies": {
"bluebird": "^3.4.0",
"concat-stream": "^1.5.0",
"coveralls": "^2.11.2",
"express": "^4.13.0",
"mocha": "^3.1.2",
"nyc": "^8.3.1",
"xo": "^0.17.0"
},
"directories": {},
"dist": {
"shasum": "8e34298cbd2e176f254effec75a1c78cc849fd37",
"tarball": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.0.0.tgz"
},
"files": [
"index.js",
"create.js",
"http.js",
"https.js"
],
"gitHead": "4010b44f4715c89dd75037f66458b93fd59599a1",
"homepage": "https://github.com/olalonde/follow-redirects",
"keywords": [
"http",
"https",
"url",
"redirect",
"client",
"location",
"utility"
],
"license": "MIT",
"main": "index.js",
"maintainers": [
{
"name": "jamestalmage",
"email": "james@talmage.io"
},
{
"name": "olalonde",
"email": "olalonde@gmail.com"
},
{
"name": "rubenverborgh",
"email": "ruben.verborgh@gmail.com"
}
],
"name": "follow-redirects",
"nyc": {
"reporter": [
"lcov",
"text"
]
},
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/olalonde/follow-redirects.git"
},
"scripts": {
"test": "xo && BLUEBIRD_DEBUG=1 nyc mocha"
},
"version": "1.0.0",
"xo": {
"envs": [
"mocha"
]
}
}