1'use strict'; 2/* 3 * When using the object form of http.request and using an IPv6 address 4 * as a hostname, and using a non-standard port, the Host header 5 * is improperly formatted. 6 * Issue: https://github.com/nodejs/node/issues/5308 7 * As per https://tools.ietf.org/html/rfc7230#section-5.4 and 8 * https://tools.ietf.org/html/rfc3986#section-3.2.2 9 * the IPv6 address should be enclosed in square brackets 10 */ 11 12const common = require('../common'); 13const assert = require('assert'); 14const http = require('http'); 15const net = require('net'); 16 17const requests = [ 18 { host: 'foo:1234', headers: { expectedhost: 'foo:1234:80' } }, 19 { host: '::1', headers: { expectedhost: '[::1]:80' } } 20]; 21 22function createLocalConnection(options) { 23 options.host = undefined; 24 options.port = this.port; 25 options.path = undefined; 26 return net.createConnection(options); 27} 28 29http.createServer(common.mustCall(function(req, res) { 30 this.requests = this.requests || 0; 31 assert.strictEqual(req.headers.host, req.headers.expectedhost); 32 res.end(); 33 if (++this.requests === requests.length) 34 this.close(); 35}, requests.length)).listen(0, function() { 36 const address = this.address(); 37 for (let i = 0; i < requests.length; ++i) { 38 requests[i].createConnection = 39 common.mustCall(createLocalConnection.bind(address)); 40 http.get(requests[i]); 41 } 42}); 43