Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IPv6 hostname normalization #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -23,8 +23,9 @@ http.request = function (opts, cb) {
var path = opts.path || '/'

// Necessary for IPv6 addresses
if (host && host.indexOf(':') !== -1)
host = '[' + host + ']'
if (host && host === opts.hostname && host.indexOf(':') !== -1 && host.indexOf('[') !== 0) {
host = '[' + host + ']'
}

// This may be a relative url. The browser should always be able to interpret it correctly.
opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
Expand Down Expand Up @@ -82,4 +83,4 @@ http.METHODS = [
'TRACE',
'UNLOCK',
'UNSUBSCRIBE'
]
]
43 changes: 42 additions & 1 deletion test/node/http-browserify.js
Expand Up @@ -121,7 +121,7 @@ test('Test withCredentials param', function(t) {
t.end()
})

test('Test ipv6 address', function(t) {
test('Test ipv6 address as string', function(t) {
var testUrl = 'http://[::1]:80/foo'
var request = http.get(testUrl, noop)

Expand All @@ -130,6 +130,47 @@ test('Test ipv6 address', function(t) {
t.end()
})

test('Test ipv6 address as opts with hostname [brackets] and port', function(t) {
var opts = {
protocol: "http:",
hostname: "[::1]",
port: "80",
path: "/foo"
}
var request = http.get(opts, noop)

var resolved = url.resolve(location, request._opts.url)
t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct')
t.end()
})

test('Test ipv6 address as opts with hostname [no brackets] and port', function(t) {
var opts = {
protocol: "http:",
hostname: "::1",
port: "80",
path: "/foo"
}
var request = http.get(opts, noop)

var resolved = url.resolve(location, request._opts.url)
t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct')
t.end()
})

test('Test ipv6 address as opts with host', function(t) {
var opts = {
protocol: "http:",
host: "[::1]:80",
path: "/foo"
}
var request = http.get(opts, noop)

var resolved = url.resolve(location, request._opts.url)
t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct')
t.end()
})

test('Test relative path in url', function(t) {
var params = { path: './bar' }
var request = http.get(params, noop)
Expand Down