Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.122 diff -u -r1.122 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 11 Mar 2003 22:20:09 -0000 1.122 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 13 Mar 2003 15:56:40 -0000 @@ -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 {@link 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(); } /** Index: test/org/apache/commons/httpclient/SimpleHttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v retrieving revision 1.10 diff -u -r1.10 SimpleHttpConnection.java --- test/org/apache/commons/httpclient/SimpleHttpConnection.java 16 Feb 2003 13:08:34 -0000 1.10 +++ test/org/apache/commons/httpclient/SimpleHttpConnection.java 13 Mar 2003 15:56:39 -0000 @@ -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; } Index: test/org/apache/commons/httpclient/SimpleHttpMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpMethod.java,v retrieving revision 1.5 diff -u -r1.5 SimpleHttpMethod.java --- test/org/apache/commons/httpclient/SimpleHttpMethod.java 11 Feb 2003 03:23:05 -0000 1.5 +++ test/org/apache/commons/httpclient/SimpleHttpMethod.java 13 Mar 2003 15:56:39 -0000 @@ -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"); + } } Index: test/org/apache/commons/httpclient/TestNoHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v retrieving revision 1.19 diff -u -r1.19 TestNoHost.java --- test/org/apache/commons/httpclient/TestNoHost.java 28 Jan 2003 05:17:22 -0000 1.19 +++ test/org/apache/commons/httpclient/TestNoHost.java 13 Mar 2003 15:56:39 -0000 @@ -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; } Index: test/org/apache/commons/httpclient/TestRequestLine.java =================================================================== RCS file: test/org/apache/commons/httpclient/TestRequestLine.java diff -N test/org/apache/commons/httpclient/TestRequestLine.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/TestRequestLine.java 13 Mar 2003 15:56:39 -0000 @@ -0,0 +1,165 @@ +/* + * ==================================================================== + * + * 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 apache@apache.org. + * + * 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 + * . + * + * [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 {@link StatusLine}. + * + * @author oleg Kalnichevski + * @version $Id: TestStatusLine.java,v 1.6 2003/02/07 04:50:03 jsdever 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", "!@#$%^&"), + 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)); + } +}