mirror of
https://github.com/denoland/deno.git
synced 2024-10-29 08:58:01 -04:00
fix(std/http): Support ipv6 parsing (#5263)
This commit is contained in:
parent
06f34a1aed
commit
53f8d96a1f
2 changed files with 77 additions and 3 deletions
|
@ -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);
|
||||||
|
|
|
@ -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();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue