Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:21:17 GMT
  2. Package org.json

File Cookie.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
59% of files have more coverage

Code metrics

26
60
4
1
199
111
23
0.38
15
4
5.75

Classes

Class Line # Actions
34 60 23
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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. JSON and
29    * Cookies are both notations for name/value pairs.
30    *
31    * @author JSON.org
32    * @version 2015-12-09
33    */
 
34    public class Cookie
35    {
36   
37    /**
38    * Produce a copy of a string in which the characters '+', '%', '=', ';' and
39    * control characters are replaced with "%hh". This is a gentle form of URL
40    * encoding, attempting to cause as little distortion to the string as
41    * possible. The characters '=' and ';' are meta characters in cookies. By
42    * convention, they are escaped using the URL-encoding. This is only a
43    * convention, not a standard. Often, cookies are expected to have encoded
44    * values. We encode '=' and ';' because we must. We encode '%' and '+'
45    * because they are meta characters in URL encoding.
46    *
47    * @param string
48    * The source string.
49    * @return The escaped result.
50    */
 
51  0 toggle public static String escape(String string)
52    {
53  0 char c;
54  0 String s = string.trim();
55  0 int length = s.length();
56  0 StringBuilder sb = new StringBuilder(length);
57  0 for (int i = 0; i < length; i += 1)
58    {
59  0 c = s.charAt(i);
60  0 if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';')
61    {
62  0 sb.append('%');
63  0 sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16));
64  0 sb.append(Character.forDigit((char) (c & 0x0f), 16));
65    }
66    else
67    {
68  0 sb.append(c);
69    }
70    }
71  0 return sb.toString();
72    }
73   
74    /**
75    * Convert a cookie specification string into a JSONObject. The string will
76    * contain a name value pair separated by '='. The name and the value will be
77    * unescaped, possibly converting '+' and '%' sequences. The cookie properties
78    * may follow, separated by ';', also represented as name=value (except the
79    * secure property, which does not have a value). The name will be stored
80    * under the key "name", and the value will be stored under the key "value".
81    * This method does not do checking or validation of the parameters. It only
82    * converts the cookie string into a JSONObject.
83    *
84    * @param string
85    * The cookie specification string.
86    * @return A JSONObject containing "name", "value", and possibly other
87    * members.
88    * @throws JSONException
89    */
 
90  0 toggle public static JSONObject toJSONObject(String string) throws JSONException
91    {
92  0 String name;
93  0 JSONObject jo = new JSONObject();
94  0 Object value;
95  0 JSONTokener x = new JSONTokener(string);
96  0 jo.put("name", x.nextTo('='));
97  0 x.next('=');
98  0 jo.put("value", x.nextTo(';'));
99  0 x.next();
100  0 while (x.more())
101    {
102  0 name = unescape(x.nextTo("=;"));
103  0 if (x.next() != '=')
104    {
105  0 if (name.equals("secure"))
106    {
107  0 value = Boolean.TRUE;
108    }
109    else
110    {
111  0 throw x.syntaxError("Missing '=' in cookie parameter.");
112    }
113    }
114    else
115    {
116  0 value = unescape(x.nextTo(';'));
117  0 x.next();
118    }
119  0 jo.put(name, value);
120    }
121  0 return jo;
122    }
123   
124    /**
125    * Convert a JSONObject into a cookie specification string. The JSONObject
126    * must contain "name" and "value" members. If the JSONObject contains
127    * "expires", "domain", "path", or "secure" members, they will be appended to
128    * the cookie specification string. All other members are ignored.
129    *
130    * @param jo
131    * A JSONObject
132    * @return A cookie specification string
133    * @throws JSONException
134    */
 
135  0 toggle public static String toString(JSONObject jo) throws JSONException
136    {
137  0 StringBuilder sb = new StringBuilder();
138   
139  0 sb.append(escape(jo.getString("name")));
140  0 sb.append("=");
141  0 sb.append(escape(jo.getString("value")));
142  0 if (jo.has("expires"))
143    {
144  0 sb.append(";expires=");
145  0 sb.append(jo.getString("expires"));
146    }
147  0 if (jo.has("domain"))
148    {
149  0 sb.append(";domain=");
150  0 sb.append(escape(jo.getString("domain")));
151    }
152  0 if (jo.has("path"))
153    {
154  0 sb.append(";path=");
155  0 sb.append(escape(jo.getString("path")));
156    }
157  0 if (jo.optBoolean("secure"))
158    {
159  0 sb.append(";secure");
160    }
161  0 return sb.toString();
162    }
163   
164    /**
165    * Convert <code>%</code><i>hh</i> sequences to single characters, and convert
166    * plus to space.
167    *
168    * @param string
169    * A string that may contain
170    * <code>+</code>&nbsp;<small>(plus)</small> and
171    * <code>%</code><i>hh</i> sequences.
172    * @return The unescaped string.
173    */
 
174  0 toggle public static String unescape(String string)
175    {
176  0 int length = string.length();
177  0 StringBuilder sb = new StringBuilder(length);
178  0 for (int i = 0; i < length; ++i)
179    {
180  0 char c = string.charAt(i);
181  0 if (c == '+')
182    {
183  0 c = ' ';
184    }
185  0 else if (c == '%' && i + 2 < length)
186    {
187  0 int d = JSONTokener.dehexchar(string.charAt(i + 1));
188  0 int e = JSONTokener.dehexchar(string.charAt(i + 2));
189  0 if (d >= 0 && e >= 0)
190    {
191  0 c = (char) (d * 16 + e);
192  0 i += 2;
193    }
194    }
195  0 sb.append(c);
196    }
197  0 return sb.toString();
198    }
199    }