jericho 2002/10/22 18:39:39 Modified: httpclient/src/java/org/apache/commons/httpclient/util URIUtil.java httpclient/src/test/org/apache/commons/httpclient TestWebappRedirect.java httpclient/src/java/org/apache/commons/httpclient/methods PostMethod.java httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java Log: - Add encodeInQuery in URIUtil class for HTTP URL (It's useful for query inside manipulation) - Apply the encodeInQuery method for inside of the query component Revision Changes Path 1.6 +47 -16 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java Index: URIUtil.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- URIUtil.java 21 Oct 2002 12:48:00 -0000 1.5 +++ URIUtil.java 23 Oct 2002 01:39:39 -0000 1.6 @@ -83,6 +83,16 @@ protected static final BitSet empty = new BitSet(1); + protected static final BitSet allowed_in_query = new BitSet(256); + // Static initializer for allowed_in_query + static { + allowed_in_query.or(URI.allowed_query); + allowed_in_query.clear('?'); + allowed_in_query.clear('='); + allowed_in_query.clear('&'); + allowed_in_query.clear('#'); + } + // ---------------------------------------------------------- URI utilities /** @@ -195,6 +205,8 @@ /** * Get the all escaped and encoded string with the default protocl charset. + * It's the same function to use <code>encode(String unescaped, Bitset + * empty, URI.getProtocolCharset())</code>. * * @param unescaped an unescaped string * @param charset the charset @@ -210,6 +222,8 @@ /** * Get the all escaped and encoded string with a given charset. + * It's the same function to use <code>encode(String unescaped, Bitset + * empty, String charset)</code>. * * @param unescaped an unescaped string * @param charset the charset @@ -298,8 +312,8 @@ /** - * Escape and encode a string regarded as the query component of an URI with - * the default protocol charset. + * Escape and encode a string regarded as inside the query component of an + * URI with the default protocol charset. * * @param unescaped an unescaped string * @return the escaped string @@ -307,14 +321,14 @@ * @see URI#getProtocolCharset * @see #encode */ - public static String encodeQuery(String unescaped) throws URIException { + public static String encodeInQuery(String unescaped) throws URIException { return encodeQuery(unescaped, URI.getProtocolCharset()); } /** - * Escape and encode a string regarded as the query component of an URI with - * a given charset. + * Escape and encode a string regarded as inside the query component of an + * URI with a given charset. * * @param unescaped an unescaped string * @param charset the charset @@ -322,27 +336,44 @@ * @exception URIException * @see #encode */ - public static String encodeQuery(String unescaped, String charset) + public static String encodeInQuery(String unescaped, String charset) throws URIException { - return encode(unescaped, URI.allowed_query, charset); + return encode(unescaped, allowed_in_query, charset); } /** - * Escape and encode a given string and the default protocol charset. + * Escape and encode a string regarded as the query component of an URI with + * the default protocol charset. * - * @param unescaped a string + * @param unescaped an unescaped string * @return the escaped string * @exception URIException * @see URI#getProtocolCharset - * @see Coder#encode + * @see #encode */ - public static String encode(String unescaped) + public static String encodeQuery(String unescaped) throws URIException { + return encodeQuery(unescaped, URI.getProtocolCharset()); + } + + + /** + * Escape and encode a string regarded as the query component of an URI with + * a given charset. + * + * @param unescaped an unescaped string + * @param charset the charset + * @return the escaped string + * @exception URIException + * @see #encode + */ + public static String encodeQuery(String unescaped, String charset) throws URIException { - return encode(unescaped, empty, URI.getProtocolCharset()); + return encode(unescaped, URI.allowed_query, charset); } + /** * Escape and encode a given string with allowed characters not to be 1.9 +9 -9 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappRedirect.java Index: TestWebappRedirect.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappRedirect.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- TestWebappRedirect.java 22 Oct 2002 18:23:25 -0000 1.8 +++ TestWebappRedirect.java 23 Oct 2002 01:39:39 -0000 1.9 @@ -171,9 +171,9 @@ String qs = "http://" + host + ":" + port + "/" + context + "/params?foo=bar&bar=foo"; for(int i=0;i<2;i++) { - qs = "http://" + host + ":" + port + "/" + context + "/redirect?to=" + URIUtil.encodeQuery(qs); + qs = "http://" + host + ":" + port + "/" + context + "/redirect?to=" + URIUtil.encodeInQuery(qs); } - method.setQueryString("to=" + URIUtil.encodeQuery(qs)); + method.setQueryString("to=" + URIUtil.encodeInQuery(qs)); method.setUseDisk(false); try { client.executeMethod(method); @@ -209,7 +209,7 @@ HttpClient client = new HttpClient(); client.startSession(host, port); PostMethod method = new PostMethod("/" + context + "/redirect"); - method.setQueryString("to=" + URIUtil.encodeQuery("http://" + host + ":" + port + "/" + context + "/params?foo=bar&bar=foo")); + method.setQueryString("to=" + URIUtil.encodeInQuery("http://" + host + ":" + port + "/" + context + "/params?foo=bar&bar=foo")); method.setRequestBody(new ByteArrayInputStream(body.getBytes())); method.setRequestContentLength(body.length()); //unbuffered request method.setFollowRedirects(true); @@ -224,7 +224,7 @@ assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,method.getStatusCode()); method = new PostMethod("/" + context + "/redirect"); - method.setQueryString("to=" + URIUtil.encodeQuery("http://" + host + ":" + port + "/" + context + "/params?foo=bar&bar=foo")); + method.setQueryString("to=" + URIUtil.encodeInQuery("http://" + host + ":" + port + "/" + context + "/params?foo=bar&bar=foo")); method.setRequestBody(new ByteArrayInputStream(body.getBytes())); method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO); //buffered request method.setFollowRedirects(true); @@ -244,7 +244,7 @@ HttpClient client = new HttpClient(); client.startSession(host, port); PutMethod method = new PutMethod("/" + context + "/redirect"); - method.setQueryString("to=" + URIUtil.encodeQuery("http://" + host + ":" + port + "/" + context + "/body?foo=bar&bar=foo")); + method.setQueryString("to=" + URIUtil.encodeInQuery("http://" + host + ":" + port + "/" + context + "/body?foo=bar&bar=foo")); method.setRequestBody("This is data to be sent in the body of an HTTP PUT."); try { client.executeMethod(method); 1.22 +5 -5 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java Index: PostMethod.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- PostMethod.java 21 Oct 2002 14:07:48 -0000 1.21 +++ PostMethod.java 23 Oct 2002 01:39:39 -0000 1.22 @@ -767,7 +767,7 @@ String queryName = null; try { - queryName = URIUtil.encodeQuery(parameter.getName()); + queryName = URIUtil.encodeInQuery(parameter.getName()); } catch (URIException urie) { log.error("URI query name encoding error", urie); queryName = parameter.getName(); @@ -775,7 +775,7 @@ buff.append(queryName).append("="); String queryValue = null; try { - queryValue = URIUtil.encodeQuery(parameter.getValue()); + queryValue = URIUtil.encodeInQuery(parameter.getValue()); } catch (URIException urie) { log.error("URI query value encoding error", urie); queryValue = parameter.getValue(); 1.71 +8 -7 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java Index: HttpMethodBase.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.70 retrieving revision 1.71 diff -u -r1.70 -r1.71 --- HttpMethodBase.java 22 Oct 2002 18:23:24 -0000 1.70 +++ HttpMethodBase.java 23 Oct 2002 01:39:39 -0000 1.71 @@ -379,7 +379,8 @@ /** * Sets the query string. * The user must ensure that the string is properly URL encoded. - * URIUtil.encodeQuery can be used to encode parameter names and values. + * URIUtil.encodeAll, URIUtil.encodeInQuery or URIUtil.encodeQuery can be + * used to encode parameter names and values. * The query string should not start with the question mark character. * * @param queryString the query string @@ -407,7 +408,7 @@ } String queryName = null; try { - queryName = URIUtil.encodeQuery(params[i].getName()); + queryName = URIUtil.encodeInQuery(params[i].getName()); } catch (URIException urie) { log.error("URI query name encoding error", urie); queryName = params[i].getName(); @@ -416,7 +417,7 @@ if (params[i].getValue() != null) { String queryValue = null; try { - queryValue = URIUtil.encodeQuery(params[i].getValue()); + queryValue = URIUtil.encodeInQuery(params[i].getValue()); } catch (URIException urie) { log.error("URI query value encoding error", urie); queryValue = params[i].getValue();
-- To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>