URLDecoder.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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 java.io.UnsupportedEncodingException;
  33. /**
  34. * This utility class contains static methods that converts a
  35. * string encoded in the x-www-form-urlencoded format to the original
  36. * text. The x-www-form-urlencoded format replaces certain disallowed
  37. * characters with encoded equivalents. All upper case and lower case
  38. * letters in the US alphabet remain as is, the space character (' ')
  39. * is replaced with '+' sign, and all other characters are converted to a
  40. * "%XX" format where XX is the hexadecimal representation of that character
  41. * in a given character encoding (default is "UTF-8").
  42. * <p>
  43. * This method is very useful for decoding strings sent to CGI scripts
  44. *
  45. * Written using on-line Java Platform 1.2/1.4 API Specification.
  46. * Status: Believed complete and correct.
  47. *
  48. * @since 1.2
  49. *
  50. * @author Warren Levy <warrenl@cygnus.com>
  51. * @author Aaron M. Renn (arenn@urbanophile.com) (documentation comments)
  52. * @author Mark Wielaard (mark@klomp.org)
  53. */
  54. public class URLDecoder
  55. {
  56. /**
  57. * Public contructor. Note that this class has only static methods.
  58. */
  59. public URLDecoder ()
  60. {
  61. }
  62. /**
  63. * This method translates the passed in string from x-www-form-urlencoded
  64. * format using the default encoding "UTF-8" to decode the hex encoded
  65. * unsafe characters.
  66. *
  67. * @param s the String to convert
  68. *
  69. * @return the converted String
  70. */
  71. public static String decode(String s)
  72. {
  73. try
  74. {
  75. return decode(s, "UTF-8");
  76. }
  77. catch (UnsupportedEncodingException uee)
  78. {
  79. // Should never happen since UTF-8 encoding should always be supported
  80. return s;
  81. }
  82. }
  83. /**
  84. * This method translates the passed in string from x-www-form-urlencoded
  85. * format using the given character encoding to decode the hex encoded
  86. * unsafe characters.
  87. *
  88. * This implementation will decode the string even if it contains
  89. * unsafe characters (characters that should have been encoded) or if the
  90. * two characters following a % do not represent a hex encoded byte.
  91. * In those cases the unsafe character or the % character will be added
  92. * verbatim to the decoded result.
  93. *
  94. * @param s the String to convert
  95. * @param encoding the character encoding to use the decode the hex encoded
  96. * unsafe characters
  97. *
  98. * @return the converted String
  99. *
  100. * @exception UnsupportedEncodingException If the named encoding is not
  101. * supported
  102. *
  103. * @since 1.4
  104. */
  105. public static String decode(String s, String encoding)
  106. throws UnsupportedEncodingException
  107. {
  108. // First convert all '+' characters to spaces.
  109. String str = s.replace('+', ' ');
  110. // Then go through the whole string looking for byte encoded characters
  111. int i;
  112. int start = 0;
  113. byte[] bytes = null;
  114. int length = str.length();
  115. StringBuffer result = new StringBuffer(length);
  116. while ((i = str.indexOf('%', start)) >= 0)
  117. {
  118. // Add all non-encoded characters to the result buffer
  119. result.append(str.substring(start, i));
  120. start = i;
  121. // Get all consecutive encoded bytes
  122. while ((i+2 < length) && (str.charAt(i) == '%'))
  123. i += 3;
  124. // Decode all these bytes
  125. if ((bytes == null) || (bytes.length < ((i-start)/3)))
  126. bytes = new byte[((i-start)/3)];
  127. int index = 0;
  128. try
  129. {
  130. while (start < i)
  131. {
  132. String sub = str.substring(start + 1, start + 3);
  133. bytes[index] = (byte)Integer.parseInt(sub, 16);
  134. index++;
  135. start += 3;
  136. }
  137. }
  138. catch (NumberFormatException nfe)
  139. {
  140. // One of the hex encoded strings was bad
  141. }
  142. // Add the bytes as characters according to the given encoding
  143. result.append(new String(bytes, 0, index, encoding));
  144. // Make sure we skip to just after a % sign
  145. // There might not have been enough encoded characters after the %
  146. // or the hex chars were not actually hex chars (NumberFormatException)
  147. if (start < length && s.charAt(start) == '%')
  148. {
  149. result.append('%');
  150. start++;
  151. }
  152. }
  153. // Add any characters left
  154. if (start < str.length())
  155. result.append(str.substring(start));
  156. return result.toString();
  157. }
  158. } // class URLDecoder