1 | | /* | | 1 | | /* |
2 | | * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpHeader.java,v 1.3 2001/07/22 20 :25: 07 pier Exp $ | | 2 | | * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpHeader.java,v 1.4 2002/03/18 07:15:40 remm Exp $ |
3 | | * $Revision: 1.3 $ | | 3 | | * $Revision: 1.4 $ |
4 | | * $Date: 2001/07/22 20 :25: 07 $ | | 4 | | * $Date: 2002/03/18 07:15:40 $ |
5 | | * | | 5 | | * |
6 | | * ==================================================================== | | 6 | | * ==================================================================== |
7 | | * | | 7 | | * |
8 | | * The Apache Software License, Version 1.1 | | 8 | | * The Apache Software License, Version 1.1 |
9 | | * | | 9 | | * |
10 | | * Copyright (c) 1999 The Apache Software Foundation. All rights | | 10 | | * Copyright (c) 1999 The Apache Software Foundation. All rights |
11 | | * reserved. | | 11 | | * reserved. |
12 | | * | | 12 | | * |
13 | | * Redistribution and use in source and binary forms, with or without | | 13 | | * Redistribution and use in source and binary forms, with or without |
14 | | * modification, are permitted provided that the following conditions | | 14 | | * modification, are permitted provided that the following conditions |
15 | | * are met: | | 15 | | * are met: |
16 | | * | | 16 | | * |
17 | | * 1. Redistributions of source code must retain the above copyright | | 17 | | * 1. Redistributions of source code must retain the above copyright |
18 | | * notice, this list of conditions and the following disclaimer. | | 18 | | * notice, this list of conditions and the following disclaimer. |
19 | | * | | 19 | | * |
20 | | * 2. Redistributions in binary form must reproduce the above copyright | | 20 | | * 2. Redistributions in binary form must reproduce the above copyright |
21 | | * notice, this list of conditions and the following disclaimer in | | 21 | | * notice, this list of conditions and the following disclaimer in |
22 | | * the documentation and/or other materials provided with the | | 22 | | * the documentation and/or other materials provided with the |
23 | | * distribution. | | 23 | | * distribution. |
24 | | * | | 24 | | * |
25 | | * 3. The end-user documentation included with the redistribution, if | | 25 | | * 3. The end-user documentation included with the redistribution, if |
26 | | * any, must include the following acknowlegement: | | 26 | | * any, must include the following acknowlegement: |
27 | | * "This product includes software developed by the | | 27 | | * "This product includes software developed by the |
28 | | * Apache Software Foundation (http://www.apache.org/)." | | 28 | | * Apache Software Foundation (http://www.apache.org/)." |
29 | | * Alternately, this acknowlegement may appear in the software itself, | | 29 | | * Alternately, this acknowlegement may appear in the software itself, |
30 | | * if and wherever such third-party acknowlegements normally appear. | | 30 | | * if and wherever such third-party acknowlegements normally appear. |
31 | | * | | 31 | | * |
32 | | * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software | | 32 | | * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software |
33 | | * Foundation" must not be used to endorse or promote products derived | | 33 | | * Foundation" must not be used to endorse or promote products derived |
34 | | * from this software without prior written permission. For written | | 34 | | * from this software without prior written permission. For written |
35 | | * permission, please contact apache@apache.org. | | 35 | | * permission, please contact apache@apache.org. |
36 | | * | | 36 | | * |
37 | | * 5. Products derived from this software may not be called "Apache" | | 37 | | * 5. Products derived from this software may not be called "Apache" |
38 | | * nor may "Apache" appear in their names without prior written | | 38 | | * nor may "Apache" appear in their names without prior written |
39 | | * permission of the Apache Group. | | 39 | | * permission of the Apache Group. |
40 | | * | | 40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | | 41 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
42 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 42 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
43 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | 43 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
44 | | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | | 44 | | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
45 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | | 45 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
46 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | | 46 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
47 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | | 47 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
48 | | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | | 48 | | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
49 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | | 49 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
50 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | | 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 | | 51 | | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
52 | | * SUCH DAMAGE. | | 52 | | * SUCH DAMAGE. |
53 | | * ==================================================================== | | 53 | | * ==================================================================== |
54 | | * | | 54 | | * |
55 | | * This software consists of voluntary contributions made by many | | 55 | | * This software consists of voluntary contributions made by many |
56 | | * individuals on behalf of the Apache Software Foundation. For more | | 56 | | * individuals on behalf of the Apache Software Foundation. For more |
57 | | * information on the Apache Software Foundation, please see | | 57 | | * information on the Apache Software Foundation, please see |
58 | | * <http://www.apache.org/>. | | 58 | | * <http://www.apache.org/>. |
59 | | * | | 59 | | * |
60 | | * [Additional notices, if required by prior licensing conditions] | | 60 | | * [Additional notices, if required by prior licensing conditions] |
61 | | * | | 61 | | * |
62 | | */ | | 62 | | */ |
63 | | | | 63 | | |
64 | | | | 64 | | |
65 | | package org.apache.catalina.connector.http; | | 65 | | package org.apache.catalina.connector.http; |
66 | | | | 66 | | |
67 | | | | 67 | | |
68 | | import java.io.IOException; | | 68 | | import java.io.IOException; |
69 | | import java.net.InetAddress; | | 69 | | import java.net.InetAddress; |
70 | | import javax.servlet.ServletInputStream; | | 70 | | import javax.servlet.ServletInputStream; |
71 | | import org.apache.catalina.connector.HttpRequestBase; | | 71 | | import org.apache.catalina.connector.HttpRequestBase; |
72 | | | | 72 | | |
73 | | | | 73 | | |
74 | | /** | | 74 | | /** |
75 | | * HTTP header enum type. | | 75 | | * HTTP header enum type. |
76 | | * | | 76 | | * |
77 | | * @author Remy Maucherat | | 77 | | * @author Remy Maucherat |
78 | | * @version $Revision: 1.3 $ $Date: 2001/07/22 20 :25: 07 $ | | 78 | | * @version $Revision: 1.4 $ $Date: 2002/03/18 07:15:40 $ |
| | | | 79 | | * @deprecated |
79 | | */ | | 80 | | */ |
80 | | | | 81 | | |
81 | | final class HttpHeader { | | 82 | | final class HttpHeader { |
82 | | | | 83 | | |
83 | | | | 84 | | |
84 | | // -------------------------------------------------------------- Constants | | 85 | | // -------------------------------------------------------------- Constants |
85 | | | | 86 | | |
86 | | | | 87 | | |
87 | | public static final int INITIAL_NAME_SIZE = 32; | | 88 | | public static final int INITIAL_NAME_SIZE = 32; |
88 | | public static final int INITIAL_VALUE_SIZE = 64; | | 89 | | public static final int INITIAL_VALUE_SIZE = 64; |
89 | | public static final int MAX_NAME_SIZE = 128; | | 90 | | public static final int MAX_NAME_SIZE = 128; |
90 | | public static final int MAX_VALUE_SIZE = 4096; | | 91 | | public static final int MAX_VALUE_SIZE = 4096; |
91 | | | | 92 | | |
92 | | | | 93 | | |
93 | | // ----------------------------------------------------------- Constructors | | 94 | | // ----------------------------------------------------------- Constructors |
94 | | | | 95 | | |
95 | | | | 96 | | |
96 | | public HttpHeader() { | | 97 | | public HttpHeader() { |
97 | | | | 98 | | |
98 | | this(new char[INITIAL_NAME_SIZE], 0, new char[INITIAL_VALUE_SIZE], 0); | | 99 | | this(new char[INITIAL_NAME_SIZE], 0, new char[INITIAL_VALUE_SIZE], 0); |
99 | | | | 100 | | |
100 | | } | | 101 | | } |
101 | | | | 102 | | |
102 | | | | 103 | | |
103 | | public HttpHeader(char[] name, int nameEnd, char[] value, int valueEnd) { | | 104 | | public HttpHeader(char[] name, int nameEnd, char[] value, int valueEnd) { |
104 | | | | 105 | | |
105 | | this.name = name; | | 106 | | this.name = name; |
106 | | this.nameEnd = nameEnd; | | 107 | | this.nameEnd = nameEnd; |
107 | | this.value = value; | | 108 | | this.value = value; |
108 | | this.valueEnd = valueEnd; | | 109 | | this.valueEnd = valueEnd; |
109 | | | | 110 | | |
110 | | } | | 111 | | } |
111 | | | | 112 | | |
112 | | | | 113 | | |
113 | | public HttpHeader(String name, String value) { | | 114 | | public HttpHeader(String name, String value) { |
114 | | | | 115 | | |
115 | | this.name = name.toLowerCase().toCharArray(); | | 116 | | this.name = name.toLowerCase().toCharArray(); |
116 | | this.nameEnd = name.length(); | | 117 | | this.nameEnd = name.length(); |
117 | | this.value = value.toCharArray(); | | 118 | | this.value = value.toCharArray(); |
118 | | this.valueEnd = value.length(); | | 119 | | this.valueEnd = value.length(); |
119 | | | | 120 | | |
120 | | } | | 121 | | } |
121 | | | | 122 | | |
122 | | | | 123 | | |
123 | | // ----------------------------------------------------- Instance Variables | | 124 | | // ----------------------------------------------------- Instance Variables |
124 | | | | 125 | | |
125 | | | | 126 | | |
126 | | public char[] name; | | 127 | | public char[] name; |
127 | | public int nameEnd; | | 128 | | public int nameEnd; |
128 | | public char[] value; | | 129 | | public char[] value; |
129 | | public int valueEnd; | | 130 | | public int valueEnd; |
130 | | protected int hashCode = 0; | | 131 | | protected int hashCode = 0; |
131 | | | | 132 | | |
132 | | | | 133 | | |
133 | | // ------------------------------------------------------------- Properties | | 134 | | // ------------------------------------------------------------- Properties |
134 | | | | 135 | | |
135 | | | | 136 | | |
136 | | // --------------------------------------------------------- Public Methods | | 137 | | // --------------------------------------------------------- Public Methods |
137 | | | | 138 | | |
138 | | | | 139 | | |
139 | | /** | | 140 | | /** |
140 | | * Release all object references, and initialize instance variables, in | | 141 | | * Release all object references, and initialize instance variables, in |
141 | | * preparation for reuse of this object. | | 142 | | * preparation for reuse of this object. |
142 | | */ | | 143 | | */ |
143 | | public void recycle() { | | 144 | | public void recycle() { |
144 | | | | 145 | | |
145 | | nameEnd = 0; | | 146 | | nameEnd = 0; |
146 | | valueEnd = 0; | | 147 | | valueEnd = 0; |
147 | | hashCode = 0; | | 148 | | hashCode = 0; |
148 | | | | 149 | | |
149 | | } | | 150 | | } |
150 | | | | 151 | | |
151 | | | | 152 | | |
152 | | /** | | 153 | | /** |
153 | | * Test if the name of the header is equal to the given char array. | | 154 | | * Test if the name of the header is equal to the given char array. |
154 | | * All the characters must already be lower case. | | 155 | | * All the characters must already be lower case. |
155 | | */ | | 156 | | */ |
156 | | public boolean equals(char[] buf) { | | 157 | | public boolean equals(char[] buf) { |
157 | | return equals(buf, buf.length); | | 158 | | return equals(buf, buf.length); |
158 | | } | | 159 | | } |
159 | | | | 160 | | |
160 | | | | 161 | | |
161 | | /** | | 162 | | /** |
162 | | * Test if the name of the header is equal to the given char array. | | 163 | | * Test if the name of the header is equal to the given char array. |
163 | | * All the characters must already be lower case. | | 164 | | * All the characters must already be lower case. |
164 | | */ | | 165 | | */ |
165 | | public boolean equals(char[] buf, int end) { | | 166 | | public boolean equals(char[] buf, int end) { |
166 | | if (end != nameEnd) | | 167 | | if (end != nameEnd) |
167 | | return false; | | 168 | | return false; |
168 | | for (int i=0; i<end; i++) { | | 169 | | for (int i=0; i<end; i++) { |
169 | | if (buf[i] != name[i]) | | 170 | | if (buf[i] != name[i]) |
170 | | return false; | | 171 | | return false; |
171 | | } | | 172 | | } |
172 | | return true; | | 173 | | return true; |
173 | | } | | 174 | | } |
174 | | | | 175 | | |
175 | | | | 176 | | |
176 | | /** | | 177 | | /** |
177 | | * Test if the name of the header is equal to the given string. | | 178 | | * Test if the name of the header is equal to the given string. |
178 | | * The String given must be made of lower case characters. | | 179 | | * The String given must be made of lower case characters. |
179 | | */ | | 180 | | */ |
180 | | public boolean equals(String str) { | | 181 | | public boolean equals(String str) { |
181 | | return equals(str.toCharArray(), str.length()); | | 182 | | return equals(str.toCharArray(), str.length()); |
182 | | } | | 183 | | } |
183 | | | | 184 | | |
184 | | | | 185 | | |
185 | | /** | | 186 | | /** |
186 | | * Test if the value of the header is equal to the given char array. | | 187 | | * Test if the value of the header is equal to the given char array. |
187 | | */ | | 188 | | */ |
188 | | public boolean valueEquals(char[] buf) { | | 189 | | public boolean valueEquals(char[] buf) { |
189 | | return valueEquals(buf, buf.length); | | 190 | | return valueEquals(buf, buf.length); |
190 | | } | | 191 | | } |
191 | | | | 192 | | |
192 | | | | 193 | | |
193 | | /** | | 194 | | /** |
194 | | * Test if the value of the header is equal to the given char array. | | 195 | | * Test if the value of the header is equal to the given char array. |
195 | | */ | | 196 | | */ |
196 | | public boolean valueEquals(char[] buf, int end) { | | 197 | | public boolean valueEquals(char[] buf, int end) { |
197 | | if (end != valueEnd) | | 198 | | if (end != valueEnd) |
198 | | return false; | | 199 | | return false; |
199 | | for (int i=0; i<end; i++) { | | 200 | | for (int i=0; i<end; i++) { |
200 | | if (buf[i] != value[i]) | | 201 | | if (buf[i] != value[i]) |
201 | | return false; | | 202 | | return false; |
202 | | } | | 203 | | } |
203 | | return true; | | 204 | | return true; |
204 | | } | | 205 | | } |
205 | | | | 206 | | |
206 | | | | 207 | | |
207 | | /** | | 208 | | /** |
208 | | * Test if the value of the header is equal to the given string. | | 209 | | * Test if the value of the header is equal to the given string. |
209 | | */ | | 210 | | */ |
210 | | public boolean valueEquals(String str) { | | 211 | | public boolean valueEquals(String str) { |
211 | | return valueEquals(str.toCharArray(), str.length()); | | 212 | | return valueEquals(str.toCharArray(), str.length()); |
212 | | } | | 213 | | } |
213 | | | | 214 | | |
214 | | | | 215 | | |
215 | | /** | | 216 | | /** |
216 | | * Test if the value of the header includes the given char array. | | 217 | | * Test if the value of the header includes the given char array. |
217 | | */ | | 218 | | */ |
218 | | public boolean valueIncludes(char[] buf) { | | 219 | | public boolean valueIncludes(char[] buf) { |
219 | | return valueIncludes(buf, buf.length); | | 220 | | return valueIncludes(buf, buf.length); |
220 | | } | | 221 | | } |
221 | | | | 222 | | |
222 | | | | 223 | | |
223 | | /** | | 224 | | /** |
224 | | * Test if the value of the header includes the given char array. | | 225 | | * Test if the value of the header includes the given char array. |
225 | | */ | | 226 | | */ |
226 | | public boolean valueIncludes(char[] buf, int end) { | | 227 | | public boolean valueIncludes(char[] buf, int end) { |
227 | | char firstChar = buf[0]; | | 228 | | char firstChar = buf[0]; |
228 | | int pos = 0; | | 229 | | int pos = 0; |
229 | | while (pos < valueEnd) { | | 230 | | while (pos < valueEnd) { |
230 | | pos = valueIndexOf(firstChar, pos); | | 231 | | pos = valueIndexOf(firstChar, pos); |
231 | | if (pos == -1) | | 232 | | if (pos == -1) |
232 | | return false; | | 233 | | return false; |
233 | | if ((valueEnd - pos) < end) | | 234 | | if ((valueEnd - pos) < end) |
234 | | return false; | | 235 | | return false; |
235 | | for (int i = 0; i < end; i++) { | | 236 | | for (int i = 0; i < end; i++) { |
236 | | if (value[i + pos] != buf[i]) | | 237 | | if (value[i + pos] != buf[i]) |
237 | | break; | | 238 | | break; |
238 | | if (i == (end-1)) | | 239 | | if (i == (end-1)) |
239 | | return true; | | 240 | | return true; |
240 | | } | | 241 | | } |
241 | | pos++; | | 242 | | pos++; |
242 | | } | | 243 | | } |
243 | | return false; | | 244 | | return false; |
244 | | } | | 245 | | } |
245 | | | | 246 | | |
246 | | | | 247 | | |
247 | | /** | | 248 | | /** |
248 | | * Test if the value of the header includes the given string. | | 249 | | * Test if the value of the header includes the given string. |
249 | | */ | | 250 | | */ |
250 | | public boolean valueIncludes(String str) { | | 251 | | public boolean valueIncludes(String str) { |
251 | | return valueIncludes(str.toCharArray(), str.length()); | | 252 | | return valueIncludes(str.toCharArray(), str.length()); |
252 | | } | | 253 | | } |
253 | | | | 254 | | |
254 | | | | 255 | | |
255 | | /** | | 256 | | /** |
256 | | * Returns the index of a character in the value. | | 257 | | * Returns the index of a character in the value. |
257 | | */ | | 258 | | */ |
258 | | public int valueIndexOf(char c, int start) { | | 259 | | public int valueIndexOf(char c, int start) { |
259 | | for (int i=start; i<valueEnd; i++) { | | 260 | | for (int i=start; i<valueEnd; i++) { |
260 | | if (value[i] == c) | | 261 | | if (value[i] == c) |
261 | | return i; | | 262 | | return i; |
262 | | } | | 263 | | } |
263 | | return -1; | | 264 | | return -1; |
264 | | } | | 265 | | } |
265 | | | | 266 | | |
266 | | | | 267 | | |
267 | | /** | | 268 | | /** |
268 | | * Test if the name of the header is equal to the given header. | | 269 | | * Test if the name of the header is equal to the given header. |
269 | | * All the characters in the name must already be lower case. | | 270 | | * All the characters in the name must already be lower case. |
270 | | */ | | 271 | | */ |
271 | | public boolean equals(HttpHeader header) { | | 272 | | public boolean equals(HttpHeader header) { |
272 | | return (equals(header.name, header.nameEnd)); | | 273 | | return (equals(header.name, header.nameEnd)); |
273 | | } | | 274 | | } |
274 | | | | 275 | | |
275 | | | | 276 | | |
276 | | /** | | 277 | | /** |
277 | | * Test if the name and value of the header is equal to the given header. | | 278 | | * Test if the name and value of the header is equal to the given header. |
278 | | * All the characters in the name must already be lower case. | | 279 | | * All the characters in the name must already be lower case. |
279 | | */ | | 280 | | */ |
280 | | public boolean headerEquals(HttpHeader header) { | | 281 | | public boolean headerEquals(HttpHeader header) { |
281 | | return (equals(header.name, header.nameEnd)) | | 282 | | return (equals(header.name, header.nameEnd)) |
282 | | && (valueEquals(header.value, header.valueEnd)); | | 283 | | && (valueEquals(header.value, header.valueEnd)); |
283 | | } | | 284 | | } |
284 | | | | 285 | | |
285 | | | | 286 | | |
286 | | // --------------------------------------------------------- Object Methods | | 287 | | // --------------------------------------------------------- Object Methods |
287 | | | | 288 | | |
288 | | | | 289 | | |
289 | | /** | | 290 | | /** |
290 | | * Return hash code. The hash code of the HttpHeader object is the same | | 291 | | * Return hash code. The hash code of the HttpHeader object is the same |
291 | | * as returned by new String(name, 0, nameEnd).hashCode(). | | 292 | | * as returned by new String(name, 0, nameEnd).hashCode(). |
292 | | */ | | 293 | | */ |
293 | | public int hashCode() { | | 294 | | public int hashCode() { |
294 | | int h = hashCode; | | 295 | | int h = hashCode; |
295 | | if (h == 0) { | | 296 | | if (h == 0) { |
296 | | int off = 0; | | 297 | | int off = 0; |
297 | | char val[] = name; | | 298 | | char val[] = name; |
298 | | int len = nameEnd; | | 299 | | int len = nameEnd; |
299 | | for (int i = 0; i < len; i++) | | 300 | | for (int i = 0; i < len; i++) |
300 | | h = 31*h + val[off++]; | | 301 | | h = 31*h + val[off++]; |
301 | | hashCode = h; | | 302 | | hashCode = h; |
302 | | } | | 303 | | } |
303 | | return h; | | 304 | | return h; |
304 | | } | | 305 | | } |
305 | | | | 306 | | |
306 | | | | 307 | | |
307 | | public boolean equals(Object obj) { | | 308 | | public boolean equals(Object obj) { |
308 | | if (obj instanceof String) { | | 309 | | if (obj instanceof String) { |
309 | | return equals(((String) obj).toLowerCase()); | | 310 | | return equals(((String) obj).toLowerCase()); |
310 | | } else if (obj instanceof HttpHeader) { | | 311 | | } else if (obj instanceof HttpHeader) { |
311 | | return equals((HttpHeader) obj); | | 312 | | return equals((HttpHeader) obj); |
312 | | } | | 313 | | } |
313 | | return false; | | 314 | | return false; |
314 | | } | | 315 | | } |
315 | | | | 316 | | |
316 | | | | 317 | | |
317 | | } | | 318 | | } |