001: /*
002: * Cookie.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: Cookie.java,v 1.1.1.1 2002/09/24 16:09:17 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.Vector;
025: import java.net.URL;
026:
027: public final class Cookie {
028: private static Vector cookies;
029:
030: private String name;
031: private String value;
032: private String domain;
033: private String path;
034: private String expires;
035: private boolean secure;
036:
037: private Cookie() {
038: }
039:
040: public static void setCookie(URL url, String s) {
041: Cookie cookie = new Cookie();
042: String remaining = s.trim();
043: while (remaining.length() > 0) {
044: int index = remaining.indexOf('=');
045: if (index < 0) {
046: cookie.set(remaining, null);
047: break;
048: }
049: String key = remaining.substring(0, index);
050: remaining = remaining.substring(index + 1);
051: index = remaining.indexOf(';');
052: String value;
053: if (index < 0) {
054: value = remaining;
055: cookie.set(key, value);
056: break;
057: } else {
058: value = remaining.substring(0, index);
059: cookie.set(key, value);
060: remaining = remaining.substring(index + 1).trim();
061: }
062: }
063: if (cookie.domain == null)
064: cookie.domain = url.getHost();
065: if (cookie.path == null) {
066: // URL.getPath() is only available in Java 1.3!
067: String file = url.getFile();
068: int index = file.lastIndexOf('?');
069: if (index >= 0)
070: cookie.path = file.substring(0, index);
071: else
072: cookie.path = file;
073: }
074: if (cookie.isValid())
075: addCookie(cookie);
076: }
077:
078: // BUG! Cookies with more specific path mappings should be sent first.
079: public static String getCookie(URL url) {
080: if (cookies == null)
081: return null;
082: String host = url.getHost();
083: // URL.getPath() is only available in Java 1.3!
084: String file = url.getFile();
085: int index = file.lastIndexOf('?');
086: String path = index >= 0 ? file.substring(0, index) : file;
087: FastStringBuffer sb = new FastStringBuffer(256);
088: for (int i = cookies.size() - 1; i >= 0; i--) {
089: Cookie cookie = (Cookie) cookies.get(i);
090: if (cookie.domain != null && host.endsWith(cookie.domain)) {
091: if (cookie.path != null && path.startsWith(cookie.path)) {
092: if (sb.length() > 0)
093: sb.append("; ");
094: sb.append(cookie.name);
095: sb.append('=');
096: sb.append(cookie.value);
097: }
098: }
099: }
100: if (sb.length() == 0)
101: return null;
102: return sb.toString();
103: }
104:
105: public static void deleteCookies() {
106: cookies = null;
107: }
108:
109: private static void addCookie(Cookie cookie) {
110: if (cookies != null) {
111: for (int i = cookies.size() - 1; i >= 0; i--) {
112: Cookie c = (Cookie) cookies.get(i);
113: if (c.domain.equals(cookie.domain)
114: && c.path.equals(cookie.path)
115: && c.name.equals(cookie.name)) {
116: // BUG! Should delete cookie here if new cookie's
117: // expiration time is in the past.
118: c.value = cookie.value;
119: return;
120: }
121: }
122: } else
123: cookies = new Vector();
124: cookies.add(cookie);
125: }
126:
127: private void set(String key, String s) {
128: if (key.equals("domain"))
129: domain = s;
130: else if (key.equals("path"))
131: path = s;
132: else if (key.equals("expires"))
133: expires = s;
134: else if (key.equals("secure"))
135: secure = true;
136: else {
137: name = key;
138: value = s;
139: }
140: }
141:
142: private boolean isValid() {
143: if (name != null && name.length() > 0)
144: if (value != null && value.length() > 0)
145: return true;
146: return false;
147: }
148: }
|