Skip to content

Commit d7207f7

Browse files
committed
Added support for X-Forwarded-Host in reverse proxy scenarios
1 parent c0fc998 commit d7207f7

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/com/googlecode/utterlyidle/BaseUri.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import com.googlecode.totallylazy.io.Uri;
55

66
import static com.googlecode.totallylazy.io.Uri.uri;
7+
import static com.googlecode.utterlyidle.HttpHeaders.HOST;
8+
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_HOST;
79
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_PROTO;
810
import static java.lang.String.format;
911

10-
public class BaseUri implements Value<Uri> {
11-
private final Uri uri;
12-
12+
public class BaseUri extends Value.Type<Uri> implements Value<Uri> {
1313
public BaseUri(Uri uri) {
14-
this.uri = uri;
14+
super(uri);
1515
}
1616

1717
public static BaseUri baseUri(Uri uri){
@@ -23,7 +23,7 @@ public static BaseUri baseUri(String uri){
2323
}
2424

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

36-
@Override
37-
public Uri value() {
38-
return uri;
39-
}
40-
4136
@Override
4237
public String toString() {
43-
return uri.toString();
38+
return value().toString();
4439
}
4540
}

src/com/googlecode/utterlyidle/HttpHeaders.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class HttpHeaders {
3030
public static final String SET_COOKIE = "Set-Cookie";
3131
public static final String X_FORWARDED_FOR = "X-Forwarded-For";
3232
public static final String X_FORWARDED_PROTO = "X-Forwarded-Proto";
33+
public static final String X_FORWARDED_HOST = "X-Forwarded-Host";
3334
public static final String X_FRAME_OPTIONS = "X-Frame-Options";
3435
public static final String X_CORRELATION_ID = "X-CorrelationID";
3536
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.googlecode.utterlyidle;
2+
3+
import org.junit.Test;
4+
5+
import static com.googlecode.totallylazy.Assert.assertThat;
6+
import static com.googlecode.totallylazy.predicates.Predicates.is;
7+
import static com.googlecode.utterlyidle.BasePath.basePath;
8+
import static com.googlecode.utterlyidle.BaseUri.baseUri;
9+
import static com.googlecode.utterlyidle.HttpHeaders.HOST;
10+
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_HOST;
11+
import static com.googlecode.utterlyidle.HttpHeaders.X_FORWARDED_PROTO;
12+
import static com.googlecode.utterlyidle.Request.get;
13+
14+
public class BaseUriTest {
15+
@Test
16+
public void whenNoHostDefaultToBasePath() throws Exception {
17+
assertThat(baseUri(get("foo"), basePath("/root/")), is(baseUri("/root/")));
18+
}
19+
20+
@Test
21+
public void whenHostIsSetUseThat() throws Exception {
22+
assertThat(baseUri(get("foo").header(HOST, "server"), basePath("/root/")), is(baseUri("http://server/root/")));
23+
}
24+
25+
@Test
26+
public void whenXForwardedHostIsSetUseThatOverHost() throws Exception {
27+
assertThat(baseUri(get("foo").header(HOST, "internal").header(X_FORWARDED_HOST, "external"), basePath("/root/")), is(baseUri("http://external/root/")));
28+
}
29+
30+
@Test
31+
public void whenXForwardedProtoIsSetUseThatOverDefaultHttp() throws Exception {
32+
assertThat(baseUri(get("foo").header(HOST, "server").header(X_FORWARDED_PROTO, "https"), basePath("/root/")), is(baseUri("https://server/root/")));
33+
}
34+
}

0 commit comments

Comments
 (0)