-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e9a9fae
Showing
8 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
node_modules/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
language: node_js | ||
node_js: | ||
- "0.6" | ||
- "0.8" | ||
- "0.10" | ||
- "0.11" | ||
matrix: | ||
allow_failures: | ||
- node_js: "0.11" | ||
fast_finish: true | ||
script: | ||
- "test $TRAVIS_NODE_VERSION != '0.6' || npm test" | ||
- "test $TRAVIS_NODE_VERSION = '0.6' || npm run-script test-travis" | ||
after_script: | ||
- "test $TRAVIS_NODE_VERSION = '0.10' && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.1.0 / 2014-09-21 | ||
================== | ||
|
||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2014 Douglas Christopher Wilson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# forwarded | ||
|
||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
[![Node.js Version][node-version-image]][node-version-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Test Coverage][coveralls-image]][coveralls-url] | ||
|
||
Parse HTTP X-Forwarded-For header | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install forwarded | ||
``` | ||
|
||
## API | ||
|
||
```js | ||
var forwarded = require('forwarded') | ||
``` | ||
|
||
### forwarded(req) | ||
|
||
```js | ||
var addresses = forwarded(req) | ||
``` | ||
|
||
Parse the `X-Forwarded-For` header from the request. Returns an array | ||
of the addresses, including the socket address for the `req`. In reverse | ||
order (i.e. index `0` is the socket address and the last index is the | ||
furthest address, typically the end-user). | ||
|
||
## Testing | ||
|
||
```sh | ||
$ npm test | ||
``` | ||
|
||
## License | ||
|
||
[MIT](LICENSE) | ||
|
||
[npm-image]: https://img.shields.io/npm/v/forwarded.svg?style=flat | ||
[npm-url]: https://npmjs.org/package/forwarded | ||
[node-version-image]: https://img.shields.io/node/v/forwarded.svg?style=flat | ||
[node-version-url]: http://nodejs.org/download/ | ||
[travis-image]: https://img.shields.io/travis/jshttp/forwarded.svg?style=flat | ||
[travis-url]: https://travis-ci.org/jshttp/forwarded | ||
[coveralls-image]: https://img.shields.io/coveralls/jshttp/forwarded.svg?style=flat | ||
[coveralls-url]: https://coveralls.io/r/jshttp/forwarded?branch=master | ||
[downloads-image]: https://img.shields.io/npm/dm/forwarded.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/forwarded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/*! | ||
* forwarded | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module exports. | ||
*/ | ||
|
||
module.exports = forwarded | ||
|
||
/** | ||
* Get all addresses in the request, using the `X-Forwarded-For` header. | ||
* | ||
* @param {Object} req | ||
* @api public | ||
*/ | ||
|
||
function forwarded(req) { | ||
if (!req) { | ||
throw new TypeError('argument req is required') | ||
} | ||
|
||
// simple header parsing | ||
var proxyAddrs = (req.headers['x-forwarded-for'] || '') | ||
.split(/ *, */) | ||
.filter(Boolean) | ||
.reverse() | ||
var socketAddr = req.connection.remoteAddress | ||
var addrs = [socketAddr].concat(proxyAddrs) | ||
|
||
// return all addresses | ||
return addrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "forwarded", | ||
"description": "Parse HTTP X-Forwarded-For header", | ||
"version": "0.1.0", | ||
"contributors": [ | ||
"Douglas Christopher Wilson <doug@somethingdoug.com>" | ||
], | ||
"license": "MIT", | ||
"keywords": [ | ||
"x-forwarded-for", | ||
"http", | ||
"req" | ||
], | ||
"repository": "jshttp/forwarded", | ||
"devDependencies": { | ||
"istanbul": "0.3.2", | ||
"mocha": "~1.21.4" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"HISTORY.md", | ||
"README.md", | ||
"index.js" | ||
], | ||
"engines": { | ||
"node": ">= 0.6" | ||
}, | ||
"scripts": { | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
var assert = require('assert') | ||
var forwarded = require('..') | ||
|
||
describe('forwarded(req)', function () { | ||
it('should require req', function () { | ||
assert.throws(forwarded.bind(null), /argument req.*required/) | ||
}) | ||
|
||
it('should work with X-Forwarded-For header', function () { | ||
var req = createReq('127.0.0.1') | ||
assert.deepEqual(forwarded(req), ['127.0.0.1']) | ||
}) | ||
|
||
it('should include entries from X-Forwarded-For', function () { | ||
var req = createReq('127.0.0.1', { | ||
'x-forwarded-for': '10.0.0.2, 10.0.0.1' | ||
}) | ||
assert.deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2']) | ||
}) | ||
|
||
it('should skip blank entries', function () { | ||
var req = createReq('127.0.0.1', { | ||
'x-forwarded-for': '10.0.0.2,, 10.0.0.1' | ||
}) | ||
assert.deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2']) | ||
}) | ||
}) | ||
|
||
function createReq(socketAddr, headers) { | ||
return { | ||
connection: { | ||
remoteAddress: socketAddr | ||
}, | ||
headers: headers || {} | ||
}; | ||
} |