You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by bc...@locus.apache.org on 2000/12/21 05:22:09 UTC
cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/lib Cookie.java HeaderElement.java
bcholmes 00/12/20 20:22:09
Modified: src/webdav/client/src/org/apache/webdav/lib Cookie.java
HeaderElement.java
Log:
Minor revisions to Cookie and HeaderElement
- fixed minor bug in HeaderElement for quoted cookies
Revision Changes Path
1.2 +34 -54 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/Cookie.java
Index: Cookie.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/Cookie.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Cookie.java 2000/11/22 06:19:08 1.1
+++ Cookie.java 2000/12/21 04:22:08 1.2
@@ -1,10 +1,13 @@
/*
+ * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/Cookie.java,v 1.2 2000/12/21 04:22:08 bcholmes Exp $
+ * $Revision: 1.2 $
+ * $Date: 2000/12/21 04:22:08 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -12,7 +15,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -20,15 +23,15 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
- * from this software without prior written permission. For written
+ * from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
@@ -56,7 +59,7 @@
*
* [Additional notices, if required by prior licensing conditions]
*
- */
+ */
package org.apache.webdav.lib;
import java.io.Serializable;
@@ -67,7 +70,7 @@
/**
* This class represents an http cookie as specified in RFC 2109.
- *
+ *
*
* @author B.C. Holmes
*/
@@ -81,7 +84,7 @@
protected Date m_expiryDate;
protected String m_path;
protected boolean m_secure;
-
+
// FIXME: JServ doesn't appear to explicitly set the version
protected int m_version = 1;
@@ -114,21 +117,21 @@
* null if no such comment has been defined.
*
* @see setComment
- */
+ */
public String getComment() {
return m_comment;
}
/**
* If a user agent (web browser) presents this cookie to a user, the
- * cookie's purpose will be described using this comment.
+ * cookie's purpose will be described using this comment.
*
* @see getComment
*/
public void setComment(String comment) {
m_comment = comment;
}
-
+
/**
* @return the expiry date of this cookie, or null if none set.
*/
@@ -154,7 +157,7 @@
* Returns the domain of this cookie.
*
* @see setDomain
- */
+ */
public String getDomain() {
return m_domain;
}
@@ -181,11 +184,11 @@
return m_path;
}
- /**
- * This cookie should be presented only with requests beginning with this URL.
- * Read RFC 2109 for a specification of the default behaviour. Basically, URLs
- * in the same "directory" as the one which set the cookie, and in subdirectories,
- * can all see the cookie unless a different path is set.
+ /**
+ * This cookie should be presented only with requests beginning with this URL.
+ * Read RFC 2109 for a specification of the default behaviour. Basically, URLs
+ * in the same "directory" as the one which set the cookie, and in subdirectories,
+ * can all see the cookie unless a different path is set.
*/
public void setPath(String path) {
m_path = path;
@@ -209,16 +212,16 @@
public void setSecure (boolean secure) {
m_secure = secure;
}
+
-
public int getVersion() {
return m_version;
}
-
+
public void setVersion(int version) {
m_version = version;
}
-
+
/**
* @return true if this cookie has expired
*/
@@ -261,9 +264,9 @@
return string;
}
-
- public static Header createCookieHeader(String domain,
- String path, Vector cookies) {
+
+ public static Header createCookieHeader(String domain,
+ String path, Vector cookies) {
domain = domain.toLowerCase();
StringBuffer value = new StringBuffer("$Version=1");
// FIXME: cookies are supposed to be ordered with "better"
@@ -279,7 +282,7 @@
}
return new Header("cookie", value.toString());
}
-
+
public String toString() {
String string = toExternalForm();
if (m_secure) {
@@ -287,12 +290,12 @@
}
return string;
}
-
+
/**
* Parses the Set-Cookie header into an array of Cookies.
*
* <P>The syntax for the Set-Cookie response header is:
- *
+ *
* <PRE>
* set-cookie = "Set-Cookie:" cookies
* cookies = 1#cookie
@@ -312,10 +315,10 @@
* @return an array of Cookies as parsed from the Set-Cookie header
* @exception WebdavException if an error occurs during parsing
*/
- protected static Cookie[] parse(String domain, Header setCookie)
+ public static Cookie[] parse(String domain, Header setCookie)
throws WebdavException {
- HeaderElement[] headerElements =
+ HeaderElement[] headerElements =
HeaderElement.parse(setCookie.getValue());
Cookie[] cookies = new Cookie[headerElements.length];
@@ -343,7 +346,7 @@
name.equals("comment")) &&
parameters[j].getValue() == null) {
throw new WebdavException(
- "Bad Set-Cookie header: " + setCookie.getValue() +
+ "Bad Set-Cookie header: " + setCookie.getValue() +
"\nMissing value for " +
parameters[j].getName() +
" attribute in cookie '" +
@@ -401,9 +404,9 @@
// domain must be either .local or must contain at least two dots
if (!cookie.getDomain().equals("localhost")) {
-
+
// must have at least two dots
- if ((cookie.getDomain().indexOf('.') ==
+ if ((cookie.getDomain().indexOf('.') ==
cookie.getDomain().lastIndexOf('.'))) {
throw new WebdavException(
"Bad Set-Cookie header: " + setCookie.getValue() +
@@ -432,29 +435,6 @@
}
return cookies;
- }
-
- public static void main(String[] args) {
- // let's test this class
- try {
- String headerValue = "custno = 12345; comment=test; version=1," +
- " name=John; version=1; max-age=600; secure; domain=.apache.org";
- Cookie[] cookies = Cookie.parse("www.apache.org", new Header("set-cookie", headerValue));
- for (int i = 0; i < cookies.length; i++) {
- System.out.println("name =>" + cookies[i].getName());
- System.out.println("value=>" + cookies[i].getValue());
- System.out.println("domain =>" + cookies[i].getDomain());
- System.out.println("secure =>" + cookies[i].getSecure());
- System.out.println("version=>" + cookies[i].getVersion());
- System.out.println("comment=>" + cookies[i].getComment());
- if (cookies[i].isToBeDiscarded()) {
- System.out.println("expires =>" + cookies[i].getExpiryDate());
- System.out.println("expired =>" + cookies[i].isExpired());
- }
- }
- } catch (Exception exception) {
- System.out.println(exception);
- }
}
}
1.2 +33 -28 jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/HeaderElement.java
Index: HeaderElement.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/HeaderElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HeaderElement.java 2000/11/22 06:19:08 1.1
+++ HeaderElement.java 2000/12/21 04:22:09 1.2
@@ -1,4 +1,7 @@
/*
+ * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/HeaderElement.java,v 1.2 2000/12/21 04:22:09 bcholmes Exp $
+ * $Revision: 1.2 $
+ * $Date: 2000/12/21 04:22:09 $
*
* ====================================================================
*
@@ -66,8 +69,8 @@
import java.util.Vector;
/**
- * Some HTTP headers (such as the set-cookie header) have values that
- * can be decomposed into multiple elements. Such headers must be in the
+ * Some HTTP headers (such as the set-cookie header) have values that
+ * can be decomposed into multiple elements. Such headers must be in the
* following form:
*
* <PRE>
@@ -85,9 +88,9 @@
* quoted-char = "\" char
* </PRE>
*
- * <P> Any amount of white space is allowed between any part of the
- * header, element or param and is ignored. A missing value in any
- * element or param will be stored as the empty string; if the "="
+ * <P> Any amount of white space is allowed between any part of the
+ * header, element or param and is ignored. A missing value in any
+ * element or param will be stored as the empty string; if the "="
* is also missing <var>null</var> will be stored instead.
*
* <P> This class represents an individual header element. This
@@ -115,16 +118,16 @@
public HeaderElement(String name, String value) {
super(name, value);
}
-
+
/**
* Constructor.
*/
- public HeaderElement(String name, String value,
+ public HeaderElement(String name, String value,
NameValuePair[] parameters) {
super(name, value);
this.parameters = parameters;
}
-
+
// -------------------------------------------------------- Class Variables
private static final BitSet SEPARATORS = new BitSet(128);
@@ -193,14 +196,14 @@
public NameValuePair[] getParameters() {
return this.parameters;
}
-
+
// --------------------------------------------------------- Public Methods
/**
* This parses the value part of a header. The result is an array of
* HeaderElement objects.
*
- * @param headerValue the string representation of the header value
+ * @param headerValue the string representation of the header value
* (as received from the web server).
* @return a Vector containing <var>Header</var> elements.
* @exception WebdavException if the above syntax rules are violated.
@@ -212,13 +215,13 @@
return null;
Vector elements = new Vector();
- StringTokenizer tokenizer =
+ StringTokenizer tokenizer =
new StringTokenizer(headerValue.trim(), ",");
-
+
while (tokenizer.countTokens() > 0) {
String nextToken = tokenizer.nextToken();
-
- // careful... there may have been a comma in a quoted string
+
+ // careful... there may have been a comma in a quoted string
try {
while (HeaderElement.hasOddNumberOfQuotationMarks(
nextToken)) {
@@ -228,7 +231,7 @@
throw new WebdavException(
"Bad header format: wrong number of quotation marks");
}
-
+
String tmp = nextToken.trim();
if (!tmp.endsWith(";")) {
tmp += ";";
@@ -244,9 +247,9 @@
NameValuePair pair = parsePair(header, startPos, i);
if (pair == null) {
throw new WebdavException(
- "Bad header format: empty name/value pair in" +
+ "Bad header format: empty name/value pair in" +
nextToken);
-
+
// the first name/value pair are handled differently
} else if (startPos == 0) {
element.setName(pair.getName());
@@ -255,7 +258,7 @@
parameters.addElement(pair);
}
startPos = i + 1;
- } else if (header[i] == '"' &&
+ } else if (header[i] == '"' &&
!(inAString && i > 0 && header[i-1] == '\\')) {
inAString = !inAString;
}
@@ -268,7 +271,7 @@
element.parameters = tmp2;
parameters.clear();
}
-
+
// and save the header element into the list of header elements
elements.addElement(element);
}
@@ -284,26 +287,28 @@
while ((start = string.indexOf('"', start+1)) != -1) {
odd = !odd;
}
- return false;
+ return odd;
}
-
- private final static NameValuePair parsePair(char[] header, int start, int end)
+
+ private final static NameValuePair parsePair(
+ char[] header, int start, int end)
throws WebdavException {
+
boolean done = false;
NameValuePair pair = null;
String name = new String(header, start, end - start).trim();
String value = null;
- int index = name.indexOf("=");
+ int index = name.indexOf("=");
if (index >= 0) {
if ((index + 1) < name.length()) {
value = name.substring(index+1).trim();
-
+
// strip quotation marks
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1,value.length()-1);
}
-
+
// is there anything left?
if (value.length() == 0) {
value = null;
@@ -311,18 +316,18 @@
}
name = name.substring(0,index).trim();
}
-
+
if (name != null && name.length() > 0) {
pair = new NameValuePair(name, value);
}
return pair;
}
-
+
public static void main(String[] args) {
// let's test this class
try {
- String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
+ String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
"name5=value5, name6= ; name7 = value7; name8 = \" name8\"";
HeaderElement[] elements = HeaderElement.parse(headerValue);
for (int i = 0; i < elements.length; i++) {