You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2006/04/30 15:29:01 UTC
svn commit: r398339 -
/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
Author: olegk
Date: Sun Apr 30 06:29:00 2006
New Revision: 398339
URL: http://svn.apache.org/viewcvs?rev=398339&view=rev
Log:
Handle the requirement of the section 3.2.2 postulating that the first occurrence of an attribute must take precedence a little more elegantly
Modified:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java?rev=398339&r1=398338&r2=398339&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java Sun Apr 30 06:29:00 2006
@@ -266,8 +266,15 @@
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);
+ // Eliminate duplicate attribues. The first occurence takes precedence
+ Map attribmap = new HashMap(parameters.length);
+ for (int j = parameters.length - 1; j >= 0; j--) {
+ NameValuePair param = parameters[j];
+ attribmap.put(param.getName().toLowerCase(), param);
+ }
+ for (Iterator it = attribmap.entrySet().iterator(); it.hasNext(); ) {
+ Map.Entry entry = (Map.Entry) it.next();
+ parseAttribute((NameValuePair) entry.getValue(), cookie);
}
}
cookies.add(cookie);
@@ -597,18 +604,16 @@
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- if (!cookie.isPathAttributeSpecified()) {
- if (path == null) {
- throw new MalformedCookieException(
- "Missing value for path attribute");
- }
- if (path.trim().equals("")) {
- throw new MalformedCookieException(
- "Blank value for path attribute");
- }
- cookie.setPath(path);
- cookie.setPathAttributeSpecified(true);
+ if (path == null) {
+ throw new MalformedCookieException(
+ "Missing value for path attribute");
}
+ if (path.trim().equals("")) {
+ throw new MalformedCookieException(
+ "Blank value for path attribute");
+ }
+ cookie.setPath(path);
+ cookie.setPathAttributeSpecified(true);
}
/**
@@ -684,23 +689,21 @@
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- if (!cookie.isDomainAttributeSpecified()) {
- //TODO (jain): how do we handle the case when domain is specified and equals host?
- if (domain == null) {
- throw new MalformedCookieException(
- "Missing value for domain attribute");
- }
- if (domain.trim().equals("")) {
- throw new MalformedCookieException(
- "Blank value for domain attribute");
- }
- domain = domain.toLowerCase();
- // put a leading dot if domain does not start with a dot
- if (!domain.startsWith("."))
- domain = "." + domain;
- cookie.setDomain(domain);
- cookie.setDomainAttributeSpecified(true);
+ //TODO (jain): how do we handle the case when domain is specified and equals host?
+ if (domain == null) {
+ throw new MalformedCookieException(
+ "Missing value for domain attribute");
}
+ if (domain.trim().equals("")) {
+ throw new MalformedCookieException(
+ "Blank value for domain attribute");
+ }
+ domain = domain.toLowerCase();
+ // put a leading dot if domain does not start with a dot
+ if (!domain.startsWith("."))
+ domain = "." + domain;
+ cookie.setDomain(domain);
+ cookie.setDomainAttributeSpecified(true);
}
/**
@@ -816,19 +819,17 @@
}
if (cookie instanceof Cookie2) {
Cookie2 cookie2 = (Cookie2) cookie;
- if (!cookie2.isPortAttributeSpecified()) {
- if ((portValue == null) || (portValue.trim().equals(""))) {
- // If the Port attribute is present but has no value, the
- // cookie can only be sent to the request-port.
- // Since the default port list contains only request-port, we don't
- // need to do anything here.
- cookie2.setPortAttributeBlank(true);
- } else {
- int[] ports = parsePortAttribute(portValue);
- cookie2.setPorts(ports);
- }
- cookie2.setPortAttributeSpecified(true);
+ if ((portValue == null) || (portValue.trim().equals(""))) {
+ // If the Port attribute is present but has no value, the
+ // cookie can only be sent to the request-port.
+ // Since the default port list contains only request-port, we don't
+ // need to do anything here.
+ cookie2.setPortAttributeBlank(true);
+ } else {
+ int[] ports = parsePortAttribute(portValue);
+ cookie2.setPorts(ports);
}
+ cookie2.setPortAttributeSpecified(true);
}
}
@@ -902,23 +903,20 @@
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- if (cookie.getExpiryDate() == null) {
- if (value == null) {
- throw new MalformedCookieException(
- "Missing value for max-age attribute");
- }
- int age = -1;
- try {
- age = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- age = -1;
- }
- if (age < 0) {
- throw new MalformedCookieException ("Invalid max-age attribute.");
- }
- cookie.setExpiryDate(
- new Date(System.currentTimeMillis() + age * 1000L));
- }
+ if (value == null) {
+ throw new MalformedCookieException(
+ "Missing value for max-age attribute");
+ }
+ int age = -1;
+ try {
+ age = Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ age = -1;
+ }
+ if (age < 0) {
+ throw new MalformedCookieException ("Invalid max-age attribute.");
+ }
+ cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
}
/**
@@ -1048,23 +1046,21 @@
}
if (cookie instanceof Cookie2) {
Cookie2 cookie2 = (Cookie2) cookie;
- if (!cookie2.isVersionAttributeSpecified()) {
- if (value == null) {
- throw new MalformedCookieException(
- "Missing value for version attribute");
- }
- int version = -1;
- try {
- version = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- version = -1;
- }
- if (version < 0) {
- throw new MalformedCookieException("Invalid cookie version.");
- }
- cookie2.setVersion(Integer.parseInt(value));
- cookie2.setVersionAttributeSpecified(true);
+ if (value == null) {
+ throw new MalformedCookieException(
+ "Missing value for version attribute");
+ }
+ int version = -1;
+ try {
+ version = Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ version = -1;
+ }
+ if (version < 0) {
+ throw new MalformedCookieException("Invalid cookie version.");
}
+ cookie2.setVersion(Integer.parseInt(value));
+ cookie2.setVersionAttributeSpecified(true);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org