Skip to content

Commit

Permalink
Add server.options.info.remote. Closes #4034
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Feb 3, 2020
1 parent c6fe471 commit c3cc6a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ Default value: the operating system hostname and if not available, to `'localhos
The public hostname or IP address. Used to set [`server.info.host`](#server.info) and
[`server.info.uri`](#server.info) and as [`address`](#server.options.address) is none provided.

### <a name="server.options.info.remote" /> `server.options.info.remote`

Default value: `false`.

If `true`, the `request.info.remoteAddress` and `request.info.remotePort` are populated when the request is received which can consume more resource (but is ok if the information is needed, especially for aborted requests). When `false`, the fields are only populated upon demand (but will be `undefined` if accessed after the request is aborted).

#### <a name="server.options.listener" /> `server.options.listener`

Default value: none.
Expand Down
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ internals.server = Joi.object({
.allow(false)
.default(),
host: Joi.string().hostname().allow(null),
info: Joi.object({
remote: Joi.boolean().default(false)
})
.default({}),
listener: Joi.any(),
load: Joi.object({
sampleInterval: Joi.number().integer().min(0).default(0)
Expand Down
5 changes: 5 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@ internals.Info = class {
this.cors = null;
this.responded = 0;
this.completed = 0;

if (request._core.settings.info.remote) {
this.remoteAddress;
this.remotePort;
}
}

get remoteAddress() {
Expand Down
6 changes: 5 additions & 1 deletion test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,16 @@ describe('Request', () => {
return stream;
};

const server = Hapi.server();
const server = Hapi.server({ info: { remote: true } });
server.route({ method: 'GET', path: '/', handler });

let disconnected = 0;
let info;
const onRequest = (request, h) => {

request.events.once('disconnect', () => {

info = request.info;
++disconnected;
});

Expand Down Expand Up @@ -462,6 +464,8 @@ describe('Request', () => {
});

await server.stop();
expect(info.remotePort).to.exist();
expect(info.remoteAddress).to.exist();
});

it('handles aborted requests (pre response)', async () => {
Expand Down

0 comments on commit c3cc6a1

Please sign in to comment.