1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-12 02:27:46 -05:00

fix(std/http): Support ipv6 parsing (#5263)

This commit is contained in:
Oron Sharabi 2020-06-29 17:39:17 +03:00 committed by GitHub
parent 06f34a1aed
commit 53f8d96a1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 3 deletions

View file

@ -237,6 +237,34 @@ export class Server implements AsyncIterable<ServerRequest> {
/** Options for creating an HTTP server. */ /** Options for creating an HTTP server. */
export type HTTPOptions = Omit<Deno.ListenOptions, "transport">; export type HTTPOptions = Omit<Deno.ListenOptions, "transport">;
/**
* Parse addr from string
*
* const addr = "::1:8000";
* parseAddrFromString(addr);
*
* @param addr Address string
*/
export function _parseAddrFromStr(addr: string): HTTPOptions {
let url: URL;
try {
url = new URL(`http://${addr}`);
} catch {
throw new TypeError("Invalid address.");
}
if (
url.username ||
url.password ||
url.pathname != "/" ||
url.search ||
url.hash
) {
throw new TypeError("Invalid address.");
}
return { hostname: url.hostname, port: Number(url.port) };
}
/** /**
* Create a HTTP server * Create a HTTP server
* *
@ -249,8 +277,7 @@ export type HTTPOptions = Omit<Deno.ListenOptions, "transport">;
*/ */
export function serve(addr: string | HTTPOptions): Server { export function serve(addr: string | HTTPOptions): Server {
if (typeof addr === "string") { if (typeof addr === "string") {
const [hostname, port] = addr.split(":"); addr = _parseAddrFromStr(addr);
addr = { hostname, port: Number(port) };
} }
const listener = Deno.listen(addr); const listener = Deno.listen(addr);

View file

@ -13,7 +13,14 @@ import {
assertStringContains, assertStringContains,
assertThrowsAsync, assertThrowsAsync,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
import { Response, ServerRequest, Server, serve, serveTLS } from "./server.ts"; import {
Response,
ServerRequest,
Server,
serve,
serveTLS,
_parseAddrFromStr,
} from "./server.ts";
import { BufReader, BufWriter } from "../io/bufio.ts"; import { BufReader, BufWriter } from "../io/bufio.ts";
import { delay } from "../async/delay.ts"; import { delay } from "../async/delay.ts";
import { encode, decode } from "../encoding/utf8.ts"; import { encode, decode } from "../encoding/utf8.ts";
@ -612,3 +619,43 @@ Deno.test({
} }
}, },
}); });
Deno.test({
name: "server.serve() should be able to parse IPV4 address",
fn: (): void => {
const server = serve("127.0.0.1:8124");
const expected = {
hostname: "127.0.0.1",
port: 8124,
transport: "tcp",
};
assertEquals(expected, server.listener.addr);
server.close();
},
});
Deno.test({
name: "server.parseAddrFromStr() should be able to parse IPV6 address",
fn: (): void => {
const addr = _parseAddrFromStr("[::1]:8124");
const expected = {
hostname: "[::1]",
port: 8124,
};
assertEquals(expected, addr);
},
});
Deno.test({
name: "server.serve() should be able to parse IPV6 address",
fn: (): void => {
const server = serve("[::1]:8124");
const expected = {
hostname: "::1",
port: 8124,
transport: "tcp",
};
assertEquals(expected, server.listener.addr);
server.close();
},
});