Skip to content

Commit 75d7d64

Browse files
committed
Fix context path bug on SSLHandler and ctx.getRequestUrl
1 parent d82f27b commit 75d7d64

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ public interface DefaultContext extends Context {
223223
if (port.length() > 0 && !port.endsWith("80") && !hostAndPort.endsWith("443")) {
224224
url.append(":").append(port);
225225
}
226-
String contextPath = getContextPath();
227-
if (contextPath.length() > 0 && !contextPath.equals("/")) {
228-
url.append(contextPath);
229-
}
230226
url.append(pathString());
231227
url.append(queryString());
232228

jooby/src/main/java/io/jooby/SSLHandler.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ public SSLHandler(boolean useProxy) {
8080
buff.append(":").append(port);
8181
}
8282
}
83-
String contextPath = ctx.getContextPath();
84-
if (!contextPath.equals("/")) {
85-
buff.append(contextPath);
86-
}
8783
buff.append(ctx.pathString());
8884
buff.append(ctx.queryString());
8985
ctx.sendRedirect(buff.toString());

tests/src/test/java/io/jooby/FeaturedTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,6 +2978,36 @@ public void requestUrl() {
29782978
assertEquals("http://first/somepath?useProxy=true", rsp.body().string());
29792979
});
29802980
});
2981+
2982+
new JoobyRunner(app -> {
2983+
app.setContextPath("/x");
2984+
app.get("/{path}", ctx -> ctx.getRequestURL(ctx.query("useProxy").booleanValue(false)));
2985+
}).ready(client -> {
2986+
client.get("/x/somepath", rsp -> {
2987+
assertEquals("http://localhost:" + client.getPort() + "/x/somepath", rsp.body().string());
2988+
});
2989+
client.header("X-Forwarded-Host", "myhost");
2990+
client.get("/x/somepath", rsp -> {
2991+
assertEquals("http://localhost:" + client.getPort() + "/x/somepath", rsp.body().string());
2992+
});
2993+
client.header("X-Forwarded-Host", "myhost");
2994+
client.header("X-Forwarded-Proto", "https");
2995+
client.get("/x/somepath?useProxy=true", rsp -> {
2996+
assertEquals("https://myhost/x/somepath?useProxy=true", rsp.body().string());
2997+
});
2998+
client.header("X-Forwarded-Host", "myhost:80");
2999+
client.get("/x/somepath?useProxy=true", rsp -> {
3000+
assertEquals("http://myhost/x/somepath?useProxy=true", rsp.body().string());
3001+
});
3002+
client.header("X-Forwarded-Host", "myhost:90");
3003+
client.get("/x/somepath?useProxy=true", rsp -> {
3004+
assertEquals("http://myhost:90/x/somepath?useProxy=true", rsp.body().string());
3005+
});
3006+
client.header("X-Forwarded-Host", "first,second");
3007+
client.get("/x/somepath?useProxy=true", rsp -> {
3008+
assertEquals("http://first/x/somepath?useProxy=true", rsp.body().string());
3009+
});
3010+
});
29813011
}
29823012

29833013
private byte[][] partition(byte[] bytes, int size) {

tests/src/test/java/io/jooby/HttpsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ public void httpsX509() {
4949

5050
@Test
5151
public void forceSSL() {
52+
new JoobyRunner(app -> {
53+
54+
app.setContextPath("/secure");
55+
56+
app.setServerOptions(new ServerOptions().setSecurePort(8433));
57+
58+
app.before(new SSLHandler(true));
59+
60+
app.get("/{path}", ctx -> ctx.pathString());
61+
}).dontFollowRedirects().ready((http, https, server) -> {
62+
http.get("/secure/path", rsp -> {
63+
assertEquals("https://localhost:" + https.getPort() + "/secure/path",
64+
rsp.header("Location"));
65+
assertEquals(302, rsp.code());
66+
});
67+
68+
http.header("X-Forwarded-Host", "myhost.org");
69+
http.get("/secure/path?a=b", rsp -> {
70+
assertEquals("https://myhost.org/secure/path?a=b",
71+
rsp.header("Location"));
72+
assertEquals(302, rsp.code());
73+
});
74+
});
75+
5276
new JoobyRunner(app -> {
5377

5478
app.setServerOptions(new ServerOptions().setSecurePort(8433));
@@ -70,6 +94,7 @@ public void forceSSL() {
7094
assertEquals(302, rsp.code());
7195
});
7296
});
97+
7398
}
7499

75100
@Test
@@ -95,5 +120,27 @@ public void forceSSLStatic() {
95120
assertEquals(302, rsp.code());
96121
});
97122
});
123+
124+
new JoobyRunner(app -> {
125+
app.setContextPath("/ppp");
126+
app.setServerOptions(new ServerOptions().setSecurePort(8433));
127+
128+
app.before(new SSLHandler("static.org"));
129+
130+
app.get("/{path}", ctx -> ctx.pathString());
131+
}).dontFollowRedirects().ready(http -> {
132+
http.get("/ppp/path", rsp -> {
133+
assertEquals("https://static.org/ppp/path",
134+
rsp.header("Location"));
135+
assertEquals(302, rsp.code());
136+
});
137+
138+
http.header("X-Forwarded-Host", "myhost.org");
139+
http.get("/ppp/path?a=b", rsp -> {
140+
assertEquals("https://static.org/ppp/path?a=b",
141+
rsp.header("Location"));
142+
assertEquals(302, rsp.code());
143+
});
144+
});
98145
}
99146
}

0 commit comments

Comments
 (0)