src/share/classes/java/net/HttpCookie.java

Print this page




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.util.List;
  29 import java.util.StringTokenizer;
  30 import java.util.NoSuchElementException;
  31 import java.text.SimpleDateFormat;
  32 import java.util.TimeZone;
  33 import java.util.Date;
  34 
  35 import java.lang.NullPointerException;  // for javadoc
  36 import java.util.Locale;

  37 
  38 /**
  39  * An HttpCookie object represents an http cookie, which carries state
  40  * information between server and user agent. Cookie is widely adopted
  41  * to create stateful sessions.
  42  *
  43  * <p>There are 3 http cookie specifications:
  44  * <blockquote>
  45  *   Netscape draft<br>
  46  *   RFC 2109 - <a href="http://www.ietf.org/rfc/rfc2109.txt">
  47  * <i>http://www.ietf.org/rfc/rfc2109.txt</i></a><br>
  48  *   RFC 2965 - <a href="http://www.ietf.org/rfc/rfc2965.txt">
  49  * <i>http://www.ietf.org/rfc/rfc2965.txt</i></a>
  50  * </blockquote>
  51  *
  52  * <p>HttpCookie class can accept all these 3 forms of syntax.
  53  *
  54  * @author Edward Wang
  55  * @since 1.6
  56  */


 800      * have same name (case-insensitive),
 801      * and have same path (case-sensitive).
 802      *
 803      * @return          <tt>true</tt> if 2 http cookies equal to each other;
 804      *                  otherwise, <tt>false</tt>
 805      */
 806     @Override
 807     public boolean equals(Object obj) {
 808         if (obj == this)
 809             return true;
 810         if (!(obj instanceof HttpCookie))
 811             return false;
 812         HttpCookie other = (HttpCookie)obj;
 813 
 814         // One http cookie equals to another cookie (RFC 2965 sec. 3.3.3) if:
 815         //   1. they come from same domain (case-insensitive),
 816         //   2. have same name (case-insensitive),
 817         //   3. and have same path (case-sensitive).
 818         return equalsIgnoreCase(getName(), other.getName()) &&
 819                equalsIgnoreCase(getDomain(), other.getDomain()) &&
 820                equals(getPath(), other.getPath());
 821     }
 822 
 823 
 824     /**
 825      * Return hash code of this http cookie. The result is the sum of
 826      * hash code value of three significant components of this cookie:
 827      * name, domain, and path.
 828      * That is, the hash code is the value of the expression:
 829      * <blockquote>
 830      * getName().toLowerCase().hashCode()<br>
 831      * + getDomain().toLowerCase().hashCode()<br>
 832      * + getPath().hashCode()
 833      * </blockquote>
 834      *
 835      * @return          this http cookie's hash code
 836      */
 837     @Override
 838     public int hashCode() {
 839         int h1 = name.toLowerCase().hashCode();
 840         int h2 = (domain!=null) ? domain.toLowerCase().hashCode() : 0;


1145     private static String stripOffSurroundingQuote(String str) {
1146         if (str != null && str.length() > 2 &&
1147             str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
1148             return str.substring(1, str.length() - 1);
1149         }
1150         if (str != null && str.length() > 2 &&
1151             str.charAt(0) == '\'' && str.charAt(str.length() - 1) == '\'') {
1152             return str.substring(1, str.length() - 1);
1153         }
1154         return str;
1155     }
1156 
1157     private static boolean equalsIgnoreCase(String s, String t) {
1158         if (s == t) return true;
1159         if ((s != null) && (t != null)) {
1160             return s.equalsIgnoreCase(t);
1161         }
1162         return false;
1163     }
1164 
1165     private static boolean equals(String s, String t) {
1166         if (s == t) return true;
1167         if ((s != null) && (t != null)) {
1168             return s.equals(t);
1169         }
1170         return false;
1171     }
1172 
1173     private static boolean startsWithIgnoreCase(String s, String start) {
1174         if (s == null || start == null) return false;
1175 
1176         if (s.length() >= start.length() &&
1177                 start.equalsIgnoreCase(s.substring(0, start.length()))) {
1178             return true;
1179         }
1180 
1181         return false;
1182     }
1183 
1184     /*
1185      * Split cookie header string according to rfc 2965:
1186      *   1) split where it is a comma;
1187      *   2) but not the comma surrounding by double-quotes, which is the comma
1188      *      inside port list or embeded URIs.
1189      *
1190      * @param header            the cookie header string to split
1191      *
1192      * @return                  list of strings; never null




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.util.List;
  29 import java.util.StringTokenizer;
  30 import java.util.NoSuchElementException;
  31 import java.text.SimpleDateFormat;
  32 import java.util.TimeZone;
  33 import java.util.Date;
  34 
  35 import java.lang.NullPointerException;  // for javadoc
  36 import java.util.Locale;
  37 import java.util.Objects;
  38 
  39 /**
  40  * An HttpCookie object represents an http cookie, which carries state
  41  * information between server and user agent. Cookie is widely adopted
  42  * to create stateful sessions.
  43  *
  44  * <p>There are 3 http cookie specifications:
  45  * <blockquote>
  46  *   Netscape draft<br>
  47  *   RFC 2109 - <a href="http://www.ietf.org/rfc/rfc2109.txt">
  48  * <i>http://www.ietf.org/rfc/rfc2109.txt</i></a><br>
  49  *   RFC 2965 - <a href="http://www.ietf.org/rfc/rfc2965.txt">
  50  * <i>http://www.ietf.org/rfc/rfc2965.txt</i></a>
  51  * </blockquote>
  52  *
  53  * <p>HttpCookie class can accept all these 3 forms of syntax.
  54  *
  55  * @author Edward Wang
  56  * @since 1.6
  57  */


 801      * have same name (case-insensitive),
 802      * and have same path (case-sensitive).
 803      *
 804      * @return          <tt>true</tt> if 2 http cookies equal to each other;
 805      *                  otherwise, <tt>false</tt>
 806      */
 807     @Override
 808     public boolean equals(Object obj) {
 809         if (obj == this)
 810             return true;
 811         if (!(obj instanceof HttpCookie))
 812             return false;
 813         HttpCookie other = (HttpCookie)obj;
 814 
 815         // One http cookie equals to another cookie (RFC 2965 sec. 3.3.3) if:
 816         //   1. they come from same domain (case-insensitive),
 817         //   2. have same name (case-insensitive),
 818         //   3. and have same path (case-sensitive).
 819         return equalsIgnoreCase(getName(), other.getName()) &&
 820                equalsIgnoreCase(getDomain(), other.getDomain()) &&
 821                Objects.equals(getPath(), other.getPath());
 822     }
 823 
 824 
 825     /**
 826      * Return hash code of this http cookie. The result is the sum of
 827      * hash code value of three significant components of this cookie:
 828      * name, domain, and path.
 829      * That is, the hash code is the value of the expression:
 830      * <blockquote>
 831      * getName().toLowerCase().hashCode()<br>
 832      * + getDomain().toLowerCase().hashCode()<br>
 833      * + getPath().hashCode()
 834      * </blockquote>
 835      *
 836      * @return          this http cookie's hash code
 837      */
 838     @Override
 839     public int hashCode() {
 840         int h1 = name.toLowerCase().hashCode();
 841         int h2 = (domain!=null) ? domain.toLowerCase().hashCode() : 0;


1146     private static String stripOffSurroundingQuote(String str) {
1147         if (str != null && str.length() > 2 &&
1148             str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
1149             return str.substring(1, str.length() - 1);
1150         }
1151         if (str != null && str.length() > 2 &&
1152             str.charAt(0) == '\'' && str.charAt(str.length() - 1) == '\'') {
1153             return str.substring(1, str.length() - 1);
1154         }
1155         return str;
1156     }
1157 
1158     private static boolean equalsIgnoreCase(String s, String t) {
1159         if (s == t) return true;
1160         if ((s != null) && (t != null)) {
1161             return s.equalsIgnoreCase(t);
1162         }
1163         return false;
1164     }
1165 








1166     private static boolean startsWithIgnoreCase(String s, String start) {
1167         if (s == null || start == null) return false;
1168 
1169         if (s.length() >= start.length() &&
1170                 start.equalsIgnoreCase(s.substring(0, start.length()))) {
1171             return true;
1172         }
1173 
1174         return false;
1175     }
1176 
1177     /*
1178      * Split cookie header string according to rfc 2965:
1179      *   1) split where it is a comma;
1180      *   2) but not the comma surrounding by double-quotes, which is the comma
1181      *      inside port list or embeded URIs.
1182      *
1183      * @param header            the cookie header string to split
1184      *
1185      * @return                  list of strings; never null