|  | 
| 23 | 23 | import java.io.InterruptedIOException; | 
| 24 | 24 | import java.io.PrintStream; | 
| 25 | 25 | import java.net.BindException; | 
|  | 26 | +import java.net.InetAddress; | 
| 26 | 27 | import java.net.InetSocketAddress; | 
| 27 | 28 | import java.net.MalformedURLException; | 
| 28 | 29 | import java.net.URI; | 
| @@ -549,25 +550,50 @@ public HttpServer2 build() throws IOException { | 
| 549 | 550 |       } | 
| 550 | 551 | 
 | 
| 551 | 552 |       for (URI ep : endpoints) { | 
| 552 |  | -        final ServerConnector connector; | 
|  | 553 | +        // | 
|  | 554 | +        // To enable dual-stack or IPv6 support, use InetAddress | 
|  | 555 | +        // .getAllByName(hostname) to resolve the IP addresses of a host. | 
|  | 556 | +        // When the system property java.net.preferIPv4Stack is set to true, | 
|  | 557 | +        // only IPv4 addresses are returned, and any IPv6 addresses are | 
|  | 558 | +        // ignored, so no extra check is needed to exclude IPv6. | 
|  | 559 | +        // When java.net.preferIPv4Stack is false, both IPv4 and IPv6 | 
|  | 560 | +        // addresses may be returned, and any IPv6 addresses will also be | 
|  | 561 | +        // added as connectors. | 
|  | 562 | +        // To disable IPv4, you need to configure the OS at the system level. | 
|  | 563 | +        // | 
|  | 564 | +        InetAddress[] addresses = InetAddress.getAllByName(ep.getHost()); | 
|  | 565 | +        server = addConnectors( | 
|  | 566 | +            ep, addresses, server, httpConfig, backlogSize, idleTimeout); | 
|  | 567 | +      } | 
|  | 568 | +      server.loadListeners(); | 
|  | 569 | +      return server; | 
|  | 570 | +    } | 
|  | 571 | + | 
|  | 572 | +    @VisibleForTesting | 
|  | 573 | +    HttpServer2 addConnectors( | 
|  | 574 | +        URI ep, InetAddress[] addresses, HttpServer2 server, | 
|  | 575 | +        HttpConfiguration httpConfig, int backlogSize, int idleTimeout){ | 
|  | 576 | +      for (InetAddress addr : addresses) { | 
|  | 577 | +        ServerConnector connector; | 
| 553 | 578 |         String scheme = ep.getScheme(); | 
| 554 | 579 |         if (HTTP_SCHEME.equals(scheme)) { | 
| 555 |  | -          connector = createHttpChannelConnector(server.webServer, | 
| 556 |  | -              httpConfig); | 
|  | 580 | +          connector = createHttpChannelConnector( | 
|  | 581 | +              server.webServer, httpConfig); | 
| 557 | 582 |         } else if (HTTPS_SCHEME.equals(scheme)) { | 
| 558 |  | -          connector = createHttpsChannelConnector(server.webServer, | 
| 559 |  | -              httpConfig); | 
|  | 583 | +          connector = createHttpsChannelConnector( | 
|  | 584 | +              server.webServer, httpConfig); | 
| 560 | 585 |         } else { | 
| 561 | 586 |           throw new HadoopIllegalArgumentException( | 
| 562 | 587 |               "unknown scheme for endpoint:" + ep); | 
| 563 | 588 |         } | 
| 564 |  | -        connector.setHost(ep.getHost()); | 
|  | 589 | +        LOG.debug("Adding connector to WebServer for address {}", | 
|  | 590 | +            addr.getHostAddress()); | 
|  | 591 | +        connector.setHost(addr.getHostAddress()); | 
| 565 | 592 |         connector.setPort(ep.getPort() == -1 ? 0 : ep.getPort()); | 
| 566 | 593 |         connector.setAcceptQueueSize(backlogSize); | 
| 567 | 594 |         connector.setIdleTimeout(idleTimeout); | 
| 568 | 595 |         server.addListener(connector); | 
| 569 | 596 |       } | 
| 570 |  | -      server.loadListeners(); | 
| 571 | 597 |       return server; | 
| 572 | 598 |     } | 
| 573 | 599 | 
 | 
|  | 
0 commit comments