Skip to content

Commit

Permalink
Added support for X-Forwarded-Host in reverse proxy scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Sep 11, 2017
1 parent c0fc998 commit d7207f7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/com/googlecode/utterlyidle/BaseUri.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import com.googlecode.totallylazy.io.Uri;

import static com.googlecode.totallylazy.io.Uri.uri;
import static com.googlecode.utterlyidle.HttpHeaders.HOST;
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_HOST;
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_PROTO;
import static java.lang.String.format;

public class BaseUri implements Value<Uri> {
private final Uri uri;

public class BaseUri extends Value.Type<Uri> implements Value<Uri> {
public BaseUri(Uri uri) {
this.uri = uri;
super(uri);
}

public static BaseUri baseUri(Uri uri){
Expand All @@ -23,7 +23,7 @@ public static BaseUri baseUri(String uri){
}

public static BaseUri baseUri(Request request, BasePath basePath) {
String host = request.headers().getValue(HttpHeaders.HOST);
String host = request.headers().valueOption(X_FORWARDED_HOST).getOrElse(request.headers().getValue(HOST));
if (host == null) {
return new BaseUri(uri(basePath.toString()));
}
Expand All @@ -33,13 +33,8 @@ public static BaseUri baseUri(Request request, BasePath basePath) {
return new BaseUri(uri(format("%s://%s%s", scheme, host, basePath)));
}

@Override
public Uri value() {
return uri;
}

@Override
public String toString() {
return uri.toString();
return value().toString();
}
}
1 change: 1 addition & 0 deletions src/com/googlecode/utterlyidle/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class HttpHeaders {
public static final String SET_COOKIE = "Set-Cookie";
public static final String X_FORWARDED_FOR = "X-Forwarded-For";
public static final String X_FORWARDED_PROTO = "X-Forwarded-Proto";
public static final String X_FORWARDED_HOST = "X-Forwarded-Host";
public static final String X_FRAME_OPTIONS = "X-Frame-Options";
public static final String X_CORRELATION_ID = "X-CorrelationID";
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
Expand Down
34 changes: 34 additions & 0 deletions test/com/googlecode/utterlyidle/BaseUriTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.googlecode.utterlyidle;

import org.junit.Test;

import static com.googlecode.totallylazy.Assert.assertThat;
import static com.googlecode.totallylazy.predicates.Predicates.is;
import static com.googlecode.utterlyidle.BasePath.basePath;
import static com.googlecode.utterlyidle.BaseUri.baseUri;
import static com.googlecode.utterlyidle.HttpHeaders.HOST;
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_HOST;
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_PROTO;
import static com.googlecode.utterlyidle.Request.get;

public class BaseUriTest {
@Test
public void whenNoHostDefaultToBasePath() throws Exception {
assertThat(baseUri(get("foo"), basePath("/root/")), is(baseUri("/root/")));
}

@Test
public void whenHostIsSetUseThat() throws Exception {
assertThat(baseUri(get("foo").header(HOST, "server"), basePath("/root/")), is(baseUri("http://server/root/")));
}

@Test
public void whenXForwardedHostIsSetUseThatOverHost() throws Exception {
assertThat(baseUri(get("foo").header(HOST, "internal").header(X_FORWARDED_HOST, "external"), basePath("/root/")), is(baseUri("http://external/root/")));
}

@Test
public void whenXForwardedProtoIsSetUseThatOverDefaultHttp() throws Exception {
assertThat(baseUri(get("foo").header(HOST, "server").header(X_FORWARDED_PROTO, "https"), basePath("/root/")), is(baseUri("https://server/root/")));
}
}

0 comments on commit d7207f7

Please sign in to comment.