From 8130c842d1ef96d2aed3f88d1a724a239684e40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Henrique=20H=C3=BCpner?= Date: Wed, 28 Oct 2020 09:25:35 -0300 Subject: [PATCH] Fixes issue #1245 - sendRedirect without leading slash shouldn't be relative to the current servlet (#1246) --- .../impl/DefaultWebApplicationResponse.java | 2 +- .../webapp/impl/HttpServletResponseTest.java | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/webapp/impl/src/main/java/cloud/piranha/webapp/impl/DefaultWebApplicationResponse.java b/webapp/impl/src/main/java/cloud/piranha/webapp/impl/DefaultWebApplicationResponse.java index ac242e2a62..be866931f3 100644 --- a/webapp/impl/src/main/java/cloud/piranha/webapp/impl/DefaultWebApplicationResponse.java +++ b/webapp/impl/src/main/java/cloud/piranha/webapp/impl/DefaultWebApplicationResponse.java @@ -587,7 +587,7 @@ public void sendRedirect(String location) throws IOException { } else { url = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), request.getContextPath() - + request.getServletPath() + "/" + location); + + "/" + location); } } setHeader("Location", url.toExternalForm()); diff --git a/webapp/impl/src/test/java/cloud/piranha/webapp/impl/HttpServletResponseTest.java b/webapp/impl/src/test/java/cloud/piranha/webapp/impl/HttpServletResponseTest.java index d3d615fa35..b4e54aeb5e 100644 --- a/webapp/impl/src/test/java/cloud/piranha/webapp/impl/HttpServletResponseTest.java +++ b/webapp/impl/src/test/java/cloud/piranha/webapp/impl/HttpServletResponseTest.java @@ -109,7 +109,7 @@ void testRedirect2() throws Exception { webApplication.service(request, response); assertEquals(302, response.getStatus()); assertNotNull(response.getHeader("Location")); - assertEquals("http://localhost:80/servlet2a/servlet2b", response.getHeader("Location")); + assertEquals("http://localhost:80/servlet2b", response.getHeader("Location")); } /** @@ -148,6 +148,29 @@ void testRedirect4() throws Exception { assertEquals("http://this.is.outside/and_absolute", response.getHeader("Location")); } + + /** + * Test sendRedirect method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testRedirect5() throws Exception { + webApplication.addServlet("Servlet2a", TestRedirect2aServlet.class); + webApplication.addServlet("Servlet2b", TestRedirect2bServlet.class); + webApplication.setContextPath("/app"); + webApplication.addServletMapping("Servlet2a", "/servlet2a"); + webApplication.addServletMapping("Servlet2b", "/servlet2a/servlet2b"); + request.setContextPath("/app"); + request.setServletPath("/servlet2a"); + webApplication.initialize(); + webApplication.start(); + webApplication.service(request, response); + assertEquals(302, response.getStatus()); + assertNotNull(response.getHeader("Location")); + assertEquals("http://localhost:80/app/servlet2b", response.getHeader("Location")); + } + /** * Test setDateHeader method. */