提交 0961d7ab 编写于 作者: L lorainsu

[JENKINS-58041] Add support for IPv6 when getting "Jenkins URL"<br><br>Default...

[JENKINS-58041] Add support for IPv6 when getting "Jenkins URL"<br><br>Default "Jenkins URL" do not support IPv6 address。<br>For example my Jenkins server address is "[http://[6000::24]:8080/jenkins"|http://[6000::24]:8080/jenkins]<br>But the default value of "Jenkins URL" in "Jenkins Location" Section of "Configuration System" webpage is: “http://[6000:8080/jenkins/”<br><br>We fixed the bug in Jenkins.getRootUrlFromRequest()
上级 bc40ab3a
......@@ -2319,22 +2319,27 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve
String scheme = getXForwardedHeader(req, "X-Forwarded-Proto", req.getScheme());
buf.append(scheme).append("://");
String host = getXForwardedHeader(req, "X-Forwarded-Host", req.getServerName());
int index = host.indexOf(':');
int index = host.lastIndexOf(':');
int port = req.getServerPort();
if (index == -1) {
// Almost everyone else except Nginx put the host and port in separate headers
buf.append(host);
} else {
// Nginx uses the same spec as for the Host header, i.e. hostname:port
buf.append(host, 0, index);
if (index + 1 < host.length()) {
try {
port = Integer.parseInt(host.substring(index + 1));
} catch (NumberFormatException e) {
// ignore
if (host.startsWith("[") && host.endsWith("]")) {
// support IPv6 address
buf.append(host);
} else {
// Nginx uses the same spec as for the Host header, i.e. hostname:port
buf.append(host, 0, index);
if (index + 1 < host.length()) {
try {
port = Integer.parseInt(host.substring(index + 1));
} catch (NumberFormatException e) {
// ignore
}
}
// but if a user has configured Nginx with an X-Forwarded-Port, that will win out.
}
// but if a user has configured Nginx with an X-Forwarded-Port, that will win out.
}
String forwardedPort = getXForwardedHeader(req, "X-Forwarded-Port", null);
if (forwardedPort != null) {
......
......@@ -141,7 +141,31 @@ public class JenkinsGetRootUrlTest {
withHeader("X-Forwarded-Proto", "https");
rootUrlFromRequestIs("https://ci/jenkins/");
}
@Issue("JENKINS-58041")
@Test
public void useForwardedProtoWithIPv6WhenPresent() {
configured("http://[::1]/jenkins/");
// Without a forwarded protocol, it should use the request protocol
accessing("http://[::1]/jenkins/");
rootUrlFromRequestIs("http://[::1]/jenkins/");
accessing("http://[::1]:8080/jenkins/");
rootUrlFromRequestIs("http://[::1]:8080/jenkins/");
// With a forwarded protocol, it should use the forwarded protocol
accessing("http://[::1]/jenkins/");
withHeader("X-Forwarded-Host", "[::2]");
rootUrlFromRequestIs("http://[::2]/jenkins/");
accessing("http://[::1]:8080/jenkins/");
withHeader("X-Forwarded-Proto", "https");
withHeader("X-Forwarded-Host", "[::1]:8443");
rootUrlFromRequestIs("https://[::1]:8443/jenkins/");
}
private void rootUrlFromRequestIs(final String expectedRootUrl) {
assertThat(jenkins.getRootUrlFromRequest(), equalTo(expectedRootUrl));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册