Skip to content

Commit

Permalink
fix(core): identify correct default port for http and https urls (Azu…
Browse files Browse the repository at this point in the history
…re#524)

Ensure the correct default port is parsed from http and https URLs.

Before this change, URLs used to identify remote API servers might be parsed as using port 0. swa-cli would then attempt to make a TCP connection to port 0 and would eventually timeout.

With this change, ports for remote API servers are now correctly identified and connections are made to the server without the timeout occuring.

Relates to Azure#523
  • Loading branch information
danwatford authored Jul 6, 2022
1 parent 6acd1f3 commit df171a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/core/utils/net.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jest.mock("../constants", () => {});
import { logger } from "./logger";
import { address, hostnameToIpAdress, parsePort, response } from "./net";
import { address, hostnameToIpAdress, parsePort, parseUrl, response } from "./net";

describe("net utilities", () => {
describe("response()", () => {
Expand Down Expand Up @@ -298,4 +298,16 @@ describe("net utilities", () => {
expect(hostnameToIpAdress("foo.bar")).toBe("foo.bar");
});
});

describe("parseUrl()", () => {
it("should specify port 443 for https URLs", () => {
expect(parseUrl("https://foo.com")).toMatchObject({ port: 443 });
});
it("should specify port 80 for http URLs", () => {
expect(parseUrl("http://foo.com")).toMatchObject({ port: 80 });
});
it("should parse the port given in the URL", () => {
expect(parseUrl("https://foo.com:9999")).toMatchObject({ port: 9999 });
});
});
});
13 changes: 12 additions & 1 deletion src/core/utils/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ export function parseUrl(url: string | undefined) {
throw new Error(`Address: ${url} is malformed!`);
}

const { protocol, port, host, hostname } = new URL(url);
let { protocol, port, host, hostname } = new URL(url);
if (port === "") {
switch (protocol) {
case "http:":
port = "80";
break;
case "https:":
port = "443";
break;
}
}

return {
protocol,
port: Number(port),
Expand Down

0 comments on commit df171a1

Please sign in to comment.