Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HTTP.java 5.5KB

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