001: /*
002: * $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 $
003: * $Revision: 1.4 $
004: * $Date: 2002/03/18 07:15:40 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.connector.http;
065:
066: import java.io.IOException;
067: import java.net.InetAddress;
068: import javax.servlet.ServletInputStream;
069: import org.apache.catalina.connector.HttpRequestBase;
070:
071: /**
072: * HTTP header enum type.
073: *
074: * @author Remy Maucherat
075: * @version $Revision: 1.4 $ $Date: 2002/03/18 07:15:40 $
076: * @deprecated
077: */
078:
079: final class HttpHeader {
080:
081: // -------------------------------------------------------------- Constants
082:
083: public static final int INITIAL_NAME_SIZE = 32;
084: public static final int INITIAL_VALUE_SIZE = 64;
085: public static final int MAX_NAME_SIZE = 128;
086: public static final int MAX_VALUE_SIZE = 4096;
087:
088: // ----------------------------------------------------------- Constructors
089:
090: public HttpHeader() {
091:
092: this (new char[INITIAL_NAME_SIZE], 0,
093: new char[INITIAL_VALUE_SIZE], 0);
094:
095: }
096:
097: public HttpHeader(char[] name, int nameEnd, char[] value,
098: int valueEnd) {
099:
100: this .name = name;
101: this .nameEnd = nameEnd;
102: this .value = value;
103: this .valueEnd = valueEnd;
104:
105: }
106:
107: public HttpHeader(String name, String value) {
108:
109: this .name = name.toLowerCase().toCharArray();
110: this .nameEnd = name.length();
111: this .value = value.toCharArray();
112: this .valueEnd = value.length();
113:
114: }
115:
116: // ----------------------------------------------------- Instance Variables
117:
118: public char[] name;
119: public int nameEnd;
120: public char[] value;
121: public int valueEnd;
122: protected int hashCode = 0;
123:
124: // ------------------------------------------------------------- Properties
125:
126: // --------------------------------------------------------- Public Methods
127:
128: /**
129: * Release all object references, and initialize instance variables, in
130: * preparation for reuse of this object.
131: */
132: public void recycle() {
133:
134: nameEnd = 0;
135: valueEnd = 0;
136: hashCode = 0;
137:
138: }
139:
140: /**
141: * Test if the name of the header is equal to the given char array.
142: * All the characters must already be lower case.
143: */
144: public boolean equals(char[] buf) {
145: return equals(buf, buf.length);
146: }
147:
148: /**
149: * Test if the name of the header is equal to the given char array.
150: * All the characters must already be lower case.
151: */
152: public boolean equals(char[] buf, int end) {
153: if (end != nameEnd)
154: return false;
155: for (int i = 0; i < end; i++) {
156: if (buf[i] != name[i])
157: return false;
158: }
159: return true;
160: }
161:
162: /**
163: * Test if the name of the header is equal to the given string.
164: * The String given must be made of lower case characters.
165: */
166: public boolean equals(String str) {
167: return equals(str.toCharArray(), str.length());
168: }
169:
170: /**
171: * Test if the value of the header is equal to the given char array.
172: */
173: public boolean valueEquals(char[] buf) {
174: return valueEquals(buf, buf.length);
175: }
176:
177: /**
178: * Test if the value of the header is equal to the given char array.
179: */
180: public boolean valueEquals(char[] buf, int end) {
181: if (end != valueEnd)
182: return false;
183: for (int i = 0; i < end; i++) {
184: if (buf[i] != value[i])
185: return false;
186: }
187: return true;
188: }
189:
190: /**
191: * Test if the value of the header is equal to the given string.
192: */
193: public boolean valueEquals(String str) {
194: return valueEquals(str.toCharArray(), str.length());
195: }
196:
197: /**
198: * Test if the value of the header includes the given char array.
199: */
200: public boolean valueIncludes(char[] buf) {
201: return valueIncludes(buf, buf.length);
202: }
203:
204: /**
205: * Test if the value of the header includes the given char array.
206: */
207: public boolean valueIncludes(char[] buf, int end) {
208: char firstChar = buf[0];
209: int pos = 0;
210: while (pos < valueEnd) {
211: pos = valueIndexOf(firstChar, pos);
212: if (pos == -1)
213: return false;
214: if ((valueEnd - pos) < end)
215: return false;
216: for (int i = 0; i < end; i++) {
217: if (value[i + pos] != buf[i])
218: break;
219: if (i == (end - 1))
220: return true;
221: }
222: pos++;
223: }
224: return false;
225: }
226:
227: /**
228: * Test if the value of the header includes the given string.
229: */
230: public boolean valueIncludes(String str) {
231: return valueIncludes(str.toCharArray(), str.length());
232: }
233:
234: /**
235: * Returns the index of a character in the value.
236: */
237: public int valueIndexOf(char c, int start) {
238: for (int i = start; i < valueEnd; i++) {
239: if (value[i] == c)
240: return i;
241: }
242: return -1;
243: }
244:
245: /**
246: * Test if the name of the header is equal to the given header.
247: * All the characters in the name must already be lower case.
248: */
249: public boolean equals(HttpHeader header) {
250: return (equals(header.name, header.nameEnd));
251: }
252:
253: /**
254: * Test if the name and value of the header is equal to the given header.
255: * All the characters in the name must already be lower case.
256: */
257: public boolean headerEquals(HttpHeader header) {
258: return (equals(header.name, header.nameEnd))
259: && (valueEquals(header.value, header.valueEnd));
260: }
261:
262: // --------------------------------------------------------- Object Methods
263:
264: /**
265: * Return hash code. The hash code of the HttpHeader object is the same
266: * as returned by new String(name, 0, nameEnd).hashCode().
267: */
268: public int hashCode() {
269: int h = hashCode;
270: if (h == 0) {
271: int off = 0;
272: char val[] = name;
273: int len = nameEnd;
274: for (int i = 0; i < len; i++)
275: h = 31 * h + val[off++];
276: hashCode = h;
277: }
278: return h;
279: }
280:
281: public boolean equals(Object obj) {
282: if (obj instanceof String) {
283: return equals(((String) obj).toLowerCase());
284: } else if (obj instanceof HttpHeader) {
285: return equals((HttpHeader) obj);
286: }
287: return false;
288: }
289:
290: }
|