Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+76
@@ -0,0 +1,76 @@
|
||||
/*!
|
||||
* forwarded
|
||||
* Copyright(c) 2014-2017 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
* @public
|
||||
*/
|
||||
|
||||
module.exports = forwarded
|
||||
|
||||
/**
|
||||
* Get all addresses in the request, using the `X-Forwarded-For` header.
|
||||
*
|
||||
* @param {object} req
|
||||
* @return {array}
|
||||
* @public
|
||||
*/
|
||||
|
||||
function forwarded (req) {
|
||||
if (!req) {
|
||||
throw new TypeError('argument req is required')
|
||||
}
|
||||
|
||||
// simple header parsing
|
||||
var proxyAddrs = parse(req.headers['x-forwarded-for'] || '')
|
||||
var socketAddr = req.connection.remoteAddress
|
||||
var addrs = [socketAddr].concat(proxyAddrs)
|
||||
|
||||
// return all addresses
|
||||
return addrs
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the X-Forwarded-For header.
|
||||
*
|
||||
* @param {string} header
|
||||
* @private
|
||||
*/
|
||||
|
||||
function parse (header) {
|
||||
var end = header.length
|
||||
var list = []
|
||||
var start = header.length
|
||||
|
||||
// gather addresses, backwards
|
||||
for (var i = header.length - 1; i >= 0; i--) {
|
||||
switch (header.charCodeAt(i)) {
|
||||
case 0x20: /* */
|
||||
if (start === end) {
|
||||
start = end = i
|
||||
}
|
||||
break
|
||||
case 0x2c: /* , */
|
||||
if (start !== end) {
|
||||
list.push(header.substring(start, end))
|
||||
}
|
||||
start = end = i
|
||||
break
|
||||
default:
|
||||
start = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// final address
|
||||
if (start !== end) {
|
||||
list.push(header.substring(start, end))
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "forwarded",
|
||||
"description": "Parse HTTP X-Forwarded-For header",
|
||||
"version": "0.1.2",
|
||||
"contributors": [
|
||||
"Douglas Christopher Wilson <doug@somethingdoug.com>"
|
||||
],
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"x-forwarded-for",
|
||||
"http",
|
||||
"req"
|
||||
],
|
||||
"repository": "jshttp/forwarded",
|
||||
"devDependencies": {
|
||||
"beautify-benchmark": "0.2.4",
|
||||
"benchmark": "2.1.4",
|
||||
"eslint": "3.19.0",
|
||||
"eslint-config-standard": "10.2.1",
|
||||
"eslint-plugin-import": "2.7.0",
|
||||
"eslint-plugin-node": "5.1.1",
|
||||
"eslint-plugin-promise": "3.5.0",
|
||||
"eslint-plugin-standard": "3.0.1",
|
||||
"istanbul": "0.4.5",
|
||||
"mocha": "1.21.5"
|
||||
},
|
||||
"files": [
|
||||
"LICENSE",
|
||||
"HISTORY.md",
|
||||
"README.md",
|
||||
"index.js"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
},
|
||||
"scripts": {
|
||||
"bench": "node benchmark/index.js",
|
||||
"lint": "eslint .",
|
||||
"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/"
|
||||
}
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz"
|
||||
,"_integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
|
||||
,"_from": "forwarded@~0.1.2"
|
||||
}
|
||||
Reference in new issue
Block a user