View | Details | Raw Unified | Return to bug 31265
Collapse All | Expand All

(-)src/conf/catalina/slide.properties (-1 / +5 lines)
Lines 18-26 Link Here
18
# Default: true
18
# Default: true
19
org.apache.slide.search=true
19
org.apache.slide.search=true
20
20
21
# Use UTF-8
22
# Default: true
23
org.apache.slide.useUtf8=true
24
21
# URL Encoding
25
# URL Encoding
22
# Default: platform default encoding
26
# Default: platform default encoding
23
org.apache.slide.urlEncoding=UTF-8
27
org.apache.slide.urlEncoding=WINDOWS-1252
24
28
25
# Debug
29
# Debug
26
# Default: false
30
# Default: false
(-)src/share/org/apache/slide/util/Configuration.java (-3 / +37 lines)
Lines 129-135 Link Here
129
        public static final String Events = "org.apache.slide.events";
129
        public static final String Events = "org.apache.slide.events";
130
130
131
        /**
131
        /**
132
         * Property specifying the encoding for URLs.
132
         * Property specifying if UTF-8 should be used to encode URLs.
133
         * <pre>
134
         * org.apache.slide.urlEncoding
135
         * </pre>
136
         */
137
        public static final String UseUtf = "org.apache.slide.useUtf8";
138
139
        /**
140
         * Property specifying the encoding for URLs when it's not UTF-8.
133
         * <pre>
141
         * <pre>
134
         * org.apache.slide.urlEncoding
142
         * org.apache.slide.urlEncoding
135
         * </pre>
143
         * </pre>
Lines 220-225 Link Here
220
    private static boolean _events;
228
    private static boolean _events;
221
229
222
    /**
230
    /**
231
     * Use UTF-8 encoding
232
     */
233
    private static boolean _useUtf8;
234
235
    /**
223
     * URL encoding.
236
     * URL encoding.
224
     */
237
     */
225
    private static String _urlEncoding;
238
    private static String _urlEncoding;
Lines 293-307 Link Here
293
        return _events;
306
        return _events;
294
    }
307
    }
295
308
296
    
309
297
    /**
310
    /**
298
     * Returns the used URL encoding.
311
     * Returns true if UTF-8 should be used to encode urls.
312
     */
313
    public static boolean useUTF8() {
314
        return _useUtf8;
315
    }
316
317
    /**
318
     * Returns the used URL encoding when non UTF-8.
299
     */
319
     */
300
    public static String urlEncoding() {
320
    public static String urlEncoding() {
301
        return _urlEncoding;
321
        return _urlEncoding;
302
    }
322
    }
303
    
323
    
304
    /**
324
    /**
325
     * Returns the used URL encoding.
326
     */
327
    public static String realUrlEncoding() {
328
        return _useUtf8 ? "UTF-8" : _urlEncoding;
329
    }
330
331
    /**
305
     * Returns true if Principal identified locks are enabled.
332
     * Returns true if Principal identified locks are enabled.
306
     */
333
     */
307
    public static boolean usePrincipalIdentifiedLocks() {
334
    public static boolean usePrincipalIdentifiedLocks() {
Lines 444-449 Link Here
444
            _principalIdentifiedLocks = true;
471
            _principalIdentifiedLocks = true;
445
        } else {
472
        } else {
446
            _principalIdentifiedLocks = false;
473
            _principalIdentifiedLocks = false;
474
        }
475
476
        prop = _default.getProperty(Property.UseUtf, "true");
477
        if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
478
            _useUtf8 = true;
479
        } else {
480
            _useUtf8 = false;
447
        }
481
        }
448
482
449
        String defaultEncoding = new java.io.InputStreamReader(System.in).getEncoding();
483
        String defaultEncoding = new java.io.InputStreamReader(System.in).getEncoding();
(-)src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java (-3 / +3 lines)
Lines 627-634 Link Here
627
        }
627
        }
628
        
628
        
629
        // headers are "ISO-8859-1" encoded [not any more with TC 4.1.18
629
        // headers are "ISO-8859-1" encoded [not any more with TC 4.1.18
630
        // destinationUri = WebdavUtils.decodeURL(WebdavUtils.fixTomcatURL(destinationUri, "ISO-8859-1"));
630
        // destinationUri = WebdavUtils.normalizeURL(WebdavUtils.fixTomcatURL(destinationUri, "ISO-8859-1"));
631
        uri = WebdavUtils.decodeURL(uri);
631
        uri = WebdavUtils.normalizeURL(WebdavUtils.fixTomcatURL(uri));
632
        
632
        
633
        String contextPath = req.getContextPath();
633
        String contextPath = req.getContextPath();
634
        if ((contextPath != null) && (uri.startsWith(contextPath))) {
634
        if ((contextPath != null) && (uri.startsWith(contextPath))) {
Lines 1755-1761 Link Here
1755
               stContentType = ST_DEFINED;
1755
               stContentType = ST_DEFINED;
1756
               hContentType = hContentTypeStr; 
1756
               hContentType = hContentTypeStr; 
1757
            }
1757
            }
1758
            
1758
1759
        }
1759
        }
1760
        
1760
        
1761
        private List extractLockTokens(String hStr) {
1761
        private List extractLockTokens(String hStr) {
(-)src/webdav/server/org/apache/slide/webdav/method/PropFindMethod.java (-1 / +1 lines)
Lines 301-307 Link Here
301
                StringBuffer buffer = new StringBuffer();
301
                StringBuffer buffer = new StringBuffer();
302
                if (outputOptimized) {
302
                if (outputOptimized) {
303
                    
303
                    
304
                    resp.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
304
                    resp.getWriter().write("<?xml version=\"1.0\" encoding=\""+Configuration.realUrlEncoding()+"\"?>");
305
                    resp.getWriter().write("\n");
305
                    resp.getWriter().write("\n");
306
                    String namespacePrefix = multistatusElement.getNamespacePrefix();
306
                    String namespacePrefix = multistatusElement.getNamespacePrefix();
307
                    if ( (namespacePrefix != null) && (namespacePrefix.length() == 0) ) {
307
                    if ( (namespacePrefix != null) && (namespacePrefix.length() == 0) ) {
(-)src/webdav/server/org/apache/slide/webdav/util/WebdavUtils.java (-119 / +88 lines)
Lines 1-7 Link Here
1
/*
1
/*
2
 * $Header: /home/cvspublic/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/WebdavUtils.java,v 1.26 2004/09/08 04:14:31 ozeigermann Exp $
2
 * $Header: /home/cvspublic/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/WebdavUtils.java,v 1.25 2004/09/01 10:34:48 unico Exp $
3
 * $Revision: 1.26 $
3
 * $Revision: 1.25 $
4
 * $Date: 2004/09/08 04:14:31 $
4
 * $Date: 2004/09/01 10:34:48 $
5
 *
5
 *
6
 * ====================================================================
6
 * ====================================================================
7
 *
7
 *
Lines 26-31 Link Here
26
import java.security.Principal;
26
import java.security.Principal;
27
27
28
import java.io.UnsupportedEncodingException;
28
import java.io.UnsupportedEncodingException;
29
import java.net.URLDecoder;
30
import java.net.URLEncoder;
29
31
30
import javax.servlet.http.HttpServletRequest;
32
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpSession;
33
import javax.servlet.http.HttpSession;
Lines 62-143 Link Here
62
    
64
    
63
    private static final String CREDENTIALS_ATTRIBUTE =
65
    private static final String CREDENTIALS_ATTRIBUTE =
64
        "org.apache.slide.webdav.method.credentials";
66
        "org.apache.slide.webdav.method.credentials";
65
    
66
    
67
    // --------------------------------------------------------- Public Methods
68
    
69
    private static byte convertHexDigitToByte(byte b) {
70
           if(b >= 48 && b <= 57)
71
               return (byte)(b - 48);
72
           if(b >= 97 && b <= 102)
73
               return (byte)((b - 97) + 10);
74
           if(b >= 65 && b <= 70)
75
               return (byte)((b - 65) + 10);
76
           else
77
               return 0;
78
    }
79
67
80
    public static String URLDecode(String string, String enc) {
68
69
    /**
70
     * Constructs a new String based on bytes sequence contained
71
     * in the
72
     * Non-ASCII parts of the sequence will be decoded with UTF-8
73
     * if possible, or with the specified encoding.
74
     *
75
     * @param string A "String" returned by tomcat getPathInfo()
76
     * @param enc Encoding to use if non UTF-8
77
     * @return A properly encoded String object
78
     */
79
    public static String decodeString(String string, String enc)
80
        throws UnsupportedEncodingException {
81
        if(string == null)
81
        if(string == null)
82
            return null;
82
            return null;
83
        byte bytes[] = string.getBytes();
83
        StringBuffer sb = null;
84
        int len = bytes.length;
84
        int j = 0;
85
        int ix = 0;
85
        int sf = 0;
86
        int ox = 0;
86
        sb = new StringBuffer();
87
        boolean isUtf8 = true;
87
        byte utf8buffer[] = new byte[5];
88
        int shouldFollow = 0;
88
        // Get bytes without any decoding
89
        while(ix < len)  {
89
        byte bytes[] = string.getBytes("ISO-8859-1");
90
            byte b = bytes[ix++];
90
        for (int i = 0; i < bytes.length; i+=1) {
91
            if(b == 37) { // % character found, followed by hex digit
91
            byte b = bytes[i];
92
                b = (byte)((convertHexDigitToByte(bytes[ix++]) << 4) + convertHexDigitToByte(bytes[ix++]));
92
            int bb = (b >= 0) ? b : b+256;
93
                int i = 256+b;
93
//            System.out.print("0x"+Integer.toHexString(bb)+ " ");
94
                if (shouldFollow == 0) {
94
            utf8buffer[j++] = b;
95
                    if (i<256)
95
            boolean ok = false;
96
                        if (i>191)
96
            // First test if non-ascii
97
                            if (i>223)
97
            if (bb >= 128) {
98
                                if (i>239)
98
                // No ongoing UTF-8 decoding ?
99
                                    if (i>247)
99
                if (sf==0) {
100
                                        if (i>251)
100
                    // Now test if this can be a UTF-8 first byte
101
                                            isUtf8 = false;
101
                    if (bb >= 192 && i < 252) {
102
                                        else
102
                        ok = true;
103
                                            shouldFollow = 4;
103
                        // Determine UTF-8 size
104
                                    else
104
                        if (bb >= 224)
105
                                        shouldFollow = 3;
105
                            if (bb >= 240)
106
                                if (bb >= 248)
107
                                    sf = 4;
106
                                else
108
                                else
107
                                    shouldFollow = 2;
109
                                    sf = 3;
108
                            else
110
                            else
109
                                shouldFollow = 1;
111
                                sf = 2;
110
                        else
112
                        else
111
                            isUtf8 = false;
113
                            sf = 1;
112
                    else
114
                    }
113
                        shouldFollow = 0;
115
                } else if (bb >= 128 && bb < 192) {
114
                } else {
116
                    // This is a UTF-8 part
115
                    shouldFollow --;
117
                    sf--;
118
                    if (sf == 0) {
119
                        sb.append(new String(utf8buffer,0,j,"UTF-8"));
120
                        j = 0;
121
                    }
122
                    ok = true;
116
                }
123
                }
117
            } else {
118
                if (shouldFollow > 0) // this should have been followed with another hex code
119
                    isUtf8 = false;
120
                if(b == 43) // + is replaced by space
121
                    b = 32;
122
            }
124
            }
123
125
            // If there was an error during UTF-8 decoding, decode all remaining chars with default encoding
124
            bytes[ox++] = b;
126
            if (!ok) {
125
        }
127
                sb.append(new String(utf8buffer,0,j, enc));
126
        String res;
128
                j = 0;
127
        try {
129
                sf = 0;
128
            if(shouldFollow == 0 && isUtf8) {
129
                res = new String(bytes, 0, ox, "UTF-8");
130
            } else {
131
                res  = new String(bytes, 0, ox, enc);
132
            }
130
            }
133
            return res;
134
        } catch(UnsupportedEncodingException e) {
135
            e.printStackTrace();
136
        }
131
        }
137
132
        // Remaining chars
138
        return null;
133
        if (j > 0) {
134
            sb.append(new String(utf8buffer,0,j, enc));
135
        }
136
//        System.out.println();
137
//        System.out.println(sb);
138
        return sb.toString();
139
    }
139
    }
140
140
141
141
    /**
142
    /**
142
     * Return a context-relative path, beginning with a "/", that represents
143
     * Return a context-relative path, beginning with a "/", that represents
143
     * the canonical version of the specified path after ".." and "." elements
144
     * the canonical version of the specified path after ".." and "." elements
Lines 147-176 Link Here
147
     *
148
     *
148
     * @param path the path to be normalized
149
     * @param path the path to be normalized
149
     **/
150
     **/
150
    public static String decodeURL(String path) {
151
    public static String normalizeURL(String path) {
151
        return decodeURL(path, Configuration.urlEncoding());
152
    }
153
    
154
    /**
155
     * Return a context-relative path, beginning with a "/", that represents
156
     * the canonical version of the specified path after ".." and "." elements
157
     * are resolved out.  If the specified path attempts to go outside the
158
     * boundaries of the current context (i.e. too many ".." path elements
159
     * are present), return <code>null</code> instead.
160
     *
161
     * @param path the path to be normalized
162
     **/
163
    public static String decodeURL(String path, String enc) {
164
152
165
       if (path == null)
153
       if (path == null)
166
            return null;
154
            return null;
167
        
155
        
168
        // Resolve encoded characters in the normalized path,
156
        String normalized = path;
169
        // which also handles encoded spaces so we can skip that later.
157
170
        // Placed at the beginning of the chain so that encoded
171
        // bad stuff(tm) can be caught by the later checks
172
        String normalized = URLDecode(path, enc);
173
        
174
        if (normalized == null)
158
        if (normalized == null)
175
            return (null);
159
            return (null);
176
        
160
        
Lines 209-215 Link Here
209
            normalized = normalized.substring(0, index2) +
193
            normalized = normalized.substring(0, index2) +
210
                normalized.substring(index + 3);
194
                normalized.substring(index + 3);
211
        }
195
        }
212
                
196
        normalized = normalized.replace('?','_');
213
        // Return the normalized path that we have completed
197
        // Return the normalized path that we have completed
214
        return (normalized);
198
        return (normalized);
215
    }
199
    }
Lines 221-227 Link Here
221
     * @param path the path to be rewritten
205
     * @param path the path to be rewritten
222
     **/
206
     **/
223
    public static String encodeURL(String path) {
207
    public static String encodeURL(String path) {
224
        return URLUtil.URLEncode(path, Configuration.urlEncoding());
208
        return URLUtil.URLEncode(path, Configuration.realUrlEncoding());
225
    }
209
    }
226
    
210
    
227
    
211
    
Lines 317-323 Link Here
317
        if (config.isDefaultServlet()) {
301
        if (config.isDefaultServlet()) {
318
            result = req.getServletPath();
302
            result = req.getServletPath();
319
        } else {
303
        } else {
320
            result = req.getPathInfo();
304
            result = req.getRequestURI();
305
            result = result.substring(req.getContextPath().length()+ req.getServletPath().length());
321
        }
306
        }
322
        
307
        
323
        // default to the namespace root if no path-info is specified
308
        // default to the namespace root if no path-info is specified
Lines 327-366 Link Here
327
        
312
        
328
        // prefix the URI with the configured scope
313
        // prefix the URI with the configured scope
329
        result = config.getScope() + result;
314
        result = config.getScope() + result;
330
        
315
331
        
316
        return normalizeURL(fixTomcatURL(result));  // the request URL is utf-8 encoded
332
        
333
        return decodeURL(fixTomcatURL(result, "UTF-8"));  // the request URL is utf-8 encoded
334
    }
317
    }
335
    
318
336
    
337
    /**
319
    /**
338
     * Returns an URL based on input. The input URL is encoded with "fromEncoding".
320
     * Returns an URL based on input. The input URL is encoded with "fromEncoding".
339
     *    The resulting URL is encoded as specified in Configuration.urlEncoding()
321
     *    The resulting URL is encoded as specified in Configuration.urlEncoding()
340
     *
322
     *
341
     * @param input the input URL
323
     * @param input the input URL
342
     * @param fromEncoding the used encoding of the input URL
343
     *
324
     *
344
     * @return a new URL encoded in Configuration.urlEncoding()
325
     * @return a new URL encoded in Configuration.urlEncoding()
345
     **/
326
     **/
346
    public static String fixTomcatURL(String input, String fromEncoding) {
327
    public static String fixTomcatURL(String input) {
347
        
328
        return fixTomcatHeader(input, "UTF-8");
348
        if (input == null) return null;
349
        
350
        String result = null;
351
        try {
352
            
353
            //          printString(input.substring(27,input.length()));
354
            //          byte[] a = input.getBytes("UTF-8");
355
            result = encodeURL(new String(input.getBytes(fromEncoding), Configuration.urlEncoding()),
356
                               Configuration.urlEncoding());
357
            //          printString(result.substring(27,result.length()));
358
            //          byte[] b = result.getBytes(Configuration.urlEncoding());
359
            //          System.out.println("Length b " + b.length);
360
        } catch (Exception e) { e.printStackTrace(); }
361
        //      System.out.println("Length = " + input.length());
362
        //      System.out.println("Length = " + result.length());
363
        return result;
364
    }
329
    }
365
    
330
    
366
    
331
    
Lines 375-387 Link Here
375
     * @return a new header value encoded in toEncoding
340
     * @return a new header value encoded in toEncoding
376
     **/
341
     **/
377
    public static String fixTomcatHeader(String header, String toEncoding) {
342
    public static String fixTomcatHeader(String header, String toEncoding) {
378
        
343
        // todo: toEncoding parameter not used anymore
379
        if (header == null) return null;
344
        if (header == null) return null;
380
        
345
381
        String result = null;
346
        String result = null;
382
        try {
347
        try {
383
            result = URLUtil.URLDecode(new String(header.getBytes("ISO-8859-1"), toEncoding),
348
            result = URLUtil.URLDecode(header.getBytes("ISO-8859-1"),"ISO-8859-1");
384
                                       toEncoding);
349
            result = decodeString(result,Configuration.urlEncoding());
350
            if (!Configuration.useUTF8()) {
351
                // Remove unsupported characters
352
                result = new String(result.getBytes(Configuration.urlEncoding()),Configuration.urlEncoding());
353
            }
385
        } catch (Exception e) { e.printStackTrace(); }
354
        } catch (Exception e) { e.printStackTrace(); }
386
        return result;
355
        return result;
387
    }
356
    }

Return to bug 31265