001: // HttpCookieList.java
002: // $Id: HttpCookieList.java,v 1.12 2007/03/20 15:29:47 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import java.util.Vector;
009:
010: public class HttpCookieList extends BasicValue {
011: Vector cookies = null;
012:
013: protected void updateByteValue() {
014: HttpBuffer buf = new HttpBuffer();
015: // Dump all cookies:
016: int sz = cookies.size();
017: for (int i = 0; i < sz; i++) {
018: HttpCookie c = (HttpCookie) cookies.elementAt(i);
019: if (i != 0) {
020: buf.append((byte) ';');
021: buf.append((byte) ' ');
022: }
023: // Dump the cookie values:
024: buf.append(c.getName(), (byte) '=', c.getValue());
025: }
026: raw = buf.getByteCopy();
027: roff = 0;
028: rlen = raw.length;
029: }
030:
031: protected void deprecatedUpdateByteValue() {
032: //deprecated because of the specification
033: HttpBuffer buf = new HttpBuffer();
034: // Dump all cookies:
035: int sz = cookies.size();
036: for (int i = 0; i < sz; i++) {
037: HttpCookie c = (HttpCookie) cookies.elementAt(i);
038: if (i == 0) {
039: // We use the first cookie version here:
040: buf.append("$Version", (byte) '=', c.getVersion());
041: buf.append((byte) ';');
042: } else {
043: buf.append((byte) ',');
044: buf.append((byte) ' ');
045: }
046: // Dump the cookie values:
047: buf.append(c.getName(), (byte) '=', c.getValue());
048: // buf.append((byte) ';');
049: String s = c.getPath();
050: if (s != null) {
051: buf.append((byte) ';');
052: buf.append("$Path", (byte) '=', s);
053: }
054: if ((s = c.getDomain()) != null) {
055: buf.append((byte) ';');
056: buf.append("$Domain", (byte) '=', s);
057: }
058: }
059: raw = buf.getByteCopy();
060: roff = 0;
061: rlen = raw.length;
062: }
063:
064: /**
065: * parse the Cookie Header according to the Netscape Specification:
066: * http://www.netscape.com/newsref/std/cookie_spec.html
067: * @exception HttpParserException if parsing failed.
068: */
069: protected void parse() throws HttpParserException {
070: ParseState cv = new ParseState(roff, rlen);
071: ParseState it = new ParseState(0, 0);
072: cv.separator = (byte) ';';
073: cv.spaceIsSep = false;
074: it.separator = (byte) '=';
075:
076: while (HttpParser.nextItem(raw, cv) >= 0) {
077: it.ioff = cv.start;
078: it.bufend = cv.end;
079: if (HttpParser.nextItem(raw, it) < 0)
080: error("Invalid item in cookie value.");
081: String item = it.toString(raw);
082: if (item.charAt(0) == '$')
083: continue;
084: HttpCookie c = new HttpCookie();
085: // Get the item's value:
086: it.prepare();
087: if (HttpParser.nextItem(raw, it) < 0) {
088: // if the cookie has no value, simply give it an empty
089: // value. The cookie spec does not say whether valueless
090: // cookies are not allowed and to simply set it to a blank
091: // string seems to be the most robust behavior because
092: // javascripting in browsers can set valueless cookies.
093: c.setValue("");
094: } else {
095: // if the cookie has a value, it has only one, and the
096: // parser will cut to the next instance of the separator
097: // so either we crawl using a simple loop, or we set the
098: // end of the ParseState to the one previously computed (safer)
099: it.end = cv.end;
100: c.setValue(it.toString(raw));
101: }
102: c.setName(item);
103: cookies.addElement(c);
104: }
105: }
106:
107: /**
108: * parse.
109: * @exception HttpParserException if parsing failed.
110: */
111: protected void deprecatedParse() throws HttpParserException {
112: // Requires a small twist, but:
113: ParseState cv = new ParseState(roff, rlen);
114: ParseState it = new ParseState(0, 0);
115: ParseState val = new ParseState(0, 0);
116: cv.separator = (byte) ';';
117: cv.spaceIsSep = false;
118: it.separator = (byte) '=';
119: val.separator = (byte) ';';
120: val.spaceIsSep = false;
121:
122: // We will get only one cokoie for the time being:
123: HttpCookie c = new HttpCookie();
124: while (HttpParser.nextItem(raw, cv) >= 0) {
125: it.ioff = cv.start;
126: it.bufend = cv.end;
127: // HttpCookie c = new HttpCookie();
128: if (HttpParser.nextItem(raw, it) < 0)
129: error("Invalid item in cookie value.");
130: String item = it.toString(raw, true);
131: // Get the item's value:
132: it.prepare();
133: if (HttpParser.nextItem(raw, it) < 0)
134: error("Cookie item [" + item + "] has no value.");
135: if (item.equals("$path")) {
136: c.setPath(it.toString(raw));
137: } else if (item.equals("$domain")) {
138: c.setDomain(it.toString(raw));
139: } else if (item.equals("$version")) {
140: c.setVersion(Integer.parseInt(it.toString(raw)));
141: // c.setVersion(HttpParser.parseInt(raw, it));
142: } else {
143: if (c.getName() != null)
144: error("Invalid cookie item [" + item + "]");
145: c.setName(item);
146: val.ioff = it.start;
147: val.bufend = cv.end;
148: HttpParser.nextItem(raw, val);
149: c.setValue(it.toString(raw));
150: }
151: cv.prepare();
152: }
153: cookies.addElement(c);
154: }
155:
156: /**
157: * Get this HTTP value, parsed value.
158: */
159:
160: public Object getValue() {
161: return this ;
162: }
163:
164: /**
165: * Add a cookie to this header value.
166: * @param name The name of the cookie to add.
167: * @param value It's value.
168: * @return A HttpCookie instance, tha represents this cookie in the header
169: * value.
170: */
171:
172: public HttpCookie addCookie(String name, String value) {
173: validate();
174: HttpCookie c = new HttpCookie(true, name, value);
175: cookies.addElement(c);
176: return c;
177: }
178:
179: /**
180: * Remove a cookie from this header value.
181: * @param name The name of the cookie to remove.
182: * @return A boolean, <strong>true</strong> If the cookie was found, and
183: * removed, <strong>false</strong> otherwise.
184: */
185:
186: public boolean removeCookie(String name) {
187: validate();
188: int sz = cookies.size();
189: for (int i = 0; i < sz; i++) {
190: HttpCookie c = (HttpCookie) cookies.elementAt(i);
191: if (c.getName().equals(name)) {
192: cookies.removeElementAt(i);
193: return true;
194: }
195: }
196: return false;
197: }
198:
199: /**
200: * Lookup a cookie by name.
201: * @param name The name of the cooie to lookup.
202: * @return A HttpCookie instance, or <strong>null</strong> if not found.
203: */
204:
205: public HttpCookie getCookie(String name) {
206: validate();
207: int sz = cookies.size();
208: for (int i = 0; i < sz; i++) {
209: HttpCookie c = (HttpCookie) cookies.elementAt(i);
210: if (c.getName().equalsIgnoreCase(name))
211: return c;
212: }
213: return null;
214: }
215:
216: public HttpCookie[] getCookies() {
217: validate();
218: HttpCookie cooks[] = new HttpCookie[cookies.size()];
219: cookies.copyInto(cooks);
220: return cooks;
221: }
222:
223: HttpCookieList(HttpCookie c[]) {
224: this .isValid = true;
225: this .cookies = new Vector(8);
226: if (c != null) {
227: // FIXME Don't tell me this is broken, I *know* it
228: for (int i = 0; i < c.length; i++)
229: cookies.addElement(c[i]);
230: }
231: }
232:
233: public HttpCookieList() {
234: this .isValid = false;
235: this .cookies = new Vector(2);
236: }
237:
238: }
|