Skip to content

Commit

Permalink
Move request.info to class
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Oct 31, 2019
1 parent 6b67d24 commit acab470
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
11 changes: 11 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ exports = module.exports = internals.Core = class {
return info;
}

_counter() {

const next = ++this.requestCounter.value;

if (this.requestCounter.value > this.requestCounter.max) {
this.requestCounter.value = this.requestCounter.min;
}

return next - 1;
}

_createCache(configs) {

Hoek.assert(this.phase !== 'initializing', 'Cannot provision server cache while server is initializing');
Expand Down
58 changes: 38 additions & 20 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ exports = module.exports = internals.Request = class {

this.app = options.app ? Object.assign({}, options.app) : {}; // Place for application-specific state without conflicts with hapi, should not be used by plugins (shallow cloned)
this.headers = req.headers;
this.info = internals.info(this._core, req);
this.jsonp = null;
this.logs = [];
this.method = req.method.toLowerCase();
Expand All @@ -60,6 +59,8 @@ exports = module.exports = internals.Request = class {
this.server = server;
this.state = null;

this.info = new internals.Info(this);

this.auth = {
isAuthenticated: false,
isAuthorized: false,
Expand Down Expand Up @@ -601,33 +602,50 @@ exports = module.exports = internals.Request = class {
internals.Request.reserved = internals.reserved;


internals.info = function (core, req) {
internals.Info = class {

constructor(request) {

this._request = request;

const host = req.headers.host ? req.headers.host.trim() : '';
const received = Date.now();
const req = request.raw.req;
const host = req.headers.host ? req.headers.host.trim() : '';
const received = Date.now();

const info = {
received,
remoteAddress: req.connection.remoteAddress,
remotePort: req.connection.remotePort || '',
referrer: req.headers.referrer || req.headers.referer || '',
host,
hostname: host.split(':')[0],
id: `${received}:${core.info.id}:${core.requestCounter.value++}`,
this.received = received;
this.referrer = req.headers.referrer || req.headers.referer || '';
this.host = host;
this.hostname = host.split(':')[0];
this.id = `${received}:${request._core.info.id}:${request._core._counter()}`;

this._remoteAddress = null;
this._remotePort = null;

// Assigned later

acceptEncoding: null,
cors: null,
responded: 0,
completed: 0
};
this.acceptEncoding = null;
this.cors = null;
this.responded = 0;
this.completed = 0;
}

get remoteAddress() {

if (core.requestCounter.value > core.requestCounter.max) {
core.requestCounter.value = core.requestCounter.min;
if (!this._remoteAddress) {
this._remoteAddress = this._request.raw.req.connection.remoteAddress;
}

return this._remoteAddress;
}

return info;
get remotePort() {

if (this._remotePort === null) {
this._remotePort = this._request.raw.req.connection.remotePort || '';
}

return this._remotePort;
}
};


Expand Down
16 changes: 15 additions & 1 deletion test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ describe('Request', () => {
}

expect(request.info.remoteAddress).to.equal(expectedClientAddress);
expect(request.info.remoteAddress).to.equal(request.info.remoteAddress);
expect(request.info.remotePort).to.be.above(0);

// Call twice to reuse cached values

expect(request.info.remoteAddress).to.equal(expectedClientAddress);
expect(request.info.remotePort).to.be.above(0);

return 'ok';
};

Expand All @@ -124,6 +130,14 @@ describe('Request', () => {
await server.stop();
});

it('sets port to nothing when not available', async () => {

const server = Hapi.server({ debug: false });
server.route({ method: 'GET', path: '/', handler: (request) => request.info.remotePort === '' });
const res = await server.inject('/');
expect(res.result).to.equal(true);
});

it('sets referrer', async () => {

const server = Hapi.server();
Expand Down

0 comments on commit acab470

Please sign in to comment.