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