2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
|
// following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
2020-11-17 13:10:49 +01:00
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
|
ArrayPrototypePush,
|
|
|
|
|
ArrayPrototypeShift,
|
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
|
ArrayPrototypeUnshift,
|
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
|
JSONStringify,
|
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-02 01:17:07 +02:00
|
|
|
NumberParseInt,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectAssign,
|
|
|
|
|
ObjectSetPrototypeOf,
|
2023-06-12 10:47:25 +03:00
|
|
|
ReflectApply,
|
2020-11-17 13:10:49 +01:00
|
|
|
ReflectConstruct,
|
2024-10-07 11:47:44 +02:00
|
|
|
SymbolAsyncDispose,
|
2019-11-22 18:04:46 +01:00
|
|
|
} = primordials;
|
2019-04-09 09:55:53 +02:00
|
|
|
|
2022-05-21 17:53:17 +08:00
|
|
|
const {
|
|
|
|
|
assertCrypto,
|
|
|
|
|
kEmptyObject,
|
2023-06-25 23:29:58 +03:00
|
|
|
promisify,
|
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-02 01:17:07 +02:00
|
|
|
once,
|
2022-05-21 17:53:17 +08:00
|
|
|
} = require('internal/util');
|
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-02 01:17:07 +02:00
|
|
|
const { ERR_PROXY_TUNNEL } = require('internal/errors').codes;
|
2022-05-21 17:53:17 +08:00
|
|
|
assertCrypto();
|
2016-03-08 15:31:31 -08:00
|
|
|
|
2015-01-21 11:36:59 -05:00
|
|
|
const tls = require('tls');
|
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-02 01:17:07 +02:00
|
|
|
const {
|
|
|
|
|
kProxyConfig,
|
|
|
|
|
checkShouldUseProxy,
|
|
|
|
|
filterEnvForProxies,
|
|
|
|
|
kWaitForProxyTunnel,
|
|
|
|
|
} = require('internal/http');
|
2017-10-22 17:23:43 -07:00
|
|
|
const { Agent: HttpAgent } = require('_http_agent');
|
|
|
|
|
const {
|
2023-06-12 10:47:25 +03:00
|
|
|
httpServerPreClose,
|
2017-10-22 17:23:43 -07:00
|
|
|
Server: HttpServer,
|
2022-04-13 16:47:59 +02:00
|
|
|
setupConnectionsTracking,
|
2021-06-17 22:29:03 +08:00
|
|
|
storeHTTPOptions,
|
2018-05-31 14:26:31 -07:00
|
|
|
_connectionListener,
|
2017-10-22 17:23:43 -07:00
|
|
|
} = require('_http_server');
|
|
|
|
|
const { ClientRequest } = require('_http_client');
|
2020-03-14 07:55:44 -04:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('https', (fn) => {
|
|
|
|
|
debug = fn;
|
|
|
|
|
});
|
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-02 01:17:07 +02:00
|
|
|
const net = require('net');
|
2023-02-27 09:31:18 -05:00
|
|
|
const { URL, urlToHttpOptions, isURL } = require('internal/url');
|
2023-01-25 17:52:33 +09:00
|
|
|
const { validateObject } = require('internal/validators');
|
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-02 01:17:07 +02:00
|
|
|
const { isIP, isIPv6 } = require('internal/net');
|
|
|
|
|
const assert = require('internal/assert');
|
2025-07-26 22:43:10 +02:00
|
|
|
const { getOptionValue } = require('internal/options');
|
2011-01-02 01:13:56 -08:00
|
|
|
|
|
|
|
|
function Server(opts, requestListener) {
|
|
|
|
|
if (!(this instanceof Server)) return new Server(opts, requestListener);
|
2011-04-14 10:53:39 +07:00
|
|
|
|
2024-08-31 23:04:10 -04:00
|
|
|
let ALPNProtocols = ['http/1.1'];
|
2017-06-10 14:09:35 -04:00
|
|
|
if (typeof opts === 'function') {
|
|
|
|
|
requestListener = opts;
|
2023-01-25 17:52:33 +09:00
|
|
|
opts = kEmptyObject;
|
|
|
|
|
} else if (opts == null) {
|
|
|
|
|
opts = kEmptyObject;
|
|
|
|
|
} else {
|
|
|
|
|
validateObject(opts, 'options');
|
2024-08-31 23:04:10 -04:00
|
|
|
// Only one of ALPNProtocols and ALPNCallback can be set, so make sure we
|
|
|
|
|
// only set a default ALPNProtocols if the caller has not set either of them
|
|
|
|
|
if (opts.ALPNProtocols || opts.ALPNCallback)
|
|
|
|
|
ALPNProtocols = undefined;
|
2015-04-23 15:25:15 +09:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 22:29:03 +08:00
|
|
|
FunctionPrototypeCall(storeHTTPOptions, this, opts);
|
2023-01-25 17:52:33 +09:00
|
|
|
FunctionPrototypeCall(tls.Server, this,
|
|
|
|
|
{
|
|
|
|
|
noDelay: true,
|
2024-08-31 23:04:10 -04:00
|
|
|
ALPNProtocols,
|
2023-01-25 17:52:33 +09:00
|
|
|
...opts,
|
|
|
|
|
},
|
|
|
|
|
_connectionListener);
|
2011-01-02 01:13:56 -08:00
|
|
|
|
2011-02-07 21:11:43 -08:00
|
|
|
this.httpAllowHalfOpen = false;
|
|
|
|
|
|
2011-01-02 01:13:56 -08:00
|
|
|
if (requestListener) {
|
|
|
|
|
this.addListener('request', requestListener);
|
|
|
|
|
}
|
2012-10-08 01:22:44 +02:00
|
|
|
|
2016-10-21 03:21:47 +00:00
|
|
|
this.addListener('tlsClientError', function addListener(err, conn) {
|
2016-01-06 17:00:27 -05:00
|
|
|
if (!this.emit('clientError', err, conn))
|
|
|
|
|
conn.destroy(err);
|
2012-10-08 01:22:44 +02:00
|
|
|
});
|
2013-04-30 12:43:32 +02:00
|
|
|
|
2019-05-03 16:45:57 -07:00
|
|
|
this.timeout = 0;
|
2018-04-23 03:48:41 +09:00
|
|
|
this.maxHeadersCount = null;
|
2023-07-24 22:55:19 +02:00
|
|
|
this.on('listening', setupConnectionsTracking);
|
2011-01-02 01:13:56 -08:00
|
|
|
}
|
2023-07-24 22:55:19 +02:00
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf(Server.prototype, tls.Server.prototype);
|
|
|
|
|
ObjectSetPrototypeOf(Server, tls.Server);
|
2011-01-02 01:13:56 -08:00
|
|
|
|
2022-04-28 12:05:55 +02:00
|
|
|
Server.prototype.closeAllConnections = HttpServer.prototype.closeAllConnections;
|
|
|
|
|
|
|
|
|
|
Server.prototype.closeIdleConnections = HttpServer.prototype.closeIdleConnections;
|
|
|
|
|
|
2017-10-22 17:23:43 -07:00
|
|
|
Server.prototype.setTimeout = HttpServer.prototype.setTimeout;
|
2011-01-02 01:13:56 -08:00
|
|
|
|
2025-05-12 21:28:05 +09:00
|
|
|
Server.prototype.close = function close() {
|
2023-06-12 10:47:25 +03:00
|
|
|
httpServerPreClose(this);
|
|
|
|
|
ReflectApply(tls.Server.prototype.close, this, arguments);
|
2024-02-27 00:07:39 +08:00
|
|
|
return this;
|
2023-06-12 10:47:25 +03:00
|
|
|
};
|
|
|
|
|
|
2023-06-25 23:29:58 +03:00
|
|
|
Server.prototype[SymbolAsyncDispose] = async function() {
|
2025-05-25 21:44:43 +08:00
|
|
|
await FunctionPrototypeCall(promisify(this.close), this);
|
2023-06-25 23:29:58 +03:00
|
|
|
};
|
|
|
|
|
|
2021-05-08 04:57:48 +04:30
|
|
|
/**
|
|
|
|
|
* Creates a new `https.Server` instance.
|
|
|
|
|
* @param {{
|
|
|
|
|
* IncomingMessage?: IncomingMessage;
|
|
|
|
|
* ServerResponse?: ServerResponse;
|
|
|
|
|
* insecureHTTPParser?: boolean;
|
|
|
|
|
* maxHeaderSize?: number;
|
|
|
|
|
* }} [opts]
|
|
|
|
|
* @param {Function} [requestListener]
|
|
|
|
|
* @returns {Server}
|
|
|
|
|
*/
|
2017-10-22 17:23:43 -07:00
|
|
|
function createServer(opts, requestListener) {
|
2011-01-02 01:13:56 -08:00
|
|
|
return new Server(opts, requestListener);
|
2017-10-22 17:23:43 -07:00
|
|
|
}
|
2011-01-21 13:12:35 -08:00
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
// When proxying a HTTPS request, the following needs to be done:
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc9110#CONNECT
|
|
|
|
|
// 1. Send a CONNECT request to the proxy server.
|
|
|
|
|
// 2. Wait for 200 connection established response to establish the tunnel.
|
|
|
|
|
// 3. Perform TLS handshake with the endpoint over the socket.
|
|
|
|
|
// 4. Tunnel the request using the established connection.
|
|
|
|
|
//
|
|
|
|
|
// This function computes the tunnel configuration for HTTPS requests.
|
|
|
|
|
// The handling of the tunnel connection is done in createConnection.
|
|
|
|
|
function getTunnelConfigForProxiedHttps(agent, reqOptions) {
|
|
|
|
|
if (!agent[kProxyConfig]) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if ((reqOptions.protocol || agent.protocol) !== 'https:') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
const shouldUseProxy = checkShouldUseProxy(agent[kProxyConfig], reqOptions);
|
|
|
|
|
debug(`getTunnelConfigForProxiedHttps should use proxy for ${reqOptions.host}:${reqOptions.port}:`, shouldUseProxy);
|
|
|
|
|
if (!shouldUseProxy) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
const { auth, href } = agent[kProxyConfig];
|
|
|
|
|
// The request is a HTTPS request, assemble the payload for establishing the tunnel.
|
|
|
|
|
const requestHost = isIPv6(reqOptions.host) ? `[${reqOptions.host}]` : reqOptions.host;
|
|
|
|
|
const requestPort = reqOptions.port || agent.defaultPort;
|
|
|
|
|
const endpoint = `${requestHost}:${requestPort}`;
|
|
|
|
|
// The ClientRequest constructor should already have validated the host and the port.
|
|
|
|
|
// When the request options come from a string invalid characters would be stripped away,
|
|
|
|
|
// when it's an object ERR_INVALID_CHAR would be thrown. Here we just assert in case
|
|
|
|
|
// agent.createConnection() is called with invalid options.
|
|
|
|
|
assert(!endpoint.includes('\r'));
|
|
|
|
|
assert(!endpoint.includes('\n'));
|
|
|
|
|
|
|
|
|
|
let payload = `CONNECT ${endpoint} HTTP/1.1\r\n`;
|
|
|
|
|
// The parseProxyConfigFromEnv() method should have already validated the authorization header
|
|
|
|
|
// value.
|
|
|
|
|
if (auth) {
|
|
|
|
|
payload += `proxy-authorization: ${auth}\r\n`;
|
|
|
|
|
}
|
|
|
|
|
if (agent.keepAlive || agent.maxSockets !== Infinity) {
|
|
|
|
|
payload += 'proxy-connection: keep-alive\r\n';
|
|
|
|
|
}
|
|
|
|
|
payload += `host: ${endpoint}`;
|
|
|
|
|
payload += '\r\n\r\n';
|
|
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
proxyTunnelPayload: payload,
|
|
|
|
|
requestOptions: { // Options used for the request sent after the tunnel is established.
|
|
|
|
|
__proto__: null,
|
|
|
|
|
servername: reqOptions.servername || (isIP(reqOptions.host) ? undefined : reqOptions.host),
|
|
|
|
|
...reqOptions,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
debug(`updated request for HTTPS proxy ${href} with`, result);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
2011-01-21 13:12:35 -08:00
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
function establishTunnel(agent, socket, options, tunnelConfig, afterSocket) {
|
|
|
|
|
const { proxyTunnelPayload } = tunnelConfig;
|
|
|
|
|
// By default, the socket is in paused mode. Read to look for the 200
|
|
|
|
|
// connection established response.
|
|
|
|
|
function read() {
|
|
|
|
|
let chunk;
|
|
|
|
|
while ((chunk = socket.read()) !== null) {
|
|
|
|
|
if (onProxyData(chunk) !== -1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
socket.on('readable', read);
|
|
|
|
|
}
|
2011-10-04 20:51:34 +02:00
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
function cleanup() {
|
|
|
|
|
socket.removeListener('end', onProxyEnd);
|
|
|
|
|
socket.removeListener('error', onProxyError);
|
|
|
|
|
socket.removeListener('readable', read);
|
|
|
|
|
socket.setTimeout(0); // Clear the timeout for the tunnel establishment.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onProxyError(err) {
|
|
|
|
|
debug('onProxyError', err);
|
|
|
|
|
cleanup();
|
|
|
|
|
afterSocket(err, socket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the headers from the chunks and check for the status code. If it fails we
|
|
|
|
|
// clean up the socket and return an error. Otherwise we establish the tunnel.
|
|
|
|
|
let buffer = '';
|
|
|
|
|
function onProxyData(chunk) {
|
|
|
|
|
const str = chunk.toString();
|
|
|
|
|
debug('onProxyData', str);
|
|
|
|
|
buffer += str;
|
|
|
|
|
const headerEndIndex = buffer.indexOf('\r\n\r\n');
|
|
|
|
|
if (headerEndIndex === -1) return headerEndIndex;
|
|
|
|
|
const statusLine = buffer.substring(0, buffer.indexOf('\r\n'));
|
|
|
|
|
const statusCode = statusLine.split(' ')[1];
|
|
|
|
|
if (statusCode !== '200') {
|
|
|
|
|
debug(`onProxyData receives ${statusCode}, cleaning up`);
|
|
|
|
|
cleanup();
|
|
|
|
|
const targetHost = proxyTunnelPayload.split('\r')[0].split(' ')[1];
|
|
|
|
|
const message = `Failed to establish tunnel to ${targetHost} via ${agent[kProxyConfig].href}: ${statusLine}`;
|
|
|
|
|
const err = new ERR_PROXY_TUNNEL(message);
|
|
|
|
|
err.statusCode = NumberParseInt(statusCode);
|
|
|
|
|
afterSocket(err, socket);
|
|
|
|
|
} else {
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc9110#CONNECT
|
|
|
|
|
// RFC 9110 says that it can be 2xx but in the real world, proxy clients generally only
|
|
|
|
|
// accepts 200.
|
|
|
|
|
// Proxy servers are not supposed to send anything after the headers - the payload must be
|
|
|
|
|
// be empty. So after this point we will proceed with the tunnel e.g. starting TLS handshake.
|
|
|
|
|
debug('onProxyData receives 200, establishing tunnel');
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
|
|
// Reuse the tunneled socket to perform the TLS handshake with the endpoint,
|
|
|
|
|
// then send the request.
|
|
|
|
|
const { requestOptions } = tunnelConfig;
|
|
|
|
|
tunnelConfig.requestOptions = null;
|
|
|
|
|
requestOptions.socket = socket;
|
|
|
|
|
let tunneldSocket;
|
|
|
|
|
const onTLSHandshakeError = (err) => {
|
|
|
|
|
debug('Propagate error event from tunneled socket to tunnel socket');
|
|
|
|
|
afterSocket(err, tunneldSocket);
|
|
|
|
|
};
|
|
|
|
|
tunneldSocket = tls.connect(requestOptions, () => {
|
|
|
|
|
debug('TLS handshake over tunnel succeeded');
|
|
|
|
|
tunneldSocket.removeListener('error', onTLSHandshakeError);
|
|
|
|
|
afterSocket(null, tunneldSocket);
|
|
|
|
|
});
|
|
|
|
|
tunneldSocket.on('free', () => {
|
|
|
|
|
debug('Propagate free event from tunneled socket to tunnel socket');
|
|
|
|
|
socket.emit('free');
|
|
|
|
|
});
|
|
|
|
|
tunneldSocket.on('error', onTLSHandshakeError);
|
|
|
|
|
}
|
|
|
|
|
return headerEndIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onProxyEnd() {
|
|
|
|
|
cleanup();
|
|
|
|
|
const err = new ERR_PROXY_TUNNEL('Connection to establish proxy tunnel ended unexpectedly');
|
|
|
|
|
afterSocket(err, socket);
|
2012-02-23 17:37:49 +02:00
|
|
|
}
|
2012-12-28 12:40:06 +09:00
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
const proxyTunnelTimeout = tunnelConfig.requestOptions.timeout;
|
|
|
|
|
debug('proxyTunnelTimeout', proxyTunnelTimeout, options.timeout);
|
|
|
|
|
// It may be worth a separate timeout error/event.
|
|
|
|
|
// But it also makes sense to treat the tunnel establishment timeout as
|
|
|
|
|
// a normal timeout for the request.
|
|
|
|
|
function onProxyTimeout() {
|
|
|
|
|
debug('onProxyTimeout', proxyTunnelTimeout);
|
|
|
|
|
cleanup();
|
|
|
|
|
const err = new ERR_PROXY_TUNNEL(`Connection to establish proxy tunnel timed out after ${proxyTunnelTimeout}ms`);
|
|
|
|
|
err.proxyTunnelTimeout = proxyTunnelTimeout;
|
|
|
|
|
afterSocket(err, socket);
|
2012-12-28 12:40:06 +09:00
|
|
|
}
|
|
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
if (proxyTunnelTimeout && proxyTunnelTimeout > 0) {
|
|
|
|
|
debug('proxy tunnel setTimeout', proxyTunnelTimeout);
|
|
|
|
|
socket.setTimeout(proxyTunnelTimeout, onProxyTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.on('error', onProxyError);
|
|
|
|
|
socket.on('end', onProxyEnd);
|
|
|
|
|
socket.write(proxyTunnelPayload);
|
|
|
|
|
|
|
|
|
|
read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HTTPS agents.
|
|
|
|
|
// See ProxyConfig in internal/http.js for how the connection should be handled
|
|
|
|
|
// when the agent is configured to use a proxy server.
|
|
|
|
|
function createConnection(...args) {
|
|
|
|
|
// XXX: This signature (port, host, options) is different from all the other
|
|
|
|
|
// createConnection() methods.
|
|
|
|
|
let options, cb;
|
|
|
|
|
if (args[0] !== null && typeof args[0] === 'object') {
|
|
|
|
|
options = args[0];
|
|
|
|
|
} else if (args[1] !== null && typeof args[1] === 'object') {
|
|
|
|
|
options = { ...args[1] };
|
|
|
|
|
} else if (args[2] === null || typeof args[2] !== 'object') {
|
|
|
|
|
options = {};
|
|
|
|
|
} else {
|
|
|
|
|
options = { ...args[2] };
|
|
|
|
|
}
|
|
|
|
|
if (typeof args[0] === 'number') {
|
|
|
|
|
options.port = args[0];
|
|
|
|
|
}
|
|
|
|
|
if (typeof args[1] === 'string') {
|
|
|
|
|
options.host = args[1];
|
|
|
|
|
}
|
|
|
|
|
if (typeof args[args.length - 1] === 'function') {
|
|
|
|
|
cb = args[args.length - 1];
|
2012-12-28 12:40:06 +09:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 18:44:24 -07:00
|
|
|
debug('createConnection', options);
|
2015-07-22 21:18:38 -07:00
|
|
|
|
|
|
|
|
if (options._agentKey) {
|
|
|
|
|
const session = this._getSession(options._agentKey);
|
|
|
|
|
if (session) {
|
|
|
|
|
debug('reuse session for %j', options._agentKey);
|
2018-12-18 03:15:57 +01:00
|
|
|
options = {
|
|
|
|
|
session,
|
2023-02-12 19:26:21 +01:00
|
|
|
...options,
|
2018-12-18 03:15:57 +01:00
|
|
|
};
|
2015-07-22 21:18:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
let socket;
|
|
|
|
|
const tunnelConfig = getTunnelConfigForProxiedHttps(this, options);
|
|
|
|
|
debug(`https createConnection should use proxy for ${options.host}:${options.port}:`, tunnelConfig);
|
|
|
|
|
|
|
|
|
|
if (!tunnelConfig) {
|
|
|
|
|
socket = tls.connect(options);
|
|
|
|
|
} else {
|
|
|
|
|
const connectOptions = {
|
|
|
|
|
...this[kProxyConfig].proxyConnectionOptions,
|
|
|
|
|
};
|
|
|
|
|
debug('Create proxy socket', connectOptions);
|
|
|
|
|
const onError = (err) => {
|
|
|
|
|
cleanupAndPropagate(err, socket);
|
|
|
|
|
};
|
|
|
|
|
const proxyTunnelTimeout = tunnelConfig.requestOptions.timeout;
|
|
|
|
|
const onTimeout = () => {
|
|
|
|
|
const err = new ERR_PROXY_TUNNEL(`Connection to establish proxy tunnel timed out after ${proxyTunnelTimeout}ms`);
|
|
|
|
|
err.proxyTunnelTimeout = proxyTunnelTimeout;
|
|
|
|
|
cleanupAndPropagate(err, socket);
|
|
|
|
|
};
|
|
|
|
|
const cleanupAndPropagate = once((err, currentSocket) => {
|
|
|
|
|
debug('cleanupAndPropagate', err);
|
|
|
|
|
socket.removeListener('error', onError);
|
|
|
|
|
socket.removeListener('timeout', onTimeout);
|
|
|
|
|
// An error occurred during tunnel establishment, in that case just destroy the socket.
|
|
|
|
|
// and propagate the error to the callback.
|
|
|
|
|
|
|
|
|
|
// When the error comes from unexpected status code, the stream is still in good shape,
|
|
|
|
|
// in that case let req.onSocket handle the destruction instead.
|
|
|
|
|
if (err && err.code === 'ERR_PROXY_TUNNEL' && !err.statusCode) {
|
|
|
|
|
socket.destroy();
|
|
|
|
|
}
|
|
|
|
|
// This error should go to:
|
|
|
|
|
// -> oncreate in Agent.prototype.createSocket
|
|
|
|
|
// -> closure in Agent.prototype.addRequest or Agent.prototype.removeSocket
|
|
|
|
|
if (cb) {
|
|
|
|
|
cb(err, currentSocket);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const onProxyConnection = () => {
|
|
|
|
|
socket.removeListener('error', onError);
|
|
|
|
|
establishTunnel(this, socket, options, tunnelConfig, cleanupAndPropagate);
|
|
|
|
|
};
|
|
|
|
|
if (this[kProxyConfig].protocol === 'http:') {
|
|
|
|
|
socket = net.connect(connectOptions, onProxyConnection);
|
|
|
|
|
} else {
|
|
|
|
|
socket = tls.connect(connectOptions, onProxyConnection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.on('error', onError);
|
|
|
|
|
if (proxyTunnelTimeout) {
|
|
|
|
|
socket.setTimeout(proxyTunnelTimeout, onTimeout);
|
|
|
|
|
}
|
|
|
|
|
socket[kWaitForProxyTunnel] = true;
|
|
|
|
|
}
|
2015-07-22 21:18:38 -07:00
|
|
|
|
2019-01-30 12:18:04 -08:00
|
|
|
if (options._agentKey) {
|
|
|
|
|
// Cache new session for reuse
|
|
|
|
|
socket.on('session', (session) => {
|
|
|
|
|
this._cacheSession(options._agentKey, session);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Evict session on error
|
|
|
|
|
socket.once('close', (err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
this._evictSession(options._agentKey);
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-01-30 18:49:11 -05:00
|
|
|
|
2015-07-22 21:18:38 -07:00
|
|
|
return socket;
|
2012-02-18 15:01:35 -08:00
|
|
|
}
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2021-05-08 04:57:48 +04:30
|
|
|
/**
|
|
|
|
|
* Creates a new `HttpAgent` instance.
|
|
|
|
|
* @param {{
|
|
|
|
|
* keepAlive?: boolean;
|
|
|
|
|
* keepAliveMsecs?: number;
|
|
|
|
|
* maxSockets?: number;
|
|
|
|
|
* maxTotalSockets?: number;
|
|
|
|
|
* maxFreeSockets?: number;
|
|
|
|
|
* scheduling?: string;
|
|
|
|
|
* timeout?: number;
|
|
|
|
|
* maxCachedSessions?: number;
|
|
|
|
|
* servername?: string;
|
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-02 01:17:07 +02:00
|
|
|
* defaultPort?: number;
|
|
|
|
|
* protocol?: string;
|
|
|
|
|
* proxyEnv?: object;
|
2021-05-08 04:57:48 +04:30
|
|
|
* }} [options]
|
2025-07-18 05:28:21 -04:00
|
|
|
* @class
|
2021-05-08 04:57:48 +04:30
|
|
|
*/
|
2011-01-21 13:12:35 -08:00
|
|
|
function Agent(options) {
|
2017-05-09 14:32:34 -04:00
|
|
|
if (!(this instanceof Agent))
|
|
|
|
|
return new Agent(options);
|
|
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
options = { __proto__: null, ...options };
|
|
|
|
|
options.defaultPort ??= 443;
|
|
|
|
|
options.protocol ??= 'https:';
|
2020-11-17 13:10:49 +01:00
|
|
|
FunctionPrototypeCall(HttpAgent, this, options);
|
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-02 01:17:07 +02:00
|
|
|
|
2015-07-22 21:18:38 -07:00
|
|
|
this.maxCachedSessions = this.options.maxCachedSessions;
|
|
|
|
|
if (this.maxCachedSessions === undefined)
|
|
|
|
|
this.maxCachedSessions = 100;
|
|
|
|
|
|
|
|
|
|
this._sessionCache = {
|
|
|
|
|
map: {},
|
2023-02-12 19:26:21 +01:00
|
|
|
list: [],
|
2015-07-22 21:18:38 -07:00
|
|
|
};
|
2012-02-18 15:01:35 -08:00
|
|
|
}
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf(Agent.prototype, HttpAgent.prototype);
|
|
|
|
|
ObjectSetPrototypeOf(Agent, HttpAgent);
|
2013-05-21 14:02:18 -07:00
|
|
|
Agent.prototype.createConnection = createConnection;
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2021-05-08 04:57:48 +04:30
|
|
|
/**
|
|
|
|
|
* Gets a unique name for a set of options.
|
|
|
|
|
* @param {{
|
|
|
|
|
* host: string;
|
|
|
|
|
* port: number;
|
|
|
|
|
* localAddress: string;
|
|
|
|
|
* family: number;
|
|
|
|
|
* }} [options]
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
2022-05-21 17:53:17 +08:00
|
|
|
Agent.prototype.getName = function getName(options = kEmptyObject) {
|
2020-11-17 13:10:49 +01:00
|
|
|
let name = FunctionPrototypeCall(HttpAgent.prototype.getName, this, options);
|
2013-05-22 18:44:24 -07:00
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.ca)
|
|
|
|
|
name += options.ca;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.cert)
|
|
|
|
|
name += options.cert;
|
|
|
|
|
|
2016-04-15 16:49:36 +02:00
|
|
|
name += ':';
|
|
|
|
|
if (options.clientCertEngine)
|
|
|
|
|
name += options.clientCertEngine;
|
|
|
|
|
|
2013-05-22 18:44:24 -07:00
|
|
|
name += ':';
|
|
|
|
|
if (options.ciphers)
|
|
|
|
|
name += options.ciphers;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.key)
|
|
|
|
|
name += options.key;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.pfx)
|
|
|
|
|
name += options.pfx;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
2015-01-28 20:05:53 -05:00
|
|
|
if (options.rejectUnauthorized !== undefined)
|
2013-05-22 18:44:24 -07:00
|
|
|
name += options.rejectUnauthorized;
|
|
|
|
|
|
2015-12-22 13:22:52 -05:00
|
|
|
name += ':';
|
|
|
|
|
if (options.servername && options.servername !== options.host)
|
|
|
|
|
name += options.servername;
|
|
|
|
|
|
2018-05-06 13:52:34 +09:00
|
|
|
name += ':';
|
|
|
|
|
if (options.minVersion)
|
|
|
|
|
name += options.minVersion;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.maxVersion)
|
|
|
|
|
name += options.maxVersion;
|
|
|
|
|
|
2016-11-02 23:20:12 +01:00
|
|
|
name += ':';
|
|
|
|
|
if (options.secureProtocol)
|
|
|
|
|
name += options.secureProtocol;
|
|
|
|
|
|
2017-10-16 21:23:29 -07:00
|
|
|
name += ':';
|
|
|
|
|
if (options.crl)
|
|
|
|
|
name += options.crl;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.honorCipherOrder !== undefined)
|
|
|
|
|
name += options.honorCipherOrder;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.ecdhCurve)
|
|
|
|
|
name += options.ecdhCurve;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.dhparam)
|
|
|
|
|
name += options.dhparam;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.secureOptions !== undefined)
|
|
|
|
|
name += options.secureOptions;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.sessionIdContext)
|
|
|
|
|
name += options.sessionIdContext;
|
|
|
|
|
|
2020-12-06 19:06:07 +01:00
|
|
|
name += ':';
|
|
|
|
|
if (options.sigalgs)
|
|
|
|
|
name += JSONStringify(options.sigalgs);
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.privateKeyIdentifier)
|
|
|
|
|
name += options.privateKeyIdentifier;
|
|
|
|
|
|
|
|
|
|
name += ':';
|
|
|
|
|
if (options.privateKeyEngine)
|
|
|
|
|
name += options.privateKeyEngine;
|
|
|
|
|
|
2013-05-22 18:44:24 -07:00
|
|
|
return name;
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-22 21:18:38 -07:00
|
|
|
Agent.prototype._getSession = function _getSession(key) {
|
|
|
|
|
return this._sessionCache.map[key];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Agent.prototype._cacheSession = function _cacheSession(key, session) {
|
2015-12-11 20:11:17 -05:00
|
|
|
// Cache is disabled
|
|
|
|
|
if (this.maxCachedSessions === 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-07-22 21:18:38 -07:00
|
|
|
// Fast case - update existing entry
|
|
|
|
|
if (this._sessionCache.map[key]) {
|
|
|
|
|
this._sessionCache.map[key] = session;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put new entry
|
|
|
|
|
if (this._sessionCache.list.length >= this.maxCachedSessions) {
|
2020-11-17 13:10:49 +01:00
|
|
|
const oldKey = ArrayPrototypeShift(this._sessionCache.list);
|
2015-07-22 21:18:38 -07:00
|
|
|
debug('evicting %j', oldKey);
|
|
|
|
|
delete this._sessionCache.map[oldKey];
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 13:10:49 +01:00
|
|
|
ArrayPrototypePush(this._sessionCache.list, key);
|
2015-07-22 21:18:38 -07:00
|
|
|
this._sessionCache.map[key] = session;
|
|
|
|
|
};
|
|
|
|
|
|
2016-01-30 18:49:11 -05:00
|
|
|
Agent.prototype._evictSession = function _evictSession(key) {
|
2020-11-17 13:10:49 +01:00
|
|
|
const index = ArrayPrototypeIndexOf(this._sessionCache.list, key);
|
2016-01-30 18:49:11 -05:00
|
|
|
if (index === -1)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-11-17 13:10:49 +01:00
|
|
|
ArrayPrototypeSplice(this._sessionCache.list, index, 1);
|
2016-01-30 18:49:11 -05:00
|
|
|
delete this._sessionCache.map[key];
|
|
|
|
|
};
|
|
|
|
|
|
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-02 01:17:07 +02:00
|
|
|
const globalAgent = new Agent({
|
|
|
|
|
keepAlive: true, scheduling: 'lifo', timeout: 5000,
|
2025-07-26 22:43:10 +02:00
|
|
|
// This normalized from both --use-env-proxy and NODE_USE_ENV_PROXY settings.
|
|
|
|
|
proxyEnv: getOptionValue('--use-env-proxy') ? filterEnvForProxies(process.env) : undefined,
|
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-02 01:17:07 +02:00
|
|
|
});
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2021-05-08 04:57:48 +04:30
|
|
|
/**
|
|
|
|
|
* Makes a request to a secure web server.
|
|
|
|
|
* @param {...any} args
|
|
|
|
|
* @returns {ClientRequest}
|
|
|
|
|
*/
|
2018-07-29 01:00:28 -04:00
|
|
|
function request(...args) {
|
|
|
|
|
let options = {};
|
|
|
|
|
|
|
|
|
|
if (typeof args[0] === 'string') {
|
2020-11-17 13:10:49 +01:00
|
|
|
const urlStr = ArrayPrototypeShift(args);
|
2021-01-09 18:17:43 +05:30
|
|
|
options = urlToHttpOptions(new URL(urlStr));
|
2023-02-27 09:31:18 -05:00
|
|
|
} else if (isURL(args[0])) {
|
2021-01-06 16:10:28 +08:00
|
|
|
options = urlToHttpOptions(ArrayPrototypeShift(args));
|
2018-07-29 01:00:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args[0] && typeof args[0] !== 'function') {
|
2020-11-17 13:10:49 +01:00
|
|
|
ObjectAssign(options, ArrayPrototypeShift(args));
|
2014-02-25 14:15:02 -08:00
|
|
|
}
|
2018-07-29 01:00:28 -04:00
|
|
|
|
2018-12-21 20:17:15 +02:00
|
|
|
options._defaultAgent = module.exports.globalAgent;
|
2020-11-17 13:10:49 +01:00
|
|
|
ArrayPrototypeUnshift(args, options);
|
2018-07-29 01:00:28 -04:00
|
|
|
|
2020-11-17 13:10:49 +01:00
|
|
|
return ReflectConstruct(ClientRequest, args);
|
2017-10-22 17:23:43 -07:00
|
|
|
}
|
2011-01-21 13:21:01 -08:00
|
|
|
|
2021-05-08 04:57:48 +04:30
|
|
|
/**
|
|
|
|
|
* Makes a GET request to a secure web server.
|
|
|
|
|
* @param {string | URL} input
|
|
|
|
|
* @param {{
|
|
|
|
|
* agent?: Agent | boolean;
|
|
|
|
|
* auth?: string;
|
|
|
|
|
* createConnection?: Function;
|
|
|
|
|
* defaultPort?: number;
|
|
|
|
|
* family?: number;
|
2022-03-26 20:48:11 -07:00
|
|
|
* headers?: object;
|
2021-05-08 04:57:48 +04:30
|
|
|
* hints?: number;
|
|
|
|
|
* host?: string;
|
|
|
|
|
* hostname?: string;
|
|
|
|
|
* insecureHTTPParser?: boolean;
|
2023-09-29 20:14:24 +09:00
|
|
|
* joinDuplicateHeaders?: boolean;
|
2021-05-08 04:57:48 +04:30
|
|
|
* localAddress?: string;
|
|
|
|
|
* localPort?: number;
|
|
|
|
|
* lookup?: Function;
|
|
|
|
|
* maxHeaderSize?: number;
|
|
|
|
|
* method?: string;
|
|
|
|
|
* path?: string;
|
|
|
|
|
* port?: number;
|
|
|
|
|
* protocol?: string;
|
|
|
|
|
* setHost?: boolean;
|
|
|
|
|
* socketPath?: string;
|
|
|
|
|
* timeout?: number;
|
|
|
|
|
* signal?: AbortSignal;
|
2023-09-29 20:14:24 +09:00
|
|
|
* uniqueHeaders?: Array;
|
2021-05-08 04:57:48 +04:30
|
|
|
* } | string | URL} [options]
|
|
|
|
|
* @param {Function} [cb]
|
|
|
|
|
* @returns {ClientRequest}
|
|
|
|
|
*/
|
2018-07-29 01:00:28 -04:00
|
|
|
function get(input, options, cb) {
|
|
|
|
|
const req = request(input, options, cb);
|
2014-02-25 14:15:02 -08:00
|
|
|
req.end();
|
|
|
|
|
return req;
|
2017-10-22 17:23:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
Agent,
|
|
|
|
|
globalAgent,
|
|
|
|
|
Server,
|
|
|
|
|
createServer,
|
|
|
|
|
get,
|
2023-02-12 19:26:21 +01:00
|
|
|
request,
|
2011-01-21 13:21:01 -08:00
|
|
|
};
|