annotate lib/org.json_2.0/src/org/json/HTTP.java @ 0:db87c1b7eb6d

initial
author dwinter
date Wed, 03 Nov 2010 12:18:46 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1 package org.json;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
2
db87c1b7eb6d initial
dwinter
parents:
diff changeset
3 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
4 Copyright (c) 2002 JSON.org
db87c1b7eb6d initial
dwinter
parents:
diff changeset
5
db87c1b7eb6d initial
dwinter
parents:
diff changeset
6 Permission is hereby granted, free of charge, to any person obtaining a copy
db87c1b7eb6d initial
dwinter
parents:
diff changeset
7 of this software and associated documentation files (the "Software"), to deal
db87c1b7eb6d initial
dwinter
parents:
diff changeset
8 in the Software without restriction, including without limitation the rights
db87c1b7eb6d initial
dwinter
parents:
diff changeset
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
db87c1b7eb6d initial
dwinter
parents:
diff changeset
10 copies of the Software, and to permit persons to whom the Software is
db87c1b7eb6d initial
dwinter
parents:
diff changeset
11 furnished to do so, subject to the following conditions:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
12
db87c1b7eb6d initial
dwinter
parents:
diff changeset
13 The above copyright notice and this permission notice shall be included in all
db87c1b7eb6d initial
dwinter
parents:
diff changeset
14 copies or substantial portions of the Software.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
15
db87c1b7eb6d initial
dwinter
parents:
diff changeset
16 The Software shall be used for Good, not Evil.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
17
db87c1b7eb6d initial
dwinter
parents:
diff changeset
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
db87c1b7eb6d initial
dwinter
parents:
diff changeset
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
db87c1b7eb6d initial
dwinter
parents:
diff changeset
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
db87c1b7eb6d initial
dwinter
parents:
diff changeset
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
db87c1b7eb6d initial
dwinter
parents:
diff changeset
24 SOFTWARE.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
25 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
26
db87c1b7eb6d initial
dwinter
parents:
diff changeset
27 import java.util.Iterator;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
28
db87c1b7eb6d initial
dwinter
parents:
diff changeset
29 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
30 * Convert an HTTP header to a JSONObject and back.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
31 * @author JSON.org
db87c1b7eb6d initial
dwinter
parents:
diff changeset
32 * @version 2008-09-18
db87c1b7eb6d initial
dwinter
parents:
diff changeset
33 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
34 public class HTTP {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
35
db87c1b7eb6d initial
dwinter
parents:
diff changeset
36 /** Carriage return/line feed. */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
37 public static final String CRLF = "\r\n";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
38
db87c1b7eb6d initial
dwinter
parents:
diff changeset
39 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
40 * Convert an HTTP header string into a JSONObject. It can be a request
db87c1b7eb6d initial
dwinter
parents:
diff changeset
41 * header or a response header. A request header will contain
db87c1b7eb6d initial
dwinter
parents:
diff changeset
42 * <pre>{
db87c1b7eb6d initial
dwinter
parents:
diff changeset
43 * Method: "POST" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
44 * "Request-URI": "/" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
45 * "HTTP-Version": "HTTP/1.1" (for example)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
46 * }</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
47 * A response header will contain
db87c1b7eb6d initial
dwinter
parents:
diff changeset
48 * <pre>{
db87c1b7eb6d initial
dwinter
parents:
diff changeset
49 * "HTTP-Version": "HTTP/1.1" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
50 * "Status-Code": "200" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
51 * "Reason-Phrase": "OK" (for example)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
52 * }</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
53 * In addition, the other parameters in the header will be captured, using
db87c1b7eb6d initial
dwinter
parents:
diff changeset
54 * the HTTP field names as JSON names, so that <pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
55 * Date: Sun, 26 May 2002 18:06:04 GMT
db87c1b7eb6d initial
dwinter
parents:
diff changeset
56 * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
db87c1b7eb6d initial
dwinter
parents:
diff changeset
57 * Cache-Control: no-cache</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
58 * become
db87c1b7eb6d initial
dwinter
parents:
diff changeset
59 * <pre>{...
db87c1b7eb6d initial
dwinter
parents:
diff changeset
60 * Date: "Sun, 26 May 2002 18:06:04 GMT",
db87c1b7eb6d initial
dwinter
parents:
diff changeset
61 * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
db87c1b7eb6d initial
dwinter
parents:
diff changeset
62 * "Cache-Control": "no-cache",
db87c1b7eb6d initial
dwinter
parents:
diff changeset
63 * ...}</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
64 * It does no further checking or conversion. It does not parse dates.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
65 * It does not do '%' transforms on URLs.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
66 * @param string An HTTP header string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
67 * @return A JSONObject containing the elements and attributes
db87c1b7eb6d initial
dwinter
parents:
diff changeset
68 * of the XML string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
69 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
70 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
71 public static JSONObject toJSONObject(String string) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
72 JSONObject o = new JSONObject();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
73 HTTPTokener x = new HTTPTokener(string);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
74 String t;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
75
db87c1b7eb6d initial
dwinter
parents:
diff changeset
76 t = x.nextToken();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
77 if (t.toUpperCase().startsWith("HTTP")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
78
db87c1b7eb6d initial
dwinter
parents:
diff changeset
79 // Response
db87c1b7eb6d initial
dwinter
parents:
diff changeset
80
db87c1b7eb6d initial
dwinter
parents:
diff changeset
81 o.put("HTTP-Version", t);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
82 o.put("Status-Code", x.nextToken());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
83 o.put("Reason-Phrase", x.nextTo('\0'));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
84 x.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
85
db87c1b7eb6d initial
dwinter
parents:
diff changeset
86 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
87
db87c1b7eb6d initial
dwinter
parents:
diff changeset
88 // Request
db87c1b7eb6d initial
dwinter
parents:
diff changeset
89
db87c1b7eb6d initial
dwinter
parents:
diff changeset
90 o.put("Method", t);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
91 o.put("Request-URI", x.nextToken());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
92 o.put("HTTP-Version", x.nextToken());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
93 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
94
db87c1b7eb6d initial
dwinter
parents:
diff changeset
95 // Fields
db87c1b7eb6d initial
dwinter
parents:
diff changeset
96
db87c1b7eb6d initial
dwinter
parents:
diff changeset
97 while (x.more()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
98 String name = x.nextTo(':');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
99 x.next(':');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
100 o.put(name, x.nextTo('\0'));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
101 x.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
102 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
103 return o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
104 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
105
db87c1b7eb6d initial
dwinter
parents:
diff changeset
106
db87c1b7eb6d initial
dwinter
parents:
diff changeset
107 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
108 * Convert a JSONObject into an HTTP header. A request header must contain
db87c1b7eb6d initial
dwinter
parents:
diff changeset
109 * <pre>{
db87c1b7eb6d initial
dwinter
parents:
diff changeset
110 * Method: "POST" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
111 * "Request-URI": "/" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
112 * "HTTP-Version": "HTTP/1.1" (for example)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
113 * }</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
114 * A response header must contain
db87c1b7eb6d initial
dwinter
parents:
diff changeset
115 * <pre>{
db87c1b7eb6d initial
dwinter
parents:
diff changeset
116 * "HTTP-Version": "HTTP/1.1" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
117 * "Status-Code": "200" (for example),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
118 * "Reason-Phrase": "OK" (for example)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
119 * }</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
120 * Any other members of the JSONObject will be output as HTTP fields.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
121 * The result will end with two CRLF pairs.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
122 * @param o A JSONObject
db87c1b7eb6d initial
dwinter
parents:
diff changeset
123 * @return An HTTP header string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
124 * @throws JSONException if the object does not contain enough
db87c1b7eb6d initial
dwinter
parents:
diff changeset
125 * information.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
126 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
127 public static String toString(JSONObject o) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
128 Iterator keys = o.keys();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
129 String s;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
130 StringBuffer sb = new StringBuffer();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
131 if (o.has("Status-Code") && o.has("Reason-Phrase")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
132 sb.append(o.getString("HTTP-Version"));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
133 sb.append(' ');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
134 sb.append(o.getString("Status-Code"));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
135 sb.append(' ');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
136 sb.append(o.getString("Reason-Phrase"));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
137 } else if (o.has("Method") && o.has("Request-URI")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
138 sb.append(o.getString("Method"));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
139 sb.append(' ');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
140 sb.append('"');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
141 sb.append(o.getString("Request-URI"));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
142 sb.append('"');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
143 sb.append(' ');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
144 sb.append(o.getString("HTTP-Version"));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
145 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
146 throw new JSONException("Not enough material for an HTTP header.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
147 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
148 sb.append(CRLF);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
149 while (keys.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
150 s = keys.next().toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
151 if (!s.equals("HTTP-Version") && !s.equals("Status-Code") &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
152 !s.equals("Reason-Phrase") && !s.equals("Method") &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
153 !s.equals("Request-URI") && !o.isNull(s)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
154 sb.append(s);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
155 sb.append(": ");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
156 sb.append(o.getString(s));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
157 sb.append(CRLF);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
158 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
159 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
160 sb.append(CRLF);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
161 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
162 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
163 }