root / src / org / json / Cookie.java @ 4bf05cad
History | View | Annotate | Download (6.3 kB)
1 | 14ad7326 | pastith | package org.json; |
---|---|---|---|
2 | 14ad7326 | pastith | |
3 | 14ad7326 | pastith | /*
|
4 | 14ad7326 | pastith | Copyright (c) 2002 JSON.org
|
5 | 14ad7326 | pastith | |
6 | 14ad7326 | pastith | Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 14ad7326 | pastith | of this software and associated documentation files (the "Software"), to deal
|
8 | 14ad7326 | pastith | in the Software without restriction, including without limitation the rights
|
9 | 14ad7326 | pastith | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 14ad7326 | pastith | copies of the Software, and to permit persons to whom the Software is
|
11 | 14ad7326 | pastith | furnished to do so, subject to the following conditions:
|
12 | 14ad7326 | pastith | |
13 | 14ad7326 | pastith | The above copyright notice and this permission notice shall be included in all
|
14 | 14ad7326 | pastith | copies or substantial portions of the Software.
|
15 | 14ad7326 | pastith | |
16 | 14ad7326 | pastith | The Software shall be used for Good, not Evil.
|
17 | 14ad7326 | pastith | |
18 | 14ad7326 | pastith | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 | 14ad7326 | pastith | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 | 14ad7326 | pastith | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21 | 14ad7326 | pastith | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 | 14ad7326 | pastith | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 | 14ad7326 | pastith | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24 | 14ad7326 | pastith | SOFTWARE.
|
25 | 14ad7326 | pastith | */
|
26 | 14ad7326 | pastith | |
27 | 14ad7326 | pastith | /**
|
28 | 14ad7326 | pastith | * Convert a web browser cookie specification to a JSONObject and back.
|
29 | 14ad7326 | pastith | * JSON and Cookies are both notations for name/value pairs.
|
30 | 14ad7326 | pastith | * @author JSON.org
|
31 | 14ad7326 | pastith | * @version 2008-09-18
|
32 | 14ad7326 | pastith | */
|
33 | 14ad7326 | pastith | public class Cookie { |
34 | 14ad7326 | pastith | |
35 | 14ad7326 | pastith | /**
|
36 | 14ad7326 | pastith | * Produce a copy of a string in which the characters '+', '%', '=', ';'
|
37 | 14ad7326 | pastith | * and control characters are replaced with "%hh". This is a gentle form
|
38 | 14ad7326 | pastith | * of URL encoding, attempting to cause as little distortion to the
|
39 | 14ad7326 | pastith | * string as possible. The characters '=' and ';' are meta characters in
|
40 | 14ad7326 | pastith | * cookies. By convention, they are escaped using the URL-encoding. This is
|
41 | 14ad7326 | pastith | * only a convention, not a standard. Often, cookies are expected to have
|
42 | 14ad7326 | pastith | * encoded values. We encode '=' and ';' because we must. We encode '%' and
|
43 | 14ad7326 | pastith | * '+' because they are meta characters in URL encoding.
|
44 | 14ad7326 | pastith | * @param string The source string.
|
45 | 14ad7326 | pastith | * @return The escaped result.
|
46 | 14ad7326 | pastith | */
|
47 | 14ad7326 | pastith | public static String escape(String string) { |
48 | 14ad7326 | pastith | char c;
|
49 | 14ad7326 | pastith | String s = string.trim();
|
50 | 14ad7326 | pastith | StringBuffer sb = new StringBuffer(); |
51 | 14ad7326 | pastith | int len = s.length();
|
52 | 14ad7326 | pastith | for (int i = 0; i < len; i += 1) { |
53 | 14ad7326 | pastith | c = s.charAt(i); |
54 | 14ad7326 | pastith | if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') { |
55 | 14ad7326 | pastith | sb.append('%');
|
56 | 14ad7326 | pastith | sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16)); |
57 | 14ad7326 | pastith | sb.append(Character.forDigit((char)(c & 0x0f), 16)); |
58 | 14ad7326 | pastith | } else {
|
59 | 14ad7326 | pastith | sb.append(c); |
60 | 14ad7326 | pastith | } |
61 | 14ad7326 | pastith | } |
62 | 14ad7326 | pastith | return sb.toString();
|
63 | 14ad7326 | pastith | } |
64 | 14ad7326 | pastith | |
65 | 14ad7326 | pastith | |
66 | 14ad7326 | pastith | /**
|
67 | 14ad7326 | pastith | * Convert a cookie specification string into a JSONObject. The string
|
68 | 14ad7326 | pastith | * will contain a name value pair separated by '='. The name and the value
|
69 | 14ad7326 | pastith | * will be unescaped, possibly converting '+' and '%' sequences. The
|
70 | 14ad7326 | pastith | * cookie properties may follow, separated by ';', also represented as
|
71 | 14ad7326 | pastith | * name=value (except the secure property, which does not have a value).
|
72 | 14ad7326 | pastith | * The name will be stored under the key "name", and the value will be
|
73 | 14ad7326 | pastith | * stored under the key "value". This method does not do checking or
|
74 | 14ad7326 | pastith | * validation of the parameters. It only converts the cookie string into
|
75 | 14ad7326 | pastith | * a JSONObject.
|
76 | 14ad7326 | pastith | * @param string The cookie specification string.
|
77 | 14ad7326 | pastith | * @return A JSONObject containing "name", "value", and possibly other
|
78 | 14ad7326 | pastith | * members.
|
79 | 14ad7326 | pastith | * @throws JSONException
|
80 | 14ad7326 | pastith | */
|
81 | 14ad7326 | pastith | public static JSONObject toJSONObject(String string) throws JSONException { |
82 | 14ad7326 | pastith | String n;
|
83 | 14ad7326 | pastith | JSONObject o = new JSONObject();
|
84 | 14ad7326 | pastith | Object v;
|
85 | 14ad7326 | pastith | JSONTokener x = new JSONTokener(string);
|
86 | 14ad7326 | pastith | o.put("name", x.nextTo('=')); |
87 | 14ad7326 | pastith | x.next('=');
|
88 | 14ad7326 | pastith | o.put("value", x.nextTo(';')); |
89 | 14ad7326 | pastith | x.next(); |
90 | 14ad7326 | pastith | while (x.more()) {
|
91 | 14ad7326 | pastith | n = unescape(x.nextTo("=;"));
|
92 | 14ad7326 | pastith | if (x.next() != '=') { |
93 | 14ad7326 | pastith | if (n.equals("secure")) { |
94 | 14ad7326 | pastith | v = Boolean.TRUE;
|
95 | 14ad7326 | pastith | } else {
|
96 | 14ad7326 | pastith | throw x.syntaxError("Missing '=' in cookie parameter."); |
97 | 14ad7326 | pastith | } |
98 | 14ad7326 | pastith | } else {
|
99 | 14ad7326 | pastith | v = unescape(x.nextTo(';'));
|
100 | 14ad7326 | pastith | x.next(); |
101 | 14ad7326 | pastith | } |
102 | 14ad7326 | pastith | o.put(n, v); |
103 | 14ad7326 | pastith | } |
104 | 14ad7326 | pastith | return o;
|
105 | 14ad7326 | pastith | } |
106 | 14ad7326 | pastith | |
107 | 14ad7326 | pastith | |
108 | 14ad7326 | pastith | /**
|
109 | 14ad7326 | pastith | * Convert a JSONObject into a cookie specification string. The JSONObject
|
110 | 14ad7326 | pastith | * must contain "name" and "value" members.
|
111 | 14ad7326 | pastith | * If the JSONObject contains "expires", "domain", "path", or "secure"
|
112 | 14ad7326 | pastith | * members, they will be appended to the cookie specification string.
|
113 | 14ad7326 | pastith | * All other members are ignored.
|
114 | 14ad7326 | pastith | * @param o A JSONObject
|
115 | 14ad7326 | pastith | * @return A cookie specification string
|
116 | 14ad7326 | pastith | * @throws JSONException
|
117 | 14ad7326 | pastith | */
|
118 | 14ad7326 | pastith | public static String toString(JSONObject o) throws JSONException { |
119 | 14ad7326 | pastith | StringBuffer sb = new StringBuffer(); |
120 | 14ad7326 | pastith | |
121 | 14ad7326 | pastith | sb.append(escape(o.getString("name")));
|
122 | 14ad7326 | pastith | sb.append("=");
|
123 | 14ad7326 | pastith | sb.append(escape(o.getString("value")));
|
124 | 14ad7326 | pastith | if (o.has("expires")) { |
125 | 14ad7326 | pastith | sb.append(";expires=");
|
126 | 14ad7326 | pastith | sb.append(o.getString("expires"));
|
127 | 14ad7326 | pastith | } |
128 | 14ad7326 | pastith | if (o.has("domain")) { |
129 | 14ad7326 | pastith | sb.append(";domain=");
|
130 | 14ad7326 | pastith | sb.append(escape(o.getString("domain")));
|
131 | 14ad7326 | pastith | } |
132 | 14ad7326 | pastith | if (o.has("path")) { |
133 | 14ad7326 | pastith | sb.append(";path=");
|
134 | 14ad7326 | pastith | sb.append(escape(o.getString("path")));
|
135 | 14ad7326 | pastith | } |
136 | 14ad7326 | pastith | if (o.optBoolean("secure")) { |
137 | 14ad7326 | pastith | sb.append(";secure");
|
138 | 14ad7326 | pastith | } |
139 | 14ad7326 | pastith | return sb.toString();
|
140 | 14ad7326 | pastith | } |
141 | 14ad7326 | pastith | |
142 | 14ad7326 | pastith | /**
|
143 | 14ad7326 | pastith | * Convert <code>%</code><i>hh</i> sequences to single characters, and
|
144 | 14ad7326 | pastith | * convert plus to space.
|
145 | 14ad7326 | pastith | * @param s A string that may contain
|
146 | 14ad7326 | pastith | * <code>+</code> <small>(plus)</small> and
|
147 | 14ad7326 | pastith | * <code>%</code><i>hh</i> sequences.
|
148 | 14ad7326 | pastith | * @return The unescaped string.
|
149 | 14ad7326 | pastith | */
|
150 | 14ad7326 | pastith | public static String unescape(String s) { |
151 | 14ad7326 | pastith | int len = s.length();
|
152 | 14ad7326 | pastith | StringBuffer b = new StringBuffer(); |
153 | 14ad7326 | pastith | for (int i = 0; i < len; ++i) { |
154 | 14ad7326 | pastith | char c = s.charAt(i);
|
155 | 14ad7326 | pastith | if (c == '+') { |
156 | 14ad7326 | pastith | c = ' ';
|
157 | 14ad7326 | pastith | } else if (c == '%' && i + 2 < len) { |
158 | 14ad7326 | pastith | int d = JSONTokener.dehexchar(s.charAt(i + 1)); |
159 | 14ad7326 | pastith | int e = JSONTokener.dehexchar(s.charAt(i + 2)); |
160 | 14ad7326 | pastith | if (d >= 0 && e >= 0) { |
161 | 14ad7326 | pastith | c = (char)(d * 16 + e); |
162 | 14ad7326 | pastith | i += 2;
|
163 | 14ad7326 | pastith | } |
164 | 14ad7326 | pastith | } |
165 | 14ad7326 | pastith | b.append(c); |
166 | 14ad7326 | pastith | } |
167 | 14ad7326 | pastith | return b.toString();
|
168 | 14ad7326 | pastith | } |
169 | 14ad7326 | pastith | } |