Skip to content

Commit 4695a5d

Browse files
committed
Fix bugs when using proxies with downstream HTTP/2
Without this, HTTPS HTTP/2 requests expect upstream to be HTTP/2, and so they convert headers etc to HTTP/2 format. With proxies, it's never HTTP/2, so all requests fail complaining about invalid HTTP/1 headers (:scheme et al).
1 parent 3feeba5 commit 4695a5d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/rules/requests/request-handlers.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,17 +1156,7 @@ export class PassThroughHandler extends Serializable implements RequestHandler {
11561156

11571157
// We only do H2 upstream for HTTPS. Http2-wrapper doesn't support H2C, it's rarely used
11581158
// and we can't use ALPN to detect HTTP/2 support cleanly.
1159-
const shouldTryH2Upstream = isH2Downstream && protocol === 'https:';
1160-
1161-
let makeRequest = (
1162-
shouldTryH2Upstream
1163-
? h2Client.auto
1164-
// HTTP/1 + TLS
1165-
: protocol === 'https:'
1166-
? https.request
1167-
// HTTP/1 plaintext:
1168-
: http.request
1169-
) as typeof https.request;
1159+
let shouldTryH2Upstream = isH2Downstream && protocol === 'https:';
11701160

11711161
const effectivePort = !!port
11721162
? parseInt(port, 10)
@@ -1192,6 +1182,21 @@ export class PassThroughHandler extends Serializable implements RequestHandler {
11921182
proxyConfig: this.proxyConfig
11931183
});
11941184

1185+
if (agent && !('http2' in agent)) {
1186+
// I.e. only use HTTP/2 if we're using an HTTP/2-compatible agent
1187+
shouldTryH2Upstream = false;
1188+
}
1189+
1190+
let makeRequest = (
1191+
shouldTryH2Upstream
1192+
? h2Client.auto
1193+
// HTTP/1 + TLS
1194+
: protocol === 'https:'
1195+
? https.request
1196+
// HTTP/1 plaintext:
1197+
: http.request
1198+
) as typeof https.request;
1199+
11951200
if (isH2Downstream && shouldTryH2Upstream) {
11961201
// We drop all incoming pseudoheaders, and regenerate them (except legally modified ones)
11971202
headers = _.pickBy(headers, (value, key) =>

0 commit comments

Comments
 (0)