summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-05-29 23:05:45 +0200
committerGitHub <noreply@github.com>2023-05-29 23:05:45 +0200
commitd90a75c0361e2d2bf45d6605cb0c7cb6d1a335a7 (patch)
tree107f9f3be7a17511b53b99a36084ab172c3a04b8
parentfc6ba92024d76d44349c36dcedd13994116db45b (diff)
fix: use proper ALPN protocols if HTTP client is HTTP/1.1 only (#19303)
Closes https://github.com/denoland/deno/issues/16923 --------- Co-authored-by: crowlkats <crowlkats@toaxl.com> Co-authored-by: Matt Mastracci <matthew@mastracci.com>
-rw-r--r--cli/tests/node_compat/config.jsonc3
-rw-r--r--cli/tests/node_compat/test/internet/test-http-https-default-ports.js46
-rw-r--r--cli/tests/unit_node/http_test.ts22
-rw-r--r--ext/fetch/lib.rs9
-rw-r--r--ext/node/polyfills/http.ts2
-rw-r--r--tools/node_compat/TODO.md3
6 files changed, 81 insertions, 4 deletions
diff --git a/cli/tests/node_compat/config.jsonc b/cli/tests/node_compat/config.jsonc
index ef2f4fccb..823bb9704 100644
--- a/cli/tests/node_compat/config.jsonc
+++ b/cli/tests/node_compat/config.jsonc
@@ -151,7 +151,8 @@
"test-dns-promises-resolve.js",
"test-dns-regress-6244.js",
"test-dns-setserver-in-callback-of-resolve4.js",
- "test-dns.js"
+ "test-dns.js",
+ "test-http-https-default-ports.js"
],
"parallel": [
"test-assert-async.js",
diff --git a/cli/tests/node_compat/test/internet/test-http-https-default-ports.js b/cli/tests/node_compat/test/internet/test-http-https-default-ports.js
new file mode 100644
index 000000000..521439ca8
--- /dev/null
+++ b/cli/tests/node_compat/test/internet/test-http-https-default-ports.js
@@ -0,0 +1,46 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+const common = require('../common');
+const { addresses } = require('../common/internet');
+
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+
+const https = require('https');
+
+const http = require('http');
+
+https.get(`https://${addresses.INET_HOST}/`, common.mustCall((res) => {
+ res.resume();
+}));
+
+http.get(`http://${addresses.INET_HOST}/`, common.mustCall((res) => {
+ res.resume();
+}));
diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts
index 5eb8c15bd..d1ed11632 100644
--- a/cli/tests/unit_node/http_test.ts
+++ b/cli/tests/unit_node/http_test.ts
@@ -2,6 +2,7 @@
import EventEmitter from "node:events";
import http, { type RequestOptions } from "node:http";
+import https from "node:https";
import {
assert,
assertEquals,
@@ -509,3 +510,24 @@ Deno.test("[node/http] ClientRequest handle non-string headers", async () => {
await def;
assertEquals(headers!["1"], "2");
});
+
+Deno.test("[node/http] ClientRequest uses HTTP/1.1", async () => {
+ let body = "";
+ const def = deferred();
+ const req = https.request("https://localhost:5545/http_version", {
+ method: "POST",
+ headers: { 1: 2 },
+ }, (resp) => {
+ resp.on("data", (chunk) => {
+ body += chunk;
+ });
+
+ resp.on("end", () => {
+ def.resolve();
+ });
+ });
+ req.once("error", (e) => def.reject(e));
+ req.end();
+ await def;
+ assertEquals(body, "HTTP/1.1");
+});
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs
index 9fdf80eec..a36512c77 100644
--- a/ext/fetch/lib.rs
+++ b/ext/fetch/lib.rs
@@ -745,7 +745,14 @@ pub fn create_http_client(
options.client_cert_chain_and_key,
)?;
- tls_config.alpn_protocols = vec!["h2".into(), "http/1.1".into()];
+ let mut alpn_protocols = vec![];
+ if options.http2 {
+ alpn_protocols.push("h2".into());
+ }
+ if options.http1 {
+ alpn_protocols.push("http/1.1".into());
+ }
+ tls_config.alpn_protocols = alpn_protocols;
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, user_agent.parse().unwrap());
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts
index 71186e4e7..3350e8f6e 100644
--- a/ext/node/polyfills/http.ts
+++ b/ext/node/polyfills/http.ts
@@ -641,6 +641,8 @@ class ClientRequest extends OutgoingMessage {
}
this._client.close();
const incoming = new IncomingMessageForClient(this.socket);
+ incoming.req = this;
+ this.res = incoming;
// TODO(@crowlKats):
// incoming.httpVersionMajor = versionMajor;
diff --git a/tools/node_compat/TODO.md b/tools/node_compat/TODO.md
index 3aff62668..ff8409a92 100644
--- a/tools/node_compat/TODO.md
+++ b/tools/node_compat/TODO.md
@@ -3,7 +3,7 @@
NOTE: This file should not be manually edited. Please edit 'cli/tests/node_compat/config.json' and run 'tools/node_compat/setup.ts' instead.
-Total: 2935
+Total: 2934
- [abort/test-abort-backtrace.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-backtrace.js)
- [abort/test-abort-fatal-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-fatal-error.js)
@@ -88,7 +88,6 @@ Total: 2935
- [internet/test-dns-cares-domains.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-dns-cares-domains.js)
- [internet/test-dns-txt-sigsegv.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-dns-txt-sigsegv.js)
- [internet/test-http-dns-fail.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-http-dns-fail.js)
-- [internet/test-http-https-default-ports.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-http-https-default-ports.js)
- [internet/test-http2-issue-32922.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-http2-issue-32922.js)
- [internet/test-https-issue-43963.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-https-issue-43963.js)
- [internet/test-inspector-help-page.js](https://github.com/nodejs/node/tree/v18.12.1/test/internet/test-inspector-help-page.js)