Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8d0c638

Browse filesBrowse files
illBeRoyaddaleax
authored andcommitted
lib: support overriding http\s.globalAgent
Overriding `require('http[s]').globalAgent` is now respected by consequent requests. In order to achieve that, the following changes were made: 1. Implmentation in `http`: `module.exports.globalAgent` is now defined through `Object.defineProperty`. Its getter and setter return \ set `require('_http_agent').globalAgent`. 2. Implementation in `https`: the https `globalAgent` is not the same as `_http_agent`, and is defined in `https` module itself. Therefore, the fix here was to simply use `module.exports.globalAgent` to support mutation. 3. According tests were added for both `http` and `https`, where in both we create a server, set the default agent to a newly created instance and make a request to that server. We then assert that the given instance was actually used by inspecting its sockets property. Fixes: #23281 PR-URL: #25170 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ce7bbd2 commit 8d0c638
Copy full SHA for 8d0c638

File tree

Expand file treeCollapse file tree

4 files changed

+78
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+78
-4
lines changed
Open diff view settings
Collapse file

‎lib/http.js‎

Copy file name to clipboardExpand all lines: lib/http.js
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323

24-
const { Agent, globalAgent } = require('_http_agent');
24+
const httpAgent = require('_http_agent');
2525
const { ClientRequest } = require('_http_client');
2626
const { methods } = require('_http_common');
2727
const { IncomingMessage } = require('_http_incoming');
@@ -52,9 +52,8 @@ module.exports = {
5252
_connectionListener,
5353
METHODS: methods.slice().sort(),
5454
STATUS_CODES,
55-
Agent,
55+
Agent: httpAgent.Agent,
5656
ClientRequest,
57-
globalAgent,
5857
IncomingMessage,
5958
OutgoingMessage,
6059
Server,
@@ -76,3 +75,14 @@ Object.defineProperty(module.exports, 'maxHeaderSize', {
7675
return maxHeaderSize;
7776
}
7877
});
78+
79+
Object.defineProperty(module.exports, 'globalAgent', {
80+
configurable: true,
81+
enumerable: true,
82+
get() {
83+
return httpAgent.globalAgent;
84+
},
85+
set(value) {
86+
httpAgent.globalAgent = value;
87+
}
88+
});
Collapse file

‎lib/https.js‎

Copy file name to clipboardExpand all lines: lib/https.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function request(...args) {
294294
options = util._extend(options, args.shift());
295295
}
296296

297-
options._defaultAgent = globalAgent;
297+
options._defaultAgent = module.exports.globalAgent;
298298
args.unshift(options);
299299

300300
return new ClientRequest(...args);
Collapse file
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const server = http.Server(common.mustCall((req, res) => {
7+
res.writeHead(200);
8+
res.end('Hello, World!');
9+
}));
10+
11+
server.listen(0, common.mustCall(() => {
12+
const agent = new http.Agent();
13+
const name = agent.getName({ port: server.address().port });
14+
http.globalAgent = agent;
15+
16+
makeRequest();
17+
assert(agent.sockets.hasOwnProperty(name)); // agent has indeed been used
18+
}));
19+
20+
function makeRequest() {
21+
const req = http.get({
22+
port: server.address().port
23+
});
24+
req.on('close', () =>
25+
server.close());
26+
}
Collapse file
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const https = require('https');
6+
7+
if (!common.hasCrypto)
8+
common.skip('missing crypto');
9+
10+
// Disable strict server certificate validation by the client
11+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
12+
13+
const options = {
14+
key: fixtures.readKey('agent1-key.pem'),
15+
cert: fixtures.readKey('agent1-cert.pem')
16+
};
17+
18+
const server = https.Server(options, common.mustCall((req, res) => {
19+
res.writeHead(200);
20+
res.end('Hello, World!');
21+
}));
22+
23+
server.listen(0, common.mustCall(() => {
24+
const agent = new https.Agent();
25+
const name = agent.getName({ port: server.address().port });
26+
https.globalAgent = agent;
27+
28+
makeRequest();
29+
assert(agent.sockets.hasOwnProperty(name)); // agent has indeed been used
30+
}));
31+
32+
function makeRequest() {
33+
const req = https.get({
34+
port: server.address().port
35+
});
36+
req.on('close', () =>
37+
server.close());
38+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.