1 /*
2 * Copyright 1999,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 package org.apache.catalina.util;
19
20 import java.text;
21 import java.util;
22
23 import javax.servlet.http.Cookie;
24
25 // XXX use only one Date instance/request, reuse it.
26
27 /**
28 * Cookie utils - generate cookie header, etc
29 *
30 * @author Original Author Unknown
31 * @author duncan@eng.sun.com
32 */
33 public class CookieTools {
34
35 /** Return the header name to set the cookie, based on cookie
36 * version
37 */
38 public static String getCookieHeaderName(Cookie cookie) {
39 int version = cookie.getVersion();
40
41 if (version == 1) {
42 return "Set-Cookie2";
43 } else {
44 return "Set-Cookie";
45 }
46 }
47
48 /** Return the header value used to set this cookie
49 * @deprecated Use StringBuffer version
50 */
51 public static String getCookieHeaderValue(Cookie cookie) {
52 StringBuffer buf = new StringBuffer();
53 getCookieHeaderValue( cookie, buf );
54 return buf.toString();
55 }
56
57 /** Return the header value used to set this cookie
58 */
59 public static void getCookieHeaderValue(Cookie cookie, StringBuffer buf) {
60 int version = cookie.getVersion();
61
62 // this part is the same for all cookies
63
64 String name = cookie.getName(); // Avoid NPE on malformed cookies
65 if (name == null)
66 name = "";
67 String value = cookie.getValue();
68 if (value == null)
69 value = "";
70
71 buf.append(name);
72 buf.append("=");
73 maybeQuote(version, buf, value);
74
75 // add version 1 specific information
76 if (version == 1) {
77 // Version=1 ... required
78 buf.append ("; Version=1");
79
80 // Comment=comment
81 if (cookie.getComment() != null) {
82 buf.append ("; Comment=");
83 maybeQuote (version, buf, cookie.getComment());
84 }
85 }
86
87 // add domain information, if present
88
89 if (cookie.getDomain() != null) {
90 buf.append("; Domain=");
91 maybeQuote (version, buf, cookie.getDomain());
92 }
93
94 // Max-Age=secs/Discard ... or use old "Expires" format
95 if (cookie.getMaxAge() >= 0) {
96 if (version == 0) {
97 buf.append ("; Expires=");
98 if (cookie.getMaxAge() == 0)
99 DateTool.oldCookieFormat.format(new Date(10000), buf,
100 new FieldPosition(0));
101 else
102 DateTool.oldCookieFormat.format
103 (new Date( System.currentTimeMillis() +
104 cookie.getMaxAge() *1000L), buf,
105 new FieldPosition(0));
106 } else {
107 buf.append ("; Max-Age=");
108 buf.append (cookie.getMaxAge());
109 }
110 } else if (version == 1)
111 buf.append ("; Discard");
112
113 // Path=path
114 if (cookie.getPath() != null) {
115 buf.append ("; Path=");
116 maybeQuote (version, buf, cookie.getPath());
117 }
118
119 // Secure
120 if (cookie.getSecure()) {
121 buf.append ("; Secure");
122 }
123 }
124
125 static void maybeQuote (int version, StringBuffer buf,
126 String value)
127 {
128 if (version == 0 || isToken (value))
129 buf.append (value);
130 else {
131 buf.append ('"');
132 buf.append (value);
133 buf.append ('"');
134 }
135 }
136
137 //
138 // from RFC 2068, token special case characters
139 //
140 private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
141
142 /*
143 * Return true iff the string counts as an HTTP/1.1 "token".
144 */
145 private static boolean isToken (String value) {
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
158 }