Skip to content

Commit

Permalink
[FIXED JENKINS-10675] use X-Forwarded-Proto if present
Browse files Browse the repository at this point in the history
(cherry picked from commit ddfa65f)
  • Loading branch information
Ian Hopkins authored and olivergondza committed Oct 31, 2013
1 parent ca35f24 commit 2235fcd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -1909,7 +1909,12 @@ public boolean isRootUrlSecure() {
public String getRootUrlFromRequest() {
StaplerRequest req = Stapler.getCurrentRequest();
StringBuilder buf = new StringBuilder();
buf.append(req.getScheme()+"://");
String scheme = req.getScheme();
String forwardedScheme = req.getHeader("X-Forwarded-Proto");
if (forwardedScheme != null) {
scheme = forwardedScheme;
}
buf.append(scheme+"://");
buf.append(req.getServerName());
if(req.getServerPort()!=80)
buf.append(':').append(req.getServerPort());
Expand Down
29 changes: 29 additions & 0 deletions core/src/test/java/jenkins/model/JenkinsGetRootUrlTest.java
Expand Up @@ -102,6 +102,30 @@ public void doNotInheritProtocolWhenDispatchingRequest2() {
accessing("http://localhost:8080/");
rootUrlIs("https://ci/jenkins/");
}

@Bug(10675)
@Test
public void useForwardedProtoWhenPresent() {
configured("https://ci/jenkins/");

// Without a forwarded protocol, it should use the request protocol
accessing("http://ci/jenkins/");
rootUrlFromRequestIs("http://ci/jenkins/");

// With a forwarded protocol, it should use the forwarded protocol
accessing("http://ci/jenkins/");
withHeader("X-Forwarded-Proto", "https");
rootUrlFromRequestIs("https://ci/jenkins/");

accessing("https://ci/jenkins/");
withHeader("X-Forwarded-Proto", "http");
rootUrlFromRequestIs("http://ci/jenkins/");
}

private void rootUrlFromRequestIs(final String expectedRootUrl) {

assertThat(jenkins.getRootUrlFromRequest(), equalTo(expectedRootUrl));
}

private void rootUrlIs(final String expectedRootUrl) {

Expand All @@ -112,6 +136,11 @@ private void configured(final String configuredHost) {

when(config.getUrl()).thenReturn(configuredHost);
}

private void withHeader(String name, String value) {
final StaplerRequest req = Stapler.getCurrentRequest();
when(req.getHeader(name)).thenReturn(value);
}

private void accessing(final String realUrl) {

Expand Down

0 comments on commit 2235fcd

Please sign in to comment.