Skip to content

Commit fbeac4a

Browse files
author
Eran Hammer
committed
uri connection option. Closes hapijs#2249
1 parent 8b8c715 commit fbeac4a

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

docs/Reference.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ the sole connection where:
273273
- `'https'` - HTTPS.
274274
- `'socket'` - UNIX domain socket or Windows named pipe.
275275
- `uri` - a string representing the connection (e.g. 'http://example.com:8080' or
276-
'socket:/unix/domain/socket/path'). If no `port` is available or set to `0`, the `uri` will not
277-
include a port component.
276+
'socket:/unix/domain/socket/path'). Contains the `uri` setting if provided, otherwise constructed
277+
from the available settings. If no `port` is available or set to `0`, the `uri` will not include
278+
a port component.
278279

279280
When the server contains more than one connection, each [`server.connections`](#serverconnections)
280281
array member provides its own `connection.info`.
@@ -753,6 +754,8 @@ Adds an incoming server connection where:
753754
uses an available port when the server is started (and assigned to `server.info.port`). If `port`
754755
is a string containing a '/' character, it is used as a UNIX domain socket path and if it starts
755756
with '\\.\pipe' as a Windows named pipe.
757+
- `uri` - the full public URI without the path (e.g. 'http://example.com:8080'). If present, used
758+
as the connection `info.uri` otherwise constructed from the connection settings.
756759
- `listener` - optional node.js HTTP (or HTTPS)
757760
[`http.Server`](http://nodejs.org/api/http.html#http_class_http_server) object or any compatible
758761
object. If the `listener` needs to be manually started, set `autoListen` to `false`.

lib/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ exports = module.exports = internals.Connection = function (server, options) {
9898
id: Os.hostname() + ':' + process.pid + ':' + now.toString(36)
9999
};
100100

101-
this.info.uri = this.info.protocol + ':' + (this.type === 'tcp' ? '//' + this.info.host + (this.info.port ? ':' + this.info.port : '') : this.info.port);
101+
this.info.uri = (this.settings.uri || (this.info.protocol + ':' + (this.type === 'tcp' ? '//' + this.info.host + (this.info.port ? ':' + this.info.port : '') : this.info.port)));
102102
};
103103

104104
Hoek.inherits(internals.Connection, Events.EventEmitter);
@@ -118,7 +118,7 @@ internals.Connection.prototype._init = function () {
118118
var address = self.listener.address();
119119
self.info.address = address.address;
120120
self.info.port = address.port;
121-
self.info.uri = self.info.protocol + '://' + self.info.host + ':' + self.info.port;
121+
self.info.uri = (self.settings.uri || (self.info.protocol + '://' + self.info.host + ':' + self.info.port));
122122
}
123123
});
124124

lib/schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ internals.connection = internals.connectionBase.keys({
199199
Joi.string().regex(/^\\\\\.\\pipe\\/) // Windows named pipe
200200
])
201201
.allow(null),
202-
tls: Joi.object().allow(null)
202+
tls: Joi.object().allow(null),
203+
uri: Joi.string().regex(/[^/]$/)
203204
});
204205

205206

test/connection.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ describe('Connection', function () {
9898
});
9999
});
100100

101+
it('uses uri when present instead of host and port', function (done) {
102+
103+
var server = new Hapi.Server();
104+
server.connection({ host: 'no.such.domain.hapi', address: 'localhost', uri: 'http://uri.example.com:8080' });
105+
expect(server.info.uri).to.equal('http://uri.example.com:8080');
106+
server.start(function () {
107+
108+
expect(server.info.host).to.equal('no.such.domain.hapi');
109+
expect(server.info.address).to.equal('127.0.0.1');
110+
expect(server.info.uri).to.equal('http://uri.example.com:8080');
111+
done();
112+
});
113+
});
114+
115+
it('throws on uri ending with /', function (done) {
116+
117+
var server = new Hapi.Server();
118+
expect(function () {
119+
120+
server.connection({ uri: 'http://uri.example.com:8080/' });
121+
}).to.throw(/Invalid connection options/);
122+
done();
123+
});
124+
101125
it('creates a server listening on a unix domain socket', { skip: process.platform === 'win32' }, function (done) {
102126

103127
var port = Path.join(__dirname, 'hapi-server.socket');

0 commit comments

Comments
 (0)