4 Copyright (c) 2002 JSON.org
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:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
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
27 import java.util.Locale;
30 * Convert an HTTP header to a JSONObject and back.
38 /** Carriage return/line feed. */
39 public static final String CRLF = "\r\n";
42 * Convert an HTTP header string into a JSONObject. It can be a request header
43 * or a response header. A request header will contain
47 * Method: "POST" (for example),
48 * "Request-URI": "/" (for example),
49 * "HTTP-Version": "HTTP/1.1" (for example)
53 * A response header will contain
57 * "HTTP-Version": "HTTP/1.1" (for example),
58 * "Status-Code": "200" (for example),
59 * "Reason-Phrase": "OK" (for example)
63 * In addition, the other parameters in the header will be captured, using the
64 * HTTP field names as JSON names, so that
67 * Date: Sun, 26 May 2002 18:06:04 GMT
68 * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
69 * Cache-Control: no-cache
76 * Date: "Sun, 26 May 2002 18:06:04 GMT",
77 * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
78 * "Cache-Control": "no-cache",
82 * It does no further checking or conversion. It does not parse dates. It does
83 * not do '%' transforms on URLs.
86 * An HTTP header string.
87 * @return A JSONObject containing the elements and attributes of the XML
89 * @throws JSONException
91 public static JSONObject toJSONObject(String string) throws JSONException
93 JSONObject jo = new JSONObject();
94 HTTPTokener x = new HTTPTokener(string);
97 token = x.nextToken();
98 if (token.toUpperCase(Locale.ROOT).startsWith("HTTP"))
103 jo.put("HTTP-Version", token);
104 jo.put("Status-Code", x.nextToken());
105 jo.put("Reason-Phrase", x.nextTo('\0'));
114 jo.put("Method", token);
115 jo.put("Request-URI", x.nextToken());
116 jo.put("HTTP-Version", x.nextToken());
123 String name = x.nextTo(':');
125 jo.put(name, x.nextTo('\0'));
132 * Convert a JSONObject into an HTTP header. A request header must contain
136 * Method: "POST" (for example),
137 * "Request-URI": "/" (for example),
138 * "HTTP-Version": "HTTP/1.1" (for example)
142 * A response header must contain
146 * "HTTP-Version": "HTTP/1.1" (for example),
147 * "Status-Code": "200" (for example),
148 * "Reason-Phrase": "OK" (for example)
152 * Any other members of the JSONObject will be output as HTTP fields. The
153 * result will end with two CRLF pairs.
157 * @return An HTTP header string.
158 * @throws JSONException
159 * if the object does not contain enough information.
161 public static String toString(JSONObject jo) throws JSONException
163 StringBuilder sb = new StringBuilder();
164 if (jo.has("Status-Code") && jo.has("Reason-Phrase"))
166 sb.append(jo.getString("HTTP-Version"));
168 sb.append(jo.getString("Status-Code"));
170 sb.append(jo.getString("Reason-Phrase"));
172 else if (jo.has("Method") && jo.has("Request-URI"))
174 sb.append(jo.getString("Method"));
177 sb.append(jo.getString("Request-URI"));
180 sb.append(jo.getString("HTTP-Version"));
184 throw new JSONException("Not enough material for an HTTP header.");
187 // Don't use the new entrySet API to maintain Android support
188 for (final String key : jo.keySet())
190 String value = jo.optString(key);
191 if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key)
192 && !"Reason-Phrase".equals(key) && !"Method".equals(key)
193 && !"Request-URI".equals(key)
194 && !JSONObject.NULL.equals(value))
198 sb.append(jo.optString(key));
203 return sb.toString();