Skip to content

Commit 5b83a0c

Browse files
committed
fix(http-server): use loopback instead of 0.0.0.0/[::] in URLs
Host values `0.0.0.0` (IPv4) and `::` (IPv6), which mean "any host", are not supported by certain browsers and platforms, e.g. Safari on MacOS or Windows. This commit fixes the `url` getter to convert these "any host" values into a loopback address (`127.0.0.1` for IPV4 or `::1` for IPv6).
1 parent 650b387 commit 5b83a0c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/http-server/src/http-server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ export class HttpServer {
145145
public get url(): string {
146146
let host = this.host;
147147
if (this._address.family === 'IPv6') {
148+
if (host === '::') host = '::1';
148149
host = `[${host}]`;
150+
} else if (host === '0.0.0.0') {
151+
host = '127.0.0.1';
149152
}
150153
return `${this._protocol}://${host}:${this.port}`;
151154
}

packages/http-server/test/integration/http-server.integration.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ describe('HttpServer (integration)', () => {
177177
expect(response.statusCode).to.equal(200);
178178
});
179179

180+
it('converts host from [::] to [::1] in url', async () => {
181+
// Safari on MacOS does not support http://[::]:3000/
182+
server = new HttpServer(dummyRequestHandler, {host: '::'});
183+
await server.start();
184+
expect(server.url).to.equal(`http://[::1]:${server.port}`);
185+
});
186+
187+
it('converts host from 0.0.0.0 to 127.0.0.1 in url', async () => {
188+
// Windows does not support http://0.0.0.0:3000/
189+
server = new HttpServer(dummyRequestHandler, {host: '0.0.0.0'});
190+
await server.start();
191+
expect(server.url).to.equal(`http://127.0.0.1:${server.port}`);
192+
});
193+
180194
function dummyRequestHandler(req: ServerRequest, res: ServerResponse): void {
181195
res.end();
182196
}

0 commit comments

Comments
 (0)