summaryrefslogtreecommitdiff
path: root/std/http/server.ts
diff options
context:
space:
mode:
authorOron Sharabi <oron@bigpanda.io>2020-06-29 17:39:17 +0300
committerGitHub <noreply@github.com>2020-06-29 10:39:17 -0400
commit53f8d96a1faf3b0f97e793b2e4ea86c64c12cbfa (patch)
treed0517ef072593265d8a96aaec8ad7cc3279f96b3 /std/http/server.ts
parent06f34a1aed2541866f32438d7bbc75e35c1d5dc3 (diff)
fix(std/http): Support ipv6 parsing (#5263)
Diffstat (limited to 'std/http/server.ts')
-rw-r--r--std/http/server.ts31
1 files changed, 29 insertions, 2 deletions
diff --git a/std/http/server.ts b/std/http/server.ts
index effa7b4b9..82e5f8169 100644
--- a/std/http/server.ts
+++ b/std/http/server.ts
@@ -238,6 +238,34 @@ export class Server implements AsyncIterable<ServerRequest> {
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
*
* import { serve } from "https://deno.land/std/http/server.ts";
@@ -249,8 +277,7 @@ export type HTTPOptions = Omit<Deno.ListenOptions, "transport">;
*/
export function serve(addr: string | HTTPOptions): Server {
if (typeof addr === "string") {
- const [hostname, port] = addr.split(":");
- addr = { hostname, port: Number(port) };
+ addr = _parseAddrFromStr(addr);
}
const listener = Deno.listen(addr);