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