1 /* 2 * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/util/Attic/CookieUtils.java,v 1.2 2000/02/26 02:32:26 shawn Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2000/02/26 02:32:26 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999 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", "Tomcat", 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 65 package org.apache.tomcat.util; 66 67 import java.text; 68 import java.util; 69 70 import javax.servlet.http.Cookie; 71 72 /** 73 * 74 * 75 * @author Original Author Unknown 76 * @author duncan@eng.sun.com 77 */ 78 79 public class CookieUtils { 80 81 public static String getCookieHeader(Cookie cookie) { 82 StringBuffer buf = new StringBuffer(); 83 int version = cookie.getVersion(); 84 85 // this part is the same for all cookies 86 87 buf.append(cookie.getName()); 88 buf.append("="); 89 maybeQuote(version, buf, cookie.getValue()); 90 91 // add version 1 specific information 92 if (version == 1) { 93 // Version=1 ... required 94 buf.append (";Version=1"); 95 96 // Comment=comment 97 if (cookie.getComment() != null) { 98 buf.append (";Comment="); 99 maybeQuote (version, buf, cookie.getComment()); 100 } 101 } 102 103 // add domain information, if present 104 105 if (cookie.getDomain() != null) { 106 buf.append(";Domain="); 107 maybeQuote (version, buf, cookie.getDomain()); 108 } 109 110 // Max-Age=secs/Discard ... or use old "Expires" format 111 if (cookie.getMaxAge() >= 0) { 112 if (version == 0) { 113 buf.append (";Expires="); 114 new OldCookieExpiry (cookie.getMaxAge()).append (buf); 115 } else { 116 buf.append (";MaxAge="); 117 buf.append (cookie.getMaxAge()); 118 } 119 } else if (version == 1) 120 buf.append (";Discard"); 121 122 // Path=path 123 if (cookie.getPath() != null) { 124 buf.append (";Path="); 125 maybeQuote (version, buf, cookie.getPath()); 126 } 127 128 // Secure 129 if (cookie.getSecure()) { 130 buf.append (";Secure"); 131 } 132 133 // form up the final header string 134 135 if (version == 1) { 136 Cookie c = (Cookie)cookie.clone(); 137 c.setVersion(0); 138 StringBuffer buf2 = new StringBuffer("Set-Cookie2: "); 139 buf2.append(buf + "\r\n"); 140 141 buf2.append(getCookieHeader(c)); 142 return buf2.toString(); 143 } else { 144 return "Set-Cookie: " + buf.toString(); 145 } 146 } 147 148 /* 149 * Appends the string to the buffer, unquoted if it's an HTTP/1.1 150 * token (or we're using "old format" cookies, which don't seem 151 * to understand quoting anywhere) else quoted. 152 * 153 * XXX This can't be the right way to deal with strings that embed 154 * double quotes ... 155 */ 156 private static void maybeQuote (int version, StringBuffer buf, 157 String value) 158 { 159 if (version == 0 || isToken (value)) 160 buf.append (value); 161 else { 162 buf.append ('"'); 163 buf.append (value); 164 buf.append ('"'); 165 } 166 } 167 168 // 169 // from RFC 2068, token special case characters 170 // 171 private static final String tspecials = "()<>@,;:\\\"/[]?={} \t"; 172 173 /* 174 * Return true iff the string counts as an HTTP/1.1 "token". 175 */ 176 private static boolean isToken (String value) { 177 int len = value.length (); 178 179 for (int i = 0; i < len; i++) { 180 char c = value.charAt (i); 181 182 if (c < 0x20 || c >= 0x7f || tspecials.indexOf (c) != -1) 183 return false; 184 } 185 return true; 186 } 187 188 /* 189 * The original Netscape cookie spec had a funky string format 190 * for dates ... RFC 1123 GMT format, but dashes in two places 191 * where spaces would normally live. RFC 2109 simplified that, 192 * deleting date parsing entirely. 193 */ 194 static class OldCookieExpiry extends HttpDate { 195 OldCookieExpiry (long maxAge) { 196 super(); 197 setTime(getCurrentTime() + maxAge * 1000); 198 } 199 // Wdy, DD-Mon-YYYY HH:MM:SS GMT 200 void append (StringBuffer buf) { 201 String pattern = "EEE, dd-MMM-yyyyy HH:mm:ss z"; 202 Locale loc = Locale.US; 203 SimpleDateFormat df = new SimpleDateFormat(pattern, loc); 204 String str = df.format(calendar.getTime()); 205 buf.append(str); 206 } 207 } 208 209 }