Merge pull request #215 from feross/upgradereq

Delete `socket.upgradeReq` when it is no longer needed to reduce memory usage
This commit is contained in:
Feross Aboukhadijeh 2017-03-08 01:32:57 -08:00 committed by GitHub
commit 0c973661d2

View File

@ -52,15 +52,27 @@ function parseWebSocketRequest (socket, opts, params) {
throw new Error('invalid action in WS request: ' + params.action) throw new Error('invalid action in WS request: ' + params.action)
} }
params.ip = opts.trustProxy // On first parse, save important data from `socket.upgradeReq` and delete it
? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress // to reduce memory usage.
: socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 if (socket.upgradeReq) {
params.port = socket.upgradeReq.connection.remotePort socket.ip = opts.trustProxy
if (params.port) { ? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port : socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
socket.port = socket.upgradeReq.connection.remotePort
if (socket.port) {
socket.addr = (common.IPV6_RE.test(socket.ip) ? '[' + socket.ip + ']' : socket.ip) + ':' + socket.port
}
socket.headers = socket.upgradeReq.headers
// Delete `socket.upgradeReq` when it is no longer needed to reduce memory usage
socket.upgradeReq = null
} }
params.headers = socket.upgradeReq.headers params.ip = socket.ip
params.port = socket.port
params.addr = socket.addr
params.headers = socket.headers
return params return params
} }