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*/ 026 027import java.util.Iterator; 028 029/** 030 * Convert an HTTP header to a JSONObject and back. 031 * @author JSON.org 032 * @version 2015-12-09 033 */ 034public class HTTP { 035 036 /** Carriage return/line feed. */ 037 public static final String CRLF = "\r\n"; 038 039 /** 040 * Convert an HTTP header string into a JSONObject. It can be a request 041 * header or a response header. A request header will contain 042 * <pre>{ 043 * Method: "POST" (for example), 044 * "Request-URI": "/" (for example), 045 * "HTTP-Version": "HTTP/1.1" (for example) 046 * }</pre> 047 * A response header will contain 048 * <pre>{ 049 * "HTTP-Version": "HTTP/1.1" (for example), 050 * "Status-Code": "200" (for example), 051 * "Reason-Phrase": "OK" (for example) 052 * }</pre> 053 * In addition, the other parameters in the header will be captured, using 054 * the HTTP field names as JSON names, so that <pre> 055 * Date: Sun, 26 May 2002 18:06:04 GMT 056 * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s 057 * Cache-Control: no-cache</pre> 058 * become 059 * <pre>{... 060 * Date: "Sun, 26 May 2002 18:06:04 GMT", 061 * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s", 062 * "Cache-Control": "no-cache", 063 * ...}</pre> 064 * It does no further checking or conversion. It does not parse dates. 065 * It does not do '%' transforms on URLs. 066 * @param string An HTTP header string. 067 * @return A JSONObject containing the elements and attributes 068 * of the XML string. 069 * @throws JSONException 070 */ 071 public static JSONObject toJSONObject(String string) throws JSONException { 072 JSONObject jo = new JSONObject(); 073 HTTPTokener x = new HTTPTokener(string); 074 String token; 075 076 token = x.nextToken(); 077 if (token.toUpperCase().startsWith("HTTP")) { 078 079// Response 080 081 jo.put("HTTP-Version", token); 082 jo.put("Status-Code", x.nextToken()); 083 jo.put("Reason-Phrase", x.nextTo('\0')); 084 x.next(); 085 086 } else { 087 088// Request 089 090 jo.put("Method", token); 091 jo.put("Request-URI", x.nextToken()); 092 jo.put("HTTP-Version", x.nextToken()); 093 } 094 095// Fields 096 097 while (x.more()) { 098 String name = x.nextTo(':'); 099 x.next(':'); 100 jo.put(name, x.nextTo('\0')); 101 x.next(); 102 } 103 return jo; 104 } 105 106 107 /** 108 * Convert a JSONObject into an HTTP header. A request header must contain 109 * <pre>{ 110 * Method: "POST" (for example), 111 * "Request-URI": "/" (for example), 112 * "HTTP-Version": "HTTP/1.1" (for example) 113 * }</pre> 114 * A response header must contain 115 * <pre>{ 116 * "HTTP-Version": "HTTP/1.1" (for example), 117 * "Status-Code": "200" (for example), 118 * "Reason-Phrase": "OK" (for example) 119 * }</pre> 120 * Any other members of the JSONObject will be output as HTTP fields. 121 * The result will end with two CRLF pairs. 122 * @param jo A JSONObject 123 * @return An HTTP header string. 124 * @throws JSONException if the object does not contain enough 125 * information. 126 */ 127 public static String toString(JSONObject jo) throws JSONException { 128 Iterator<String> keys = jo.keys(); 129 String string; 130 StringBuilder sb = new StringBuilder(); 131 if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { 132 sb.append(jo.getString("HTTP-Version")); 133 sb.append(' '); 134 sb.append(jo.getString("Status-Code")); 135 sb.append(' '); 136 sb.append(jo.getString("Reason-Phrase")); 137 } else if (jo.has("Method") && jo.has("Request-URI")) { 138 sb.append(jo.getString("Method")); 139 sb.append(' '); 140 sb.append('"'); 141 sb.append(jo.getString("Request-URI")); 142 sb.append('"'); 143 sb.append(' '); 144 sb.append(jo.getString("HTTP-Version")); 145 } else { 146 throw new JSONException("Not enough material for an HTTP header."); 147 } 148 sb.append(CRLF); 149 while (keys.hasNext()) { 150 string = keys.next(); 151 if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && 152 !"Reason-Phrase".equals(string) && !"Method".equals(string) && 153 !"Request-URI".equals(string) && !jo.isNull(string)) { 154 sb.append(string); 155 sb.append(": "); 156 sb.append(jo.getString(string)); 157 sb.append(CRLF); 158 } 159 } 160 sb.append(CRLF); 161 return sb.toString(); 162 } 163}