Updated Branches:
  refs/heads/master 77ac60071 -> 6f0863f43

WICKET-4789 url rendering regression: keep trailing empty segment if base still 
has segments but relative has not


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/6f0863f4
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6f0863f4
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6f0863f4

Branch: refs/heads/master
Commit: 6f0863f431747e71b7a39a2c2ee44900955a858d
Parents: 77ac600
Author: svenmeier <[email protected]>
Authored: Tue Oct 2 18:01:22 2012 +0200
Committer: svenmeier <[email protected]>
Committed: Tue Oct 2 18:01:22 2012 +0200

----------------------------------------------------------------------
 .../main/java/org/apache/wicket/request/Url.java   |   77 ++++++++-------
 .../java/org/apache/wicket/request/UrlTest.java    |   29 +++++-
 2 files changed, 70 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/6f0863f4/wicket-request/src/main/java/org/apache/wicket/request/Url.java
----------------------------------------------------------------------
diff --git a/wicket-request/src/main/java/org/apache/wicket/request/Url.java 
b/wicket-request/src/main/java/org/apache/wicket/request/Url.java
index 364b5d3..9fb454c 100755
--- a/wicket-request/src/main/java/org/apache/wicket/request/Url.java
+++ b/wicket-request/src/main/java/org/apache/wicket/request/Url.java
@@ -81,8 +81,7 @@ public class Url implements Serializable
         * 
         * @author igor
         */
-       public static enum StringMode 
-       {
+       public static enum StringMode {
                /** local urls are rendered without the host name */
                LOCAL,
                /**
@@ -123,13 +122,13 @@ public class Url implements Serializable
        {
                Args.notNull(url, "url");
 
-               this.protocol = url.protocol;
-               this.host = url.host;
-               this.port = url.port;
-               this.segments = new ArrayList<String>(url.segments);
-               this.parameters = new ArrayList<QueryParameter>(url.parameters);
-               this.charsetName = url.charsetName;
-               this._charset = url._charset;
+               protocol = url.protocol;
+               host = url.host;
+               port = url.port;
+               segments = new ArrayList<String>(url.segments);
+               parameters = new ArrayList<QueryParameter>(url.parameters);
+               charsetName = url.charsetName;
+               _charset = url._charset;
        }
 
        /**
@@ -617,7 +616,7 @@ public class Url implements Serializable
                return toString(getCharset());
        }
 
-        /**
+       /**
         * Stringizes this url
         * 
         * @param mode
@@ -938,12 +937,12 @@ public class Url implements Serializable
        {
                if (getSegments().size() > 0)
                {
-                       // strip the first non-folder segment
+                       // strip the first non-folder segment (if it is not 
empty)
                        getSegments().remove(getSegments().size() - 1);
                }
 
-               // remove leading './' (current folder) and empty segments, 
process any ../ segments from the
-               // relative url
+               // remove leading './' (current folder) and empty segments, 
process any ../ segments from
+               // the relative url
                while (!relative.getSegments().isEmpty())
                {
                        if (".".equals(relative.getSegments().get(0)))
@@ -968,6 +967,11 @@ public class Url implements Serializable
                        }
                }
 
+               if (!getSegments().isEmpty() && 
relative.getSegments().isEmpty())
+               {
+                       getSegments().add("");
+               }
+
                // append the remaining relative segments
                getSegments().addAll(relative.getSegments());
 
@@ -1104,36 +1108,41 @@ public class Url implements Serializable
        {
                return getQueryString(getCharset());
        }
-       
-       
+
+
        /**
-        * Try to reduce url by eliminating '..' and '.' from the path where 
appropriate
-        * (this is somehow similar to {@link java.io.File#getCanonicalPath()}).
-        * Either by different / unexpected browser behavior or by malicious 
attacks it 
-        * can happen that these kind of redundant urls are processed by 
wicket. These urls 
-        * can cause some trouble when mapping the request.
-        * <p/> 
+        * Try to reduce url by eliminating '..' and '.' from the path where 
appropriate (this is
+        * somehow similar to {@link java.io.File#getCanonicalPath()}). Either 
by different / unexpected
+        * browser behavior or by malicious attacks it can happen that these 
kind of redundant urls are
+        * processed by wicket. These urls can cause some trouble when mapping 
the request.
+        * <p/>
         * <strong>example:</strong>
         * 
         * the url
         * 
-        * <pre>  /example/..;jsessionid=234792?0</pre> 
+        * <pre>
+        * /example/..;jsessionid=234792?0
+        * </pre>
         * 
-        * will not get normalized by the browser due to the ';jsessionid' 
string that 
-        * gets appended by the servlet container. After wicket strips the 
-        * jsessionid part the resulting internal url will be
+        * will not get normalized by the browser due to the ';jsessionid' 
string that gets appended by
+        * the servlet container. After wicket strips the jsessionid part the 
resulting internal url
+        * will be
         * 
-        * <pre>  /example/..</pre>
+        * <pre>
+        * /example/..
+        * </pre>
         * 
         * instead of
         * 
-        * <pre>  /</pre>
+        * <pre>
+        * /
+        * </pre>
         * 
         * <p/>
         * 
-        * This code correlates to 
-        * <a 
href="https://issues.apache.org/jira/browse/WICKET-4303";>WICKET-4303</a>
-        *
+        * This code correlates to <a
+        * 
href="https://issues.apache.org/jira/browse/WICKET-4303";>WICKET-4303</a>
+        * 
         * @return canonical url
         */
        public Url canonical()
@@ -1141,18 +1150,18 @@ public class Url implements Serializable
                Url url = new Url(this);
                url.segments.clear();
 
-               for (int i = 0; i < this.segments.size(); i++)
+               for (int i = 0; i < segments.size(); i++)
                {
-                       final String segment = this.segments.get(i);
+                       final String segment = segments.get(i);
 
-                       // drop '.' from path  
+                       // drop '.' from path
                        if (".".equals(segment))
                        {
                                continue;
                        }
 
                        // skip segment if following segment is a '..'
-                       if ((i + 1) < this.segments.size() && 
"..".equals(this.segments.get(i + 1)))
+                       if ((i + 1) < segments.size() && 
"..".equals(segments.get(i + 1)))
                        {
                                i++;
                                continue;

http://git-wip-us.apache.org/repos/asf/wicket/blob/6f0863f4/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java 
b/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
index 5cd96bb..36078a1 100644
--- a/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
+++ b/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
@@ -491,6 +491,32 @@ public class UrlTest extends Assert
        }
 
        /**
+        * @see <a 
href="https://issues.apache.org/jira/browse/WICKET-4789";>WICKET-4789</a>
+        */
+       @Test
+       public void resolveRelative_EmptyTrailingSegmentInBase()
+       {
+               Url relative = Url.parse("./?0-1.ILinkListener-link");
+               Url baseUrl = Url.parse("Home/");
+               baseUrl.resolveRelative(relative);
+
+               assertEquals("Home/?0-1.ILinkListener-link", 
baseUrl.toString());
+       }
+
+       /**
+        * @see <a 
href="https://issues.apache.org/jira/browse/WICKET-4789";>WICKET-4789</a>
+        */
+       @Test
+       public void resolveRelative_EmptyTrailingSegmentInBase2()
+       {
+               Url relative = Url.parse("./foo/?0-1.ILinkListener-link");
+               Url baseUrl = Url.parse("Home/");
+               baseUrl.resolveRelative(relative);
+
+               assertEquals("Home/foo/?0-1.ILinkListener-link", 
baseUrl.toString());
+       }
+
+       /**
         * Tries to resolve a relative url against a base that has no segments
         */
        @Test
@@ -544,8 +570,7 @@ public class UrlTest extends Assert
                Url baseUrl = Url.parse("bar/baz");
                baseUrl.resolveRelative(relative);
 
-               assertEquals("bar?a=b", baseUrl.toString());
-               assertEquals("no empty segment", 1, 
baseUrl.getSegments().size());
+               assertEquals("bar/?a=b", baseUrl.toString());
        }
 
        /**

Reply via email to