Skip to content

Commit

Permalink
[fix] handle incoming requests from HTTP/2 (#3572)
Browse files Browse the repository at this point in the history
* strip out HTTP/2 headers in getRequest

* remove workaround forcing Vite to use HTTP/1 for dev mode

* add changeset
  • Loading branch information
Conduitry authored Jan 27, 2022
1 parent 80233c2 commit b39b67f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-readers-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix handling an incoming request from HTTP/2
8 changes: 0 additions & 8 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ export async function dev({ cwd, port, host, https, config }) {
merged_config.server.https = https;
}

// by default, when enabling HTTPS in Vite, it also enables HTTP/2
// however, node-fetch's Request implementation does not like the HTTP/2 headers
// we set a no-op proxy config to force Vite to downgrade to TLS-only
// see https://vitejs.dev/config/#server-https
if (merged_config.server.https && !merged_config.server.proxy) {
merged_config.server.proxy = {};
}

if (port) {
merged_config.server.port = port;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/kit/src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,19 @@ function get_raw_body(req) {

/** @type {import('@sveltejs/kit/node').GetRequest} */
export async function getRequest(base, req) {
let headers = /** @type {Record<string, string>} */ (req.headers);
if (req.httpVersionMajor === 2) {
// we need to strip out the HTTP/2 pseudo-headers because node-fetch's
// Request implementation doesn't like them
headers = Object.assign({}, headers);
delete headers[':method'];
delete headers[':path'];
delete headers[':authority'];
delete headers[':scheme'];
}
return new Request(base + req.url, {
method: req.method,
headers: /** @type {Record<string, string>} */ (req.headers),
headers,
body: await get_raw_body(req) // TODO stream rather than buffer
});
}
Expand Down

0 comments on commit b39b67f

Please sign in to comment.