-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make http.request correctly parse {host: "hostname:port"}
The issue: http.request() treats host as an alternate form of hostname, which it sort of is, except that host is (normally) allowed to contain a port number. Before this change, if host contains a port number and there isn't a hostname field to override it, http.request() attempts a DNS lookup on the entire host field, including the port number. This always fails. The fix: If a host field is present, use the url lib to parse it and use the parsed hostname nad port values as fallbacks if options.hostname and options.port are unset. Includes tests for localhost:12346 and [::1]:12346.
- Loading branch information
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
if (!common.hasIPv6) { | ||
console.log('1..0 # Skipped: no IPv6 support'); | ||
return; | ||
} | ||
|
||
var connected = false; | ||
|
||
const server = http.createServer(function(req, res) { | ||
connected = true; | ||
res.writeHead(204); | ||
res.end(); | ||
}); | ||
|
||
server.listen(common.PORT, '::1', function() { | ||
http.get({ | ||
host: '[::1]:' + common.PORT | ||
}, function(res) { | ||
res.resume(); | ||
server.close(); | ||
}).on('error', function(e) { | ||
throw e; | ||
}); | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert(connected, 'http.request should correctly parse ' + | ||
'{host: "hostname:port"} and connect to the specified host'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
var connected = false; | ||
|
||
const server = http.createServer(function(req, res) { | ||
connected = true; | ||
res.writeHead(204); | ||
res.end(); | ||
}); | ||
|
||
server.listen(common.PORT, function() { | ||
http.get({ | ||
host: 'localhost:' + common.PORT | ||
}, function(res) { | ||
res.resume(); | ||
server.close(); | ||
}).on('error', function(e) { | ||
throw e; | ||
}); | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert(connected, 'http.request should correctly parse ' + | ||
'{host: "hostname:port"} and connect to the specified host'); | ||
}); |