View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider;
18  
19  import org.apache.commons.lang3.SystemUtils;
20  import org.apache.commons.vfs2.FileName;
21  import org.apache.commons.vfs2.FileSystemException;
22  import org.apache.commons.vfs2.FileType;
23  import org.apache.commons.vfs2.VFS;
24  
25  /**
26   * Utilities for dealing with URIs. See RFC 2396 for details.
27   */
28  public final class UriParser {
29  
30      /**
31       * The set of valid separators. These are all converted to the normalized one. Does <i>not</i> contain the
32       * normalized separator
33       */
34      // public static final char[] separators = {'\\'};
35      public static final char TRANS_SEPARATOR = '\\';
36  
37      /**
38       * The normalised separator to use.
39       */
40      private static final char SEPARATOR_CHAR = FileName.SEPARATOR_CHAR;
41  
42      private static final int HEX_BASE = 16;
43  
44      private static final int BITS_IN_HALF_BYTE = 4;
45  
46      private static final char LOW_MASK = 0x0F;
47  
48      /**
49       * Encodes and appends a string to a StringBuilder.
50       *
51       * @param buffer The StringBuilder to append to.
52       * @param unencodedValue The String to encode and append.
53       * @param reserved characters to encode.
54       */
55      public static void appendEncoded(final StringBuilder buffer, final String unencodedValue, final char[] reserved) {
56          final int offset = buffer.length();
57          buffer.append(unencodedValue);
58          encode(buffer, offset, unencodedValue.length(), reserved);
59      }
60  
61      public static void canonicalizePath(final StringBuilder buffer, final int offset, final int length,
62              final FileNameParser fileNameParser) throws FileSystemException {
63          int index = offset;
64          int count = length;
65          for (; count > 0; count--, index++) {
66              final char ch = buffer.charAt(index);
67              if (ch == '%') {
68                  if (count < 3) {
69                      throw new FileSystemException("vfs.provider/invalid-escape-sequence.error",
70                              buffer.substring(index, index + count));
71                  }
72  
73                  // Decode
74                  final int dig1 = Character.digit(buffer.charAt(index + 1), HEX_BASE);
75                  final int dig2 = Character.digit(buffer.charAt(index + 2), HEX_BASE);
76                  if (dig1 == -1 || dig2 == -1) {
77                      throw new FileSystemException("vfs.provider/invalid-escape-sequence.error",
78                              buffer.substring(index, index + 3));
79                  }
80                  final char value = (char) (dig1 << BITS_IN_HALF_BYTE | dig2);
81  
82                  final boolean match = value == '%' || fileNameParser.encodeCharacter(value);
83  
84                  if (match) {
85                      // this is a reserved character, not allowed to decode
86                      index += 2;
87                      count -= 2;
88                      continue;
89                  }
90  
91                  // Replace
92                  buffer.setCharAt(index, value);
93                  buffer.delete(index + 1, index + 3);
94                  count -= 2;
95              } else if (fileNameParser.encodeCharacter(ch)) {
96                  // Encode
97                  final char[] digits = { Character.forDigit((ch >> BITS_IN_HALF_BYTE) & LOW_MASK, HEX_BASE),
98                          Character.forDigit(ch & LOW_MASK, HEX_BASE) };
99                  buffer.setCharAt(index, '%');
100                 buffer.insert(index + 1, digits);
101                 index += 2;
102             }
103         }
104     }
105 
106     /**
107      * Decodes the String.
108      *
109      * @param uri The String to decode.
110      * @throws FileSystemException if an error occurs.
111      */
112     public static void checkUriEncoding(final String uri) throws FileSystemException {
113         decode(uri);
114     }
115 
116     /**
117      * Removes %nn encodings from a string.
118      *
119      * @param encodedStr The encoded String.
120      * @return The decoded String.
121      * @throws FileSystemException if an error occurs.
122      */
123     public static String decode(final String encodedStr) throws FileSystemException {
124         if (encodedStr == null) {
125             return null;
126         }
127         if (encodedStr.indexOf('%') < 0) {
128             return encodedStr;
129         }
130         final StringBuilder buffer = new StringBuilder(encodedStr);
131         decode(buffer, 0, buffer.length());
132         return buffer.toString();
133     }
134 
135     /**
136      * Removes %nn encodings from a string.
137      *
138      * @param buffer StringBuilder containing the string to decode.
139      * @param offset The position in the string to start decoding.
140      * @param length The number of characters to decode.
141      * @throws FileSystemException if an error occurs.
142      */
143     public static void decode(final StringBuilder buffer, final int offset, final int length)
144             throws FileSystemException {
145         int index = offset;
146         int count = length;
147         for (; count > 0; count--, index++) {
148             final char ch = buffer.charAt(index);
149             if (ch != '%') {
150                 continue;
151             }
152             if (count < 3) {
153                 throw new FileSystemException("vfs.provider/invalid-escape-sequence.error",
154                         buffer.substring(index, index + count));
155             }
156 
157             // Decode
158             final int dig1 = Character.digit(buffer.charAt(index + 1), HEX_BASE);
159             final int dig2 = Character.digit(buffer.charAt(index + 2), HEX_BASE);
160             if (dig1 == -1 || dig2 == -1) {
161                 throw new FileSystemException("vfs.provider/invalid-escape-sequence.error",
162                         buffer.substring(index, index + 3));
163             }
164             final char value = (char) (dig1 << BITS_IN_HALF_BYTE | dig2);
165 
166             // Replace
167             buffer.setCharAt(index, value);
168             buffer.delete(index + 1, index + 3);
169             count -= 2;
170         }
171     }
172 
173     /**
174      * Converts "special" characters to their %nn value.
175      *
176      * @param decodedStr The decoded String.
177      * @return The encoded String.
178      */
179     public static String encode(final String decodedStr) {
180         return encode(decodedStr, null);
181     }
182 
183     /**
184      * Converts "special" characters to their %nn value.
185      *
186      * @param decodedStr The decoded String.
187      * @param reserved Characters to encode.
188      * @return The encoded String
189      */
190     public static String encode(final String decodedStr, final char[] reserved) {
191         if (decodedStr == null) {
192             return null;
193         }
194         final StringBuilder buffer = new StringBuilder(decodedStr);
195         encode(buffer, 0, buffer.length(), reserved);
196         return buffer.toString();
197     }
198 
199     /**
200      * Encode an array of Strings.
201      *
202      * @param strings The array of Strings to encode.
203      * @return An array of encoded Strings.
204      */
205     public static String[] encode(final String[] strings) {
206         if (strings == null) {
207             return null;
208         }
209         for (int i = 0; i < strings.length; i++) {
210             strings[i] = encode(strings[i]);
211         }
212         return strings;
213     }
214 
215     /**
216      * Encodes a set of reserved characters in a StringBuilder, using the URI %nn encoding. Always encodes % characters.
217      *
218      * @param buffer The StringBuilder to append to.
219      * @param offset The position in the buffer to start encoding at.
220      * @param length The number of characters to encode.
221      * @param reserved characters to encode.
222      */
223     public static void encode(final StringBuilder buffer, final int offset, final int length, final char[] reserved) {
224         int index = offset;
225         int count = length;
226         for (; count > 0; index++, count--) {
227             final char ch = buffer.charAt(index);
228             boolean match = ch == '%';
229             if (reserved != null) {
230                 for (int i = 0; !match && i < reserved.length; i++) {
231                     if (ch == reserved[i]) {
232                         match = true;
233                         break;
234                     }
235                 }
236             }
237             if (match) {
238                 // Encode
239                 final char[] digits = { Character.forDigit((ch >> BITS_IN_HALF_BYTE) & LOW_MASK, HEX_BASE),
240                         Character.forDigit(ch & LOW_MASK, HEX_BASE) };
241                 buffer.setCharAt(index, '%');
242                 buffer.insert(index + 1, digits);
243                 index += 2;
244             }
245         }
246     }
247 
248     /**
249      * Extracts the first element of a path.
250      *
251      * @param name StringBuilder containing the path.
252      * @return The first element of the path.
253      */
254     public static String extractFirstElement(final StringBuilder name) {
255         final int len = name.length();
256         if (len < 1) {
257             return null;
258         }
259         int startPos = 0;
260         if (name.charAt(0) == SEPARATOR_CHAR) {
261             startPos = 1;
262         }
263         for (int pos = startPos; pos < len; pos++) {
264             if (name.charAt(pos) == SEPARATOR_CHAR) {
265                 // Found a separator
266                 final String elem = name.substring(startPos, pos);
267                 name.delete(startPos, pos + 1);
268                 return elem;
269             }
270         }
271 
272         // No separator
273         final String elem = name.substring(startPos);
274         name.setLength(0);
275         return elem;
276     }
277 
278     /**
279      * Extract the query String from the URI.
280      *
281      * @param name StringBuilder containing the URI.
282      * @return The query string, if any. null otherwise.
283      */
284     public static String extractQueryString(final StringBuilder name) {
285         for (int pos = 0; pos < name.length(); pos++) {
286             if (name.charAt(pos) == '?') {
287                 final String queryString = name.substring(pos + 1);
288                 name.delete(pos, name.length());
289                 return queryString;
290             }
291         }
292 
293         return null;
294     }
295 
296     /**
297      * Extracts the scheme from a URI. Removes the scheme and ':' delimiter from the front of the URI.
298      * <p>
299      * The scheme is extracted based on the currently supported schemes in the system.  That is to say the schemes
300      * supported by the registered providers.
301      * </p>
302      * <p>
303      * This allows us to handle varying scheme's without making assumptions based on the ':' character.  Specifically
304      * handle scheme extraction calls for URI parameters that are not actually uri's, but may be names with ':' in them.
305      * </p>
306      * @param schemes The schemes to check.
307      * @param uri The potential URI. May also be a name.
308      * @return The scheme name. Returns null if there is no scheme.
309      * @since 2.3
310      */
311     public static String extractScheme(final String[] schemes, final String uri) {
312         return extractScheme(schemes, uri, null);
313     }
314 
315     /**
316      * Extracts the scheme from a URI. Removes the scheme and ':' delimiter from the front of the URI.
317      * <p>
318      * The scheme is extracted based on the given set of schemes. Normally, that is to say the schemes
319      * supported by the registered providers.
320      * </p>
321      * <p>
322      * This allows us to handle varying scheme's without making assumptions based on the ':' character. Specifically
323      * handle scheme extraction calls for URI parameters that are not actually URI's, but may be names with ':' in them.
324      * </p>
325      * @param schemes The schemes to check.
326      * @param uri The potential URI. May also just be a name.
327      * @param buffer Returns the remainder of the URI.
328      * @return The scheme name. Returns null if there is no scheme.
329      * @since 2.3
330      */
331     public static String extractScheme(final String[] schemes, final String uri, final StringBuilder buffer) {
332         if (buffer != null) {
333             buffer.setLength(0);
334             buffer.append(uri);
335         }
336         for (final String scheme : schemes) {
337             if (uri.startsWith(scheme + ":")) {
338                 if (buffer != null) {
339                     buffer.delete(0, uri.indexOf(':') + 1);
340                 }
341                 return scheme;
342             }
343         }
344         return null;
345     }
346 
347     /**
348      * Extracts the scheme from a URI.
349      *
350      * @param uri The URI.
351      * @return The scheme name. Returns null if there is no scheme.
352      * @deprecated Use instead {@link #extractScheme}.  Will be removed in 3.0.
353      */
354     @Deprecated
355     public static String extractScheme(final String uri) {
356         return extractScheme(uri, null);
357     }
358 
359     /**
360      * Extracts the scheme from a URI. Removes the scheme and ':' delimiter from the front of the URI.
361      *
362      * @param uri The URI.
363      * @param buffer Returns the remainder of the URI.
364      * @return The scheme name. Returns null if there is no scheme.
365      * @deprecated Use instead {@link #extractScheme}.  Will be removed in 3.0.
366      */
367     @Deprecated
368     public static String extractScheme(final String uri, final StringBuilder buffer) {
369         if (buffer != null) {
370             buffer.setLength(0);
371             buffer.append(uri);
372         }
373 
374         final int maxPos = uri.length();
375         for (int pos = 0; pos < maxPos; pos++) {
376             final char ch = uri.charAt(pos);
377 
378             if (ch == ':') {
379                 // Found the end of the scheme
380                 final String scheme = uri.substring(0, pos);
381                 if (scheme.length() <= 1 && SystemUtils.IS_OS_WINDOWS) {
382                     // This is not a scheme, but a Windows drive letter
383                     return null;
384                 }
385                 if (buffer != null) {
386                     buffer.delete(0, pos + 1);
387                 }
388                 return scheme.intern();
389             }
390 
391             if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
392                 // A scheme character
393                 continue;
394             }
395             if (pos > 0 && ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-' || ch == '.')) {
396                 // A scheme character (these are not allowed as the first
397                 // character of the scheme, but can be used as subsequent
398                 // characters.
399                 continue;
400             }
401 
402             // Not a scheme character
403             break;
404         }
405 
406         // No scheme in URI
407         return null;
408     }
409 
410     /**
411      * Normalises the separators in a name.
412      *
413      * @param name The StringBuilder containing the name
414      * @return true if the StringBuilder was modified.
415      */
416     public static boolean fixSeparators(final StringBuilder name) {
417         boolean changed = false;
418         final int maxlen = name.length();
419         for (int i = 0; i < maxlen; i++) {
420             final char ch = name.charAt(i);
421             if (ch == TRANS_SEPARATOR) {
422                 name.setCharAt(i, SEPARATOR_CHAR);
423                 changed = true;
424             }
425         }
426         return changed;
427     }
428 
429     /**
430      * Normalises a path. Does the following:
431      * <ul>
432      * <li>Removes empty path elements.
433      * <li>Handles '.' and '..' elements.
434      * <li>Removes trailing separator.
435      * </ul>
436      *
437      * Its assumed that the separators are already fixed.
438      *
439      * @param path The path to normalize.
440      * @return The FileType.
441      * @throws FileSystemException if an error occurs.
442      *
443      * @see #fixSeparators
444      */
445     public static FileType normalisePath(final StringBuilder path) throws FileSystemException {
446         FileType fileType = FileType.FOLDER;
447         if (path.length() == 0) {
448             return fileType;
449         }
450 
451         if (path.charAt(path.length() - 1) != '/') {
452             fileType = FileType.FILE;
453         }
454 
455         // Adjust separators
456         // fixSeparators(path);
457 
458         // Determine the start of the first element
459         int startFirstElem = 0;
460         if (path.charAt(0) == SEPARATOR_CHAR) {
461             if (path.length() == 1) {
462                 return fileType;
463             }
464             startFirstElem = 1;
465         }
466 
467         // Iterate over each element
468         int startElem = startFirstElem;
469         int maxlen = path.length();
470         while (startElem < maxlen) {
471             // Find the end of the element
472             int endElem = startElem;
473             for (; endElem < maxlen && path.charAt(endElem) != SEPARATOR_CHAR; endElem++) {
474             }
475 
476             final int elemLen = endElem - startElem;
477             if (elemLen == 0) {
478                 // An empty element - axe it
479                 path.delete(endElem, endElem + 1);
480                 maxlen = path.length();
481                 continue;
482             }
483             if (elemLen == 1 && path.charAt(startElem) == '.') {
484                 // A '.' element - axe it
485                 path.delete(startElem, endElem + 1);
486                 maxlen = path.length();
487                 continue;
488             }
489             if (elemLen == 2 && path.charAt(startElem) == '.' && path.charAt(startElem + 1) == '.') {
490                 // A '..' element - remove the previous element
491                 if (startElem == startFirstElem) {
492                     // Previous element is missing
493                     throw new FileSystemException("vfs.provider/invalid-relative-path.error");
494                 }
495 
496                 // Find start of previous element
497                 int pos = startElem - 2;
498                 for (; pos >= 0 && path.charAt(pos) != SEPARATOR_CHAR; pos--) {
499                 }
500                 startElem = pos + 1;
501 
502                 path.delete(startElem, endElem + 1);
503                 maxlen = path.length();
504                 continue;
505             }
506 
507             // A regular element
508             startElem = endElem + 1;
509         }
510 
511         // Remove trailing separator
512         if (!VFS.isUriStyle() && maxlen > 1 && path.charAt(maxlen - 1) == SEPARATOR_CHAR) {
513             path.delete(maxlen - 1, maxlen);
514         }
515 
516         return fileType;
517     }
518 
519     private UriParser() {
520     }
521 }