draier      2004/12/30 05:39:00

  Modified:    src/conf/catalina slide.properties
               src/share/org/apache/slide/util Configuration.java
               src/webdav/server/org/apache/slide/webdav/method
                        AbstractWebdavMethod.java PropFindMethod.java
               src/webdav/server/org/apache/slide/webdav/util
                        WebdavUtils.java
  Log:
  applied patch for url-encoding issue (#31265)
  
  PR: 31265
  
  Revision  Changes    Path
  1.5       +5 -1      jakarta-slide/src/conf/catalina/slide.properties
  
  Index: slide.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/conf/catalina/slide.properties,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- slide.properties  6 Dec 2004 08:54:14 -0000       1.4
  +++ slide.properties  30 Dec 2004 13:38:59 -0000      1.5
  @@ -22,9 +22,13 @@
   # Deafult: true
   org.apache.slide.redirectreferences=true
   
  +# Use UTF-8
  +# Default: true
  +org.apache.slide.useUtf8=true
  +
   # URL Encoding
   # Default: platform default encoding
  -org.apache.slide.urlEncoding=UTF-8
  +org.apache.slide.urlEncoding=WINDOWS-1252
   
   # Debug
   # Default: false
  
  
  
  1.22      +40 -6     
jakarta-slide/src/share/org/apache/slide/util/Configuration.java
  
  Index: Configuration.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/util/Configuration.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Configuration.java        6 Dec 2004 08:54:14 -0000       1.21
  +++ Configuration.java        30 Dec 2004 13:38:59 -0000      1.22
  @@ -139,7 +139,15 @@
   
   
           /**
  -         * Property specifying the encoding for URLs.
  +         * Property specifying if UTF-8 should be used to encode URLs.
  +         * <pre>
  +         * org.apache.slide.urlEncoding
  +         * </pre>
  +         */
  +        public static final String UseUtf = "org.apache.slide.useUtf8";
  +
  +        /**
  +         * Property specifying the encoding for URLs when it's not UTF-8.
            * <pre>
            * org.apache.slide.urlEncoding
            * </pre>
  @@ -238,6 +246,11 @@
   
   
       /**
  +     * Use UTF-8 encoding
  +     */
  +    private static boolean _useUtf8;
  +
  +    /**
        * URL encoding.
        */
       private static String _urlEncoding;
  @@ -323,13 +336,27 @@
   
   
       /**
  -     * Returns the used URL encoding.
  +     * Returns true if UTF-8 should be used to encode urls.
  +     */
  +    public static boolean useUTF8() {
  +        return _useUtf8;
  +    }
  +
  +    /**
  +     * Returns the used URL encoding when non UTF-8.
        */
       public static String urlEncoding() {
           return _urlEncoding;
       }
       
       /**
  +     * Returns the used URL encoding.
  +     */
  +    public static String realUrlEncoding() {
  +        return _useUtf8 ? "UTF-8" : _urlEncoding;
  +    }
  +
  +    /**
        * Returns true if Principal identified locks are enabled.
        */
       public static boolean usePrincipalIdentifiedLocks() {
  @@ -487,6 +514,13 @@
               _principalIdentifiedLocks = true;
           } else {
               _principalIdentifiedLocks = false;
  +        }
  +
  +        prop = _default.getProperty(Property.UseUtf, "true");
  +        if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
  +            _useUtf8 = true;
  +        } else {
  +            _useUtf8 = false;
           }
   
           String defaultEncoding = new 
java.io.InputStreamReader(System.in).getEncoding();
  
  
  
  1.71      +5 -6      
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java
  
  Index: AbstractWebdavMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java,v
  retrieving revision 1.70
  retrieving revision 1.71
  diff -u -r1.70 -r1.71
  --- AbstractWebdavMethod.java 21 Dec 2004 16:04:19 -0000      1.70
  +++ AbstractWebdavMethod.java 30 Dec 2004 13:38:59 -0000      1.71
  @@ -803,8 +803,7 @@
           }
           
           // headers are "ISO-8859-1" encoded [not any more with TC 4.1.18
  -        // destinationUri = 
WebdavUtils.decodeURL(WebdavUtils.fixTomcatURL(destinationUri, "ISO-8859-1"));
  -        uri = WebdavUtils.decodeURL(uri);
  +        uri = WebdavUtils.normalizeURL(WebdavUtils.fixTomcatURL(uri));
           
           String contextPath = req.getContextPath();
           if ((contextPath != null) && (uri.startsWith(contextPath))) {
  @@ -2225,7 +2224,7 @@
                  stContentType = ST_DEFINED;
                  hContentType = hContentTypeStr; 
               }
  -            
  +
           }
           
           private List extractLockTokens(String hStr) {
  
  
  
  1.113     +4 -4      
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/PropFindMethod.java
  
  Index: PropFindMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/PropFindMethod.java,v
  retrieving revision 1.112
  retrieving revision 1.113
  diff -u -r1.112 -r1.113
  --- PropFindMethod.java       21 Dec 2004 16:04:19 -0000      1.112
  +++ PropFindMethod.java       30 Dec 2004 13:38:59 -0000      1.113
  @@ -333,7 +333,7 @@
                   StringBuffer buffer = new StringBuffer();
                   if (outputOptimized) {
                       
  -                    resp.getWriter().write("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>");
  +                    resp.getWriter().write("<?xml version=\"1.0\" 
encoding=\""+Configuration.realUrlEncoding()+"\"?>");
                       resp.getWriter().write("\n");
                       String namespacePrefix = 
multistatusElement.getNamespacePrefix();
                       if ( (namespacePrefix != null) && 
(namespacePrefix.length() == 0) ) {
  
  
  
  1.32      +86 -120   
jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/WebdavUtils.java
  
  Index: WebdavUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/WebdavUtils.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- WebdavUtils.java  15 Dec 2004 18:03:03 -0000      1.31
  +++ WebdavUtils.java  30 Dec 2004 13:38:59 -0000      1.32
  @@ -28,6 +28,8 @@
   import java.util.Enumeration;
   
   import java.io.UnsupportedEncodingException;
  +import java.net.URLDecoder;
  +import java.net.URLEncoder;
   
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpSession;
  @@ -64,82 +66,78 @@
       
       private static final String CREDENTIALS_ATTRIBUTE =
           "org.apache.slide.webdav.method.credentials";
  -    
  -    
  -    // --------------------------------------------------------- Public 
Methods
  -    
  -    private static byte convertHexDigitToByte(byte b) {
  -           if(b >= 48 && b <= 57)
  -               return (byte)(b - 48);
  -           if(b >= 97 && b <= 102)
  -               return (byte)((b - 97) + 10);
  -           if(b >= 65 && b <= 70)
  -               return (byte)((b - 65) + 10);
  -           else
  -               return 0;
  -    }
   
  -    public static String URLDecode(String string, String enc) {
  +
  +    /**
  +     * Constructs a new String based on bytes sequence contained
  +     * in the
  +     * Non-ASCII parts of the sequence will be decoded with UTF-8
  +     * if possible, or with the specified encoding.
  +     *
  +     * @param string A "String" returned by tomcat getPathInfo()
  +     * @param enc Encoding to use if non UTF-8
  +     * @return A properly encoded String object
  +     */
  +    public static String decodeString(String string, String enc)
  +        throws UnsupportedEncodingException {
           if(string == null)
               return null;
  -        byte bytes[] = string.getBytes();
  -        int len = bytes.length;
  -        int ix = 0;
  -        int ox = 0;
  -        boolean isUtf8 = true;
  -        int shouldFollow = 0;
  -        while(ix < len)  {
  -            byte b = bytes[ix++];
  -            if(b == 37) { // % character found, followed by hex digit
  -                b = (byte)((convertHexDigitToByte(bytes[ix++]) << 4) + 
convertHexDigitToByte(bytes[ix++]));
  -                int i = 256+b;
  -                if (shouldFollow == 0) {
  -                    if (i<256)
  -                        if (i>191)
  -                            if (i>223)
  -                                if (i>239)
  -                                    if (i>247)
  -                                        if (i>251)
  -                                            isUtf8 = false;
  -                                        else
  -                                            shouldFollow = 4;
  -                                    else
  -                                        shouldFollow = 3;
  +        StringBuffer sb = null;
  +        int j = 0;
  +        int sf = 0;
  +        sb = new StringBuffer();
  +        byte utf8buffer[] = new byte[5];
  +        // Get bytes without any decoding
  +        byte bytes[] = string.getBytes("ISO-8859-1");
  +        for (int i = 0; i < bytes.length; i+=1) {
  +            byte b = bytes[i];
  +            int bb = (b >= 0) ? b : b+256;
  +            utf8buffer[j++] = b;
  +            boolean ok = false;
  +            // First test if non-ascii
  +            if (bb >= 128) {
  +                // No ongoing UTF-8 decoding ?
  +                if (sf==0) {
  +                    // Now test if this can be a UTF-8 first byte
  +                    if (bb >= 192 && i < 252) {
  +                        ok = true;
  +                        // Determine UTF-8 size
  +                        if (bb >= 224)
  +                            if (bb >= 240)
  +                                if (bb >= 248)
  +                                    sf = 4;
                                   else
  -                                    shouldFollow = 2;
  +                                    sf = 3;
                               else
  -                                shouldFollow = 1;
  +                                sf = 2;
                           else
  -                            isUtf8 = false;
  -                    else
  -                        shouldFollow = 0;
  -                } else {
  -                    shouldFollow --;
  +                            sf = 1;
  +                    }
  +                } else if (bb >= 128 && bb < 192) {
  +                    // This is a UTF-8 part
  +                    sf--;
  +                    if (sf == 0) {
  +                        sb.append(new String(utf8buffer,0,j,"UTF-8"));
  +                        j = 0;
  +                    }
  +                    ok = true;
                   }
  -            } else {
  -                if (shouldFollow > 0) // this should have been followed with 
another hex code
  -                    isUtf8 = false;
  -                if(b == 43) // + is replaced by space
  -                    b = 32;
               }
  -
  -            bytes[ox++] = b;
  -        }
  -        String res;
  -        try {
  -            if(shouldFollow == 0 && isUtf8) {
  -                res = new String(bytes, 0, ox, "UTF-8");
  -            } else {
  -                res  = new String(bytes, 0, ox, enc);
  +            // If there was an error during UTF-8 decoding, decode all 
remaining chars with default encoding
  +            if (!ok) {
  +                sb.append(new String(utf8buffer,0,j, enc));
  +                j = 0;
  +                sf = 0;
               }
  -            return res;
  -        } catch(UnsupportedEncodingException e) {
  -            e.printStackTrace();
           }
  -
  -        return null;
  +        // Remaining chars
  +        if (j > 0) {
  +            sb.append(new String(utf8buffer,0,j, enc));
  +        }
  +        return sb.toString();
       }
   
  +
       /**
        * Return a context-relative path, beginning with a "/", that represents
        * the canonical version of the specified path after ".." and "." 
elements
  @@ -149,30 +147,13 @@
        *
        * @param path the path to be normalized
        **/
  -    public static String decodeURL(String path) {
  -        return decodeURL(path, Configuration.urlEncoding());
  -    }
  -    
  -    /**
  -     * Return a context-relative path, beginning with a "/", that represents
  -     * the canonical version of the specified path after ".." and "." 
elements
  -     * are resolved out.  If the specified path attempts to go outside the
  -     * boundaries of the current context (i.e. too many ".." path elements
  -     * are present), return <code>null</code> instead.
  -     *
  -     * @param path the path to be normalized
  -     **/
  -    public static String decodeURL(String path, String enc) {
  +    public static String normalizeURL(String path) {
   
          if (path == null)
               return null;
           
  -        // Resolve encoded characters in the normalized path,
  -        // which also handles encoded spaces so we can skip that later.
  -        // Placed at the beginning of the chain so that encoded
  -        // bad stuff(tm) can be caught by the later checks
  -        String normalized = URLDecode(path, enc);
  -        
  +        String normalized = path;
  +
           if (normalized == null)
               return (null);
           
  @@ -211,7 +192,7 @@
               normalized = normalized.substring(0, index2) +
                   normalized.substring(index + 3);
           }
  -                
  +        normalized = normalized.replace('?','_');
           // Return the normalized path that we have completed
           return (normalized);
       }
  @@ -223,7 +204,7 @@
        * @param path the path to be rewritten
        **/
       public static String encodeURL(String path) {
  -        return URLUtil.URLEncode(path, Configuration.urlEncoding());
  +        return URLUtil.URLEncode(path, Configuration.realUrlEncoding());
       }
       
       
  @@ -332,7 +313,8 @@
           if (config.isDefaultServlet()) {
               result = req.getServletPath();
           } else {
  -            result = req.getPathInfo();
  +            result = req.getRequestURI();
  +            result = result.substring(req.getContextPath().length()+ 
req.getServletPath().length());
           }
           
           // default to the namespace root if no path-info is specified
  @@ -342,40 +324,20 @@
           
           // prefix the URI with the configured scope
           result = config.getScope() + result;
  -        
  -        
  -        
  -        return decodeURL(fixTomcatURL(result, "UTF-8"));  // the request URL 
is utf-8 encoded
  +
  +        return normalizeURL(fixTomcatURL(result));  // the request URL is 
utf-8 encoded
       }
  -    
  -    
  +
       /**
        * Returns an URL based on input. The input URL is encoded with 
"fromEncoding".
        *    The resulting URL is encoded as specified in 
Configuration.urlEncoding()
        *
        * @param input the input URL
  -     * @param fromEncoding the used encoding of the input URL
        *
        * @return a new URL encoded in Configuration.urlEncoding()
        **/
  -    public static String fixTomcatURL(String input, String fromEncoding) {
  -        
  -        if (input == null) return null;
  -        
  -        String result = null;
  -        try {
  -            
  -            //          printString(input.substring(27,input.length()));
  -            //          byte[] a = input.getBytes("UTF-8");
  -            result = encodeURL(new String(input.getBytes(fromEncoding), 
Configuration.urlEncoding()),
  -                               Configuration.urlEncoding());
  -            //          printString(result.substring(27,result.length()));
  -            //          byte[] b = 
result.getBytes(Configuration.urlEncoding());
  -            //          System.out.println("Length b " + b.length);
  -        } catch (Exception e) { e.printStackTrace(); }
  -        //      System.out.println("Length = " + input.length());
  -        //      System.out.println("Length = " + result.length());
  -        return result;
  +    public static String fixTomcatURL(String input) {
  +        return fixTomcatHeader(input, "UTF-8");
       }
       
       
  @@ -390,13 +352,17 @@
        * @return a new header value encoded in toEncoding
        **/
       public static String fixTomcatHeader(String header, String toEncoding) {
  -        
  +        // todo: toEncoding parameter not used anymore
           if (header == null) return null;
  -        
  +
           String result = null;
           try {
  -            result = URLUtil.URLDecode(new 
String(header.getBytes("ISO-8859-1"), toEncoding),
  -                                       toEncoding);
  +            result = 
URLUtil.URLDecode(header.getBytes("ISO-8859-1"),"ISO-8859-1");
  +            result = decodeString(result,Configuration.urlEncoding());
  +            if (!Configuration.useUTF8()) {
  +                // Remove unsupported characters
  +                result = new 
String(result.getBytes(Configuration.urlEncoding()),Configuration.urlEncoding());
  +            }
           } catch (Exception e) { e.printStackTrace(); }
           return result;
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to