Skip to content

Commit c3cc6a1

Browse files
committed
Add server.options.info.remote. Closes #4034
1 parent c6fe471 commit c3cc6a1

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ Default value: the operating system hostname and if not available, to `'localhos
142142
The public hostname or IP address. Used to set [`server.info.host`](#server.info) and
143143
[`server.info.uri`](#server.info) and as [`address`](#server.options.address) is none provided.
144144

145+
### <a name="server.options.info.remote" /> `server.options.info.remote`
146+
147+
Default value: `false`.
148+
149+
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).
150+
145151
#### <a name="server.options.listener" /> `server.options.listener`
146152

147153
Default value: none.

lib/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ internals.server = Joi.object({
249249
.allow(false)
250250
.default(),
251251
host: Joi.string().hostname().allow(null),
252+
info: Joi.object({
253+
remote: Joi.boolean().default(false)
254+
})
255+
.default({}),
252256
listener: Joi.any(),
253257
load: Joi.object({
254258
sampleInterval: Joi.number().integer().min(0).default(0)

lib/request.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,11 @@ internals.Info = class {
628628
this.cors = null;
629629
this.responded = 0;
630630
this.completed = 0;
631+
632+
if (request._core.settings.info.remote) {
633+
this.remoteAddress;
634+
this.remotePort;
635+
}
631636
}
632637

633638
get remoteAddress() {

test/request.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,16 @@ describe('Request', () => {
410410
return stream;
411411
};
412412

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

416416
let disconnected = 0;
417+
let info;
417418
const onRequest = (request, h) => {
418419

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

422+
info = request.info;
421423
++disconnected;
422424
});
423425

@@ -462,6 +464,8 @@ describe('Request', () => {
462464
});
463465

464466
await server.stop();
467+
expect(info.remotePort).to.exist();
468+
expect(info.remoteAddress).to.exist();
465469
});
466470

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

0 commit comments

Comments
 (0)