From 9e875b2a2353eb4f3c6343a22e19c01276571cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 28 Jun 2021 00:19:40 +0200 Subject: [PATCH] fix(http): remove unwrap() in HTTP bindings (#11130) --- cli/tests/unit/http_test.ts | 45 +++++++++++++++++++++++++++++++++++++ runtime/ops/http.rs | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index 0c7aec155a..ad78e1e440 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -586,3 +586,48 @@ unitTest({ perms: { net: true } }, async function httpRequestLatin1Headers() { await promise; }); + +unitTest( + { perms: { net: true } }, + async function httpServerRequestWithoutPath() { + const promise = (async () => { + const listener = Deno.listen({ port: 4501 }); + for await (const conn of listener) { + const httpConn = Deno.serveHttp(conn); + for await (const { request, respondWith } of httpConn) { + assertEquals(new URL(request.url).href, "http://127.0.0.1/"); + assertEquals(await request.text(), ""); + respondWith(new Response()); + } + break; + } + })(); + + const clientConn = await Deno.connect({ port: 4501 }); + + async function writeRequest(conn: Deno.Conn) { + const encoder = new TextEncoder(); + + const w = new BufWriter(conn); + const r = new BufReader(conn); + const body = + `CONNECT 127.0.0.1:4501 HTTP/1.1\r\nHost: 127.0.0.1:4501\r\n\r\n`; + const writeResult = await w.write(encoder.encode(body)); + assertEquals(body.length, writeResult); + await w.flush(); + const tpr = new TextProtoReader(r); + const statusLine = await tpr.readLine(); + assert(statusLine !== null); + const m = statusLine.match(/^(.+?) (.+?) (.+?)$/); + assert(m !== null, "must be matched"); + const [_, _proto, status, _ok] = m; + assertEquals(status, "200"); + const headers = await tpr.readMIMEHeader(); + assert(headers !== null); + } + + await writeRequest(clientConn); + clientConn.close(); + await promise; + }, +); diff --git a/runtime/ops/http.rs b/runtime/ops/http.rs index cd1ac9242b..01658c8020 100644 --- a/runtime/ops/http.rs +++ b/runtime/ops/http.rs @@ -222,7 +222,7 @@ async fn op_http_request_next( } else { Cow::Owned(conn_resource.addr.to_string()) }; - let path = req.uri().path_and_query().unwrap(); + let path = req.uri().path_and_query().map_or("/", |p| p.as_str()); format!("{}://{}{}", scheme, host, path) };