You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by oz...@apache.org on 2004/09/08 06:14:31 UTC

cvs commit: jakarta-slide/src/webdav/server/org/apache/slide/webdav/util WebdavUtils.java

ozeigermann    2004/09/07 21:14:31

  Modified:    src/webdav/server/org/apache/slide/webdav/util
                        WebdavUtils.java
  Log:
  Applied patch by Thomas Draier to take care of URL encoding problem 
  described in issue #31101.
  
  Revision  Changes    Path
  1.26      +79 -6     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.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- WebdavUtils.java	1 Sep 2004 10:34:48 -0000	1.25
  +++ WebdavUtils.java	8 Sep 2004 04:14:31 -0000	1.26
  @@ -25,6 +25,8 @@
   
   import java.security.Principal;
   
  +import java.io.UnsupportedEncodingException;
  +
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpSession;
   
  @@ -64,7 +66,78 @@
       
       // --------------------------------------------------------- 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) {
  +        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;
  +                                else
  +                                    shouldFollow = 2;
  +                            else
  +                                shouldFollow = 1;
  +                        else
  +                            isUtf8 = false;
  +                    else
  +                        shouldFollow = 0;
  +                } else {
  +                    shouldFollow --;
  +                }
  +            } 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);
  +            }
  +            return res;
  +        } catch(UnsupportedEncodingException e) {
  +            e.printStackTrace();
  +        }
  +
  +        return null;
  +    }
  +
       /**
        * Return a context-relative path, beginning with a "/", that represents
        * the canonical version of the specified path after ".." and "." elements
  @@ -96,7 +169,7 @@
           // 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 = URLUtil.URLDecode(path, enc);
  +        String normalized = URLDecode(path, enc);
           
           if (normalized == null)
               return (null);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org