From 12598f85811c61dbe2a205826274ec4dd6a85961 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 10 Feb 2014 16:55:21 -0500 Subject: [PATCH] Fix issue w/ use of UrlPathHelper's urlDecode property Before this change the getPathWithinServletMapping method of UrlPathHelper could not work properly when a default servlet mapping (i.e. "/") was used in combination with urlDecode=false. The fact that the getServletPath() method of HttpServletRequest always returns a decoded path was getting in the way. Although there is no way to check Servlet mappings through the Servlet API, this change aims to detect the given scenario and returns the full path following the context path thus avoiding URL decoding. Note that the same can be achieved by setting urlDecode=false and alwaysUseFullPath=true. However this change ensures that urlDecode works properly without having to know that. Issue: SPR-11101 --- .../web/util/UrlPathHelper.java | 23 +++++++++++++++---- .../web/util/UrlPathHelperTests.java | 14 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java index e4f4e825f19..4661b4af033 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java +++ b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -181,11 +181,24 @@ public class UrlPathHelper { } else { // Special case: URI is different from servlet path. - // Can happen e.g. with index page: URI="/", servletPath="/index.html" - // Use path info if available, as it indicates an index page within - // a servlet mapping. Otherwise, use the full servlet path. String pathInfo = request.getPathInfo(); - return (pathInfo != null ? pathInfo : servletPath); + if (pathInfo != null) { + // Use path info if available. Indicates index page within a servlet mapping? + // e.g. with index page: URI="/", servletPath="/index.html" + return pathInfo; + } + if (this.urlDecode == false) { + // No path info... (not mapped by prefix, nor by extension, nor "/*") + // For the default servlet mapping (i.e. "/"), urlDecode=false can + // cause issues since getServletPath() returns a decoded path. + // If decoding pathWithinApp yields a match just use pathWithinApp + path = getRemainingPath(decodeInternal(request, pathWithinApp), servletPath, false); + if (path != null) { + return pathWithinApp; + } + } + // Otherwise, use the full servlet path + return servletPath; } } diff --git a/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java b/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java index de6dd8de5d8..c4b21053d9e 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java @@ -77,6 +77,20 @@ public class UrlPathHelperTests { assertEquals("Incorrect path returned", "/welcome.html", helper.getPathWithinServletMapping(request)); } + // SPR-11101 + + @Test + public void getPathWithinServletWithoutUrlDecoding() { + request.setContextPath("/SPR-11101"); + request.setServletPath("/test_url_decoding/a/b"); + request.setRequestURI("/test_url_decoding/a%2Fb"); + + helper.setUrlDecode(false); + String actual = helper.getPathWithinServletMapping(request); + + assertEquals("/test_url_decoding/a%2Fb", actual); + } + @Test public void getRequestUri() { request.setRequestURI("/welcome.html");