1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

fix url parse bug (#3316)

This commit is contained in:
木杉 2019-11-13 02:45:48 +08:00 committed by Ry Dahl
parent 7ba42ee4a6
commit 0f33bf6885
2 changed files with 26 additions and 7 deletions

View file

@ -17,7 +17,7 @@ interface URLParts {
} }
const patterns = { const patterns = {
protocol: "(?:([^:/?#]+):)", protocol: "(?:([a-z]+):)",
authority: "(?://([^/?#]*))", authority: "(?://([^/?#]*))",
path: "([^?#]*)", path: "([^?#]*)",
query: "(\\?[^#]*)", query: "(\\?[^#]*)",
@ -228,10 +228,13 @@ export class URL {
this.username || this.password this.username || this.password
? `${this.username}${this.password ? ":" + this.password : ""}@` ? `${this.username}${this.password ? ":" + this.password : ""}@`
: ""; : "";
let slash = "";
return `${this.protocol}//${authentication}${this.host}${this.pathname}${ if (this.host || this.protocol === "file:") {
this.search slash = "//";
}${this.hash}`; }
return `${this.protocol}${slash}${authentication}${this.host}${
this.pathname
}${this.search}${this.hash}`;
} }
set href(value: string) { set href(value: string) {
@ -244,7 +247,10 @@ export class URL {
} }
get origin(): string { get origin(): string {
return `${this.protocol}//${this.host}`; if (this.host) {
return `${this.protocol}//${this.host}`;
}
return "null";
} }
get password(): string { get password(): string {

View file

@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // 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 { test(function urlParsing(): void {
const url = new URL( const url = new URL(
@ -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: "?" }' '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");
});
});