Index: java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v retrieving revision 1.7 diff -u -r1.7 NetscapeDraftSpec.java --- java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java 28 Jan 2003 04:40:23 -0000 1.7 +++ java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java 18 Jul 2003 07:48:43 -0000 @@ -69,6 +69,8 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; + +import org.apache.commons.httpclient.HeaderElement; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.Cookie; @@ -94,6 +96,85 @@ /** Default constructor */ public NetscapeDraftSpec() { super(); + } + + /** + * Parses the Set-Cookie value into an array of Cookies. + * + *

Syntax of the Set-Cookie HTTP Response Header:

+ * + *

This is the format a CGI script would use to add to + * the HTTP headers a new piece of data which is to be stored by + * the client for later retrieval.

+ * + *
+      *  Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
+      * 
+ * + * @param host the host from which the Set-Cookie value was + * received + * @param port the port from which the Set-Cookie value was + * received + * @param path the path from which the Set-Cookie value was + * received + * @param secure true when the Set-Cookie value was + * received over secure conection + * @param header the Set-Cookie received from the server + * @return an array of Cookies parsed from the Set-Cookie value + * @throws MalformedCookieException if an exception occurs during parsing + */ + public Cookie[] parse(String host, int port, String path, + boolean secure, final String header) + throws MalformedCookieException { + + LOG.trace("enter NetscapeDraftSpec.parse(String, port, path, boolean, Header)"); + + if (host == null) { + throw new IllegalArgumentException("Host of origin may not be null"); + } + if (host.trim().equals("")) { + throw new IllegalArgumentException("Host of origin may not be blank"); + } + if (port < 0) { + throw new IllegalArgumentException("Invalid port: " + port); + } + if (path == null) { + throw new IllegalArgumentException("Path of origin may not be null."); + } + if (header == null) { + throw new IllegalArgumentException("Header may not be null."); + } + + if (path.trim().equals("")) { + path = PATH_DELIM; + } + host = host.toLowerCase(); + + String defaultPath = path; + int lastSlashIndex = defaultPath.lastIndexOf(PATH_DELIM); + if (lastSlashIndex >= 0) { + if (lastSlashIndex == 0) { + //Do not remove the very first slash + lastSlashIndex = 1; + } + defaultPath = defaultPath.substring(0, lastSlashIndex); + } + HeaderElement headerelement = new HeaderElement(header.toCharArray()); + Cookie cookie = new Cookie(host, + headerelement.getName(), + headerelement.getValue(), + defaultPath, + null, + false); + // cycle through the parameters + NameValuePair[] parameters = headerelement.getParameters(); + // could be null. In case only a header element and no parameters. + if (parameters != null) { + for (int j = 0; j < parameters.length; j++) { + parseAttribute(parameters[j], cookie); + } + } + return new Cookie[] {cookie}; }