001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpRequestStream.java,v 1.11 2002/03/18 07:15:40 remm Exp $
003: * $Revision: 1.11 $
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 org.apache.catalina.Request;
068: import org.apache.catalina.connector.RequestStream;
069:
070: /**
071: *
072: *
073: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
074: * @deprecated
075: */
076: public class HttpRequestStream extends RequestStream {
077:
078: // ----------------------------------------------------------- Constructors
079:
080: /**
081: * Construct a servlet input stream associated with the specified Request.
082: *
083: * @param request The associated request
084: * @param response The associated response
085: */
086: public HttpRequestStream(HttpRequestImpl request,
087: HttpResponseImpl response) {
088:
089: super (request);
090: String transferEncoding = request
091: .getHeader("Transfer-Encoding");
092:
093: http11 = request.getProtocol().equals("HTTP/1.1");
094: chunk = ((transferEncoding != null) && (transferEncoding
095: .indexOf("chunked") != -1));
096:
097: if ((!chunk) && (length == -1)) {
098: // Ask for connection close
099: response.addHeader("Connection", "close");
100: }
101:
102: }
103:
104: // ----------------------------------------------------- Instance Variables
105:
106: /**
107: * Use chunking ?
108: */
109: protected boolean chunk = false;
110:
111: /**
112: * True if the final chunk was found.
113: */
114: protected boolean endChunk = false;
115:
116: /**
117: * Chunk buffer.
118: */
119: protected byte[] chunkBuffer = null;
120:
121: /**
122: * Chunk length.
123: */
124: protected int chunkLength = 0;
125:
126: /**
127: * Chunk buffer position.
128: */
129: protected int chunkPos = 0;
130:
131: /**
132: * HTTP/1.1 flag.
133: */
134: protected boolean http11 = false;
135:
136: // --------------------------------------------------------- Public Methods
137:
138: /**
139: * Close this input stream. No physical level I-O is performed, but
140: * any further attempt to read from this stream will throw an IOException.
141: * If a content length has been set but not all of the bytes have yet been
142: * consumed, the remaining bytes will be swallowed.
143: */
144: public void close() throws IOException {
145:
146: if (closed)
147: throw new IOException(sm
148: .getString("requestStream.close.closed"));
149:
150: if (chunk) {
151:
152: while (!endChunk) {
153: int b = read();
154: if (b < 0)
155: break;
156: }
157:
158: } else {
159:
160: if (http11 && (length > 0)) {
161: while (count < length) {
162: int b = read();
163: if (b < 0)
164: break;
165: }
166: }
167:
168: }
169:
170: closed = true;
171:
172: }
173:
174: /**
175: * Read and return a single byte from this input stream, or -1 if end of
176: * file has been encountered.
177: *
178: * @exception IOException if an input/output error occurs
179: */
180: public int read() throws IOException {
181:
182: // Has this stream been closed?
183: if (closed)
184: throw new IOException(sm
185: .getString("requestStream.read.closed"));
186:
187: if (chunk) {
188:
189: if (endChunk)
190: return (-1);
191:
192: if ((chunkBuffer == null) || (chunkPos >= chunkLength)) {
193: if (!fillChunkBuffer())
194: return (-1);
195: }
196:
197: return (chunkBuffer[chunkPos++] & 0xff);
198:
199: } else {
200:
201: return (super .read());
202:
203: }
204:
205: }
206:
207: /**
208: * Read up to <code>len</code> bytes of data from the input stream
209: * into an array of bytes. An attempt is made to read as many as
210: * <code>len</code> bytes, but a smaller number may be read,
211: * possibly zero. The number of bytes actually read is returned as
212: * an integer. This method blocks until input data is available,
213: * end of file is detected, or an exception is thrown.
214: *
215: * @param b The buffer into which the data is read
216: * @param off The start offset into array <code>b</code> at which
217: * the data is written
218: * @param len The maximum number of bytes to read
219: *
220: * @exception IOException if an input/output error occurs
221: */
222: public int read(byte b[], int off, int len) throws IOException {
223: if (chunk) {
224:
225: int avail = chunkLength - chunkPos;
226: if (avail == 0)
227: fillChunkBuffer();
228: avail = chunkLength - chunkPos;
229: if (avail == 0)
230: return (-1);
231:
232: int toCopy = avail;
233: if (avail > len)
234: toCopy = len;
235: System.arraycopy(chunkBuffer, chunkPos, b, off, toCopy);
236: chunkPos += toCopy;
237: return toCopy;
238:
239: } else {
240: return super .read(b, off, len);
241: }
242: }
243:
244: // -------------------------------------------------------- Private Methods
245:
246: /**
247: * Fill the chunk buffer.
248: */
249: private synchronized boolean fillChunkBuffer() throws IOException {
250:
251: chunkPos = 0;
252:
253: try {
254: String numberValue = readLineFromStream();
255: if (numberValue != null)
256: numberValue = numberValue.trim();
257: chunkLength = Integer.parseInt(numberValue, 16);
258: } catch (NumberFormatException e) {
259: // Critical error, unable to parse the chunk length
260: chunkLength = 0;
261: chunk = false;
262: close();
263: return false;
264: }
265:
266: if (chunkLength == 0) {
267:
268: // Skipping trailing headers, if any
269: String trailingLine = readLineFromStream();
270: while (!trailingLine.equals(""))
271: trailingLine = readLineFromStream();
272: endChunk = true;
273: return false;
274: // TODO : Should the stream be automatically closed ?
275:
276: } else {
277:
278: if ((chunkBuffer == null)
279: || (chunkLength > chunkBuffer.length))
280: chunkBuffer = new byte[chunkLength];
281:
282: // Now read the whole chunk into the buffer
283:
284: int nbRead = 0;
285: int currentRead = 0;
286:
287: while (nbRead < chunkLength) {
288: try {
289: currentRead = stream.read(chunkBuffer, nbRead,
290: chunkLength - nbRead);
291: } catch (Throwable t) {
292: t.printStackTrace();
293: throw new IOException();
294: }
295: if (currentRead < 0) {
296: throw new IOException(sm
297: .getString("requestStream.read.error"));
298: }
299: nbRead += currentRead;
300: }
301:
302: // Skipping the CRLF
303: String blank = readLineFromStream();
304:
305: }
306:
307: return true;
308:
309: }
310:
311: /**
312: * Reads the input stream, one line at a time. Reads bytes into an array,
313: * until it reads a certain number of bytes or reaches a newline character,
314: * which it reads into the array as well.
315: *
316: * @param input Input stream on which the bytes are read
317: * @return The line that was read, or <code>null</code> if end-of-file
318: * was encountered
319: * @exception IOException if an input or output exception has occurred
320: */
321: private String readLineFromStream() throws IOException {
322:
323: StringBuffer sb = new StringBuffer();
324: while (true) {
325: int ch = super .read();
326: if (ch < 0) {
327: if (sb.length() == 0) {
328: return (null);
329: } else {
330: break;
331: }
332: } else if (ch == '\r') {
333: continue;
334: } else if (ch == '\n') {
335: break;
336: }
337: sb.append((char) ch);
338: }
339: return (sb.toString());
340:
341: }
342:
343: }
|