View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import org.apache.http.ConnectionClosedException;
34  import org.apache.http.Header;
35  import org.apache.http.HttpException;
36  import org.apache.http.MalformedChunkCodingException;
37  import org.apache.http.TruncatedChunkException;
38  import org.apache.http.annotation.NotThreadSafe;
39  import org.apache.http.config.MessageConstraints;
40  import org.apache.http.io.BufferInfo;
41  import org.apache.http.io.SessionInputBuffer;
42  import org.apache.http.util.Args;
43  import org.apache.http.util.CharArrayBuffer;
44  
45  /**
46   * Implements chunked transfer coding. The content is received in small chunks.
47   * Entities transferred using this input stream can be of unlimited length.
48   * After the stream is read to the end, it provides access to the trailers,
49   * if any.
50   * <p>
51   * Note that this class NEVER closes the underlying stream, even when close
52   * gets called.  Instead, it will read until the "end" of its chunking on
53   * close, which allows for the seamless execution of subsequent HTTP 1.1
54   * requests, while not requiring the client to remember to read the entire
55   * contents of the response.
56   *
57   *
58   * @since 4.0
59   *
60   */
61  @NotThreadSafe
62  public class ChunkedInputStream extends InputStream {
63  
64      private static final int CHUNK_LEN               = 1;
65      private static final int CHUNK_DATA              = 2;
66      private static final int CHUNK_CRLF              = 3;
67      private static final int CHUNK_INVALID           = Integer.MAX_VALUE;
68  
69      private static final int BUFFER_SIZE = 2048;
70  
71      /** The session input buffer */
72      private final SessionInputBuffer in;
73      private final CharArrayBuffer buffer;
74      private final MessageConstraints constraints;
75  
76      private int state;
77  
78      /** The chunk size */
79      private int chunkSize;
80  
81      /** The current position within the current chunk */
82      private int pos;
83  
84      /** True if we've reached the end of stream */
85      private boolean eof = false;
86  
87      /** True if this stream is closed */
88      private boolean closed = false;
89  
90      private Header[] footers = new Header[] {};
91  
92      /**
93       * Wraps session input stream and reads chunk coded input.
94       *
95       * @param in The session input buffer
96       * @param constraints Message constraints. If {@code null}
97       *   {@link MessageConstraints#DEFAULT} will be used.
98       *
99       * @since 4.4
100      */
101     public ChunkedInputStream(final SessionInputBuffer in, final MessageConstraints constraints) {
102         super();
103         this.in = Args.notNull(in, "Session input buffer");
104         this.pos = 0;
105         this.buffer = new CharArrayBuffer(16);
106         this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
107         this.state = CHUNK_LEN;
108     }
109 
110     /**
111      * Wraps session input stream and reads chunk coded input.
112      *
113      * @param in The session input buffer
114      */
115     public ChunkedInputStream(final SessionInputBuffer in) {
116         this(in, null);
117     }
118 
119     @Override
120     public int available() throws IOException {
121         if (this.in instanceof BufferInfo) {
122             final int len = ((BufferInfo) this.in).length();
123             return Math.min(len, this.chunkSize - this.pos);
124         } else {
125             return 0;
126         }
127     }
128 
129     /**
130      * <p> Returns all the data in a chunked stream in coalesced form. A chunk
131      * is followed by a CRLF. The method returns -1 as soon as a chunksize of 0
132      * is detected.</p>
133      *
134      * <p> Trailer headers are read automatically at the end of the stream and
135      * can be obtained with the getResponseFooters() method.</p>
136      *
137      * @return -1 of the end of the stream has been reached or the next data
138      * byte
139      * @throws IOException in case of an I/O error
140      */
141     @Override
142     public int read() throws IOException {
143         if (this.closed) {
144             throw new IOException("Attempted read from closed stream.");
145         }
146         if (this.eof) {
147             return -1;
148         }
149         if (state != CHUNK_DATA) {
150             nextChunk();
151             if (this.eof) {
152                 return -1;
153             }
154         }
155         final int b = in.read();
156         if (b != -1) {
157             pos++;
158             if (pos >= chunkSize) {
159                 state = CHUNK_CRLF;
160             }
161         }
162         return b;
163     }
164 
165     /**
166      * Read some bytes from the stream.
167      * @param b The byte array that will hold the contents from the stream.
168      * @param off The offset into the byte array at which bytes will start to be
169      * placed.
170      * @param len the maximum number of bytes that can be returned.
171      * @return The number of bytes returned or -1 if the end of stream has been
172      * reached.
173      * @throws IOException in case of an I/O error
174      */
175     @Override
176     public int read (final byte[] b, final int off, final int len) throws IOException {
177 
178         if (closed) {
179             throw new IOException("Attempted read from closed stream.");
180         }
181 
182         if (eof) {
183             return -1;
184         }
185         if (state != CHUNK_DATA) {
186             nextChunk();
187             if (eof) {
188                 return -1;
189             }
190         }
191         final int bytesRead = in.read(b, off, Math.min(len, chunkSize - pos));
192         if (bytesRead != -1) {
193             pos += bytesRead;
194             if (pos >= chunkSize) {
195                 state = CHUNK_CRLF;
196             }
197             return bytesRead;
198         } else {
199             eof = true;
200             throw new TruncatedChunkException("Truncated chunk "
201                     + "( expected size: " + chunkSize
202                     + "; actual size: " + pos + ")");
203         }
204     }
205 
206     /**
207      * Read some bytes from the stream.
208      * @param b The byte array that will hold the contents from the stream.
209      * @return The number of bytes returned or -1 if the end of stream has been
210      * reached.
211      * @throws IOException in case of an I/O error
212      */
213     @Override
214     public int read (final byte[] b) throws IOException {
215         return read(b, 0, b.length);
216     }
217 
218     /**
219      * Read the next chunk.
220      * @throws IOException in case of an I/O error
221      */
222     private void nextChunk() throws IOException {
223         if (state == CHUNK_INVALID) {
224             throw new MalformedChunkCodingException("Corrupt data stream");
225         }
226         try {
227             chunkSize = getChunkSize();
228             if (chunkSize < 0) {
229                 throw new MalformedChunkCodingException("Negative chunk size");
230             }
231             state = CHUNK_DATA;
232             pos = 0;
233             if (chunkSize == 0) {
234                 eof = true;
235                 parseTrailerHeaders();
236             }
237         } catch (MalformedChunkCodingException ex) {
238             state = CHUNK_INVALID;
239             throw ex;
240         }
241     }
242 
243     /**
244      * Expects the stream to start with a chunksize in hex with optional
245      * comments after a semicolon. The line must end with a CRLF: "a3; some
246      * comment\r\n" Positions the stream at the start of the next line.
247      */
248     private int getChunkSize() throws IOException {
249         final int st = this.state;
250         switch (st) {
251         case CHUNK_CRLF:
252             this.buffer.clear();
253             final int bytesRead1 = this.in.readLine(this.buffer);
254             if (bytesRead1 == -1) {
255                 throw new MalformedChunkCodingException(
256                     "CRLF expected at end of chunk");
257             }
258             if (!this.buffer.isEmpty()) {
259                 throw new MalformedChunkCodingException(
260                     "Unexpected content at the end of chunk");
261             }
262             state = CHUNK_LEN;
263             //$FALL-THROUGH$
264         case CHUNK_LEN:
265             this.buffer.clear();
266             final int bytesRead2 = this.in.readLine(this.buffer);
267             if (bytesRead2 == -1) {
268                 throw new ConnectionClosedException("Premature end of chunk coded message body: " +
269                         "closing chunk expected");
270             }
271             int separator = this.buffer.indexOf(';');
272             if (separator < 0) {
273                 separator = this.buffer.length();
274             }
275             try {
276                 return Integer.parseInt(this.buffer.substringTrimmed(0, separator), 16);
277             } catch (final NumberFormatException e) {
278                 throw new MalformedChunkCodingException("Bad chunk header");
279             }
280         default:
281             throw new IllegalStateException("Inconsistent codec state");
282         }
283     }
284 
285     /**
286      * Reads and stores the Trailer headers.
287      * @throws IOException in case of an I/O error
288      */
289     private void parseTrailerHeaders() throws IOException {
290         try {
291             this.footers = AbstractMessageParser.parseHeaders(in,
292                     constraints.getMaxHeaderCount(),
293                     constraints.getMaxLineLength(),
294                     null);
295         } catch (final HttpException ex) {
296             final IOException ioe = new MalformedChunkCodingException("Invalid footer: "
297                     + ex.getMessage());
298             ioe.initCause(ex);
299             throw ioe;
300         }
301     }
302 
303     /**
304      * Upon close, this reads the remainder of the chunked message,
305      * leaving the underlying socket at a position to start reading the
306      * next response without scanning.
307      * @throws IOException in case of an I/O error
308      */
309     @Override
310     public void close() throws IOException {
311         if (!closed) {
312             try {
313                 if (!eof && state != CHUNK_INVALID) {
314                     // read and discard the remainder of the message
315                     final byte buff[] = new byte[BUFFER_SIZE];
316                     while (read(buff) >= 0) {
317                     }
318                 }
319             } finally {
320                 eof = true;
321                 closed = true;
322             }
323         }
324     }
325 
326     public Header[] getFooters() {
327         return this.footers.clone();
328     }
329 
330 }