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