1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java,v 1.14.2.1 2003/10/04 02:31:25 mbecke Exp $
3 * $Revision: 1.14.2.1 $
4 * $Date: 2003/10/04 02:31:25 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.cookie;
65
66 import org.apache.commons.httpclient.NameValuePair;
67 import org.apache.commons.httpclient.Cookie;
68
69 /***
70 * <p>RFC 2109 specific cookie management functions
71 *
72 * @author B.C. Holmes
73 * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
74 * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
75 * @author Rod Waldhoff
76 * @author dIon Gillard
77 * @author Sean C. Sullivan
78 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
79 * @author Marc A. Saegesser
80 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
81 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
82 *
83 * @since 2.0
84 */
85
86 public class RFC2109Spec extends CookieSpecBase {
87
88 /*** Default constructor */
89 public RFC2109Spec() {
90 super();
91 }
92
93
94 /***
95 * Parse RFC 2109 specific cookie attribute and update the corresponsing
96 * {@link Cookie} properties.
97 *
98 * @param attribute {@link NameValuePair} cookie attribute from the
99 * <tt>Set- Cookie</tt>
100 * @param cookie {@link Cookie} to be updated
101 * @throws MalformedCookieException if an exception occurs during parsing
102 */
103 public void parseAttribute(
104 final NameValuePair attribute, final Cookie cookie)
105 throws MalformedCookieException {
106
107 if (attribute == null) {
108 throw new IllegalArgumentException("Attribute may not be null.");
109 }
110 if (cookie == null) {
111 throw new IllegalArgumentException("Cookie may not be null.");
112 }
113 final String paramName = attribute.getName().toLowerCase();
114 final String paramValue = attribute.getValue();
115
116 if (paramName.equals("path")) {
117 if (paramValue == null) {
118 throw new MalformedCookieException(
119 "Missing value for path attribute");
120 }
121 if (paramValue.trim().equals("")) {
122 throw new MalformedCookieException(
123 "Blank value for path attribute");
124 }
125 cookie.setPath(paramValue);
126 cookie.setPathAttributeSpecified(true);
127 } else if (paramName.equals("version")) {
128
129 if (paramValue == null) {
130 throw new MalformedCookieException(
131 "Missing value for version attribute");
132 }
133 try {
134 cookie.setVersion(Integer.parseInt(paramValue));
135 } catch (NumberFormatException e) {
136 throw new MalformedCookieException("Invalid version: "
137 + e.getMessage());
138 }
139
140 } else {
141 super.parseAttribute(attribute, cookie);
142 }
143 }
144
145 /***
146 * Performs RFC 2109 compliant {@link Cookie} validation
147 *
148 * @param host the host from which the {@link Cookie} was received
149 * @param port the port from which the {@link Cookie} was received
150 * @param path the path from which the {@link Cookie} was received
151 * @param secure <tt>true</tt> when the {@link Cookie} was received using a
152 * secure connection
153 * @param cookie The cookie to validate
154 * @throws MalformedCookieException if an exception occurs during
155 * validation
156 */
157 public void validate(String host, int port, String path,
158 boolean secure, final Cookie cookie) throws MalformedCookieException {
159
160 LOG.trace("enter RFC2109Spec.validate(String, int, String, "
161 + "boolean, Cookie)");
162
163 // Perform generic validation
164 super.validate(host, port, path, secure, cookie);
165 // Perform RFC 2109 specific validation
166 if (cookie.isDomainAttributeSpecified()
167 && (!cookie.getDomain().equals(host))) {
168
169 // domain must start with dot
170 if (!cookie.getDomain().startsWith(".")) {
171 throw new MalformedCookieException("Domain attribute \""
172 + cookie.getDomain()
173 + "\" violates RFC 2109: domain must start with a dot");
174 }
175 // domain must have at least one embedded dot
176 int dotIndex = cookie.getDomain().indexOf('.', 1);
177 if (dotIndex < 0 || dotIndex == cookie.getDomain().length() - 1) {
178 throw new MalformedCookieException("Domain attribute \""
179 + cookie.getDomain()
180 + "\" violates RFC 2109: domain must contain an embedded dot");
181 }
182 host = host.toLowerCase();
183 if (host.indexOf('.') >= 0) {
184 if (!host.endsWith(cookie.getDomain())) {
185 throw new MalformedCookieException(
186 "Illegal domain attribute \"" + cookie.getDomain()
187 + "\". Domain of origin: \"" + host + "\"");
188 }
189 // host minus domain may not contain any dots
190 String hostWithoutDomain = host.substring(0, host.length()
191 - cookie.getDomain().length());
192 if (hostWithoutDomain.indexOf('.') != -1) {
193 throw new MalformedCookieException("Domain attribute \""
194 + cookie.getDomain()
195 + "\" violates RFC 2109: host minus domain may not contain any dots");
196 }
197 }
198 }
199 }
200
201
202 /***
203 * Return a name/value string suitable for sending in a <tt>"Cookie"</tt>
204 * header as defined in RFC 2109 for backward compatibility with cookie
205 * version 0
206 * @param name The name.
207 * @param value The value
208 * @param version The cookie version
209 * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
210 */
211
212 private String formatNameValuePair(
213 final String name, final String value, int version) {
214
215 final StringBuffer buffer = new StringBuffer();
216 if (version < 1) {
217 buffer.append(name);
218 buffer.append("=");
219 if (value != null) {
220 buffer.append(value);
221 }
222 } else {
223 buffer.append(name);
224 buffer.append("=\"");
225 if (value != null) {
226 buffer.append(value);
227 }
228 buffer.append("\"");
229 }
230 return buffer.toString();
231 }
232
233 /***
234 * Return a string suitable for sending in a <tt>"Cookie"</tt> header
235 * as defined in RFC 2109 for backward compatibility with cookie version 0
236 * @param cookie a {@link Cookie} to be formatted as string
237 * @param version The version to use.
238 * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
239 */
240 private String formatCookieAsVer(Cookie cookie, int version) {
241 LOG.trace("enter RFC2109Spec.formatCookieAsVer(Cookie)");
242 if (cookie == null) {
243 throw new IllegalArgumentException("Cookie may not be null");
244 }
245 StringBuffer buf = new StringBuffer();
246 buf.append(formatNameValuePair(cookie.getName(),
247 cookie.getValue(), version));
248 if (cookie.getDomain() != null
249 && cookie.isDomainAttributeSpecified()) {
250
251 buf.append("; ");
252 buf.append(formatNameValuePair("$Domain",
253 cookie.getDomain(), version));
254 }
255 if (cookie.getPath() != null && cookie.isPathAttributeSpecified()) {
256 buf.append("; ");
257 buf.append(formatNameValuePair("$Path", cookie.getPath(), version));
258 }
259 return buf.toString();
260 }
261
262
263 /***
264 * Return a string suitable for sending in a <tt>"Cookie"</tt> header as
265 * defined in RFC 2109
266 * @param cookie a {@link Cookie} to be formatted as string
267 * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
268 */
269 public String formatCookie(Cookie cookie) {
270 LOG.trace("enter RFC2109Spec.formatCookie(Cookie)");
271 if (cookie == null) {
272 throw new IllegalArgumentException("Cookie may not be null");
273 }
274 int ver = cookie.getVersion();
275 StringBuffer buffer = new StringBuffer();
276 buffer.append(formatNameValuePair("$Version",
277 Integer.toString(ver), ver));
278 buffer.append("; ");
279 buffer.append(formatCookieAsVer(cookie, ver));
280 return buffer.toString();
281 }
282
283 /***
284 * Create a RFC 2109 compliant <tt>"Cookie"</tt> header value containing all
285 * {@link Cookie}s in <i>cookies</i> suitable for sending in a <tt>"Cookie"
286 * </tt> header
287 * @param cookies an array of {@link Cookie}s to be formatted
288 * @return a string suitable for sending in a Cookie header.
289 */
290 public String formatCookies(Cookie[] cookies) {
291 LOG.trace("enter RFC2109Spec.formatCookieHeader(Cookie[])");
292 int version = Integer.MAX_VALUE;
293 // Pick the lowerest common denominator
294 for (int i = 0; i < cookies.length; i++) {
295 Cookie cookie = cookies[i];
296 if (cookie.getVersion() < version) {
297 version = cookie.getVersion();
298 }
299 }
300 final StringBuffer buffer = new StringBuffer();
301 buffer.append(formatNameValuePair("$Version",
302 Integer.toString(version), version));
303 for (int i = 0; i < cookies.length; i++) {
304 buffer.append("; ");
305 buffer.append(formatCookieAsVer(cookies[i], version));
306 }
307 return buffer.toString();
308 }
309 }
This page was automatically generated by Maven