Skip to content

Commit ec50f5a

Browse files
committedMay 13, 2024··
fix: ipv6 addresses parsing
1 parent 3317168 commit ec50f5a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
 

‎src/node/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ Request.prototype.request = function () {
749749
options.method = this.method;
750750
options.port = url.port;
751751
options.path = path;
752-
options.host = url.hostname;
752+
options.host = utils.normalizeHostname(url.hostname); // ex: [::1] -> ::1
753753
options.ca = this._ca;
754754
options.key = this._key;
755755
options.pfx = this._pfx;

‎src/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ exports.cleanHeader = (header, changesOrigin) => {
7171
return header;
7272
};
7373

74+
exports.normalizeHostname = (hostname) => {
75+
const [,normalized] = hostname.match(/^\[([^\]]+)\]$/) || [];
76+
return normalized || hostname;
77+
};
78+
7479
/**
7580
* Check if `obj` is an object.
7681
*

‎test/node/basic.js

+9
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ describe('[node] request', () => {
134134
});
135135
});
136136

137+
describe('ipv6 address', () => {
138+
it('should successfully query an ipv6 address', (done) => {
139+
request.get(`http://[::]/url?a=(b%29`).end((error, res) => {
140+
assert.equal('/url?a=(b%29', res.text);
141+
done();
142+
});
143+
});
144+
});
145+
137146
describe('.buffer()', () => {
138147
it('should enable buffering', (done) => {
139148
request

0 commit comments

Comments
 (0)
Please sign in to comment.