001package org.json;
002
003/*
004Copyright (c) 2002 JSON.org
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016The Software shall be used for Good, not Evil.
017
018THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
021AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
023OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
024SOFTWARE.
025 */
026import java.util.Iterator;
027import java.util.Locale;
028
029/**
030 * Convert an HTTP header to a JSONObject and back.
031 * @author JSON.org
032@version $Id$
033@since Ptolemy II 10.0
034 * @version 2008-09-18
035 */
036public class HTTP {
037
038    /** Carriage return/line feed. */
039    public static final String CRLF = "\r\n";
040
041    /**
042     * Convert an HTTP header string into a JSONObject. It can be a request
043     * header or a response header. A request header will contain
044     * <pre>{
045     *    Method: "POST" (for example),
046     *    "Request-URI": "/" (for example),
047     *    "HTTP-Version": "HTTP/1.1" (for example)
048     * }</pre>
049     * A response header will contain
050     * <pre>{
051     *    "HTTP-Version": "HTTP/1.1" (for example),
052     *    "Status-Code": "200" (for example),
053     *    "Reason-Phrase": "OK" (for example)
054     * }</pre>
055     * In addition, the other parameters in the header will be captured, using
056     * the HTTP field names as JSON names, so that <pre>
057     *    Date: Sun, 26 May 2002 18:06:04 GMT
058     *    Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&amp;b=2&amp;f=s
059     *    Cache-Control: no-cache</pre>
060     * become
061     * <pre>{...
062     *    Date: "Sun, 26 May 2002 18:06:04 GMT",
063     *    Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&amp;b=2&amp;f=s",
064     *    "Cache-Control": "no-cache",
065     * ...}</pre>
066     * It does no further checking or conversion. It does not parse dates.
067     * It does not do '%' transforms on URLs.
068     * @param string An HTTP header string.
069     * @return A JSONObject containing the elements and attributes
070     * of the XML string.
071     * @exception JSONException
072     */
073    public static JSONObject toJSONObject(String string) throws JSONException {
074        JSONObject o = new JSONObject();
075        HTTPTokener x = new HTTPTokener(string);
076        String t;
077
078        t = x.nextToken();
079        if (t.toUpperCase(Locale.getDefault()).startsWith("HTTP")) {
080
081            // Response
082
083            o.put("HTTP-Version", t);
084            o.put("Status-Code", x.nextToken());
085            o.put("Reason-Phrase", x.nextTo('\0'));
086            x.next();
087
088        } else {
089
090            // Request
091
092            o.put("Method", t);
093            o.put("Request-URI", x.nextToken());
094            o.put("HTTP-Version", x.nextToken());
095        }
096
097        // Fields
098
099        while (x.more()) {
100            String name = x.nextTo(':');
101            x.next(':');
102            o.put(name, x.nextTo('\0'));
103            x.next();
104        }
105        return o;
106    }
107
108    /**
109     * Convert a JSONObject into an HTTP header. A request header must contain
110     * <pre>{
111     *    Method: "POST" (for example),
112     *    "Request-URI": "/" (for example),
113     *    "HTTP-Version": "HTTP/1.1" (for example)
114     * }</pre>
115     * A response header must contain
116     * <pre>{
117     *    "HTTP-Version": "HTTP/1.1" (for example),
118     *    "Status-Code": "200" (for example),
119     *    "Reason-Phrase": "OK" (for example)
120     * }</pre>
121     * Any other members of the JSONObject will be output as HTTP fields.
122     * The result will end with two CRLF pairs.
123     * @param o A JSONObject
124     * @return An HTTP header string.
125     * @exception JSONException if the object does not contain enough
126     *  information.
127     */
128    public static String toString(JSONObject o) throws JSONException {
129        Iterator keys = o.keys();
130        String s;
131        StringBuffer sb = new StringBuffer();
132        if (o.has("Status-Code") && o.has("Reason-Phrase")) {
133            sb.append(o.getString("HTTP-Version"));
134            sb.append(' ');
135            sb.append(o.getString("Status-Code"));
136            sb.append(' ');
137            sb.append(o.getString("Reason-Phrase"));
138        } else if (o.has("Method") && o.has("Request-URI")) {
139            sb.append(o.getString("Method"));
140            sb.append(' ');
141            sb.append('"');
142            sb.append(o.getString("Request-URI"));
143            sb.append('"');
144            sb.append(' ');
145            sb.append(o.getString("HTTP-Version"));
146        } else {
147            throw new JSONException("Not enough material for an HTTP header.");
148        }
149        sb.append(CRLF);
150        while (keys.hasNext()) {
151            s = keys.next().toString();
152            if (!s.equals("HTTP-Version") && !s.equals("Status-Code")
153                    && !s.equals("Reason-Phrase") && !s.equals("Method")
154                    && !s.equals("Request-URI") && !o.isNull(s)) {
155                sb.append(s);
156                sb.append(": ");
157                sb.append(o.getString(s));
158                sb.append(CRLF);
159            }
160        }
161        sb.append(CRLF);
162        return sb.toString();
163    }
164}