Hi,

I asked about this a week or two ago - and got a useful response.
Summary: the client libraries don't currently do URL encoding. As a
result, feeding them URLs with certain characters (spaces being the most
common) causes Bad Things to happen. I asked whether it was the place of
the client to do this, or the place of the client libraries. The answer
was, I believe, that the libraries should.

I also mentioned that I had code to do this. At that point, however, I
wasn't actually on this list. As a result, I forgot about this until I
got bitten by it again today.

So, here's a patch for it. 

I'm fairly convinced that the approach I took in the URLEncoder class is
overengineered in some ways, but still manages to miss doing the 'right
thing' in certain obscure cases (that I don't think will be hit by
anyone in practice. They mostly involve when to NOT escape parts of the
url. Getting this right involves having deep structural knowledge of the
URL. Actually providing methods for all of this would be painful, a lot
of work, and probably unusable. It might be useful instead to have
another path mutator which avoids the URL encoding, though, so that
people can do the encoding themselves (with full knowledge of the
semantics of the URL being encoded)  outside of the library). So, you
might not want to use this. 

However, it does the job for me, and is better than no solution, I
think.

Attached is a very small patch for WebdavMethodBase.java, and a new file
(URLEncoder.java - I've put this in
org.apache.webdav.lib.util.URLEncoder).

Michael
Index: methods/WebdavMethodBase.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/WebdavMethodBase.java,v
retrieving revision 1.9
diff -u -r1.9 WebdavMethodBase.java
--- methods/WebdavMethodBase.java       2001/01/29 15:26:39     1.9
+++ methods/WebdavMethodBase.java       2001/02/01 05:50:50
@@ -71,6 +71,7 @@
 import org.apache.webdav.lib.WebdavClient;
 import org.apache.webdav.lib.WebdavException;
 import org.apache.webdav.lib.WebdavStatus;
+import org.apache.webdav.lib.util.URLEncoder;
 
 
 /**
@@ -215,7 +216,7 @@
         if ((path == null) || (path.equals(""))) {
             this.path = "/";
         } else {
-            this.path = path;
+            this.path = new URLEncoder().encode(path);
         }
     }
 
/*
 * $Header:$
 * $Revision:$
 * $Date:$
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 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.webdav.lib.util;

/**
 * This class provides URL encoding of strings for purposes of HTTP
 * transmission, as defined in RFC 2396
 *
 * @author Michael Smith
 * @version $Revision:$
 */

public class URLEncoder {

        private static final String defaultAcceptable = 
                
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./-_.!~*'()";

        private String acceptable;

        /**
         * Create URLEncoder object accepting default characters.
         */
        public URLEncoder() {
                acceptable = defaultAcceptable;
        }

        /**
         * Create URLEncoder object which accepts the contents of the 
         * given string as well as the defaults.
         */
        public URLEncoder(String acceptable)
        {
                this.acceptable = defaultAcceptable+acceptable;
        }

        /**
         * URL-encode string.
         * @param s String to encode
         * @return Encoded string
         */
        public String encode(String s)
        {
                StringBuffer buf = new StringBuffer(s);
                int i;
                char current;

                for(i=0; i < buf.length(); i++)
                {
                        current = buf.charAt(i);
        
                        if(acceptable.indexOf(current) == -1)
                        {
                                buf.deleteCharAt(i); // delete what we're encoding
                                buf.insert(i, "%"+Integer.toHexString((int)current));
                                i += 2; // Inserted 3 characters for URL-encoding, 
which is
                                                // two more than expected. Skip over 
them.
                        }
                }

                return buf.toString();
        }

        /**
         * Add the given character to the list of acceptable characters.
         */ 
        public void addChar(char c)
        {
                if(acceptable.indexOf(c) == -1)
                        acceptable += c;
        }

        /**
         * Delete the given character from the list of acceptable characters.
         */
        public void delChar(char c)
        {
                int i = acceptable.indexOf(c);
                if(1==-1) return;
                StringBuffer buf = new StringBuffer(acceptable);

                buf.deleteCharAt(i);

                acceptable = buf.toString();
        }



}





Reply via email to