From 163858ec182b7726cdb376601d349407852af336 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Thu, 24 Sep 2015 14:03:58 +0200 Subject: [PATCH] sendRedirect does always specify the host. use a separate method for that. (cherry picked from commit 82837bee0ed5ad7d8190bf8a2125691cc0196371) --- .../scala/mesosphere/marathon/api/WebJarServlet.scala | 9 +++++++-- .../mesosphere/marathon/api/WebJarServletTest.scala | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/scala/mesosphere/marathon/api/WebJarServlet.scala b/src/main/scala/mesosphere/marathon/api/WebJarServlet.scala index 4e7b7a28058..6bd4036c646 100644 --- a/src/main/scala/mesosphere/marathon/api/WebJarServlet.scala +++ b/src/main/scala/mesosphere/marathon/api/WebJarServlet.scala @@ -58,9 +58,9 @@ class WebJarServlet extends HttpServlet { } //special rule for accessing root -> redirect to ui main page - if (req.getRequestURI == "/") resp.sendRedirect("/ui/") + if (req.getRequestURI == "/") sendRedirect(resp, "/ui/") //if a directory is requested, redirect to trailing slash - else if (!file.contains(".")) resp.sendRedirect(req.getRequestURI + "/") //request /ui -> /ui/ + else if (!file.contains(".")) sendRedirect(resp, req.getRequestURI + "/") //request /ui -> /ui/ //if we come here, it must be a resource else sendResourceNormalized(resourceURI, mime) } @@ -73,4 +73,9 @@ class WebJarServlet extends HttpServlet { case _ => "application/octet-stream" } } + + private[this] def sendRedirect(response: HttpServletResponse, location: String): Unit = { + response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY) + response.setHeader("Location", location) + } } diff --git a/src/test/scala/mesosphere/marathon/api/WebJarServletTest.scala b/src/test/scala/mesosphere/marathon/api/WebJarServletTest.scala index 37e7c5efbf4..cb69b360985 100644 --- a/src/test/scala/mesosphere/marathon/api/WebJarServletTest.scala +++ b/src/test/scala/mesosphere/marathon/api/WebJarServletTest.scala @@ -20,7 +20,8 @@ class WebJarServletTest extends MarathonSpec with Mockito with GivenWhenThen wit servlet.doGet(request, response) Then("A redirect response is send") - verify(response, atLeastOnce).sendRedirect("/ui/") + verify(response, atLeastOnce).setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY) + verify(response, atLeastOnce).setHeader("Location", "/ui/") } test("Get a directory without leading / will send a redirect") { @@ -32,7 +33,8 @@ class WebJarServletTest extends MarathonSpec with Mockito with GivenWhenThen wit servlet.doGet(request, response) Then("A redirect response is send") - verify(response, atLeastOnce).sendRedirect("/some/directory/") + verify(response, atLeastOnce).setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY) + verify(response, atLeastOnce).setHeader("Location", "/some/directory/") } test("Get a non existing path will return 404"){