File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,17 @@ function ClientRequest(options, cb) {
8787 }
8888 if ( host && ! this . getHeader ( 'host' ) && setHost ) {
8989 var hostHeader = host ;
90+ var posColon = - 1 ;
91+
92+ // For the Host header, ensure that IPv6 addresses are enclosed
93+ // in square brackets, as defined by URI formatting
94+ // https://tools.ietf.org/html/rfc3986#section-3.2.2
95+ if ( - 1 !== ( posColon = hostHeader . indexOf ( ':' ) ) &&
96+ - 1 !== ( posColon = hostHeader . indexOf ( ':' , posColon ) ) &&
97+ '[' !== hostHeader [ 0 ] ) {
98+ hostHeader = `[${ hostHeader } ]` ;
99+ }
100+
90101 if ( port && + port !== defaultPort ) {
91102 hostHeader += ':' + port ;
92103 }
Original file line number Diff line number Diff line change 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+
12+ const common = require ( '../common' ) ;
13+ const assert = require ( 'assert' ) ;
14+ const http = require ( 'http' ) ;
15+
16+ const hostname = '::1' ;
17+ const port = common . PORT ;
18+
19+ function httpreq ( ) {
20+ var req = http . request ( {
21+ host : hostname ,
22+ port : port ,
23+ path : '/' ,
24+ method : 'GET'
25+ } ) ;
26+ req . end ( ) ;
27+ }
28+
29+ if ( ! common . hasIPv6 ) {
30+ console . error ( 'Skipping test, no IPv6 support' ) ;
31+ return ;
32+ }
33+
34+ const server = http . createServer ( common . mustCall ( function ( req , res ) {
35+ assert . ok ( req . headers . host , `[${ hostname } ]` ) ;
36+ res . end ( ) ;
37+ server . close ( true ) ;
38+ } ) ) ;
39+
40+ server . listen ( port , hostname , ( ) => httpreq ( ) ) ;
You can’t perform that action at this time.
0 commit comments