Skip to content

Commit

Permalink
perf: reduce overhead when no X-Forwarded-For header
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 15, 2017
1 parent bbdcf0b commit ad1efea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* perf: reduce overhead when no `X-Forwarded-For` header

0.1.1 / 2017-09-10
==================

Expand Down
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,29 @@ function forwarded (req) {
}

// simple header parsing
var proxyAddrs = (req.headers['x-forwarded-for'] || '')
.trim()
.split(TOKEN_LIST_REGEXP)
.filter(Boolean)
.reverse()
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) {
if (!header) {
return []
}

return header
.trim()
.split(TOKEN_LIST_REGEXP)
.filter(Boolean)
.reverse()
}

0 comments on commit ad1efea

Please sign in to comment.