Skip to content

Commit

Permalink
fix url parse bug (denoland#3316)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhmushan authored and bartlomieju committed Dec 28, 2019
1 parent a2c91cf commit c469dd4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
18 changes: 12 additions & 6 deletions cli/js/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface URLParts {
}

const patterns = {
protocol: "(?:([^:/?#]+):)",
protocol: "(?:([a-z]+):)",
authority: "(?://([^/?#]*))",
path: "([^?#]*)",
query: "(\\?[^#]*)",
Expand Down Expand Up @@ -228,10 +228,13 @@ export class URL {
this.username || this.password
? `${this.username}${this.password ? ":" + this.password : ""}@`
: "";

return `${this.protocol}//${authentication}${this.host}${this.pathname}${
this.search
}${this.hash}`;
let slash = "";
if (this.host || this.protocol === "file:") {
slash = "//";
}
return `${this.protocol}${slash}${authentication}${this.host}${
this.pathname
}${this.search}${this.hash}`;
}

set href(value: string) {
Expand All @@ -244,7 +247,10 @@ export class URL {
}

get origin(): string {
return `${this.protocol}//${this.host}`;
if (this.host) {
return `${this.protocol}//${this.host}`;
}
return "null";
}

get password(): string {
Expand Down
15 changes: 14 additions & 1 deletion cli/js/url_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEquals } from "./test_util.ts";
import { test, assert, assertEquals, assertThrows } from "./test_util.ts";

test(function urlParsing(): void {
const url = new URL(
Expand Down Expand Up @@ -187,3 +187,16 @@ test(function customInspectFunction(): void {
'URL { href: "http://example.com/?", origin: "http://example.com", protocol: "http:", username: "", password: "", host: "example.com", hostname: "example.com", port: "", pathname: "/", hash: "", search: "?" }'
);
});

test(function protocolNotHttpOrFile() {
const url = new URL("about:blank");
assertEquals(url.href, "about:blank");
assertEquals(url.protocol, "about:");
assertEquals(url.origin, "null");
});

test(function createBadUrl(): void {
assertThrows(() => {
new URL("0.0.0.0:8080");
});
});

0 comments on commit c469dd4

Please sign in to comment.