You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jb...@apache.org on 2007/03/06 10:05:58 UTC

svn commit: r515036 - in /incubator/wicket/branches/wicket-1.x/wicket/src: main/java/wicket/ main/java/wicket/markup/html/form/ main/java/wicket/markup/resolver/ main/java/wicket/protocol/http/ main/java/wicket/protocol/http/request/ main/java/wicket/u...

Author: jbq
Date: Tue Mar  6 01:05:57 2007
New Revision: 515036

URL: http://svn.apache.org/viewvc?view=rev&rev=515036
Log:
WICKET-358 Properly decode URLs

Added:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java   (with props)
Modified:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/PageParameters.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/form/Form.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/resolver/AutoLinkResolver.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebRequestWithCryptedUrl.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategy.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/value/ValueMap.java
    incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/protocol/http/WebRequestTest.java

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/PageParameters.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/PageParameters.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/PageParameters.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/PageParameters.java Tue Mar  6 01:05:57 2007
@@ -18,6 +18,7 @@
 
 import java.util.Map;
 
+import wicket.protocol.http.RequestUtils;
 import wicket.util.string.IStringIterator;
 import wicket.util.string.StringList;
 import wicket.util.value.ValueMap;
@@ -84,6 +85,11 @@
 	 * @param delimiter
 	 *            Delimiter string used to separate key/value pairs
 	 * @see ValueMap#ValueMap(String)
+	 * 
+	 * @deprecated Please use
+	 *             {@link RequestUtils#decodeParameters(String, ValueMap)} to
+	 *             decode a request URL, or
+	 *             {@link ValueMap#ValueMap(String, String)} for other usecases.
 	 */
 	public PageParameters(final String keyValuePairs, final String delimiter)
 	{

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/form/Form.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/form/Form.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/form/Form.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/html/form/Form.java Tue Mar  6 01:05:57 2007
@@ -20,7 +20,6 @@
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
-import java.util.StringTokenizer;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -42,6 +41,7 @@
 import wicket.markup.html.form.validation.IFormValidator;
 import wicket.model.IModel;
 import wicket.model.Model;
+import wicket.protocol.http.RequestUtils;
 import wicket.protocol.http.WebRequest;
 import wicket.protocol.http.WebRequestCycle;
 import wicket.request.IRequestCycleProcessor;
@@ -53,6 +53,7 @@
 import wicket.util.string.interpolator.MapVariableInterpolator;
 import wicket.util.upload.FileUploadException;
 import wicket.util.upload.FileUploadBase.SizeLimitExceededException;
+import wicket.util.value.ValueMap;
 
 /**
  * Base class for forms. To implement a form, subclass this class, add
@@ -1280,7 +1281,7 @@
 
 		private final String url;
 
-		private final Map params = new HashMap(4);
+		private final ValueMap params = new ValueMap();
 
 		/**
 		 * Construct.
@@ -1293,23 +1294,8 @@
 			this.realRequest = realRequest;
 			this.url = realRequest.decodeURL(url);
 
-			String queryPart = this.url.substring(this.url.indexOf("?") + 1);
-			StringTokenizer paramsSt = new StringTokenizer(queryPart, "&");
-			while (paramsSt.hasMoreTokens())
-			{
-				String param = paramsSt.nextToken();
-				int equalsSign = param.indexOf("=");
-				if (equalsSign >= 0)
-				{
-					String paramName = param.substring(0, equalsSign);
-					String value = param.substring(equalsSign + 1);
-					params.put(paramName, value);
-				}
-				else
-				{
-					params.put(param, "");
-				}
-			}
+			String queryString = this.url.substring(this.url.indexOf("?") + 1);
+			RequestUtils.decodeParameters(queryString, params);
 		}
 
 		/**

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/resolver/AutoLinkResolver.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/resolver/AutoLinkResolver.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/resolver/AutoLinkResolver.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/resolver/AutoLinkResolver.java Tue Mar  6 01:05:57 2007
@@ -38,9 +38,9 @@
 import wicket.markup.html.link.BookmarkablePageLink;
 import wicket.markup.html.link.ExternalLink;
 import wicket.markup.parser.filter.WicketLinkTagHandler;
+import wicket.protocol.http.RequestUtils;
 import wicket.util.lang.Packages;
 import wicket.util.string.Strings;
-import wicket.util.value.ValueMap;
 
 /**
  * The AutoLinkResolver is responsible to handle automatic link resolution. Tags
@@ -255,7 +255,8 @@
 			if (queryStringPos != -1)
 			{
 				final String queryString = reference.substring(queryStringPos + 1);
-				pageParameters = new PageParameters(new ValueMap(queryString, "&"));
+				pageParameters = new PageParameters();
+				RequestUtils.decodeParameters(queryString, pageParameters);
 				infoPath = reference.substring(0, queryStringPos);
 			}
 			else
@@ -840,4 +841,4 @@
 
 		return autoComponent;
 	}
-}
\ No newline at end of file
+}

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockHttpServletRequest.java Tue Mar  6 01:05:57 2007
@@ -23,7 +23,6 @@
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.security.Principal;
 import java.text.DateFormat;
@@ -36,7 +35,6 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
-import java.util.StringTokenizer;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
@@ -1128,17 +1126,7 @@
 			path = url.substring(0, index);
 
 			String queryString = url.substring(index + 1);
-			StringTokenizer st = new StringTokenizer(queryString, "&");
-			while (st.hasMoreTokens())
-			{
-				String token = st.nextToken();
-				int tmp = token.indexOf("=");
-				if (tmp != -1)
-				{
-					setParameter(token.substring(0, tmp), token.substring(tmp + 1));
-				}
-			}
-
+			RequestUtils.decodeParameters(queryString, parameters);
 		}
 	}
 
@@ -1273,24 +1261,8 @@
 	{
 		parameters.clear();
 
-		final String paramPart = redirect.substring(redirect.indexOf('?') + 1);
-		final String[] paramTuples = paramPart.split("&");
-		for (int t = 0; t < paramTuples.length; t++)
-		{
-			final String[] bits = paramTuples[t].split("=");
-			if (bits.length == 2)
-			{
-				try
-				{
-					parameters.put(URLDecoder.decode(bits[0], "UTF-8"), URLDecoder.decode(bits[1],
-							"UTF-8"));
-				}
-				catch (UnsupportedEncodingException e)
-				{
-					// Should never happen
-				}
-			}
-		}
+		final String queryString = redirect.substring(redirect.indexOf('?') + 1);
+		RequestUtils.decodeParameters(queryString, parameters);
 	}
 
 	/**

Added: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java?view=auto&rev=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java (added)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java Tue Mar  6 01:05:57 2007
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package wicket.protocol.http;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+
+import wicket.util.value.ValueMap;
+
+public class RequestUtils
+{
+	public static void decodeParameters(String queryString, ValueMap params)
+	{
+		final String[] paramTuples = queryString.split("&");
+		for (int t = 0; t < paramTuples.length; t++)
+		{
+			final String[] bits = paramTuples[t].split("=");
+			try
+			{
+				if (bits.length == 2)
+				{
+					params.add(URLDecoder.decode(bits[0], "UTF-8"), URLDecoder.decode(bits[1],
+							"UTF-8"));
+				}
+				else
+				{
+					params.add(URLDecoder.decode(bits[0], "UTF-8"), "");
+				}
+			}
+			catch (UnsupportedEncodingException e)
+			{
+				// Should never happen
+			}
+		}
+	}
+}

Propchange: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/RequestUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebRequestWithCryptedUrl.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebRequestWithCryptedUrl.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebRequestWithCryptedUrl.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebRequestWithCryptedUrl.java Tue Mar  6 01:05:57 2007
@@ -26,12 +26,11 @@
 import javax.servlet.http.HttpServletRequest;
 
 import wicket.Application;
+import wicket.PageParameters;
 import wicket.WicketRuntimeException;
 import wicket.protocol.http.request.WebRequestCodingStrategy;
 import wicket.protocol.http.servlet.ServletWebRequest;
 import wicket.util.crypt.ICrypt;
-import wicket.util.string.IStringIterator;
-import wicket.util.string.StringList;
 import wicket.util.string.Strings;
 import wicket.util.value.ValueMap;
 
@@ -77,7 +76,8 @@
 			this.queryString = rebuildUrl(queryString);
 
 			// extract parameter key/value pairs from the query string
-			this.parameters = analyzeQueryString(this.queryString);
+			this.parameters = new PageParameters();
+			RequestUtils.decodeParameters(this.queryString, this.parameters);
 		}
 		else
 		{
@@ -180,67 +180,6 @@
 				WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME + "=");
 
 		return queryString.toString();
-	}
-
-	/**
-	 * Extract key/value pairs from query string
-	 * 
-	 * @param queryString
-	 *            The query string
-	 * @return A map of query string parameter keys and values
-	 */
-	private ValueMap analyzeQueryString(final String queryString)
-	{
-		final ValueMap params = new ValueMap();
-
-		// Get a list of strings separated by the delimiter
-		final StringList pairs = StringList.tokenize(queryString, "&");
-
-		// Go through each string in the list
-		for (IStringIterator iterator = pairs.iterator(); iterator.hasNext();)
-		{
-			// Get the next key value pair
-			final String pair = iterator.next();
-
-			// separate key and value
-			final int pos = pair.indexOf("=");
-			if (pos < 0)
-			{
-				String[] prevValue = (String[])params.get(pair);
-				if (prevValue != null)
-				{
-					String[] newValue = new String[prevValue.length + 1];
-					System.arraycopy(prevValue, 0, newValue, 0, prevValue.length);
-					newValue[prevValue.length] = "";
-					params.put(pair, newValue);
-				}
-				else
-				{
-					// Parameter without value
-					params.put(pair, new String[] { "" });
-				}
-			}
-			else
-			{
-				final String key = pair.substring(0, pos);
-				final String value = pair.substring(pos + 1);
-				String[] prevValue = (String[])params.get(key);
-				if (prevValue != null)
-				{
-					String[] newValue = new String[prevValue.length + 1];
-					System.arraycopy(prevValue, 0, newValue, 0, prevValue.length);
-					newValue[prevValue.length] = value;
-					params.put(key, newValue);
-				}
-				else
-				{
-					// Parameter without value
-					params.put(key, new String[] { value });
-				}
-			}
-		}
-
-		return params;
 	}
 
 	/**

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategy.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategy.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategy.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/request/CryptedUrlWebRequestCodingStrategy.java Tue Mar  6 01:05:57 2007
@@ -29,16 +29,17 @@
 
 import wicket.Application;
 import wicket.IRequestTarget;
-import wicket.PageParameters;
 import wicket.Request;
 import wicket.RequestCycle;
 import wicket.WicketRuntimeException;
+import wicket.protocol.http.RequestUtils;
 import wicket.request.IRequestCodingStrategy;
 import wicket.request.RequestParameters;
 import wicket.request.target.coding.IRequestTargetUrlCodingStrategy;
 import wicket.util.crypt.ICrypt;
 import wicket.util.string.AppendingStringBuffer;
 import wicket.util.string.Strings;
+import wicket.util.value.ValueMap;
 
 /**
  * This is a request coding strategy which encrypts the URL and hence makes it
@@ -406,7 +407,8 @@
 			}
 
 			// Add ALL of the params from the decoded 'x' param
-			PageParameters params = new PageParameters(decodedParamReplacement, "&");
+			ValueMap params = new ValueMap();
+			RequestUtils.decodeParameters(decodedParamReplacement, params);
 			this.parameterMap.putAll(params);
 
 			// Rebuild the URL with the 'x' param removed

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/value/ValueMap.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/value/ValueMap.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/value/ValueMap.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/value/ValueMap.java Tue Mar  6 01:05:57 2007
@@ -81,7 +81,8 @@
 	}
 
 	/**
-	 * Constructor.
+	 * Constructor. NOTE: Please use RequestUtils.decodeParameters() if you wish
+	 * to properly decode a request URL.
 	 * 
 	 * @param keyValuePairs
 	 *            List of key value pairs separated by commas. For example,
@@ -93,7 +94,8 @@
 	}
 
 	/**
-	 * Constructor.
+	 * Constructor. NOTE: Please use RequestUtils.decodeParameters() if you wish
+	 * to properly decode a request URL.
 	 * 
 	 * @param keyValuePairs
 	 *            List of key value pairs separated by a given delimiter. For

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/protocol/http/WebRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/protocol/http/WebRequestTest.java?view=diff&rev=515036&r1=515035&r2=515036
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/protocol/http/WebRequestTest.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/protocol/http/WebRequestTest.java Tue Mar  6 01:05:57 2007
@@ -16,6 +16,7 @@
  */
 package wicket.protocol.http;
 
+import wicket.WicketTestCase;
 import wicket.protocol.http.servlet.ServletWebRequest;
 import wicket.util.tester.WicketTester;
 import junit.framework.TestCase;
@@ -25,7 +26,7 @@
  * 
  * @author Frank Bille (billen)
  */
-public class WebRequestTest extends TestCase
+public class WebRequestTest extends WicketTestCase
 {
 	/**
 	 * Test that ajax is true when the ajax header is present in the request
@@ -34,7 +35,7 @@
 	{
 		assertWithHeader("Wicket-Ajax", "true", true);
 	}
-	
+
 	/**
 	 * Test that it also works when there are other "positive" values than true.
 	 */
@@ -45,7 +46,7 @@
 		assertWithHeader("Wicket-Ajax", "on", true);
 		assertWithHeader("Wicket-Ajax", "y", true);
 	}
-	
+
 	/**
 	 * Test that it's not ajax.
 	 */
@@ -64,11 +65,40 @@
 
 	private void assertWithHeader(String header, String value, boolean isAjax)
 	{
-		MockHttpServletRequest mockRequest = new WicketTester().getServletRequest();
+		MockHttpServletRequest mockRequest = tester.getServletRequest();
 		mockRequest.addHeader(header, value);
 
 		WebRequest webRequest = new ServletWebRequest(mockRequest);
 
 		assertEquals(isAjax, webRequest.isAjax());
+	}
+
+	public void testStringArray()
+	{
+		MockHttpServletRequest mockRequest = tester.getServletRequest();
+		mockRequest.setRequestToRedirectString("?a=1&a=2");
+		Object obj = mockRequest.getParameterMap().get("a");
+		assertTrue("Expected " + new String[0].getClass() + ", got " + obj.getClass(),
+				obj instanceof String[]);
+	}
+
+	public void testStringEncoding()
+	{
+		MockHttpServletRequest mockRequest = tester.getServletRequest();
+		mockRequest.setRequestToRedirectString("?a=%20");
+		String value = mockRequest.getParameter("a");
+		assertEquals(" ", value);
+	}
+
+	public void testEmptyParam()
+	{
+		MockHttpServletRequest mockRequest = tester.getServletRequest();
+		mockRequest.setRequestToRedirectString("?a=");
+		String value = mockRequest.getParameter("a");
+		assertEquals("", value);
+
+		mockRequest.setRequestToRedirectString("?a");
+		value = mockRequest.getParameter("a");
+		assertEquals("", value);
 	}
 }