req: Cache the request IP

This commit is contained in:
PlasmaPower 2016-04-03 20:30:06 -06:00 committed by jongleberry
parent 826ad83db6
commit 54e58d3523
4 changed files with 36 additions and 31 deletions

View file

@ -158,6 +158,7 @@ module.exports = class Application extends Emitter {
keys: this.keys,
secure: request.secure
});
request.ip = request.ips[0] || req.socket.remoteAddress || '';
context.accept = request.accept = accepts(req);
context.state = {};
return context;

View file

@ -366,19 +366,6 @@ module.exports = {
return 'https' == this.protocol;
},
/**
* Return the remote address, or when
* `app.proxy` is `true` return
* the upstream addr.
*
* @return {String}
* @api public
*/
get ip() {
return this.ips[0] || this.socket.remoteAddress || '';
},
/**
* When `app.proxy` is `true`, parse
* the "X-Forwarded-For" ip address list.

View file

@ -4,18 +4,18 @@
const Stream = require('stream');
const Koa = require('../..');
module.exports = (req, res) => {
module.exports = (req, res, app) => {
const socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
req.socket = req.socket || socket;
res.socket = res.socket || socket;
req = Object.assign({ headers: {}, socket }, Stream.Readable.prototype, req);
res = Object.assign({ _headers: {}, socket }, Stream.Writable.prototype, res);
req.socket.remoteAddress = req.socket.remoteAddress || '127.0.0.1';
app = app || new Koa();
res.getHeader = k => res._headers[k.toLowerCase()];
res.setHeader = (k, v) => res._headers[k.toLowerCase()] = v;
res.removeHeader = (k, v) => delete res._headers[k.toLowerCase()];
return (new Koa()).createContext(req, res);
return app.createContext(req, res);
};
module.exports.request = (req, res) => module.exports(req, res).request;
module.exports.request = (req, res, app) => module.exports(req, res, app).request;
module.exports.response = (req, res) => module.exports(req, res).response;
module.exports.response = (req, res, app) => module.exports(req, res, app).response;

View file

@ -1,32 +1,49 @@
'use strict';
const request = require('../helpers/context').request;
const assert = require('assert');
const Stream = require('stream');
const Koa = require('../..');
const Request = require('../helpers/context').request;
describe('req.ip', () => {
describe('with req.ips present', () => {
it('should return req.ips[0]', () => {
const req = request();
req.app.proxy = true;
req.header['x-forwarded-for'] = '127.0.0.1';
const app = new Koa();
const req = { headers: {}, socket: new Stream.Duplex() };
app.proxy = true;
req.headers['x-forwarded-for'] = '127.0.0.1';
req.socket.remoteAddress = '127.0.0.2';
req.ip.should.equal('127.0.0.1');
const request = Request(req, undefined, app);
request.ip.should.equal('127.0.0.1');
});
});
describe('with no req.ips present', () => {
it('should return req.socket.remoteAddress', () => {
const req = request();
const req = { socket: new Stream.Duplex() };
req.socket.remoteAddress = '127.0.0.2';
req.ip.should.equal('127.0.0.2');
const request = Request(req);
request.ip.should.equal('127.0.0.2');
});
describe('with req.socket.remoteAddress not present', () => {
it('should return an empty string', () => {
const req = request();
req.socket.remoteAddress = null;
req.ip.should.equal('');
const socket = new Stream.Duplex();
Object.defineProperty(socket, 'remoteAddress', {
get: () => undefined, // So that the helper doesn't override it with a reasonable value
set: () => {}
});
assert.equal(Request({ socket }).ip, '');
});
});
});
it('should be cached', () => {
const req = { socket: new Stream.Duplex() };
req.socket.remoteAddress = '127.0.0.2';
const request = Request(req);
req.socket.remoteAddress = '127.0.0.1';
request.ip.should.equal('127.0.0.2');
});
});