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.nio.codecs;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.nio.channels.ReadableByteChannel;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.apache.http.Header;
37  import org.apache.http.MalformedChunkCodingException;
38  import org.apache.http.ParseException;
39  import org.apache.http.TruncatedChunkException;
40  import org.apache.http.annotation.NotThreadSafe;
41  import org.apache.http.impl.io.HttpTransportMetricsImpl;
42  import org.apache.http.message.BufferedHeader;
43  import org.apache.http.nio.reactor.SessionInputBuffer;
44  import org.apache.http.util.Args;
45  import org.apache.http.util.CharArrayBuffer;
46  
47  /**
48   * Implements chunked transfer coding. The content is received in small chunks.
49   * Entities transferred using this encoder can be of unlimited length.
50   *
51   * @since 4.0
52   */
53  @NotThreadSafe
54  public class ChunkDecoder extends AbstractContentDecoder {
55  
56      private static final int READ_CONTENT   = 0;
57      private static final int READ_FOOTERS  = 1;
58      private static final int COMPLETED      = 2;
59  
60      private int state;
61      private boolean endOfChunk;
62      private boolean endOfStream;
63  
64      private CharArrayBuffer lineBuf;
65      private int chunkSize;
66      private int pos;
67  
68      private final List<CharArrayBuffer> trailerBufs;
69  
70      private Header[] footers;
71  
72      public ChunkDecoder(
73              final ReadableByteChannel channel,
74              final SessionInputBuffer buffer,
75              final HttpTransportMetricsImpl metrics) {
76          super(channel, buffer, metrics);
77          this.state = READ_CONTENT;
78          this.chunkSize = -1;
79          this.pos = 0;
80          this.endOfChunk = false;
81          this.endOfStream = false;
82          this.trailerBufs = new ArrayList<CharArrayBuffer>();
83      }
84  
85      private void readChunkHead() throws IOException {
86          if (this.lineBuf == null) {
87              this.lineBuf = new CharArrayBuffer(32);
88          } else {
89              this.lineBuf.clear();
90          }
91          if (this.endOfChunk) {
92              if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
93                  if (!this.lineBuf.isEmpty()) {
94                      throw new MalformedChunkCodingException("CRLF expected at end of chunk");
95                  }
96              } else {
97                  if (this.buffer.length() > 2) {
98                      throw new MalformedChunkCodingException("CRLF expected at end of chunk");
99                  }
100                 return;
101             }
102             this.endOfChunk = false;
103         }
104         if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
105             int separator = this.lineBuf.indexOf(';');
106             if (separator < 0) {
107                 separator = this.lineBuf.length();
108             }
109             try {
110                 final String s = this.lineBuf.substringTrimmed(0, separator);
111                 this.chunkSize = Integer.parseInt(s, 16);
112             } catch (final NumberFormatException e) {
113                 throw new MalformedChunkCodingException("Bad chunk header");
114             }
115             this.pos = 0;
116         }
117     }
118 
119     private void parseHeader() {
120         final CharArrayBuffer current = this.lineBuf;
121         final int count = this.trailerBufs.size();
122         if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) {
123             // Handle folded header line
124             final CharArrayBuffer previous = this.trailerBufs.get(count - 1);
125             int i = 0;
126             while (i < current.length()) {
127                 final char ch = current.charAt(i);
128                 if (ch != ' ' && ch != '\t') {
129                     break;
130                 }
131                 i++;
132             }
133             previous.append(' ');
134             previous.append(current, i, current.length() - i);
135         } else {
136             this.trailerBufs.add(current);
137             this.lineBuf = null;
138         }
139     }
140 
141     private void processFooters() throws IOException {
142         final int count = this.trailerBufs.size();
143         if (count > 0) {
144             this.footers = new Header[this.trailerBufs.size()];
145             for (int i = 0; i < this.trailerBufs.size(); i++) {
146                 try {
147                     this.footers[i] = new BufferedHeader(this.trailerBufs.get(i));
148                 } catch (final ParseException ex) {
149                     throw new IOException(ex.getMessage());
150                 }
151             }
152         }
153         this.trailerBufs.clear();
154     }
155 
156     public int read(final ByteBuffer dst) throws IOException {
157         Args.notNull(dst, "Byte buffer");
158         if (this.state == COMPLETED) {
159             return -1;
160         }
161 
162         int totalRead = 0;
163         while (this.state != COMPLETED) {
164 
165             if (!this.buffer.hasData() || this.chunkSize == -1) {
166                 final int bytesRead = fillBufferFromChannel();
167                 if (bytesRead == -1) {
168                     this.endOfStream = true;
169                 }
170             }
171 
172             switch (this.state) {
173             case READ_CONTENT:
174 
175                 if (this.chunkSize == -1) {
176                     readChunkHead();
177                     if (this.chunkSize == -1) {
178                         // Unable to read a chunk head
179                         if (this.endOfStream) {
180                             this.state = COMPLETED;
181                             this.completed = true;
182                         }
183                         return totalRead;
184                     }
185                     if (this.chunkSize == 0) {
186                         // Last chunk. Read footers
187                         this.chunkSize = -1;
188                         this.state = READ_FOOTERS;
189                         break;
190                     }
191                 }
192                 final int maxLen = this.chunkSize - this.pos;
193                 final int len = this.buffer.read(dst, maxLen);
194                 if (len > 0) {
195                     this.pos += len;
196                     totalRead += len;
197                 } else {
198                     if (!this.buffer.hasData() && this.endOfStream) {
199                         this.state = COMPLETED;
200                         this.completed = true;
201                         throw new TruncatedChunkException("Truncated chunk "
202                                 + "( expected size: " + this.chunkSize
203                                 + "; actual size: " + this.pos + ")");
204                     }
205                 }
206 
207                 if (this.pos == this.chunkSize) {
208                     // At the end of the chunk
209                     this.chunkSize = -1;
210                     this.pos = 0;
211                     this.endOfChunk = true;
212                     break;
213                 }
214                 return totalRead;
215             case READ_FOOTERS:
216                 if (this.lineBuf == null) {
217                     this.lineBuf = new CharArrayBuffer(32);
218                 } else {
219                     this.lineBuf.clear();
220                 }
221                 if (!this.buffer.readLine(this.lineBuf, this.endOfStream)) {
222                     // Unable to read a footer
223                     if (this.endOfStream) {
224                         this.state = COMPLETED;
225                         this.completed = true;
226                     }
227                     return totalRead;
228                 }
229                 if (this.lineBuf.length() > 0) {
230                     parseHeader();
231                 } else {
232                     this.state = COMPLETED;
233                     this.completed = true;
234                     processFooters();
235                 }
236                 break;
237             }
238 
239         }
240         return totalRead;
241     }
242 
243     public Header[] getFooters() {
244         if (this.footers != null) {
245             return this.footers.clone();
246         } else {
247             return new Header[] {};
248         }
249     }
250 
251     @Override
252     public String toString() {
253         final StringBuilder sb = new StringBuilder();
254         sb.append("[chunk-coded; completed: ");
255         sb.append(this.completed);
256         sb.append("]");
257         return sb.toString();
258     }
259 
260 }