olegk 2003/03/13 09:51:28 Modified: httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java httpclient/src/test/org/apache/commons/httpclient SimpleHttpConnection.java SimpleHttpMethod.java TestNoHost.java Added: httpclient/src/test/org/apache/commons/httpclient TestRequestLine.java Log: Bug #12798 fix http://nagoya.apache.org/bugzilla/show_bug.cgi?id=12798 Changelog: - HttpMethodBase#generateRequestLine() refactored - Path is no more url-encoded in HttpMethodBase#generateRequestLine() Contributed by Oleg Kalnichevski Revision Changes Path 1.123 +48 -46 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.122 retrieving revision 1.123 diff -u -r1.122 -r1.123 --- HttpMethodBase.java 11 Mar 2003 22:20:09 -0000 1.122 +++ HttpMethodBase.java 13 Mar 2003 17:51:28 -0000 1.123 @@ -76,6 +76,7 @@ import org.apache.commons.httpclient.cookie.MalformedCookieException; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.cookie.CookieSpec; +import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.util.URIUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -238,8 +239,11 @@ /** * Constructor specifying a URI. + * It is responsibility of the caller to ensure that URI elements + * (path & query parameters) are properly encoded (URL safe). * - * @param uri either an absolute or relative URI + * @param uri either an absolute or relative URI. The URI is expected + * to be URL-encoded * * @throws IllegalArgumentException when URI is invalid * @throws IllegalStateException when protocol of the absolute URI is not recognised @@ -253,15 +257,7 @@ if (uri == null || uri.equals("")) { uri = "/"; } - - URI parsedURI; - try { - // for an escaped form on communication functions - parsedURI = new URI(uri.toCharArray()); - } catch (URIException urie) { - // it is supposed that an URI is sent not to be escaped - parsedURI = new URI(uri); - } + URI parsedURI = new URI(uri); // only set the host if specified by the URI if (parsedURI.isAbsoluteURI()) { @@ -400,8 +396,11 @@ /** * Set the path part of my request. + * It is responsibility of the caller to ensure that the path is + * properly encoded (URL safe). * - * @param path the path to request + * @param path the path to request. The path is expected + * to be URL-encoded */ public void setPath(String path) { this.path = path; @@ -458,7 +457,8 @@ * Set my query string. * * @param params an array of [EMAIL PROTECTED] NameValuePair}s to add as query string - * parameterss + * parameters. The name/value pairs will be automcatically + * URL encoded */ public void setQueryString(NameValuePair[] params) { LOG.trace("enter HttpMethodBase.setQueryString(NameValuePair[])"); @@ -1555,50 +1555,52 @@ * @param name the method name generate a request for * @param requestPath the path string for the request * @param query the query string for the request - * @param protocol the protocol to use (e.g. HTTP/1.0) + * @param version the protocol version to use (e.g. HTTP/1.0) * * @return a line to send to the server that will fulfil the request */ protected static String generateRequestLine(HttpConnection connection, - String name, String requestPath, String query, String protocol) { + String name, String requestPath, String query, String version) { LOG.trace("enter HttpMethodBase.generateRequestLine(HttpConnection, " + "String, String, String, String)"); StringBuffer buf = new StringBuffer(); - String path = null; - try { - path = (requestPath == null) ? "/" : URIUtil.encodePath(requestPath); - } catch (URIException urie) { - LOG.error("URI path encoding error"); - path = requestPath; + // Append method name + buf.append(name); + buf.append(" "); + // Absolute or relative URL? + if (!connection.isTransparent()) { + Protocol protocol = connection.getProtocol(); + buf.append(protocol.getScheme().toLowerCase()); + buf.append("://"); + buf.append(connection.getHost()); + if ((connection.getPort() != -1) && (connection.getPort() != protocol.getDefaultPort())) { + buf.append(":"); + buf.append(connection.getPort()); + } + } + // Append path, if any + if (requestPath == null) { + buf.append("/"); + } else { + if (!connection.isTransparent() && !requestPath.startsWith("/")) { + buf.append("/"); + } + buf.append(requestPath); } - buf.append(path); + // Append query, if any if (query != null) { if (query.indexOf("?") != 0) { buf.append("?"); } - String queryString = null; - queryString = (query == null) ? "/" : query; - buf.append(queryString); - } - - if (!connection.isProxied() || connection.isTransparent()) { - return (name + " " + buf.toString() + " " + protocol + "\r\n"); - } else { - if (connection.isSecure()) { - return (name + " https://" + connection.getHost() - + ((443 == connection.getPort() - || -1 == connection.getPort()) - ? "" : (":" + connection.getPort())) + buf.toString() - + " " + protocol + "\r\n"); - } else { - return (name + " http://" + connection.getHost() - + ((80 == connection.getPort() - || -1 == connection.getPort()) - ? "" : (":" + connection.getPort())) + buf.toString() - + " " + protocol + "\r\n"); - } + buf.append(query); } + // Append protocol + buf.append(" "); + buf.append(version); + buf.append("\r\n"); + + return buf.toString(); } /** 1.11 +12 -9 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java Index: SimpleHttpConnection.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- SimpleHttpConnection.java 16 Feb 2003 13:08:34 -0000 1.10 +++ SimpleHttpConnection.java 13 Mar 2003 17:51:28 -0000 1.11 @@ -114,6 +114,15 @@ super(null, -1, "localhost", 80, Protocol.getProtocol("http")); } + public SimpleHttpConnection( + String proxyHost, + int proxyPort, + String host, + int port, + Protocol protocol) { + super(proxyHost, proxyPort, host, port, protocol); + } + public SimpleHttpConnection(String host, int port){ super(host, port, Protocol.getProtocol("http")); } @@ -191,12 +200,6 @@ return str; } - public boolean waitForResponse(long timeout_ms) - throws IOException, IllegalStateException { - return true; - } - - public InputStream getResponseInputStream() { return inputStream; } 1.6 +9 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpMethod.java Index: SimpleHttpMethod.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpMethod.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- SimpleHttpMethod.java 11 Feb 2003 03:23:05 -0000 1.5 +++ SimpleHttpMethod.java 13 Mar 2003 17:51:28 -0000 1.6 @@ -139,5 +139,10 @@ ensureResponseHeaderIsSet(); return super.getResponseHeaders(); } - + + + public String getTestRequestLine(HttpConnection connection) { + return HttpMethodBase.generateRequestLine(connection, + this.getName(), this.getPath(), this.getQueryString(), "HTTP/1.1"); + } } 1.20 +5 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java Index: TestNoHost.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- TestNoHost.java 28 Jan 2003 05:17:22 -0000 1.19 +++ TestNoHost.java 13 Mar 2003 17:51:28 -0000 1.20 @@ -100,6 +100,7 @@ suite.addTest(TestRequestHeaders.suite()); suite.addTest(TestStreams.suite()); suite.addTest(TestStatusLine.suite()); + suite.addTest(TestRequestLine.suite()); suite.addTest(TestPartsNoHost.suite()); return suite; } 1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java Index: TestRequestLine.java =================================================================== /* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.commons.httpclient; import org.apache.commons.httpclient.protocol.Protocol; import junit.framework.*; /** * Simple tests for [EMAIL PROTECTED] StatusLine}. * * @author <a href="mailto:[EMAIL PROTECTED]">oleg Kalnichevski</a> * @version $Id: TestRequestLine.java,v 1.1 2003/03/13 17:51:28 olegk Exp $ */ public class TestRequestLine extends TestCase { private StatusLine statusLine = null; // ------------------------------------------------------------ Constructor public TestRequestLine(String testName) { super(testName); } // ------------------------------------------------------------------- Main public static void main(String args[]) { String[] testCaseName = { TestRequestLine.class.getName() }; junit.textui.TestRunner.main(testCaseName); } // ------------------------------------------------------- TestCase Methods public static Test suite() { return new TestSuite(TestRequestLine.class); } // ------------------------------------------------------ Protected Methods // ----------------------------------------------------------- Test Methods public void testRequestLineGeneral() throws Exception { SimpleHttpConnection conn = null; SimpleHttpMethod method = null; conn = new SimpleHttpConnection(null, -1, "localhost", 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple / HTTP/1.1\r\n", method.getTestRequestLine(conn)); method = new SimpleHttpMethod("stuff"); assertEquals("Simple stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); conn = new SimpleHttpConnection("proxy", 8080, "localhost", 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); method = new SimpleHttpMethod("stuff"); assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); conn = new SimpleHttpConnection("proxy", 8080, "localhost", -1, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); method = new SimpleHttpMethod("stuff"); assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); conn = new SimpleHttpConnection("proxy", 8080, "localhost", 666, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple http://localhost:666/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); method = new SimpleHttpMethod("stuff"); assertEquals("Simple http://localhost:666/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); } public void testRequestLineQuery() throws Exception { SimpleHttpConnection conn = null; SimpleHttpMethod method = null; conn = new SimpleHttpConnection(null, -1, "localhost", 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); method.setQueryString( new NameValuePair[] { new NameValuePair("param1", "[EMAIL PROTECTED]&"), new NameValuePair("param2", "some stuff") } ); assertEquals("Simple /?param1=!%40%23%24%25%5E%26¶m2=some%20stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); } public void testRequestLinePath() throws Exception { SimpleHttpConnection conn = null; SimpleHttpMethod method = null; conn = new SimpleHttpConnection(null, -1, "localhost", 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); method.setPath("/some%20stuff/"); assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); method = new SimpleHttpMethod("/some%20stuff/"); assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]