Repository: vscode
Issue: Proxy agent patch prevents extensions from re-using HTTP connections
Labels: bug,proxy

<!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->
<!-- 🕮 Read our guide about submitting issues: https://github.com/microsoft/vscode/wiki/Submitting-Bugs-and-Suggestions -->
<!-- 🔎 Search existing issues to avoid creating duplicates. -->
<!-- 🧪 Test using the latest Insiders build to see if your issue has already been fixed: https://code.visualstudio.com/insiders/ -->
<!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->
<!-- 🔧 Launch with `code --disable-extensions` to check. -->
Does this issue occur when all extensions are disabled?: Yes

<!-- 🪓 If you answered No above, use 'Help: Start Extension Bisect' from Command Palette to try to identify the cause. -->
<!-- 📣 Issues caused by an extension need to be reported directly to the extension publisher. The 'Help > Report Issue' dialog can assist with this. -->
- VS Code Version: 1.75.0
- OS Version: Darwin x64 21.6.0

Steps to Reproduce:

1. Create a new extension and call `https.get` or `https.request` with an agent set to keep connections alive
2. Change your `http.proxySupport` setting to `override` (the default)
3. Observe that connections are not being reused. This has performance implications as creating sockets is not cheap.

The following code snippet should help demonstrate this problem with notes describing what conditions are needed for connections to persist:
```ts
const opt: https.RequestOptions = {
    host: 'example.com',
    port: 443,
    agent: new https.Agent({
        keepAlive: true,
        keepAliveMsecs: 60000,
    }),

    // Explicit headers because something is clobbering the agent's `keepAlive` option
    // Omitting this causes all connections to close regardless of `http.proxySupport`
    headers: { Connection: 'keep-alive' }
}

https.get(opt, r1 => {
    // Without explicit header in `opt` -> `close`
    // With explicit header in `opt` -> `undefined` (implies keep the connection alive)
    console.log('first request connection header ->', r1.headers.connection) 

    // Consume data so we can use the socket again
    r1.on('data', () => {}) 
    r1.on('end', () => {
        https.get(opt, r2 => {
            // `http.proxySupport` set to `override` -> always `false`
            // `http.proxySupport` set to `off` with no header -> `false`
            // `http.proxySupport` set to `off` AND explicit header -> `true`
            console.log('did reuse socket ->', (r2 as any).req.reusedSocket)
        })
    })
}) 
```

This issue likely originates from [here](https://github.com/microsoft/vscode-proxy-agent/blob/49c0f39c327ce408ce69247a3db10a2b313d44a2/src/index.ts#L331). I'm not entirely sure why an explicit header is needed, but it might be because the proxy patch bypasses logic that would normally set the header. [This line](https://github.com/microsoft/vscode-proxy-agent/blob/49c0f39c327ce408ce69247a3db10a2b313d44a2/src/agent.ts#L150) appears to be called even when `http.proxySupport` is set to `off`.