node/doc/api
Joyee Cheung 036b1fd66d
http,https: add built-in proxy support in http/https.request and Agent
This patch implements proxy support for HTTP and HTTPS clients and
agents in the `http` and `https` built-ins`. When NODE_USE_ENV_PROXY
is set to 1, the default global agent would parse the
HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
settings from the environment variables, and proxy the requests
sent through the built-in http/https client accordingly.

To support this, `http.Agent` and `https.Agent` now accept a few new
options:

- `proxyEnv`: when it's an object, the agent would read and parse
  the HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
  properties from it, and apply them based on the protocol it uses
  to send requests. This option allows custom agents to
  reuse built-in proxy support by composing options. Global agents
  set this to `process.env` when NODE_USE_ENV_PROXY is 1.
- `defaultPort` and `protocol`: these allow setting of the default port
  and protocol of the agents. We also need these when configuring
  proxy settings and deciding whether a request should be proxied.

Implementation-wise, this adds a `ProxyConfig` internal class to handle
parsing and application of proxy configurations. The configuration
is parsed during agent construction. When requests are made,
the `createConnection()` methods on the agents would check whether
the request should be proxied. If yes, they either connect to the
proxy server (in the case of HTTP reqeusts) or establish a tunnel
(in the case of HTTPS requests) through either a TCP socket (if the
proxy uses HTTP) or a TLS socket (if the proxy uses HTTPS).

When proxying HTTPS requests through a tunnel, the connection listener
is invoked after the tunnel is established. Tunnel establishment uses
the timeout of the request options, if there is one. Otherwise it uses
the timeout of the agent.

If an error is encountered during tunnel establishment, an
ERR_PROXY_TUNNEL would be emitted on the returned socket. If the proxy
server sends a errored status code, the error would contain an
`statusCode` property. If the error is caused by timeout, the error
would contain a `proxyTunnelTimeout` property.

This implementation honors the built-in socket pool and socket limits.
Pooled sockets are still keyed by request endpoints, they are just
connected to the proxy server instead, and the persistence of the
connection can be maintained as long as the proxy server respects
connection/proxy-connection or persist by default (HTTP/1.1)

PR-URL: https://github.com/nodejs/node/pull/58980
Refs: https://github.com/nodejs/node/issues/57872
Refs: https://github.com/nodejs/node/issues/8381
Refs: https://github.com/nodejs/node/issues/15620
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-07-18 09:06:32 +02:00
..
addons.md doc: fix c++ addon hello world sample 2024-12-09 17:43:50 +00:00
assert.md doc: add missing <code> blocks 2025-07-10 18:02:06 +00:00
async_context.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
async_hooks.md doc,tools: enforce use of node: prefix 2024-07-21 15:44:27 +00:00
buffer.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
child_process.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
cli.md doc: add stability index to the --watch-kill-signal flag 2025-07-10 18:57:31 +00:00
cluster.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
console.md doc: add a note on console stream behavior 2024-11-02 12:37:09 +00:00
corepack.md doc: clarify future Corepack removal in v25+ 2025-04-14 03:25:03 +00:00
crypto.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
debugger.md inspector: introduce the --inspect-wait flag 2024-05-11 18:48:30 +00:00
deprecations.md crypto: runtime-deprecate default shake128/256 output lengths 2025-07-11 14:21:46 +00:00
dgram.md 2025-06-09, Version 24.2.0 (Current) 2025-06-09 23:45:29 +02:00
diagnostics_channel.md http2: add diagnostics channel 'http2.server.stream.close' 2025-06-11 11:57:51 +00:00
dns.md dns: support max timeout 2025-07-10 15:50:43 +00:00
documentation.md doc: clarify removal of experimental API does not require a deprecation 2024-11-08 13:02:49 +00:00
domain.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
embedding.md doc: improve c++ embedder API doc 2024-11-01 03:43:53 +00:00
errors.md http,https: add built-in proxy support in http/https.request and Agent 2025-07-18 09:06:32 +02:00
esm.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
events.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
fs.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
globals.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
http.md http,https: add built-in proxy support in http/https.request and Agent 2025-07-18 09:06:32 +02:00
http2.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
https.md http,https: add built-in proxy support in http/https.request and Agent 2025-07-18 09:06:32 +02:00
index.md doc: remove link to QUIC.md 2025-04-04 12:53:31 +00:00
inspector.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
intl.md doc: improve documentation about ICU data fallback 2023-09-22 13:24:23 +00:00
module.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
modules.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
n-api.md doc: punctuation fix for Node-API versioning clarification 2025-06-12 21:12:33 +00:00
net.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
os.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
packages.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
path.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
perf_hooks.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
permissions.md doc: remove broken link to permission model source code 2025-07-08 05:22:36 +00:00
process.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
punycode.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
querystring.md doc: get rid of unnecessary eslint-skip comments 2023-11-22 19:03:33 +00:00
quic.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
readline.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
repl.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
report.md 2025-01-07, Version 22.13.0 'Jod' (LTS) 2025-01-07 14:36:47 -05:00
single-executable-applications.md doc: make MDN links to global classes more consistent 2025-02-07 14:33:06 +00:00
sqlite.md doc: add missing section for setReturnArrays in sqlite.md 2025-07-17 17:57:08 +02:00
stream.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
string_decoder.md doc: add esm examples to node:string_decoder 2024-10-26 20:36:25 +00:00
synopsis.md doc: clarify explaination text on how to run the example 2023-10-06 13:37:31 +00:00
test.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
timers.md 2025-06-09, Version 24.2.0 (Current) 2025-06-09 23:45:29 +02:00
tls.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
tracing.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
tty.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
typescript.md 2025-06-24, Version 24.3.0 (Current) 2025-06-24 19:39:30 -03:00
url.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
util.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
v8.md 2025-06-24, Version 22.17.0 'Jod' (LTS) 2025-06-25 02:08:38 +02:00
vm.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
wasi.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
webcrypto.md 2025-06-23, Version 20.19.3 'Iron' (LTS) 2025-06-23 10:32:15 +02:00
webstreams.md doc: add node.js streams references to Web Streams doc 2025-03-18 19:22:10 +00:00
worker_threads.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00
zlib.md doc: fix typed list formatting 2025-07-15 17:18:42 +00:00