Java IRC bot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Cookie.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /**
  22. * Convert a web browser cookie specification to a JSONObject and back.
  23. * JSON and Cookies are both notations for name/value pairs.
  24. * @author JSON.org
  25. * @version 2008-09-18
  26. */
  27. public class Cookie {
  28. /**
  29. * Produce a copy of a string in which the characters '+', '%', '=', ';'
  30. * and control characters are replaced with "%hh". This is a gentle form
  31. * of URL encoding, attempting to cause as little distortion to the
  32. * string as possible. The characters '=' and ';' are meta characters in
  33. * cookies. By convention, they are escaped using the URL-encoding. This is
  34. * only a convention, not a standard. Often, cookies are expected to have
  35. * encoded values. We encode '=' and ';' because we must. We encode '%' and
  36. * '+' because they are meta characters in URL encoding.
  37. * @param string The source string.
  38. * @return The escaped result.
  39. */
  40. public static String escape(String string) {
  41. char c;
  42. String s = string.trim();
  43. StringBuffer sb = new StringBuffer();
  44. int len = s.length();
  45. for (int i = 0; i < len; i += 1) {
  46. c = s.charAt(i);
  47. if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
  48. sb.append('%');
  49. sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
  50. sb.append(Character.forDigit((char)(c & 0x0f), 16));
  51. } else {
  52. sb.append(c);
  53. }
  54. }
  55. return sb.toString();
  56. }
  57. /**
  58. * Convert a cookie specification string into a JSONObject. The string
  59. * will contain a name value pair separated by '='. The name and the value
  60. * will be unescaped, possibly converting '+' and '%' sequences. The
  61. * cookie properties may follow, separated by ';', also represented as
  62. * name=value (except the secure property, which does not have a value).
  63. * The name will be stored under the key "name", and the value will be
  64. * stored under the key "value". This method does not do checking or
  65. * validation of the parameters. It only converts the cookie string into
  66. * a JSONObject.
  67. * @param string The cookie specification string.
  68. * @return A JSONObject containing "name", "value", and possibly other
  69. * members.
  70. * @throws JSONException
  71. */
  72. public static JSONObject toJSONObject(String string) throws JSONException {
  73. String n;
  74. JSONObject o = new JSONObject();
  75. Object v;
  76. JSONTokener x = new JSONTokener(string);
  77. o.put("name", x.nextTo('='));
  78. x.next('=');
  79. o.put("value", x.nextTo(';'));
  80. x.next();
  81. while (x.more()) {
  82. n = unescape(x.nextTo("=;"));
  83. if (x.next() != '=') {
  84. if (n.equals("secure")) {
  85. v = Boolean.TRUE;
  86. } else {
  87. throw x.syntaxError("Missing '=' in cookie parameter.");
  88. }
  89. } else {
  90. v = unescape(x.nextTo(';'));
  91. x.next();
  92. }
  93. o.put(n, v);
  94. }
  95. return o;
  96. }
  97. /**
  98. * Convert a JSONObject into a cookie specification string. The JSONObject
  99. * must contain "name" and "value" members.
  100. * If the JSONObject contains "expires", "domain", "path", or "secure"
  101. * members, they will be appended to the cookie specification string.
  102. * All other members are ignored.
  103. * @param o A JSONObject
  104. * @return A cookie specification string
  105. * @throws JSONException
  106. */
  107. public static String toString(JSONObject o) throws JSONException {
  108. StringBuffer sb = new StringBuffer();
  109. sb.append(escape(o.getString("name")));
  110. sb.append("=");
  111. sb.append(escape(o.getString("value")));
  112. if (o.has("expires")) {
  113. sb.append(";expires=");
  114. sb.append(o.getString("expires"));
  115. }
  116. if (o.has("domain")) {
  117. sb.append(";domain=");
  118. sb.append(escape(o.getString("domain")));
  119. }
  120. if (o.has("path")) {
  121. sb.append(";path=");
  122. sb.append(escape(o.getString("path")));
  123. }
  124. if (o.optBoolean("secure")) {
  125. sb.append(";secure");
  126. }
  127. return sb.toString();
  128. }
  129. /**
  130. * Convert <code>%</code><i>hh</i> sequences to single characters, and
  131. * convert plus to space.
  132. * @param s A string that may contain
  133. * <code>+</code>&nbsp;<small>(plus)</small> and
  134. * <code>%</code><i>hh</i> sequences.
  135. * @return The unescaped string.
  136. */
  137. public static String unescape(String s) {
  138. int len = s.length();
  139. StringBuffer b = new StringBuffer();
  140. for (int i = 0; i < len; ++i) {
  141. char c = s.charAt(i);
  142. if (c == '+') {
  143. c = ' ';
  144. } else if (c == '%' && i + 2 < len) {
  145. int d = JSONTokener.dehexchar(s.charAt(i + 1));
  146. int e = JSONTokener.dehexchar(s.charAt(i + 2));
  147. if (d >= 0 && e >= 0) {
  148. c = (char)(d * 16 + e);
  149. i += 2;
  150. }
  151. }
  152. b.append(c);
  153. }
  154. return b.toString();
  155. }
  156. }