oglueck 2003/01/14 02:16:55 Modified: httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java httpclient/src/test/org/apache/commons/httpclient TestHttpUrlMethod.java Log: Fixed exception on non-standard protocol schemes. Contributed by Michael Becke Reviewed by Oleg Kalnichevski Revision Changes Path 1.94 +31 -31 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.93 retrieving revision 1.94 diff -u -r1.93 -r1.94 --- HttpMethodBase.java 20 Dec 2002 09:22:07 -0000 1.93 +++ HttpMethodBase.java 14 Jan 2003 10:16:55 -0000 1.94 @@ -158,6 +158,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Davanum Srinivas</a> * @author Ortwin Glück * @author Eric Johnson + * @author Michael Becke */ public abstract class HttpMethodBase implements HttpMethod { //~ Static variables/initializers ·········································· @@ -269,44 +270,43 @@ * Constructor specifying a URI. * * @param uri either an absolute or relative URI + * + * @throws IllegalArgumentException when URI is invalid + * @throws IllegalStateException when protocol of the absolute URI is not recognised */ public HttpMethodBase(String uri) { try { - // is this an absolute URI? - URL url = new URL( uri ); + + // create a URI and allow for null/empty uri values + if (uri == null || uri.equals( "" )) { + uri = "/"; + } + URI parsedURI = new URI( uri ); + + // only set the host if specified by the URI + if ( parsedURI.isAbsoluteURI() ) { + hostConfiguration = new HostConfiguration(); + hostConfiguration.setHost( + parsedURI.getHost(), + parsedURI.getPort(), + parsedURI.getScheme() + ); + } // set the path, defaulting to root setPath( - url.getPath() == null + parsedURI.getPath() == null ? "/" - : url.getPath() - ); - setQueryString( url.getQuery() ); - hostConfiguration = new HostConfiguration(); - hostConfiguration.setHost( - url.getHost(), - url.getPort(), - url.getProtocol() + : parsedURI.getPath() ); + setQueryString( parsedURI.getQuery() ); - } catch ( MalformedURLException e ) { - // this is not a URL - int pa = uri.indexOf("?"); - - if ( !uri.startsWith("/") ) { - // this path must be relative to root - uri = "/" + uri; - } - - if (pa < 0) { //its just a path - setPath(uri); - } else { //its a path with a query - setPath(path.substring(0, pa)); - setQueryString(uri.substring(pa+1, uri.length())); - } + } catch ( URIException e ) { + throw new IllegalArgumentException( + "Invalid uri '" + uri + "': " + e.getMessage() + ); } - } //~ Methods ································································ 1.4 +16 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpUrlMethod.java Index: TestHttpUrlMethod.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpUrlMethod.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TestHttpUrlMethod.java 19 Dec 2002 10:23:39 -0000 1.3 +++ TestHttpUrlMethod.java 14 Jan 2003 10:16:55 -0000 1.4 @@ -149,4 +149,16 @@ } + + public void testUrlGetMethodWithInvalidProtocol() { + try + { + GetMethod method = new GetMethod("crap://www.fubar.com/"); + fail("The use of invalid protocol must have resulted in an IllegalStateException"); + } + catch(IllegalStateException e) + { + //expected exception + } + } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>