1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient.cookie;
33
34 import java.util.StringTokenizer;
35 import java.util.Date;
36 import java.util.Locale;
37 import java.text.DateFormat;
38 import java.text.SimpleDateFormat;
39 import java.text.ParseException;
40 import org.apache.commons.httpclient.NameValuePair;
41 import org.apache.commons.httpclient.Cookie;
42
43 /***
44 * <P>Netscape cookie draft specific cookie management functions
45 *
46 * @author B.C. Holmes
47 * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
48 * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
49 * @author Rod Waldhoff
50 * @author dIon Gillard
51 * @author Sean C. Sullivan
52 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
53 * @author Marc A. Saegesser
54 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
55 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56 *
57 * @since 2.0
58 */
59
60 public class NetscapeDraftSpec extends CookieSpecBase {
61
62 /*** Default constructor */
63 public NetscapeDraftSpec() {
64 super();
65 }
66
67
68 /***
69 * Parse the cookie attribute and update the corresponsing {@link Cookie}
70 * properties as defined by the Netscape draft specification
71 *
72 * @param attribute {@link NameValuePair} cookie attribute from the
73 * <tt>Set- Cookie</tt>
74 * @param cookie {@link Cookie} to be updated
75 * @throws MalformedCookieException if an exception occurs during parsing
76 */
77 public void parseAttribute(
78 final NameValuePair attribute, final Cookie cookie)
79 throws MalformedCookieException {
80
81 if (attribute == null) {
82 throw new IllegalArgumentException("Attribute may not be null.");
83 }
84 if (cookie == null) {
85 throw new IllegalArgumentException("Cookie may not be null.");
86 }
87 final String paramName = attribute.getName().toLowerCase();
88 final String paramValue = attribute.getValue();
89
90 if (paramName.equals("expires")) {
91
92 if (paramValue == null) {
93 throw new MalformedCookieException(
94 "Missing value for expires attribute");
95 }
96 try {
97 DateFormat expiryFormat = new SimpleDateFormat(
98 "EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
99 Date date = expiryFormat.parse(paramValue);
100 cookie.setExpiryDate(date);
101 } catch (ParseException e) {
102 throw new MalformedCookieException("Invalid expires "
103 + "attribute: " + e.getMessage());
104 }
105 } else {
106 super.parseAttribute(attribute, cookie);
107 }
108 }
109
110 /***
111 * Performs Netscape draft compliant {@link Cookie} validation
112 *
113 * @param host the host from which the {@link Cookie} was received
114 * @param port the port from which the {@link Cookie} was received
115 * @param path the path from which the {@link Cookie} was received
116 * @param secure <tt>true</tt> when the {@link Cookie} was received
117 * using a secure connection
118 * @param cookie The cookie to validate.
119 * @throws MalformedCookieException if an exception occurs during
120 * validation
121 */
122 public void validate(String host, int port, String path,
123 boolean secure, final Cookie cookie)
124 throws MalformedCookieException {
125
126 LOG.trace("enterNetscapeDraftCookieProcessor "
127 + "RCF2109CookieProcessor.validate(Cookie)");
128
129 super.validate(host, port, path, secure, cookie);
130
131 if (host.indexOf(".") >= 0) {
132 int domainParts = new StringTokenizer(cookie.getDomain(), ".")
133 .countTokens();
134
135 if (isSpecialDomain(cookie.getDomain())) {
136 if (domainParts < 2) {
137 throw new MalformedCookieException("Domain attribute \""
138 + cookie.getDomain()
139 + "\" violates the Netscape cookie specification for "
140 + "special domains");
141 }
142 } else {
143 if (domainParts < 3) {
144 throw new MalformedCookieException("Domain attribute \""
145 + cookie.getDomain()
146 + "\" violates the Netscape cookie specification");
147 }
148 }
149 }
150 }
151
152 /***
153 * Checks if the given domain is in one of the seven special
154 * top level domains defined by the Netscape cookie specification.
155 * @param domain The domain.
156 * @return True if the specified domain is "special"
157 */
158 private static boolean isSpecialDomain(final String domain) {
159 final String ucDomain = domain.toUpperCase();
160 if (ucDomain.endsWith(".COM")
161 || ucDomain.endsWith(".EDU")
162 || ucDomain.endsWith(".NET")
163 || ucDomain.endsWith(".GOV")
164 || ucDomain.endsWith(".MIL")
165 || ucDomain.endsWith(".ORG")
166 || ucDomain.endsWith(".INT")) {
167 return true;
168 }
169 return false;
170 }
171 }