1 17 18 package org.apache.tomcat.util.http; 19 20 import java.io.Serializable ; 21 import java.text.FieldPosition ; 22 import java.util.Date ; 23 24 import org.apache.tomcat.util.buf.DateTool; 25 import org.apache.tomcat.util.buf.MessageBytes; 26 27 28 37 public class ServerCookie implements Serializable { 38 39 40 private static org.apache.commons.logging.Log log= 41 org.apache.commons.logging.LogFactory.getLog(ServerCookie.class ); 42 43 private MessageBytes name=MessageBytes.newInstance(); 44 private MessageBytes value=MessageBytes.newInstance(); 45 46 private MessageBytes comment=MessageBytes.newInstance(); private MessageBytes domain=MessageBytes.newInstance(); 49 private int maxAge = -1; private MessageBytes path=MessageBytes.newInstance(); private boolean secure; private int version = 0; 56 58 public ServerCookie() { 59 60 } 61 62 public void recycle() { 63 path.recycle(); 64 name.recycle(); 65 value.recycle(); 66 comment.recycle(); 67 maxAge=-1; 68 path.recycle(); 69 domain.recycle(); 70 version=0; 71 secure=false; 72 } 73 74 public MessageBytes getComment() { 75 return comment; 76 } 77 78 public MessageBytes getDomain() { 79 return domain; 80 } 81 82 public void setMaxAge(int expiry) { 83 maxAge = expiry; 84 } 85 86 public int getMaxAge() { 87 return maxAge; 88 } 89 90 91 public MessageBytes getPath() { 92 return path; 93 } 94 95 public void setSecure(boolean flag) { 96 secure = flag; 97 } 98 99 public boolean getSecure() { 100 return secure; 101 } 102 103 public MessageBytes getName() { 104 return name; 105 } 106 107 public MessageBytes getValue() { 108 return value; 109 } 110 111 public int getVersion() { 112 return version; 113 } 114 115 116 public void setVersion(int v) { 117 version = v; 118 } 119 120 121 123 public String toString() { 124 return "Cookie " + getName() + "=" + getValue() + " ; " 125 + getVersion() + " " + getPath() + " " + getDomain(); 126 } 127 128 private static final String tspecials = ",; "; 133 134 144 public static boolean isToken(String value) { 145 if( value==null) return true; 146 int len = value.length(); 147 148 for (int i = 0; i < len; i++) { 149 char c = value.charAt(i); 150 151 if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1) 152 return false; 153 } 154 return true; 155 } 156 157 public static boolean checkName( String name ) { 158 if (!isToken(name) 159 || name.equalsIgnoreCase("Comment") || name.equalsIgnoreCase("Discard") || name.equalsIgnoreCase("Domain") 162 || name.equalsIgnoreCase("Expires") || name.equalsIgnoreCase("Max-Age") || name.equalsIgnoreCase("Path") 165 || name.equalsIgnoreCase("Secure") 166 || name.equalsIgnoreCase("Version") 167 ) { 168 return false; 169 } 170 return true; 171 } 172 173 175 176 179 public String getCookieHeaderName() { 180 return getCookieHeaderName(version); 181 } 182 183 186 public static String getCookieHeaderName(int version) { 187 if( dbg>0 ) log( (version==1) ? "Set-Cookie2" : "Set-Cookie"); 188 if (version == 1) { 189 return "Set-Cookie"; 191 } else { 196 return "Set-Cookie"; 198 } 199 } 200 201 private static final String ancientDate=DateTool.formatOldCookie(new Date (10000)); 202 203 public static void appendCookieValue( StringBuffer buf, 204 int version, 205 String name, 206 String value, 207 String path, 208 String domain, 209 String comment, 210 int maxAge, 211 boolean isSecure ) 212 { 213 buf.append( name ); 215 buf.append("="); 216 maybeQuote(version, buf, value); 217 218 if (version == 1) { 221 buf.append ("; Version=1"); 223 224 if ( comment!=null ) { 226 buf.append ("; Comment="); 227 maybeQuote (version, buf, comment); 228 } 229 } 230 231 233 if (domain!=null) { 234 buf.append("; Domain="); 235 maybeQuote (version, buf, domain); 236 } 237 238 if (maxAge >= 0) { 240 if (version == 0) { 241 buf.append ("; Expires="); 244 if (maxAge == 0) 248 buf.append( ancientDate ); 249 else 250 DateTool.formatOldCookie 251 (new Date ( System.currentTimeMillis() + 252 maxAge *1000L), buf, 253 new FieldPosition (0)); 254 255 } else { 256 buf.append ("; Max-Age="); 257 buf.append (maxAge); 258 } 259 } 260 261 if (path!=null) { 263 buf.append ("; Path="); 264 maybeQuote (version, buf, path); 265 } 266 267 if (isSecure) { 269 buf.append ("; Secure"); 270 } 271 272 273 } 274 275 public static void maybeQuote (int version, StringBuffer buf, 276 String value) { 277 if (isToken(value)) { 279 buf.append(value); 280 } else { 281 buf.append('"'); 282 buf.append(escapeDoubleQuotes(value)); 283 buf.append('"'); 284 } 285 } 286 287 static final int dbg=1; 289 public static void log(String s ) { 290 if (log.isDebugEnabled()) 291 log.debug("ServerCookie: " + s); 292 } 293 294 295 302 private static String escapeDoubleQuotes(String s) { 303 304 if (s == null || s.length() == 0 || s.indexOf('"') == -1) { 305 return s; 306 } 307 308 StringBuffer b = new StringBuffer (); 309 for (int i = 0; i < s.length(); i++) { 310 char c = s.charAt(i); 311 if (c == '"') 312 b.append('\\').append('"'); 313 else 314 b.append(c); 315 } 316 317 return b.toString(); 318 } 319 320 } 321 322 | Popular Tags |