Skip to content

Commit e9a9fae

Browse files
committed
0.1.0
0 parents  commit e9a9fae

File tree

8 files changed

+202
-0
lines changed

8 files changed

+202
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
node_modules/
3+
npm-debug.log

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- "0.6"
4+
- "0.8"
5+
- "0.10"
6+
- "0.11"
7+
matrix:
8+
allow_failures:
9+
- node_js: "0.11"
10+
fast_finish: true
11+
script:
12+
- "test $TRAVIS_NODE_VERSION != '0.6' || npm test"
13+
- "test $TRAVIS_NODE_VERSION = '0.6' || npm run-script test-travis"
14+
after_script:
15+
- "test $TRAVIS_NODE_VERSION = '0.10' && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.1.0 / 2014-09-21
2+
==================
3+
4+
* Initial release

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2014 Douglas Christopher Wilson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# forwarded
2+
3+
[![NPM Version][npm-image]][npm-url]
4+
[![NPM Downloads][downloads-image]][downloads-url]
5+
[![Node.js Version][node-version-image]][node-version-url]
6+
[![Build Status][travis-image]][travis-url]
7+
[![Test Coverage][coveralls-image]][coveralls-url]
8+
9+
Parse HTTP X-Forwarded-For header
10+
11+
## Installation
12+
13+
```sh
14+
$ npm install forwarded
15+
```
16+
17+
## API
18+
19+
```js
20+
var forwarded = require('forwarded')
21+
```
22+
23+
### forwarded(req)
24+
25+
```js
26+
var addresses = forwarded(req)
27+
```
28+
29+
Parse the `X-Forwarded-For` header from the request. Returns an array
30+
of the addresses, including the socket address for the `req`. In reverse
31+
order (i.e. index `0` is the socket address and the last index is the
32+
furthest address, typically the end-user).
33+
34+
## Testing
35+
36+
```sh
37+
$ npm test
38+
```
39+
40+
## License
41+
42+
[MIT](LICENSE)
43+
44+
[npm-image]: https://img.shields.io/npm/v/forwarded.svg?style=flat
45+
[npm-url]: https://npmjs.org/package/forwarded
46+
[node-version-image]: https://img.shields.io/node/v/forwarded.svg?style=flat
47+
[node-version-url]: http://nodejs.org/download/
48+
[travis-image]: https://img.shields.io/travis/jshttp/forwarded.svg?style=flat
49+
[travis-url]: https://travis-ci.org/jshttp/forwarded
50+
[coveralls-image]: https://img.shields.io/coveralls/jshttp/forwarded.svg?style=flat
51+
[coveralls-url]: https://coveralls.io/r/jshttp/forwarded?branch=master
52+
[downloads-image]: https://img.shields.io/npm/dm/forwarded.svg?style=flat
53+
[downloads-url]: https://npmjs.org/package/forwarded

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
* forwarded
3+
* Copyright(c) 2014 Douglas Christopher Wilson
4+
* MIT Licensed
5+
*/
6+
7+
/**
8+
* Module exports.
9+
*/
10+
11+
module.exports = forwarded
12+
13+
/**
14+
* Get all addresses in the request, using the `X-Forwarded-For` header.
15+
*
16+
* @param {Object} req
17+
* @api public
18+
*/
19+
20+
function forwarded(req) {
21+
if (!req) {
22+
throw new TypeError('argument req is required')
23+
}
24+
25+
// simple header parsing
26+
var proxyAddrs = (req.headers['x-forwarded-for'] || '')
27+
.split(/ *, */)
28+
.filter(Boolean)
29+
.reverse()
30+
var socketAddr = req.connection.remoteAddress
31+
var addrs = [socketAddr].concat(proxyAddrs)
32+
33+
// return all addresses
34+
return addrs
35+
}

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "forwarded",
3+
"description": "Parse HTTP X-Forwarded-For header",
4+
"version": "0.1.0",
5+
"contributors": [
6+
"Douglas Christopher Wilson <doug@somethingdoug.com>"
7+
],
8+
"license": "MIT",
9+
"keywords": [
10+
"x-forwarded-for",
11+
"http",
12+
"req"
13+
],
14+
"repository": "jshttp/forwarded",
15+
"devDependencies": {
16+
"istanbul": "0.3.2",
17+
"mocha": "~1.21.4"
18+
},
19+
"files": [
20+
"LICENSE",
21+
"HISTORY.md",
22+
"README.md",
23+
"index.js"
24+
],
25+
"engines": {
26+
"node": ">= 0.6"
27+
},
28+
"scripts": {
29+
"test": "mocha --reporter spec --bail --check-leaks test/",
30+
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
31+
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
32+
}
33+
}

test/test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
var assert = require('assert')
3+
var forwarded = require('..')
4+
5+
describe('forwarded(req)', function () {
6+
it('should require req', function () {
7+
assert.throws(forwarded.bind(null), /argument req.*required/)
8+
})
9+
10+
it('should work with X-Forwarded-For header', function () {
11+
var req = createReq('127.0.0.1')
12+
assert.deepEqual(forwarded(req), ['127.0.0.1'])
13+
})
14+
15+
it('should include entries from X-Forwarded-For', function () {
16+
var req = createReq('127.0.0.1', {
17+
'x-forwarded-for': '10.0.0.2, 10.0.0.1'
18+
})
19+
assert.deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
20+
})
21+
22+
it('should skip blank entries', function () {
23+
var req = createReq('127.0.0.1', {
24+
'x-forwarded-for': '10.0.0.2,, 10.0.0.1'
25+
})
26+
assert.deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
27+
})
28+
})
29+
30+
function createReq(socketAddr, headers) {
31+
return {
32+
connection: {
33+
remoteAddress: socketAddr
34+
},
35+
headers: headers || {}
36+
};
37+
}

0 commit comments

Comments
 (0)