Skip to content

Commit 0071756

Browse files
committed
cherry-pick(#36654): Revert "fix: get rid of url.parse in network code" (#36654)
1 parent 3da07a7 commit 0071756

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

packages/playwright-core/src/server/utils/network.ts

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ export type HTTPRequestParams = {
3939
export const NET_DEFAULT_TIMEOUT = 30_000;
4040

4141
export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.IncomingMessage) => void, onError: (error: Error) => void): { cancel(error: Error | undefined): void } {
42-
const parsedUrl = new URL(params.url);
43-
const options: https.RequestOptions = {
42+
const parsedUrl = url.parse(params.url);
43+
let options: https.RequestOptions = {
44+
...parsedUrl,
4445
agent: parsedUrl.protocol === 'https:' ? httpsHappyEyeballsAgent : httpHappyEyeballsAgent,
4546
method: params.method || 'GET',
4647
headers: params.headers,
@@ -50,15 +51,19 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
5051

5152
const proxyURL = getProxyForUrl(params.url);
5253
if (proxyURL) {
53-
const parsedProxyURL = new URL(proxyURL);
54+
const parsedProxyURL = url.parse(proxyURL);
5455
if (params.url.startsWith('http:')) {
55-
parsedUrl.pathname = parsedUrl.href;
56-
parsedUrl.host = parsedProxyURL.host;
56+
options = {
57+
path: parsedUrl.href,
58+
host: parsedProxyURL.hostname,
59+
port: parsedProxyURL.port,
60+
headers: options.headers,
61+
method: options.method
62+
};
5763
} else {
58-
options.agent = new HttpsProxyAgent({
59-
...convertURLtoLegacyUrl(parsedProxyURL),
60-
secureProxy: parsedProxyURL.protocol === 'https:',
61-
});
64+
(parsedProxyURL as any).secureProxy = parsedProxyURL.protocol === 'https:';
65+
66+
options.agent = new HttpsProxyAgent(parsedProxyURL);
6267
options.rejectUnauthorized = false;
6368
}
6469
}
@@ -76,8 +81,8 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
7681
}
7782
};
7883
const request = options.protocol === 'https:' ?
79-
https.request(parsedUrl, options, requestCallback) :
80-
http.request(parsedUrl, options, requestCallback);
84+
https.request(options, requestCallback) :
85+
http.request(options, requestCallback);
8186
request.on('error', onError);
8287
if (params.socketTimeout !== undefined) {
8388
request.setTimeout(params.socketTimeout, () => {
@@ -132,27 +137,23 @@ export function createProxyAgent(proxy?: ProxySettings, forUrl?: URL) {
132137
if (!/^\w+:\/\//.test(proxyServer))
133138
proxyServer = 'http://' + proxyServer;
134139

135-
const proxyOpts = new URL(proxyServer);
140+
const proxyOpts = url.parse(proxyServer);
136141
if (proxyOpts.protocol?.startsWith('socks')) {
137142
return new SocksProxyAgent({
138143
host: proxyOpts.hostname,
139144
port: proxyOpts.port || undefined,
140145
});
141146
}
142-
if (proxy.username) {
143-
proxyOpts.username = proxy.username;
144-
proxyOpts.password = proxy.password || '';
145-
}
147+
if (proxy.username)
148+
proxyOpts.auth = `${proxy.username}:${proxy.password || ''}`;
146149

147150
if (forUrl && ['ws:', 'wss:'].includes(forUrl.protocol)) {
148151
// Force CONNECT method for WebSockets.
149-
// TODO: switch to URL instance instead of legacy object once https-proxy-agent supports it.
150-
return new HttpsProxyAgent(convertURLtoLegacyUrl(proxyOpts));
152+
return new HttpsProxyAgent(proxyOpts);
151153
}
152154

153155
// TODO: We should use HttpProxyAgent conditional on proxyOpts.protocol instead of always using CONNECT method.
154-
// TODO: switch to URL instance instead of legacy object once https-proxy-agent supports it.
155-
return new HttpsProxyAgent(convertURLtoLegacyUrl(proxyOpts));
156+
return new HttpsProxyAgent(proxyOpts);
156157
}
157158

158159
export function createHttpServer(requestListener?: (req: http.IncomingMessage, res: http.ServerResponse) => void): http.Server;
@@ -225,20 +226,3 @@ function decorateServer(server: net.Server) {
225226
return close.call(server, callback);
226227
};
227228
}
228-
229-
function convertURLtoLegacyUrl(url: URL): url.Url {
230-
return {
231-
auth: url.username ? url.username + ':' + url.password : null,
232-
hash: url.hash || null,
233-
host: url.hostname ? url.hostname + ':' + url.port : null,
234-
hostname: url.hostname || null,
235-
href: url.href,
236-
path: url.pathname + url.search,
237-
pathname: url.pathname,
238-
protocol: url.protocol,
239-
search: url.search || null,
240-
slashes: true,
241-
port: url.port || null,
242-
query: url.search.slice(1) || null,
243-
};
244-
}

tests/installation/playwright-cli-install-should-work.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import { test, expect } from './npmTest';
1717
import { chromium } from '@playwright/test';
1818
import path from 'path';
19+
import { TestProxy } from '../config/proxy';
1920

2021
test.use({ isolateBrowsers: true });
2122

@@ -60,6 +61,22 @@ test('install command should work', async ({ exec, checkInstalledSoftwareOnDisk
6061
}
6162
});
6263

64+
test('install command should work with proxy', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36650' } }, async ({ exec, checkInstalledSoftwareOnDisk }) => {
65+
await exec('npm i playwright');
66+
const proxy = await TestProxy.create(8947 + test.info().workerIndex * 4);
67+
proxy.forwardTo(443, { preserveHostname: true });
68+
await test.step('playwright install chromium', async () => {
69+
const result = await exec('npx playwright install chromium', {
70+
env: {
71+
HTTPS_PROXY: proxy.URL,
72+
}
73+
});
74+
expect(result).toHaveLoggedSoftwareDownload(['chromium', 'chromium-headless-shell', 'ffmpeg', ...extraInstalledSoftware]);
75+
await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', ...extraInstalledSoftware]);
76+
});
77+
await proxy.stop();
78+
});
79+
6380
test('should be able to remove browsers', async ({ exec, checkInstalledSoftwareOnDisk }) => {
6481
await exec('npm i playwright');
6582
await exec('npx playwright install chromium');

0 commit comments

Comments
 (0)