From edbfff7b04baffc29c01c37c595d6b3355c0dea0 Mon Sep 17 00:00:00 2001 From: Tao Date: Thu, 27 Apr 2023 19:37:52 +0800 Subject: [PATCH] fix: add Host header when used with http_proxy (#10080) Co-authored-by: Alex Rudenko --- packages/browsers/src/httpUtil.ts | 2 ++ packages/browsers/test/src/chrome/install.spec.ts | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/packages/browsers/src/httpUtil.ts b/packages/browsers/src/httpUtil.ts index ea256489..5b6150f7 100644 --- a/packages/browsers/src/httpUtil.ts +++ b/packages/browsers/src/httpUtil.ts @@ -61,6 +61,8 @@ export function httpRequest( options.hostname = proxy.hostname; options.protocol = proxy.protocol; options.port = proxy.port; + options.headers ??= {}; + options.headers['Host'] ||= url.host; } else { options.agent = createHttpsProxyAgent({ host: proxy.host, diff --git a/packages/browsers/test/src/chrome/install.spec.ts b/packages/browsers/test/src/chrome/install.spec.ts index 8ae8960e..7dc3e7eb 100644 --- a/packages/browsers/test/src/chrome/install.spec.ts +++ b/packages/browsers/test/src/chrome/install.spec.ts @@ -136,9 +136,11 @@ describe('Chrome install', () => { const proxyUrl = new URL(`http://localhost:54321`); let proxyServer: http.Server; let proxiedRequestUrls: string[] = []; + let proxiedRequestHosts: string[] = []; beforeEach(() => { proxiedRequestUrls = []; + proxiedRequestHosts = []; proxyServer = http .createServer( ( @@ -164,6 +166,7 @@ describe('Chrome install', () => { ); originalRequest.pipe(proxyRequest, {end: true}); proxiedRequestUrls.push(url); + proxiedRequestHosts.push(originalRequest.headers?.host || ''); } ) .listen({ @@ -203,6 +206,9 @@ describe('Chrome install', () => { assert.deepStrictEqual(proxiedRequestUrls, [ getServerUrl() + '/113.0.5672.0/linux64/chrome-linux64.zip', ]); + assert.deepStrictEqual(proxiedRequestHosts, [ + getServerUrl().replace('http://', ''), + ]); }); it('can download via a proxy', async function () { @@ -225,6 +231,9 @@ describe('Chrome install', () => { assert.deepStrictEqual(proxiedRequestUrls, [ getServerUrl() + '/113.0.5672.0/linux64/chrome-linux64.zip', ]); + assert.deepStrictEqual(proxiedRequestHosts, [ + getServerUrl().replace('http://', ''), + ]); }); }); });