URLDecoder.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* URLDecoder.java -- Class to decode URL's from encoded form.
  2. Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.net;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.UnsupportedEncodingException;
  34. /**
  35. * This utility class contains static methods that converts a
  36. * string encoded in the x-www-form-urlencoded format to the original
  37. * text. The x-www-form-urlencoded format replaces certain disallowed
  38. * characters with encoded equivalents. All upper case and lower case
  39. * letters in the US alphabet remain as is, the space character (' ')
  40. * is replaced with '+' sign, and all other characters are converted to a
  41. * "%XX" format where XX is the hexadecimal representation of that character
  42. * in a given character encoding (default is "UTF-8").
  43. * <p>
  44. * This method is very useful for decoding strings sent to CGI scripts
  45. *
  46. * Written using on-line Java Platform 1.2/1.4 API Specification.
  47. * Status: Believed complete and correct.
  48. *
  49. * @since 1.2
  50. *
  51. * @author Warren Levy (warrenl@cygnus.com)
  52. * @author Aaron M. Renn (arenn@urbanophile.com) (documentation comments)
  53. * @author Mark Wielaard (mark@klomp.org)
  54. */
  55. public class URLDecoder
  56. {
  57. /**
  58. * Public contructor. Note that this class has only static methods.
  59. */
  60. public URLDecoder()
  61. {
  62. }
  63. /**
  64. * This method translates the passed in string from x-www-form-urlencoded
  65. * format using the default encoding "UTF-8" to decode the hex encoded
  66. * unsafe characters.
  67. *
  68. * @param s the String to convert
  69. *
  70. * @return the converted String
  71. *
  72. * @deprecated
  73. */
  74. public static String decode(String s)
  75. {
  76. try
  77. {
  78. return decode(s, "UTF-8");
  79. }
  80. catch (UnsupportedEncodingException uee)
  81. {
  82. // Should never happen since UTF-8 encoding should always be supported
  83. return s;
  84. }
  85. }
  86. /**
  87. * This method translates the passed in string from x-www-form-urlencoded
  88. * format using the given character encoding to decode the hex encoded
  89. * unsafe characters.
  90. *
  91. * This implementation will decode the string even if it contains
  92. * unsafe characters (characters that should have been encoded) or if the
  93. * two characters following a % do not represent a hex encoded byte.
  94. * In those cases the unsafe character or the % character will be added
  95. * verbatim to the decoded result.
  96. *
  97. * @param s the String to convert
  98. * @param encoding the character encoding to use the decode the hex encoded
  99. * unsafe characters
  100. *
  101. * @return the converted String
  102. *
  103. * @exception UnsupportedEncodingException If the named encoding is not
  104. * supported
  105. *
  106. * @since 1.4
  107. */
  108. public static String decode(String s, String encoding)
  109. throws UnsupportedEncodingException
  110. {
  111. // First convert all '+' characters to spaces.
  112. String str = s.replace('+', ' ');
  113. // Then go through the whole string looking for byte encoded characters
  114. int i;
  115. int start = 0;
  116. byte[] bytes = null;
  117. int length = str.length();
  118. CPStringBuilder result = new CPStringBuilder(length);
  119. while ((i = str.indexOf('%', start)) >= 0)
  120. {
  121. // Add all non-encoded characters to the result buffer
  122. result.append(str.substring(start, i));
  123. start = i;
  124. // Get all consecutive encoded bytes
  125. while ((i + 2 < length) && (str.charAt(i) == '%'))
  126. i += 3;
  127. // Decode all these bytes
  128. if ((bytes == null) || (bytes.length < ((i - start) / 3)))
  129. bytes = new byte[((i - start) / 3)];
  130. int index = 0;
  131. try
  132. {
  133. while (start < i)
  134. {
  135. String sub = str.substring(start + 1, start + 3);
  136. bytes[index] = (byte) Integer.parseInt(sub, 16);
  137. index++;
  138. start += 3;
  139. }
  140. }
  141. catch (NumberFormatException nfe)
  142. {
  143. // One of the hex encoded strings was bad
  144. }
  145. // Add the bytes as characters according to the given encoding
  146. result.append(new String(bytes, 0, index, encoding));
  147. // Make sure we skip to just after a % sign
  148. // There might not have been enough encoded characters after the %
  149. // or the hex chars were not actually hex chars (NumberFormatException)
  150. if (start < length && s.charAt(start) == '%')
  151. {
  152. result.append('%');
  153. start++;
  154. }
  155. }
  156. // Add any characters left
  157. if (start < str.length())
  158. result.append(str.substring(start));
  159. return result.toString();
  160. }
  161. } // class URLDecoder