--- /dev/null
+// HttpCookieList.java\r
+// $Id: HttpCookieList.java,v 1.1 2010/06/15 12:19:56 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.www.http;\r
+\r
+import java.util.Vector;\r
+\r
+public class HttpCookieList extends BasicValue {\r
+ Vector cookies = null;\r
+\r
+ protected void updateByteValue() {\r
+ HttpBuffer buf = new HttpBuffer();\r
+ // Dump all cookies:\r
+ int sz = cookies.size();\r
+ for (int i = 0 ; i < sz ; i++) {\r
+ HttpCookie c = (HttpCookie) cookies.elementAt(i);\r
+ if (i != 0) {\r
+ buf.append((byte) ';');\r
+ buf.append((byte) ' ');\r
+ }\r
+ // Dump the cookie values:\r
+ buf.append(c.getName(), (byte) '=', c.getValue());\r
+ }\r
+ raw = buf.getByteCopy();\r
+ roff = 0;\r
+ rlen = raw.length;\r
+ }\r
+\r
+ protected void deprecatedUpdateByteValue() {\r
+ //deprecated because of the specification\r
+ HttpBuffer buf = new HttpBuffer();\r
+ // Dump all cookies:\r
+ int sz = cookies.size();\r
+ for (int i = 0 ; i < sz ; i++) {\r
+ HttpCookie c = (HttpCookie) cookies.elementAt(i);\r
+ if ( i == 0 ) {\r
+ // We use the first cookie version here:\r
+ buf.append("$Version", (byte) '=', c.getVersion());\r
+ buf.append((byte) ';');\r
+ } else {\r
+ buf.append((byte) ',');\r
+ buf.append((byte) ' ');\r
+ }\r
+ // Dump the cookie values:\r
+ buf.append(c.getName(), (byte) '=', c.getValue());\r
+ // buf.append((byte) ';');\r
+ String s = c.getPath();\r
+ if ( s != null ) {\r
+ buf.append((byte) ';');\r
+ buf.append("$Path", (byte) '=', s);\r
+ }\r
+ if ((s = c.getDomain()) != null) {\r
+ buf.append((byte) ';');\r
+ buf.append("$Domain", (byte) '=', s);\r
+ }\r
+ }\r
+ raw = buf.getByteCopy();\r
+ roff = 0;\r
+ rlen = raw.length;\r
+ }\r
+\r
+ /**\r
+ * parse the Cookie Header according to the Netscape Specification:\r
+ * http://www.netscape.com/newsref/std/cookie_spec.html\r
+ * @exception HttpParserException if parsing failed.\r
+ */\r
+ protected void parse() \r
+ throws HttpParserException\r
+ {\r
+ ParseState cv = new ParseState(roff, rlen);\r
+ ParseState it = new ParseState(0, 0);\r
+ cv.separator = (byte) ';';\r
+ cv.spaceIsSep = false;\r
+ it.separator = (byte) '=';\r
+\r
+ while ( HttpParser.nextItem(raw, cv) >= 0 ) {\r
+ it.ioff = cv.start;\r
+ it.bufend = cv.end;\r
+ if ( HttpParser.nextItem(raw, it) < 0 )\r
+ error("Invalid item in cookie value.");\r
+ String item = it.toString(raw);\r
+ if (item.charAt(0) == '$')\r
+ continue;\r
+ HttpCookie c = new HttpCookie();\r
+ // Get the item's value:\r
+ it.prepare();\r
+ if ( HttpParser.nextItem(raw, it) < 0 ) {\r
+ // if the cookie has no value, simply give it an empty\r
+ // value. The cookie spec does not say whether valueless\r
+ // cookies are not allowed and to simply set it to a blank\r
+ // string seems to be the most robust behavior because\r
+ // javascripting in browsers can set valueless cookies.\r
+ c.setValue("");\r
+ } else {\r
+ // if the cookie has a value, it has only one, and the\r
+ // parser will cut to the next instance of the separator\r
+ // so either we crawl using a simple loop, or we set the\r
+ // end of the ParseState to the one previously computed (safer)\r
+ it.end = cv.end;\r
+ c.setValue(it.toString(raw));\r
+ }\r
+ c.setName(item);\r
+ cookies.addElement(c);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * parse.\r
+ * @exception HttpParserException if parsing failed.\r
+ */\r
+ protected void deprecatedParse()\r
+ throws HttpParserException\r
+ {\r
+ // Requires a small twist, but:\r
+ ParseState cv = new ParseState(roff, rlen);\r
+ ParseState it = new ParseState(0, 0);\r
+ ParseState val = new ParseState(0, 0);\r
+ cv.separator = (byte) ';';\r
+ cv.spaceIsSep = false;\r
+ it.separator = (byte) '=';\r
+ val.separator = (byte) ';';\r
+ val.spaceIsSep = false;\r
+\r
+ // We will get only one cokoie for the time being:\r
+ HttpCookie c = new HttpCookie();\r
+ while ( HttpParser.nextItem(raw, cv) >= 0 ) {\r
+ it.ioff = cv.start;\r
+ it.bufend = cv.end;\r
+ // HttpCookie c = new HttpCookie();\r
+ if ( HttpParser.nextItem(raw, it) < 0 )\r
+ error("Invalid item in cookie value.");\r
+ String item = it.toString(raw, true);\r
+ // Get the item's value:\r
+ it.prepare();\r
+ if ( HttpParser.nextItem(raw, it) < 0 )\r
+ error("Cookie item ["+item+"] has no value.");\r
+ if ( item.equals("$path") ) {\r
+ c.setPath(it.toString(raw));\r
+ } else if ( item.equals("$domain") ) {\r
+ c.setDomain(it.toString(raw));\r
+ } else if ( item.equals("$version") ) {\r
+ c.setVersion(Integer.parseInt(it.toString(raw)));\r
+ // c.setVersion(HttpParser.parseInt(raw, it));\r
+ } else {\r
+ if ( c.getName() != null )\r
+ error("Invalid cookie item ["+item+"]");\r
+ c.setName(item);\r
+ val.ioff = it.start;\r
+ val.bufend = cv.end;\r
+ HttpParser.nextItem(raw, val);\r
+ c.setValue(it.toString(raw));\r
+ }\r
+ cv.prepare();\r
+ }\r
+ cookies.addElement(c);\r
+ }\r
+\r
+ /**\r
+ * Get this HTTP value, parsed value.\r
+ */\r
+\r
+ public Object getValue() {\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Add a cookie to this header value.\r
+ * @param name The name of the cookie to add.\r
+ * @param value It's value.\r
+ * @return A HttpCookie instance, tha represents this cookie in the header\r
+ * value.\r
+ */\r
+\r
+ public HttpCookie addCookie(String name, String value) {\r
+ validate();\r
+ HttpCookie c = new HttpCookie(true, name, value);\r
+ cookies.addElement(c);\r
+ return c;\r
+ }\r
+\r
+ /**\r
+ * Remove a cookie from this header value.\r
+ * @param name The name of the cookie to remove.\r
+ * @return A boolean, <strong>true</strong> If the cookie was found, and\r
+ * removed, <strong>false</strong> otherwise.\r
+ */\r
+\r
+ public boolean removeCookie(String name) {\r
+ validate();\r
+ int sz = cookies.size();\r
+ for (int i = 0 ; i < sz ; i++) {\r
+ HttpCookie c = (HttpCookie) cookies.elementAt(i);\r
+ if ( c.getName().equals(name) ) {\r
+ cookies.removeElementAt(i);\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Lookup a cookie by name.\r
+ * @param name The name of the cooie to lookup.\r
+ * @return A HttpCookie instance, or <strong>null</strong> if not found.\r
+ */\r
+\r
+ public HttpCookie getCookie(String name) {\r
+ validate();\r
+ int sz = cookies.size();\r
+ for (int i = 0 ; i < sz ; i++) {\r
+ HttpCookie c = (HttpCookie) cookies.elementAt(i);\r
+ if ( c.getName().equalsIgnoreCase(name) )\r
+ return c;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public HttpCookie[] getCookies() {\r
+ validate();\r
+ HttpCookie cooks [] = new HttpCookie[cookies.size()];\r
+ cookies.copyInto(cooks);\r
+ return cooks;\r
+ }\r
+\r
+ HttpCookieList(HttpCookie c[]) {\r
+ this.isValid = true;\r
+ this.cookies = new Vector(8);\r
+ if ( c != null ) {\r
+ // FIXME Don't tell me this is broken, I *know* it\r
+ for (int i = 0 ; i < c.length ; i++)\r
+ cookies.addElement(c[i]);\r
+ }\r
+ }\r
+\r
+ public HttpCookieList() {\r
+ this.isValid = false;\r
+ this.cookies = new Vector(2);\r
+ }\r
+\r
+}\r