mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
fix(ext/http): include port number in h2 urls (#12181)
This commit is contained in:
parent
6c007aa5ab
commit
2b6f8d0187
4 changed files with 69 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
use crate::itest;
|
||||
use deno_core::url;
|
||||
use deno_runtime::deno_fetch::reqwest;
|
||||
use deno_runtime::deno_net::ops_tls::TlsStream;
|
||||
use deno_runtime::deno_tls::rustls;
|
||||
use deno_runtime::deno_tls::webpki;
|
||||
|
@ -1229,3 +1230,51 @@ async fn listen_tls_alpn_fail() {
|
|||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http2_request_url() {
|
||||
// TLS streams require the presence of an ambient local task set to gracefully
|
||||
// close dropped connections in the background.
|
||||
LocalSet::new()
|
||||
.run_until(async {
|
||||
let mut child = util::deno_cmd()
|
||||
.current_dir(util::testdata_path())
|
||||
.arg("run")
|
||||
.arg("--unstable")
|
||||
.arg("--quiet")
|
||||
.arg("--allow-net")
|
||||
.arg("--allow-read")
|
||||
.arg("./http2_request_url.ts")
|
||||
.arg("4506")
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let stdout = child.stdout.as_mut().unwrap();
|
||||
let mut buffer = [0; 5];
|
||||
let read = stdout.read(&mut buffer).unwrap();
|
||||
assert_eq!(read, 5);
|
||||
let msg = std::str::from_utf8(&buffer).unwrap();
|
||||
assert_eq!(msg, "READY");
|
||||
|
||||
let cert = reqwest::Certificate::from_pem(include_bytes!(
|
||||
"../testdata/tls/RootCA.crt"
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.add_root_certificate(cert)
|
||||
.http2_prior_knowledge()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let res = client.get("http://127.0.0.1:4506").send().await.unwrap();
|
||||
assert_eq!(200, res.status());
|
||||
|
||||
let body = res.text().await.unwrap();
|
||||
assert_eq!(body, "http://127.0.0.1:4506/");
|
||||
|
||||
child.kill().unwrap();
|
||||
child.wait().unwrap();
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
|
12
cli/tests/testdata/http2_request_url.ts
vendored
Normal file
12
cli/tests/testdata/http2_request_url.ts
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
const listener = Deno.listen({
|
||||
port: Number(Deno.args[0]),
|
||||
});
|
||||
|
||||
console.log("READY");
|
||||
|
||||
for await (const conn of listener) {
|
||||
for await (const { request, respondWith } of Deno.serveHttp(conn)) {
|
||||
const href = new URL(request.url).href;
|
||||
respondWith(new Response(href));
|
||||
}
|
||||
}
|
|
@ -607,7 +607,7 @@ unitTest(
|
|||
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(new URL(request.url).href, "http://127.0.0.1:4501/");
|
||||
assertEquals(await request.text(), "");
|
||||
respondWith(new Response());
|
||||
}
|
||||
|
|
|
@ -280,7 +280,13 @@ fn req_url(
|
|||
scheme: &'static str,
|
||||
addr: SocketAddr,
|
||||
) -> Result<String, AnyError> {
|
||||
let host: Cow<str> = if let Some(host) = req.uri().host() {
|
||||
let host: Cow<str> = if let Some(auth) = req.uri().authority() {
|
||||
match addr.port() {
|
||||
443 if scheme == "https" => Cow::Borrowed(auth.host()),
|
||||
80 if scheme == "http" => Cow::Borrowed(auth.host()),
|
||||
_ => Cow::Borrowed(auth.as_str()), // Includes port number.
|
||||
}
|
||||
} else if let Some(host) = req.uri().host() {
|
||||
Cow::Borrowed(host)
|
||||
} else if let Some(host) = req.headers().get("HOST") {
|
||||
Cow::Borrowed(host.to_str()?)
|
||||
|
|
Loading…
Add table
Reference in a new issue