X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fjson%2FHTTP.java;fp=src%2Forg%2Fjson%2FHTTP.java;h=c74edd79b680984800669c1a6e34f75dbfbfb930;hb=3459a8a691cb22508d7067f240b7254e588e77d3;hp=84ed53baea6c092b6207883ef13e87a7925f0a73;hpb=5b27f1062b2203c4c31702e205f4c78e1992063e;p=jalview.git diff --git a/src/org/json/HTTP.java b/src/org/json/HTTP.java index 84ed53b..c74edd7 100644 --- a/src/org/json/HTTP.java +++ b/src/org/json/HTTP.java @@ -28,135 +28,178 @@ import java.util.Locale; /** * Convert an HTTP header to a JSONObject and back. + * * @author JSON.org * @version 2015-12-09 */ -public class HTTP { - - /** Carriage return/line feed. */ - public static final String CRLF = "\r\n"; - - /** - * Convert an HTTP header string into a JSONObject. It can be a request - * header or a response header. A request header will contain - *
{
-     *    Method: "POST" (for example),
-     *    "Request-URI": "/" (for example),
-     *    "HTTP-Version": "HTTP/1.1" (for example)
-     * }
- * A response header will contain - *
{
-     *    "HTTP-Version": "HTTP/1.1" (for example),
-     *    "Status-Code": "200" (for example),
-     *    "Reason-Phrase": "OK" (for example)
-     * }
- * In addition, the other parameters in the header will be captured, using - * the HTTP field names as JSON names, so that
-     *    Date: Sun, 26 May 2002 18:06:04 GMT
-     *    Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
-     *    Cache-Control: no-cache
- * become - *
{...
-     *    Date: "Sun, 26 May 2002 18:06:04 GMT",
-     *    Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
-     *    "Cache-Control": "no-cache",
-     * ...}
- * It does no further checking or conversion. It does not parse dates. - * It does not do '%' transforms on URLs. - * @param string An HTTP header string. - * @return A JSONObject containing the elements and attributes - * of the XML string. - * @throws JSONException - */ - public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject jo = new JSONObject(); - HTTPTokener x = new HTTPTokener(string); - String token; - - token = x.nextToken(); - if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) { - -// Response - - jo.put("HTTP-Version", token); - jo.put("Status-Code", x.nextToken()); - jo.put("Reason-Phrase", x.nextTo('\0')); - x.next(); - - } else { - -// Request - - jo.put("Method", token); - jo.put("Request-URI", x.nextToken()); - jo.put("HTTP-Version", x.nextToken()); - } - -// Fields - - while (x.more()) { - String name = x.nextTo(':'); - x.next(':'); - jo.put(name, x.nextTo('\0')); - x.next(); - } - return jo; +public class HTTP +{ + + /** Carriage return/line feed. */ + public static final String CRLF = "\r\n"; + + /** + * Convert an HTTP header string into a JSONObject. It can be a request header + * or a response header. A request header will contain + * + *
+   * {
+   *    Method: "POST" (for example),
+   *    "Request-URI": "/" (for example),
+   *    "HTTP-Version": "HTTP/1.1" (for example)
+   * }
+   * 
+ * + * A response header will contain + * + *
+   * {
+   *    "HTTP-Version": "HTTP/1.1" (for example),
+   *    "Status-Code": "200" (for example),
+   *    "Reason-Phrase": "OK" (for example)
+   * }
+   * 
+ * + * In addition, the other parameters in the header will be captured, using the + * HTTP field names as JSON names, so that + * + *
+   *    Date: Sun, 26 May 2002 18:06:04 GMT
+   *    Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
+   *    Cache-Control: no-cache
+   * 
+ * + * become + * + *
+   * {...
+   *    Date: "Sun, 26 May 2002 18:06:04 GMT",
+   *    Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
+   *    "Cache-Control": "no-cache",
+   * ...}
+   * 
+ * + * It does no further checking or conversion. It does not parse dates. It does + * not do '%' transforms on URLs. + * + * @param string + * An HTTP header string. + * @return A JSONObject containing the elements and attributes of the XML + * string. + * @throws JSONException + */ + public static JSONObject toJSONObject(String string) throws JSONException + { + JSONObject jo = new JSONObject(); + HTTPTokener x = new HTTPTokener(string); + String token; + + token = x.nextToken(); + if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) + { + + // Response + + jo.put("HTTP-Version", token); + jo.put("Status-Code", x.nextToken()); + jo.put("Reason-Phrase", x.nextTo('\0')); + x.next(); + } + else + { + // Request - /** - * Convert a JSONObject into an HTTP header. A request header must contain - *
{
-     *    Method: "POST" (for example),
-     *    "Request-URI": "/" (for example),
-     *    "HTTP-Version": "HTTP/1.1" (for example)
-     * }
- * A response header must contain - *
{
-     *    "HTTP-Version": "HTTP/1.1" (for example),
-     *    "Status-Code": "200" (for example),
-     *    "Reason-Phrase": "OK" (for example)
-     * }
- * Any other members of the JSONObject will be output as HTTP fields. - * The result will end with two CRLF pairs. - * @param jo A JSONObject - * @return An HTTP header string. - * @throws JSONException if the object does not contain enough - * information. - */ - public static String toString(JSONObject jo) throws JSONException { - StringBuilder sb = new StringBuilder(); - if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { - sb.append(jo.getString("HTTP-Version")); - sb.append(' '); - sb.append(jo.getString("Status-Code")); - sb.append(' '); - sb.append(jo.getString("Reason-Phrase")); - } else if (jo.has("Method") && jo.has("Request-URI")) { - sb.append(jo.getString("Method")); - sb.append(' '); - sb.append('"'); - sb.append(jo.getString("Request-URI")); - sb.append('"'); - sb.append(' '); - sb.append(jo.getString("HTTP-Version")); - } else { - throw new JSONException("Not enough material for an HTTP header."); - } - sb.append(CRLF); - // Don't use the new entrySet API to maintain Android support - for (final String key : jo.keySet()) { - String value = jo.optString(key); - if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && - !"Reason-Phrase".equals(key) && !"Method".equals(key) && - !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) { - sb.append(key); - sb.append(": "); - sb.append(jo.optString(key)); - sb.append(CRLF); - } - } + jo.put("Method", token); + jo.put("Request-URI", x.nextToken()); + jo.put("HTTP-Version", x.nextToken()); + } + + // Fields + + while (x.more()) + { + String name = x.nextTo(':'); + x.next(':'); + jo.put(name, x.nextTo('\0')); + x.next(); + } + return jo; + } + + /** + * Convert a JSONObject into an HTTP header. A request header must contain + * + *
+   * {
+   *    Method: "POST" (for example),
+   *    "Request-URI": "/" (for example),
+   *    "HTTP-Version": "HTTP/1.1" (for example)
+   * }
+   * 
+ * + * A response header must contain + * + *
+   * {
+   *    "HTTP-Version": "HTTP/1.1" (for example),
+   *    "Status-Code": "200" (for example),
+   *    "Reason-Phrase": "OK" (for example)
+   * }
+   * 
+ * + * Any other members of the JSONObject will be output as HTTP fields. The + * result will end with two CRLF pairs. + * + * @param jo + * A JSONObject + * @return An HTTP header string. + * @throws JSONException + * if the object does not contain enough information. + */ + public static String toString(JSONObject jo) throws JSONException + { + StringBuilder sb = new StringBuilder(); + if (jo.has("Status-Code") && jo.has("Reason-Phrase")) + { + sb.append(jo.getString("HTTP-Version")); + sb.append(' '); + sb.append(jo.getString("Status-Code")); + sb.append(' '); + sb.append(jo.getString("Reason-Phrase")); + } + else if (jo.has("Method") && jo.has("Request-URI")) + { + sb.append(jo.getString("Method")); + sb.append(' '); + sb.append('"'); + sb.append(jo.getString("Request-URI")); + sb.append('"'); + sb.append(' '); + sb.append(jo.getString("HTTP-Version")); + } + else + { + throw new JSONException("Not enough material for an HTTP header."); + } + sb.append(CRLF); + // Don't use the new entrySet API to maintain Android support + for (final String key : jo.keySet()) + { + String value = jo.optString(key); + if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) + && !"Reason-Phrase".equals(key) && !"Method".equals(key) + && !"Request-URI".equals(key) + && !JSONObject.NULL.equals(value)) + { + sb.append(key); + sb.append(": "); + sb.append(jo.optString(key)); sb.append(CRLF); - return sb.toString(); + } } + sb.append(CRLF); + return sb.toString(); + } }