diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 57723d9..b0abcbc 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -196,7 +196,21 @@ Agent.prototype.addRequest = function addRequest(req, options) { options.servername = options.host; const hostHeader = req.getHeader('host'); if (hostHeader) { - options.servername = hostHeader.replace(/:.*$/, ''); + // abc => abc + // abc:123 => abc + // [::1] => ::1 + // [::1]:123 => ::1 + if (hostHeader.startsWith('[')) { + const index = hostHeader.indexOf(']'); + if (index === -1) { + // Leading '[', but no ']'. Need to do something... + options.serverName = hostHeader; + } else { + options.servername = hostHeader.substr(1, index - 1); + } + } else { + options.servername = hostHeader.split(':')[0]; + } } }