Skip to content

Commit

Permalink
redirect doesn't work if application.path is set fix #469
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Sep 7, 2016
1 parent f59b177 commit 78819fe
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
48 changes: 48 additions & 0 deletions coverage-report/src/test/java/org/jooby/issues/Issue469.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jooby.issues;

import org.jooby.Results;
import org.jooby.test.ServerFeature;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class Issue469 extends ServerFeature {

{
use(ConfigFactory.empty()
.withValue("application.path", ConfigValueFactory.fromAnyRef("/469")));

get("/", req -> "OK");

get("/redirect", req -> {
return Results.redirect(req.param("p").value("/"));
});

get("/credirect", req -> {
return Results.redirect(req.contextPath() + req.param("p").value("/"));
});
}

@Test
public void redirectOptions() throws Exception {
request()
.dontFollowRedirect()
.get("/469/redirect")
.execute()
.header("Location", "/469/");

request()
.dontFollowRedirect()
.get("/469/credirect")
.execute()
.header("Location", "/469/");

request()
.dontFollowRedirect()
.get("/469/redirect?p=http://google.com")
.execute()
.header("Location", "http://google.com");
}

}
42 changes: 42 additions & 0 deletions coverage-report/src/test/java/org/jooby/issues/Issue469b.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jooby.issues;

import org.jooby.Results;
import org.jooby.test.ServerFeature;
import org.junit.Test;

public class Issue469b extends ServerFeature {

{
get("/", req -> "OK");

get("/redirect", req -> {
return Results.redirect(req.param("p").value("/"));
});

get("/credirect", req -> {
return Results.redirect(req.contextPath() + req.param("p").value("/"));
});
}

@Test
public void redirectOptions() throws Exception {
request()
.dontFollowRedirect()
.get("/redirect")
.execute()
.header("Location", "/");

request()
.dontFollowRedirect()
.get("/credirect")
.execute()
.header("Location", "/");

request()
.dontFollowRedirect()
.get("/redirect?p=http://google.com")
.execute()
.header("Location", "http://google.com");
}

}
21 changes: 15 additions & 6 deletions jooby/src/main/java/org/jooby/internal/ResponseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

public class ResponseImpl implements Response {

private static final String LOCATION = "Location";

/** Char encoded content disposition. */
private static final String CONTENT_DISPOSITION = "attachment; filename=\"%s\"; filename*=%s''%s";

Expand Down Expand Up @@ -215,10 +217,10 @@ public Response type(final MediaType type) {

@Override
public void redirect(final Status status, final String location) throws Throwable {
requireNonNull(status, "A status is required.");
requireNonNull(location, "A location is required.");
requireNonNull(status, "Status required.");
requireNonNull(location, "Location required.");

send(Results.with(status).header("Location", location));
send(Results.with(status).header(LOCATION, location));
}

@Override
Expand All @@ -228,7 +230,7 @@ public Optional<Status> status() {

@Override
public Response status(final Status status) {
this.status = requireNonNull(status, "Statusrequired.");
this.status = requireNonNull(status, "Status required.");
rsp.statusCode(status.value());
failure = status.isError();
return this;
Expand Down Expand Up @@ -408,8 +410,15 @@ private Response setHeader(final String name, final Object value) {
.collect(Collectors.toList());
rsp.header(name, values);
} else {
if ("location".equalsIgnoreCase(name) && "back".equalsIgnoreCase(value.toString())) {
rsp.header(name, referer.orElse("/"));
if (LOCATION.equalsIgnoreCase(name)) {
String location = value.toString();
String cpath = req.contextPath();
if ("back".equalsIgnoreCase(location)) {
location = referer.orElse(cpath + "/");
} else if (location.startsWith("/") && !location.startsWith(cpath)) {
location = cpath + location;
}
rsp.header(LOCATION, location);
} else {
rsp.header(name, Headers.encode(value));
}
Expand Down

0 comments on commit 78819fe

Please sign in to comment.